// pages/mine/wxml/shopregistration/shopregistration.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    frontfileid:'',
    backfileid:'',
    completefileid:'',
    frontPhoto: "images/IDfront.png",
    backPhoto: "images/IDback.png",
    completePhoto: "images/complete.png",
    frontPhotoInfo: false,
    backPhotoInfo: false,
    completePhotoInfo: false,
    imgInfo: false,
    array: ['企业店铺', '个人店铺', '拍卖行'],
    bankarray: [],
    // newarray: ['珠宝', '玉石', '古玩', '字画', '手表', '其他'],
    index: 0,
    indexs: 0,
    bankindex:0
  },
  bindPickerChange: function(e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    // if (e.detail.value == 0) {
    //   this.setData({
    //     newarray: ['珠宝', '玉石', '古玩', '字画', '手表', '其他']
    //   })
    // } else if (e.detail.value == 1) {
    //   this.setData({
    //     newarray: ['手工艺品', '二手货']
    //   })
    // } else if (e.detail.value == 2) {
    //   this.setData({
    //     newarray: ['奢侈品', '古玩']
    //   })
    // }
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChanges: function(e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      indexs: e.detail.value
    })
  },
  bindPickerChangebank: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      bankindex: e.detail.value
    })
  },
  onSelectPhoto: function(e) {
    let _this = this;
    let imgInfo = false;
    let frontPhotoInfo, backPhotoInfo, completePhotoInfo;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        //console.log(res)
        wx.uploadFile({
          url: app.globalData.prodUrl + 'web-cp-all/upload_pm/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(data) {
            var json = JSON.parse(data.data);
            if (e.currentTarget.dataset.btnname == "front") {
              frontPhotoInfo = true;
              _this.setData({
                frontfileid: json.file_id,
                frontPhoto: res.tempFilePaths[0],
                frontPhotoInfo: frontPhotoInfo
              });
              //console.log('frontPhotoInfo：' + _this.data.frontPhotoInfo);
            } else if (e.currentTarget.dataset.btnname == "back") {
              backPhotoInfo = true;
              _this.setData({
                backfileid: json.file_id,
                backPhoto: res.tempFilePaths[0],
                backPhotoInfo: backPhotoInfo
              })
              //console.log('backPhotoInfo:' + _this.data.backPhotoInfo);
            } else if (e.currentTarget.dataset.btnname == "complete") {
              completePhotoInfo = true;
              _this.setData({
                completefileid: json.file_id,
                completePhoto: res.tempFilePaths[0],
                completePhotoInfo: completePhotoInfo
              })
              //console.log('completePhotoInfo:' + _this.data.completePhotoInfo)
            }
            if (_this.data.frontPhotoInfo && _this.data.backPhotoInfo && _this.data.completePhotoInfo) {
              _this.setData({
                imgInfo: true
              })
            }
          }
        });
      }
    });
  },

  //上传
  formSubmit: function(e) {
    var _this = this;
    //console.log(_this.data.frontPhoto + ',' + _this.data.backPhoto + ',' + _this.data.completePhoto);
    //console.log(e);
    let formData = e.detail.value
    formData.frontPhotoUrl = this.data.frontPhoto[0];
    formData.backPhotoUrl = this.data.backPhoto[0];
    formData.completePhotoUrl = this.data.completePhoto[0];
    var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    var regs = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    var regname = /^([\u4e00-\u9fa5]){2,7}$/;
    if (e.detail.value.shopname !== "") {
      if (reg.test(e.detail.value.shopphone || e.detail.value.shopphone !== "")) {
        if (regs.test(e.detail.value.ID || e.detail.value.ID !== "")) {
          if (regname.test(e.detail.value.realname || e.detail.value.realname !== "")) {
            if (e.detail.value.shopaddress !== "") {
                wx.request({
                  url: app.globalData.prodUrl + 'web-pm/wx/pm/new_register_shop',
                  header: {
                    'ydw-token': wx.getStorageSync('key').ydw_token
                  },
                  data: {
                    name: e.detail.value.shopname,
                    mobile: e.detail.value.shopphone,
                    address: e.detail.value.shopaddress,
                    real_name: e.detail.value.realname,
                    identity_card: e.detail.value.ID,
                    // formId: e.detail.formId,
                    manage_range: e.detail.value.twolevelmenu,
                    shop_type: e.detail.value.choice,
                    deposit_bank_card: e.detail.value.bankcode,
                    deposit_bank: _this.data.bankarray[e.detail.value.bankname],
                    deposit_bank_name: e.detail.value.bankusername,
                    identity_file_id: _this.data.frontfileid + ',' + _this.data.backfileid + ',' + _this.data.completefileid
                  },
                  success: function(data) {
                    if (data.data.tip.msg == "提交成功") {
                      wx.showToast({
                        title: '提交成功，请耐心等待1~2个工作日',
                        icon: 'success',
                        duration: 3000,
                        success: function() {
                          wx.navigateBack({
                            delta: 1,
                            success: function() {
                              var pages = getCurrentPages();
                              var page = getCurrentPages().pop();
                              if (page == undefined || page == null) return;
                              pages[0].onLoad();
                            }
                          });
                        }
                      });
                    } else {
                      wx.showToast({
                        title: data.data.tip.msg,
                        icon: 'none',
                        duration: 2000
                      })
                    }
                  }
                })
            } else {
              wx.showToast({
                title: '请输入地址',
                icon: 'none',
                duration: 2000
              })
            }

          } else {
            wx.showToast({
              title: '请正确输入真实姓名',
              icon: 'none',
              duration: 2000
            })
          }

        } else {
          wx.showToast({
            title: '请正确输入身份证号',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        wx.showToast({
          title: '请正确输入手机号',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.showToast({
        title: '请填写店铺名称',
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_bank',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      success: function (data) {
        var arr = [];
        for(var i=0;i<data.data.data.length;i++){
          arr.push(data.data.data[i].bank_name)
        }
        _this.setData({
          bankarray:arr
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})