var app=getApp()
var that=null
var sopenFrame=null
var wait=120
var areas = null;
var BMap = null;
var bmap=null;
var CompanyCert=null;
var CompanyPic=null;
Page( { 
  data: {
    getcode:"获取验证码",
    savebutton:"提交",
    index:0,
    registertype:[{key:"会员"}],
    FHEnable4:"",
    yqm:"",
    username:"",
    phone:"",
    showyqxm:0,
    yqxm:"",
    ifnoqy:0,
    xzimage:"../../images/wxz.png",
    yzcodeid:"",
    codeimage:"",
    yzcode:"",
    sectionHidden:true,
    sectionHidden0:true,
            Provinces:[],
            Citys:[],
            Countys:[],
            hx_index:-1,
            hx_index0:-1,
            hx_index1:-1,
            area:"",
            addpic:"../../images/add.png",
            addpic0:"../../images/add.png",
            mapmsg:"",
            longitude: null,//经度
            latitude: null,//纬度 
            clongitude: null,//经度
            clatitude: null,//纬度 
            markers: [{ //标记点用于在地图上显示标记的位置
            id: 1,
            latitude: null,
            longitude: null,
            iconPath: '../../images/position-picker2.png',
            width: 60,
            height: 60,
            }],
            Companyaddress:"",
            ifzt:0
  }, // 下拉刷新
  onPullDownRefresh: function () {
     //在当前页面显示导航条加载动画
   wx.showNavigationBarLoading(); 
   //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
   wx.showLoading({
       title: '刷新中...',
   })
   that.rdata()
  },

  onLoad: function(options) {
      that = this
      wx.removeStorageSync('ifnoqy');
      if(options.ifzt){ 
        that.setData({
             ifzt:options.ifzt
        })
      }
      if(options.tguid){ 
        var tguid=options.tguid;
        if(tguid!=""){
            wx.setStorageSync('tguid', tguid) 
            that.setData({
              ifzt:1
            })
        }
      }
       
     if(options.scene){
         const tguid = decodeURIComponent(options.scene);
         if(tguid!=""){
             wx.setStorageSync('tguid', tguid) 
             that.setData({
                ifzt:1
            })
         }
     }
      that.rdata()
      areas = require('../../area.js');
      that.setData({
        Provinces:areas.areaslist
     })
      
    },rdata: function() {
      var tguid = wx.getStorageSync('tguid');
      if(that.data.ifzt==0){
        wx.getClipboardData({
          success (res){
              if(res.data.length==8 && app.isNumber(res.data)){
                 ifyqm=1;
                 that.setData({
                     yqm:res.data
                 })
              }
          }
        })
      }else{
           that.setData({
              yqm:(!tguid?"":tguid)
           })
        
      }
      that.getyzcode();
    
    },getyzcode:function() {
      
    
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        
        data: {
            action:"getregisterdata",
            yqm:that.data.yqm
        },
        method:"GET", 
        dataType:"json",  
        header: { 
          "content-type": "application/json" // 默认值
        },
        success (res) {   
          
          if(res.data.status==1){
        
                that.setData({
                     FHEnable4:res.data.FHEnable4,
                     username:res.data.username,
                     yqxm:res.data.name,
                     yzcodeid:res.data.yzid,
                     codeimage:app.globalData.apiurl + "/CheckCode0.ashx?yzcode="+res.data.yzcode
                })
               
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             }) 
          }
        }
      });
     
    },onregistertype:function(e) {
      var index=e.detail.value;
      var key=that.data.registertype[index].key
      that.setData({
        index: index
      })
     
    },onarea:function(e) {
      that.setData({
          sectionHidden:false
      })
    },ProvinceChange:function(e) {
      var index=e.detail.value;
      var citys=that.data.Provinces[index].City
      that.setData({
        hx_index: index,
        hx_index0: -1,
        hx_index1: -1,
        Citys:citys
      })
      if(citys.length==0){
        that.setData({
           sectionHidden:true,
           area:that.data.Provinces[that.data.hx_index].ProvinceName
         })
      }
    },CityChange:function(e) {
      var index=e.detail.value
      var countys=that.data.Citys[index].County
      that.setData({
        hx_index0: index,
        hx_index1: -1,
        Countys:countys
      })
      if(countys.length==0){
        that.setData({
           sectionHidden:true,
           area:that.data.Provinces[that.data.hx_index].ProvinceName+that.data.Citys[that.data.hx_index0].CityName
         })
      }
    },CountyChange:function(e) {
      that.setData({
        hx_index1: e.detail.value
      })
      that.setData({
        sectionHidden:true,
        area:that.data.Provinces[that.data.hx_index].ProvinceName+that.data.Citys[that.data.hx_index0].CityName+that.data.Countys[that.data.hx_index1].CountyName
      })
    },closesection:function(e) {
      that.setData({
        sectionHidden:true
      })
    },closesection0:function(e) {
      that.setData({
        sectionHidden0:true
      })
    },insection:function(e) {
      
    },onaddpic:function(e) {
      if(that.data.addpic=="../../images/ajaxLoader.gif"){
        wx.showToast({
          title: "正在上传中...",
          icon: "none",
          duration: 3000
         })
      }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success (res) {
              const tempFilePaths = res.tempFilePaths;
              
              that.setData({
                  addpic:"../../images/ajaxLoader.gif"
              })
              wx.uploadFile({
                url: app.globalData.apiurl + "/xapiajax.ashx", //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'image',
                formData: {
                  action:"uploadtempImage"
                },
                success (res0){
                    var data=JSON.parse(res0.data);
                    
                   
                    if(data.status==1){
                      wx.showToast({
                        title: data.msg,
                        icon: "success",
                        duration: 1000
                      })
                         CompanyCert=data.pic;
                         that.setData({
                             addpic:data.url
                        })
                      var viewid = wx.getStorageSync('viewid') 
                      var viewpoid = wx.getStorageSync('viewpoid') 
                      if(viewid){
                         wx.removeStorageSync('viewid')
                         wx.removeStorageSync('viewpoid')
                         wx.navigateTo({  
                            url: "../details/index?id=" + viewid + (viewpoid?"&poid=" +viewpoid:"")
                         })
                      }
                    }else{
                      wx.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 2000
                    })
                      that.setData({
                            addpic:"../../images/add.png"
                      })
                    }
                  
                }
              });
        }
      })
    }
    },onaddpic0:function(e) {
      if(that.data.addpic0=="../../images/ajaxLoader.gif"){
        wx.showToast({
          title: "正在上传中...",
          icon: "none",
          duration: 3000
         })
      }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success (res) {
              const tempFilePaths = res.tempFilePaths;
              
              that.setData({
                  addpic0:"../../images/ajaxLoader.gif"
              })
              wx.uploadFile({
                url: app.globalData.apiurl + "/xapiajax.ashx", //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'image',
                formData: {
                  action:"uploadtempImage"
                },
                success (res0){
                    var data=JSON.parse(res0.data);
                    
                   
                    if(data.status==1){
                      wx.showToast({
                        title: data.msg,
                        icon: "success",
                        duration: 1000
                      })
                         CompanyPic=data.pic;
                         that.setData({
                             addpic0:data.url
                        })
                    }else{
                      wx.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 2000
                    })
                      that.setData({
                            addpic0:"../../images/add.png"
                      })
                    }
                  
                }
              });
        }
      })
    }
    },getaddressValue:function(e) {
       that.setData({
           Companyaddress:e.detail.value
       })
    },maptap(e){
      if(e.detail){
      var str = 'markers[0].longitude', str2 = 'markers[0].latitude';
      that.setData({ 
        latitude: e.detail.latitude,
        longitude: e.detail.longitude,
        [str]: e.detail.longitude,
        [str2]: e.detail.latitude,
       });
      }
    },my_location(e) {
        //微信API定位,获取当前位置经纬度
      
    },markertap(e) {
    },regionchange(e){ 
      if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
          that.bmap.getCenterLocation({
          success: function (res) {
            latitude = res.latitude;
            longitude = res.longitude;
            var str = 'markers[0].longitude', str2 = 'markers[0].latitude';
            that.setData({
              longitude: res.longitude,
              latitude: res.latitude,
              clongitude: res.longitude,
              clatitude: res.latitude,
              [str]: res.longitude,
              [str2]: res.latitude,
            })
          }
        })
      }
    }
    ,onmap:function() {
        
       
        
    if(that.data.latitude&&that.data.longitude){
      that.setData({
        sectionHidden0:false
    })
    that.mapCtx = wx.createMapContext('myMap')
    // 实例化百度地图API核心类
    bmap = new BMap.BMapWX({
      ak: 'HiMGaFNdmB2VSvWSkPogESxBDaLQkOuq'
    })
      var str = 'markers[0].longitude', str2 = 'markers[0].latitude';
      that.setData({ 
        latitude: that.data.latitude,
        longitude: that.data.longitude,
        clatitude: that.data.latitude,
        clongitude: that.data.longitude,
        [str]: that.data.longitude,
        [str2]: that.data.latitude,
       });
    
    }else{
    if (that.data.hx_index!=-1 && that.data.hx_index0!=-1 && that.data.hx_index1!=-1 && that.data.Companyaddress!="") {
      that.setData({
        sectionHidden0:false
    })
    that.mapCtx = wx.createMapContext('myMap')
    // 实例化百度地图API核心类
    bmap = new BMap.BMapWX({
      ak: 'HiMGaFNdmB2VSvWSkPogESxBDaLQkOuq'
    })
      var fail = function(data) { 
        console.log(data) 
      }; 
      var success = function(data) { 
        var wxMarkerData = data.wxMarkerData; 
        var str = 'markers[0].longitude', str2 = 'markers[0].latitude';
        that.setData({ 
          latitude: wxMarkerData[0].latitude,
          longitude: wxMarkerData[0].longitude,
          clatitude: wxMarkerData[0].latitude,
          clongitude: wxMarkerData[0].longitude,
          [str]: wxMarkerData[0].longitude,
          [str2]: wxMarkerData[0].latitude,
         });
      } 
    // 发起geocoding检索请求 
    var area = "";
    if(that.data.Provinces[that.data.hx_index]){
         area = that.data.Provinces[that.data.hx_index].ProvinceName;
    }
    if(that.data.Citys[that.data.hx_index0]){
         area += that.data.Citys[that.data.hx_index0].CityName;
    }
    if(that.data.Countys[that.data.hx_index1]){
         area += that.data.Countys[that.data.hx_index1].CountyName;
    }
    bmap.geocoding({ 
        address: area+that.data.Companyaddress, 
        fail: fail, 
        success: success
    }); 
       
    } else {
      wx.showToast({
        title: "请先选择区域和填写地址！",
        icon: "none",
        duration: 3000
    })
    }
  }
    },onupdated: function(e)  {
      that.setData({ 
        loadingHidden:true
     })
    },qcbz:function(e) {
      var str = 'markers[0].longitude', str2 = 'markers[0].latitude';
      that.setData({
        latitude:null,
        latitude:null,
        clatitude:null,
        clatitude:null,
        [str]: null,
        [str2]: null,
        sectionHidden0:true,
        mapmsg:"" 
     })
    },qybz:function(e) {
      if(that.data.latitude&&that.data.longitude){
       that.setData({
          sectionHidden0:true,
          mapmsg:"已标注" 
       })
      }else{
         wx.showToast({
             title: "请点击地图标注！",
             icon: "none",
             duration: 3000
         })
      }
    },register:function(e) {
      var username = "";//e.detail.value.username.replace(/\s+/g, "");
      var realname = e.detail.value.realname.replace(/\s+/g, "");
      var phone = e.detail.value.phone.replace(/\s+/g, "");
      var code = "";
      if(that.data.FHEnable4=="0"){
            code = e.detail.value.code.replace(/\s+/g, "");
      }else{
            code = e.detail.value.yzcode.replace(/\s+/g, "");
      }
      var psd="";
      var psd2="";
      if(that.data.ifnoqy==0){
           psd=e.detail.value.psd.replace(/\s+/g, "");
           psd2=e.detail.value.psd2.replace(/\s+/g, "");
      }
      var yqm = e.detail.value.yqm.replace(/\s+/g, "");
      var CompanyName="";
      var CompanyNumber="";
      var CompanyProvince="";
      var CompanyCity="";
      var CompanyCounty="";
      var Companyaddress="";
      var Companylatitude="";
      var Companylongitude="";
      var b=true
      if(that.data.index>0){
          CompanyName = e.detail.value.CompanyName.replace(/\s+/g, "");
          CompanyNumber =e.detail.value.CompanyNumber.replace(/\s+/g, ""); 
          if(that.data.Provinces[that.data.hx_index]){
              CompanyProvince = that.data.Provinces[that.data.hx_index].ProvinceID;
          }
          if(that.data.Citys[that.data.hx_index0]){
              CompanyCity = that.data.Citys[that.data.hx_index0].CityID;
          }
          if(that.data.Countys[that.data.hx_index1]){
              CompanyCounty = that.data.Countys[that.data.hx_index1].CountyID;
          }
          Companyaddress=e.detail.value.Companyaddress.replace(/\s+/g, "");
          Companylatitude=that.data.latitude;
          Companylongitude=that.data.longitude;
          
      }
      /*if(b){
        if(username==null || username==""){
            b=false
            wx.showToast({
              title: "用户名不能为空！",
              icon: "none",
              duration: 3000
             })
        }
      }*/
    if(b){
      if(realname==null || realname==""){
          b=false
          wx.showToast({
            title: "姓名不能为空！",
            icon: "none",
            duration: 3000
           })
      }
    }
      if(b){
        if(phone==null || phone==""){
          b=false
          wx.showToast({
            title: "手机号码不能为空！",
            icon: "none",
            duration: 3000
           })
        }
      }
      if(that.data.FHEnable4=="0"){
      if(b){
        if(code==null || code==""){
          b=false
          wx.showToast({
            title: "手机验证码不能为空！",
            icon: "none",
            duration: 3000
           })
        }
      }
      }
      if(that.data.ifnoqy==0){
      if(b){
      if(psd==null || psd==""){
        b=false
        wx.showToast({
          title: "登录密码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      var patrn =/^(?=.{6,16}$)[0-9a-zA-Z_]+$/;
       if(!patrn.exec(psd)) {
        b=false
        wx.showToast({
          title: "登录密码由6-16位数字、字母、下划线组成！",
          icon: "none",
          duration: 3000
         })
       }
    }
    if(b){
      if(psd2==null || psd2==""){
        b=false
        wx.showToast({
          title: "确认登录密码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(psd!=psd2){
       b=false
       wx.showToast({
         title: "两次输入登录密码不一致！",
         icon: "none",
         duration: 3000
        })
      }
   }
  }
   
   if(b){
    if(yqm==""){
     b=false;
     wx.showToast({
       title: "邀请码不能为空！",
       icon: "none",
       duration: 3000
    })
    }
    }
   if(b){
    if(that.data.xzimage=="../../images/wxz.png"){
     b=false;
     wx.showToast({
       title: "未选择同意协议和隐私政策！",
       icon: "none",
       duration: 3000
    })
    }
    }
      if(b){
        var ifydl=0;
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        if(userId&&LoginId){
            ifydl=1;
        }
        if(that.data.savebutton=="提交"){
        that.setData({
          savebutton:"请稍候..."
      })
      wx.login({
        success: function (resc) {
          
            if (resc.code) {
      var tguid = wx.getStorageSync("tguid")
      if(that.data.index>0){
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
             action:"register",
             yqm:yqm,
             utype:that.data.index,
             username:username,
             realname:realname,
             phone:phone,
             code:code,
             lcode:resc.code,
             yzcodeid:that.data.yzcodeid,
             ifydl:ifydl,
             pwd:psd,
             CompanyName:CompanyName,
             CompanyNumber:CompanyNumber,
             CompanyCert:CompanyCert,
             CompanyPic:CompanyPic,
             CompanyProvince:CompanyProvince,
             CompanyCity:CompanyCity,
             CompanyCounty:CompanyCounty,
             Companyaddress:Companyaddress,
             Companylatitude:Companylatitude,
             Companylongitude:Companylongitude
          },
          method:"POST",
          dataType:"json", 
          header: { 
            "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success (res) { 
            that.setData({
              savebutton:"提交"
          })
            if(res.data.status==1){
               wx.removeStorageSync('ifnoqy');
               if(ifydl==0){
                wx.setStorageSync('userId', res.data.userId)
                wx.setStorageSync('LoginId', res.data.LoginId) 
                wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo))
                wx.setStorageSync("openid", res.data.userInfo.openid);
                wx.setStorageSync('IFLogin', 1) 
                if(res.data.userInfo.uLevel2==2){
                    wx.setStorageSync('IFagent', 1);
                }else{
                    wx.setStorageSync('IFagent', 0);
                }
                app.globalData.IFRefreshmy=1 
                app.globalData.IFRefreshindex=1 
                if(that.data.IFDefault==1){
                     wx.setStorageSync('LoginData', username+"|"+password)
                }else{
                     wx.removeStorageSync('LoginData')
                }
                wx.switchTab({
                     url: '../index/index'
                 })
               }else{
                wx.navigateTo({
                  url: '../myusers/index'
              })
               }
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 3000
               })
            }
            }
        })

        /*wx.uploadFile({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          filePath: FilePath,
          name: 'image',
          formData: {
             action:"register",
             yqm:that.data.yqm,
             utype:that.data.index,
             realname:realname,
             phone:phone,
             code:code,
             pwd:psd, 
             CompanyName:CompanyName,
             CompanyNumber:CompanyNumber,
             CompanyProvince:CompanyProvince,
             CompanyCity:CompanyCity,
             CompanyCounty:CompanyCounty,
             Companyaddress:Companyaddress,
             Companylatitude:Companylatitude,
             Companylongitude:Companylongitude
          },
          dataType:"json", 
          success (res) { 
            that.setData({
              savebutton:"提交"
            })
            var data=JSON.parse(res.data);
            if(data.status==1){
              wx.showToast({
                title: data.msg,
                icon: "success",
                duration: 3000
               })
               sopenFrame=setInterval(function (){
                clearInterval(sopenFrame);
                wx.navigateBack({})
                
          }, 1000)
            }else{
               wx.showToast({
                title: data.msg,
                icon: "none",
                duration: 3000
               })
            }
            }
        })*/
      }else{
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
           action:"register",
           yqm:yqm,
           utype:that.data.index,
           username:username,
           realname:realname,
           phone:phone,
           code:code,
           lcode:resc.code,
           yzcodeid:that.data.yzcodeid,
           ifydl:ifydl,
           pwd:psd
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          that.setData({
            savebutton:"提交"
        })
          if(res.data.status==1){
             wx.removeStorageSync('ifnoqy');
             that.setData({
                yzcode:""
            })
            that.getyzcode();
             if(ifydl==0){
              wx.setStorageSync('userId', res.data.userId)
              wx.setStorageSync('LoginId', res.data.LoginId) 
              wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo))
              wx.setStorageSync("openid", res.data.userInfo.openid);
              wx.setStorageSync('IFLogin', 1) 
              if(res.data.userInfo.uLevel2==2){
                  wx.setStorageSync('IFagent', 1);
              }else{
                  wx.setStorageSync('IFagent', 0);
              }
              app.globalData.IFRefreshmy=1 
              app.globalData.IFRefreshindex=1 
              if(that.data.IFDefault==1){
                   wx.setStorageSync('LoginData', username+"|"+password)
              }else{
                   wx.removeStorageSync('LoginData')
              }
               wx.switchTab({
                   url: '../index/index'
               })
             }else{
              wx.navigateTo({
                url: '../myusers/index'
            })
             }
          }else{
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          }
      })
    }
    }
  }
});
}
    }
    },getphoneValue:function(e) {
      that.setData({
        phone:e.detail.value.replace(/\s+/g, "")
      })
       
  },getyzcodeValue:function(e) {
    that.setData({
      yzcode:e.detail.value.replace(/\s+/g, "")
    })
     
},getcode:function(e) {
     
      var b=true
     
      if(b){
        if(that.data.phone==""){
          b=false
          wx.showToast({
            title: "手机号码不能为空！",
            icon: "none",
            duration: 3000
           })
        }
      }
      if(b){
        if(that.data.yzcode==""){
          b=false
          wx.showToast({
            title: "图形验证码不能为空！",
            icon: "none",
            duration: 3000
           })
        }
      }
      if(b){
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
             action:"sendphonecode",
             phone:that.data.phone,
             code:that.data.yzcode,
             yzcodeid:that.data.yzcodeid,
          },
          method:"POST",
          dataType:"json", 
          header: { 
            "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success (res) { 
            if(res.data.status==1){
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 3000
               })
               that.gettime()
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 3000
               })
            }
            }
        })
      }
    },  
    gettime:function() {
        if (wait == 0) {
            that.setData({
                getcode:"获取验证码"
            })
            wait = 120;
        } else {
            that.setData({
                 getcode: "重新发送(" + wait + ")"
             })
            wait--;
            setTimeout(function() {
                that.gettime()
            }, 1000)
        }

    },checkphone:function(e) {
        var phone=e.detail.value
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
             action:"checkphone",
             t:0,
             phone:phone
          },
          method:"POST",
          dataType:"json", 
          header: { 
            "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success (res) { 
            wx.showToast({
              title: res.data.msg,
              icon: "success",
              duration: 3000
             })
            }
        })
    },onread:function(e) {
      var id=e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../articledetail/index?id=' + id
       })
    },onxz:function(e) {
      if(that.data.xzimage=="../../images/wxz.png"){
         that.setData({
            xzimage:"../../images/xz.png"
         })
      }else{
        that.setData({
          xzimage:"../../images/wxz.png"
       })
      }
    },checkyqm:function(e) {
      var yqm=e.detail.value
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
           action:"checkyqm",
           yqm:yqm
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==1){
            that.setData({
               showyqxm:1,
               yqxm:res.data.name
            })
          }else{
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          }
      })
  }
})
