var app = getApp()
var that=null
var sopenFrame=null
var areas = 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,
    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,
            uid:0,
  },
  onReady: function(e) {
    // 使用 wx.createMapContext 获取 map 上下文 
    this.mapCtx = wx.createMapContext('myMap');
  },
  onLoad: function() {
    
      that = this
      areas = require('../../area.js'); 
      that.setData({
        Provinces:areas.areaslist,
        Citylist:areas.Citylist
      })
      that.rdata() 
    },rdata: function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      if(userId&&LoginId){
      var userInfo = JSON.parse(userInfodata)
      that.setData({
          uid:userInfo.id
       })
      }
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getbusinessinfo0",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        success (res) { 
          if(res.data.status==1){
            that.setData({
              clslist:res.data.clslist,
              loadingHidden:true
             })
          }
        }
        })
            
    },clsChange:function(e) {
      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) {
      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.hx_index==-1||that.data.hx_index0==-1){
        wx.showToast({
          title: "请选选择地区！",
          icon: "none",
          duration: 3000
         })
      }else{
//获取经纬度
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
     })
  }
})

         /* 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,
        markers:  [{
          id:8900,
          latitude: e.detail.latitude,
          longitude:  e.detail.longitude,
          iconPath: '../../images/position-picker2.png',
              width: 80,
              height: 80,
              enableDragging: true
        }],
        annotatemap:"已标注",
        
      })
      var sopenFrame0=setInterval(function (){
        clearInterval(sopenFrame0);  
      that.setData({
          loading:false 
      })
       },500)
    },
    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) {
      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){
                    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
                      })
                    }
                      
                    }
                  
                }
              });
        }
      })
    }
    },saveaddress:function(e) {
      if(that.data.savebutton=="标注好了，返回")
      {
      that.setData({
        sectionHidden0:true,
        savebutton:"提交"
      })
    }else{
     
      var name = e.detail.value.txtName.replace(/\s+/g, "");
      var number=e.detail.value.txtnumber.replace(/\s+/g, "");
      var tel=e.detail.value.txttel.replace(/\s+/g, "");
      var Province=(that.data.hx_index==-1?"":that.data.Provinces[that.data.hx_index].ProvinceID)
      var City=(that.data.hx_index0==-1?"":that.data.Citys[that.data.hx_index0].CityID)
      var County=(that.data.hx_index1==-1?"":that.data.Countys[that.data.hx_index1].CountyID)
      var detail=e.detail.value.detail.replace(/\s+/g, "");
      var fwbody=e.detail.value.fwbody
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var b=true
      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(number==""){
        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(that.data.savebutton!='提交'){
         b=false
      }
    }
      if(b){
        that.setData({
            savebutton:"请稍候..."
        })
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"updatebusinessinfo0",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           clsid:that.data.clsid,
           name:name,
           number:number,
           tel:tel,
           image0:that.data.addimage0,
           image1:that.data.addimage1,
           image2:that.data.addimage2,
           image3:that.data.addimage3,
           image4:that.data.addimage4,
           image5:that.data.addimage5,
           image6:that.data.addimage6,
           province:Province,
           city:City,
           county:County,
           address:detail,
           longitude:that.data.bzlongitude,
           latitude:that.data.bzlatitude,
           fwbody:fwbody
        },
        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 (res0) {
                              if (res0.confirm) {
            
             that.setData({
                 savebutton:"提交"
             }) 
              wx.navigateBack({})
            }
          }
        })
      }, 2000);
          }else{
            that.setData({
              savebutton:"保存"
             }) 
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          }
      })
      }
    }
    }
})
