var WxParse = null;
var app = getApp()
var that=null
var sopenFrame=null
var areas = null;
var name = "";
      var strmoney="";
      var dgzh="";
      var frname="";
      var fridcard="";
      var number="";
      var tel="";
      var Province="";
      var City="";
      var County="";
      var detail="";
      var fwbody="";
      var AccountName="";
      var BankName="";
      var BankCard="";
      var BankDepos="";
var context = null; // 使用 wx.createContext 获取绘图上下文 context
var mCanvas = null;
Page( {
  data: {
    loadingHidden:false,
    loading:false,
    sjname:"",
    number:"",
    tel:"",
    sjimage0:"../../images/add.png",
    sjimage1:"../../images/add.png",
    sjimage2:"../../images/add.png",
    sjimage3:"../../images/add.png",
    sjimage4:"../../images/add.png",
    sjimage5:"../../images/add.png",
    sjimage6:"../../images/add.png",
    addimage0:"",
    addimage1:"",
    addimage2:"",
    addimage3:"",
    addimage4:"",
    addimage5:"",
    addimage6:"",
    annotatemap:"",
    detail:"",
    fwbody:"",
    sectionHidden:true,
    sectionHidden0:true,
    IFDefault:1,
    ifedit:0,
    ifeditbank:0,
    savebutton:"修改资料",
    Provinces:[],
    Citylist:[],
            Citys:[],
            Countys:[],
            hx_index:-1,
            hx_index0:-1,
            hx_index1:-1,
            area:"",
            addressId:0,
            clslist:[],
            hc_index:-1,
            clsid:0,
            clsname:"",
            latitude:0,
            longitude:0,
            scale:15,
            markers:  [],
            bzlatitude:0,
            bzlongitude:0,
            ylmoney:0,
            ylmoney0:0,
            sjylmoney:"",
            byNumber:0,
            dgzh:"",
            frname:"",
            fridcard:"",
            index: -1,
    BankNames:[],
    khname:"",
    bankcard:"",
        bankname: "",
        BankDepos: "",
    uid:0,
    hasDraw: false, //默认没有画
    showtitle:"",
        showbody:""
  },
  onReady: function(e) {
    // 使用 wx.createMapContext 获取 map 上下文 
    this.mapCtx = wx.createMapContext('myMap');
  },
  onLoad: function() {
    
      that = this
      WxParse = require('../../wxParse/wxParse.js');
      areas = require('../../area.js'); 
     
      
      that.setData({
        Provinces:areas.areaslist,
        Citylist:areas.Citylist
      })
      that.rdata()
      this.setData({
        hasDraw: false,
      })
     
    },rdata: function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      var userInfo = JSON.parse(userInfodata)
      if(userId&&LoginId){
        var userInfo = JSON.parse(userInfodata)
        that.setData({
            uid:userInfo.id
         })
        }
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getbusinessinfo",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        success (res) { 
          if(res.data.status==1){
         
              var webbank=res.data.webbank
              
              var webbanks=webbank.split('|')
              var banks=""
              var bindex=0
              
              for(var i=0;i<webbanks.length;i++){
                    if(banks!=""){
                       banks+=",{\"key\":\""+webbanks[i]+"\"}"
                    }else{
                       banks+="{\"key\":\""+webbanks[i]+"\"}"
                    }
                    if(res.data.bank==webbanks[i]){
                        bindex = i
                    }
              }
              that.setData({
                   index: bindex,
                   BankNames:JSON.parse("["+banks+"]"),
              })
              var body = res.data.showbody;
                      WxParse.wxParse('body', 'html', body, that);
                      var index=-1
                      var index0=-1
                      var index1=-1
                      var Province=res.data.province
                      var City=res.data.city
                      var County=res.data.county
                      if(Province!=""&&City!=""&&County!=""){
                      var provinces=that.data.Provinces
                      for(var i=0;i<provinces.length;i++){
                            if(provinces[i].ProvinceID==Province){
                              index=i;
                              var citys=provinces[i].City;
                              for(var j=0;j<citys.length;j++){
                                   if(citys[j].CityID==City){
                                        index0=j;
                                        var countys=citys[j].County;
                                        for(var k=0;k<countys.length;k++){
                                             if(countys[k].CountyID==County){
                                                  index1=k;
                                                  break;
                                             } 
                                            
                                        }
                                        break;
                                   }
                                   
                              }
                              break;
                            }
                      }
                      }
                      that.setData({
                        hx_index: index
                      })
                      var citys=(index==-1?[]:that.data.Provinces[index].City)
              that.setData({
                Citys:citys,
                hx_index0: index0
              })
              var countys=(index0==-1?[]:that.data.Citys[index0].County)
              that.setData({
                Countys:countys,
                hx_index1: index1
              })
              var area=""
              if(that.data.hx_index!=-1){
                  area+=that.data.Provinces[that.data.hx_index].ProvinceName
              }
              if(that.data.hx_index0!=-1){
                  area+=that.data.Citys[that.data.hx_index0].CityName
              }
              if(that.data.hx_index1!=-1){
                   area+=that.data.Countys[that.data.hx_index1].CountyName
              }
            that.setData({
              
              clslist:res.data.clslist,
              hc_index:res.data.clsindex,
              clsid:res.data.clslist[res.data.clsindex].id,
              ylmoney:res.data.ylmoney,
              ylmoney0:res.data.ylmoney0,
              sjylmoney:res.data.sjylmoney,
              showtitle:res.data.showtitle,  
              clsname:res.data.clsname,  
              sjname:res.data.name,  
              number:res.data.xydm,  
              dgzh:res.data.dgzh==""?"无":res.data.dgzh,  
              frname:res.data.frname,  
              fridcard:res.data.fridcard,  
              tel:res.data.phone,  
              addimage0:res.data.image,
              addimage1:res.data.image0,
              addimage2:res.data.image1,
              addimage3:res.data.image2,
              addimage4:res.data.image3,
              sjimage0:res.data.minimage,  
              sjimage1:res.data.minimage0, 
              sjimage2:res.data.minimage1, 
              sjimage3:res.data.minimage2, 
              sjimage4:res.data.minimage3, 
              area:area,
              detail:res.data.address,
              annotatemap:(res.data.xcoo!=""&&res.data.ycoo!=""?"已标注":""),
              latitude: res.data.xcoo,
              longitude: res.data.ycoo,
              bzlatitude: res.data.xcoo,
              bzlongitude: res.data.ycoo,
              fwbody: res.data.range,
              khname:res.data.khname,
              bankcard:res.data.bankcard,
              bankname:res.data.bankname, 
              BankDepos:res.data.bankkhh, 
              ifedit:res.data.ifedit,
              ifeditbank:res.data.ifeditbank,
              loadingHidden:true
             })
          }
        }
        })
            
    },getylmoney:function(e) {
       
    },clsChange:function(e) {
      if(that.data.ifedit==1){
      var index=e.detail.value;
      that.setData({
        hc_index: index,
        clsid:that.data.clslist[index].id,
        clsname:that.data.clslist[index].name,
        sectionHidden0:true
      })
    }
    },onarea:function(e) {
      if(that.data.ifedit==1){
      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
      })
    },onannotatemap:function(e) {
      if(that.data.ifedit==1){
      /*if(that.data.hx_index==-1||that.data.hx_index0==-1){
        wx.showToast({
          title: "请选选择地区！",
          icon: "none",
          duration: 3000
         })
      }else{*/
        if(that.data.latitude==""||that.data.longitude==""){
//获取经纬度
wx.getLocation({
  type: 'gcj02',
  success: function (res) {
    var latitude = res.latitude
    var longitude = res.longitude
    that.setData({
      latitude: latitude,
      longitude: longitude,
    })
    
  },fail: function (res) {
    wx.showToast({
      title: "获取定位失败！",
      icon: "none",
      duration: 3000
     })
  }
})
        }else{
          that.setData({
            loading:true,
            markers:  [{
              id:8900,
              latitude: that.data.latitude,
              longitude: that.data.longitude,
              iconPath: '../../images/position-picker2.png',
                  width: 80,
                  height: 80,
                  enableDragging: true
            }]
          })
        }
          /*var center="";
          if(that.data.hx_index>-1&&that.data.hx_index0>-1){
            center=that.data.Citys[that.data.hx_index0].center
            if(that.data.hx_index1>-1){
                center=that.data.Countys[that.data.hx_index1].center
            }
            if(center!=""){
              var acenter=center.split(",");
            that.setData({
              latitude: acenter[1],
              longitude: acenter[0],
            })
            }
          }*/

          that.setData({
               sectionHidden0:false,
               loading:true,
               savebutton:"标注好了，返回"
          })
          var sopenFrame0=setInterval(function (){
            clearInterval(sopenFrame0);  
          that.setData({
              loading:false 
          })
           },1000)
       //}
      }
    },maptap:function(e) {
     
      that.setData({
        loading:true,
        bzlatitude: e.detail.latitude,
        bzlongitude: e.detail.longitude,
        annotatemap:"已标注",
        markers:  [{
          id:8900,
          latitude: e.detail.latitude,
          longitude:  e.detail.longitude,
          iconPath: '../../images/position-picker2.png',
              width: 80,
              height: 80,
              enableDragging: true
        }]
      })
      var sopenFrame0=setInterval(function (){
        clearInterval(sopenFrame0);  
      that.setData({
          loading:false 
      })
       },300)
    },
    regionchange(e) {
      console.log(e)
    },markertap:function(e) {
      
    },closesection:function(e) {
      that.setData({
        sectionHidden:true,
        
      })
    },closesection0:function(e) {
      that.setData({
        sectionHidden0:true,
        savebutton:"提交"
      })
    },insection:function(e) {
      
    },onsjimage:function(e) {
      if(that.data.ifedit==1){
      var id = e.currentTarget.dataset.id
      var yimage=that.data.sjimage0
      if(id==1){
        yimage=that.data.sjimage1
      }else if(id==2){
        yimage=that.data.sjimage2
      }else if(id==3){
        yimage=that.data.sjimage3
      }else if(id==4){
        yimage=that.data.sjimage4
      }else if(id==5){
        yimage=that.data.sjimage5
      }else if(id==6){
        yimage=that.data.sjimage6
      }
      if(yimage=="../../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;
              if(id==0){
                that.setData({
                  sjimage0:"../../images/ajaxLoader.gif"
               })
              }else if(id==1){
                that.setData({
                  sjimage1:"../../images/ajaxLoader.gif"
                })
              }else if(id==2){
                that.setData({
                  sjimage2:"../../images/ajaxLoader.gif"
                })
              }else if(id==3){
                that.setData({
                  sjimage3:"../../images/ajaxLoader.gif"
                })
              }else if(id==4){
                that.setData({
                  sjimage4:"../../images/ajaxLoader.gif"
                })
              }else if(id==5){
                that.setData({
                  sjimage5:"../../images/ajaxLoader.gif"
                })
              }else if(id==6){
                that.setData({
                  sjimage6:"../../images/ajaxLoader.gif"
                })
              }
              
              var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
              wx.uploadFile({
                url: app.globalData.apiurl + "/xapiajax.ashx", //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'image',
                formData: {
                  action:"uploadImage",
                  userId:(!userId?"":userId),
                  LoginId:(!LoginId?"":LoginId)
                },
                fail: function(err) {
                  wx.showToast({
                    title: "上传出错了",
                    icon: "none",
                    duration: 2000
                })
                if(id==0){
                  that.setData({
                    sjimage0:yimage
                 })
                }else if(id==1){
                  that.setData({
                    sjimage1:yimage
                  })
                }else if(id==2){
                  that.setData({
                    sjimage2:yimage
                  })
                }else if(id==3){
                  that.setData({
                    sjimage3:yimage
                  })
                }else if(id==4){
                  that.setData({
                    sjimage4:yimage
                  })
                }else if(id==5){
                  that.setData({
                    sjimage5:yimage
                  })
                }else if(id==6){
                  that.setData({
                    sjimage6:yimage
                  })
                }
                },
                success (res0){
                   try{
                    var data=JSON.parse(res0.data);
                    
                   
                    if(data.status==1){
                      wx.showToast({
                        title: data.msg,
                        icon: "success",
                        duration: 1000
                      })
                      if(id==0){
                        that.setData({
                          sjimage0:data.url,
                          addimage0:data.pic
                       })
                      }else if(id==1){
                        that.setData({
                          sjimage1:data.url,
                          addimage1:data.pic
                        })
                      }else if(id==2){
                        that.setData({
                          sjimage2:data.url,
                          addimage2:data.pic
                        })
                      }else if(id==3){
                        that.setData({
                          sjimage3:data.url,
                          addimage3:data.pic
                        })
                      }else if(id==4){
                        that.setData({
                          sjimage4:data.url,
                          addimage4:data.pic
                        })
                      }else if(id==5){
                        that.setData({
                          sjimage5:data.url,
                          addimage5:data.pic
                        })
                      }else if(id==6){
                        that.setData({
                          sjimage6:data.url,
                          addimage6:data.pic
                        })
                      }
                        
                    }else{
                      wx.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 2000
                    })
                    if(id==0){
                      that.setData({
                        sjimage0:yimage
                     })
                    }else if(id==1){
                      that.setData({
                        sjimage1:yimage
                      })
                    }else if(id==2){
                      that.setData({
                        sjimage2:yimage
                      })
                    }else if(id==3){
                      that.setData({
                        sjimage3:yimage
                      })
                    }else if(id==4){
                      that.setData({
                        sjimage4:yimage
                      })
                    }else if(id==5){
                      that.setData({
                        sjimage5:yimage
                      })
                    }else if(id==6){
                      that.setData({
                        sjimage6:yimage
                      })
                    }
                      
                    }
                   }catch(err){
                    wx.showToast({
                      title: "上传出错",
                      icon: "none",
                      duration: 2000
                  })
                  if(id==0){
                    that.setData({
                      sjimage0:yimage
                   })
                  }else if(id==1){
                    that.setData({
                      sjimage1:yimage
                    })
                  }else if(id==2){
                    that.setData({
                      sjimage2:yimage
                    })
                  }else if(id==3){
                    that.setData({
                      sjimage3:yimage
                    })
                  }else if(id==4){
                    that.setData({
                      sjimage4:yimage
                    })
                  }else if(id==5){
                    that.setData({
                      sjimage5:yimage
                    })
                  }else if(id==6){
                    that.setData({
                      sjimage6:yimage
                    })
                  }
                   }
                }
                
              });
        }
      })
    }
  }
    },BankChange:function(e) {
      if(that.data.ifeditbank==1){
      var index=e.detail.value;
      var key=that.data.BankNames[index].key
      that.setData({
        index: index
      })
    }
    },saveaddress:function(e) {
      if(that.data.savebutton=="修改资料"){
          name = e.detail.value.txtName.replace(/\s+/g, "");
          strmoney = e.detail.value.txtylmoney.replace(/\s+/g, "");
          dgzh=e.detail.value.txtdgzh.replace(/\s+/g, "");
          frname=e.detail.value.txtfrname.replace(/\s+/g, "");
          fridcard=e.detail.value.txtfridcard.replace(/\s+/g, "");
          number=e.detail.value.txtnumber.replace(/\s+/g, "");
          tel=e.detail.value.txttel.replace(/\s+/g, "");
          Province=(that.data.hx_index==-1?"":that.data.Provinces[that.data.hx_index].ProvinceID)
          City=(that.data.hx_index0==-1?"":that.data.Citys[that.data.hx_index0].CityID)
          County=(that.data.hx_index1==-1?"":that.data.Countys[that.data.hx_index1].CountyID)
          detail=e.detail.value.detail.replace(/\s+/g, "");
          fwbody=e.detail.value.fwbody
          AccountName=e.detail.value.AccountName.replace(/\s+/g, "");
          BankName=(that.data.index==-1?"":that.data.BankNames[that.data.index].key);
          BankCard=e.detail.value.BankCard.replace(/\s+/g, "");
          BankDepos=e.detail.value.BankDepos.replace(/\s+/g, "");
      }
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var b=true
      if(that.data.savebutton=="修改资料"){
      if(that.data.clsid==0){
        b=false
        wx.showToast({
          title: "商家分类未选择！",
          icon: "none",
          duration: 3000
         })
    }
    if(b){
      if(name==""){
          b=false
          wx.showToast({
            title: "商家名称不能为空！",
            icon: "none",
            duration: 3000
           })
      }
     }
     if(b){
      if(strmoney==""){
          b=false
          wx.showToast({
            title: "商家让利不能为空！",
            icon: "none",
            duration: 3000
           })
      }
     }
     if(b){
      if(number==""){
        b=false
        wx.showToast({
          title: "信用代码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    
    if(b){
      if(frname==""){
        b=false
        wx.showToast({
          title: "法人姓名不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(fridcard==""){
        b=false
        wx.showToast({
          title: "法人身份证号码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    
    if(b){
      if(tel==""){
        b=false
        wx.showToast({
          title: "联系电话不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.addimage0==""){
        b=false
        wx.showToast({
          title: "门头图片未上传！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.addimage1==""){
        b=false
        wx.showToast({
          title: "营业执照未上传！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.addimage2==""){
        b=false
        wx.showToast({
          title: "银行卡照片未上传！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.addimage3==""){
        b=false
        wx.showToast({
          title: "身份证正面未上传！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.addimage4==""){
        b=false
        wx.showToast({
          title: "身份证反面未上传！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.hx_index==-1){
        b=false
        wx.showToast({
          title: "省份必须选择！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.hx_index0==-1){
        b=false
        wx.showToast({
          title: "城市必须选择！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(detail==null || detail==""){
        b=false
        wx.showToast({
          title: "详细地址不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(that.data.bzlatitude==0||that.data.bzlongitude==0){
        b=false
        wx.showToast({
          title: "未标注地图！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(AccountName==null || AccountName==""){
        b=false
        wx.showToast({
          title: "开户姓名不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(BankName==null || BankName==""){
        b=false
        wx.showToast({
          title: "银行名称未选择！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(BankCard==null || BankCard==""){
        b=false
        wx.showToast({
          title: "银行卡号不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(BankDepos==null || BankDepos==""){
        b=false
        wx.showToast({
          title: "开户行不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    
     
      if(b){
        that.setData({
          savebutton:"请稍候..."
      })
     
  
            
    wx.request({
      url: app.globalData.apiurl + "/xapiajax.ashx", 
      data: { 
        action:"appupdatebusiness",
         userId:(!userId?"":userId),
         LoginId:(!LoginId?"":LoginId),
         clsid:that.data.clsid,
         name:name,
         money:strmoney,
         tel:tel,
         number:number,
         dgzh:dgzh,
         frname:frname,
         fridcard:fridcard,
         image0:that.data.addimage0,
         image1:that.data.addimage1,
         image2:that.data.addimage2,
         image3:that.data.addimage3,
         image4:that.data.addimage4,
         province:Province,
         city:City,
         county:County,
         address:detail,
         longitude:that.data.bzlongitude,
         latitude:that.data.bzlatitude,
         fwbody:fwbody,
         accountname:AccountName,
         bankname:BankName,
         bankcard:BankCard,
         bankaddress:BankDepos,
      },
      method:"POST",
      dataType:"json", 
      header: { 
        "content-type": "application/x-www-form-urlencoded" // 默认值
      },
      success (res) { 
        if(res.data.status==1){
          wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo)) 
          app.globalData.IFRefreshmy=1
         var sopenFrame0=setInterval(function (){
          clearInterval(sopenFrame0);      
         wx.showModal({
          title: "申请提示",
          content:"您的修改申请已经提交成功了，请等待审核！",
                          showCancel:false,
                          success (res1) {
                            if (res1.confirm) {
          
           
  
           wx.navigateBack({})
  
          }
        }
      })
    }, 2000);
        }else{
          that.setData({
            savebutton:"修改资料"
           }) 
           wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 3000
           })
        }
        }
    })
  
        
      }
  }else if(that.data.savebutton=="标注好了，返回"){
     that.setData({
      sectionHidden0:true,
      savebutton:"修改资料"
     }) 
  }
}

})
