// pages/bindBankCard/index.js
import { customRequest } from '../../utils/request.js';
import { baseUrl } from '../../utils/request.js';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    multiSelected: false,
    multiIndex: [0, 0],
    multiIndexTemp: [0, 0],
    // multiArray: [['无脊柱动物', '脊柱动物'], ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'], ['猪肉绦虫', '吸血虫']],
    multiArray: [[], []],
    multiArrayTemp: [[], []],
    multiArrayRecord: [[], []],
    noticeShow: false,
    cardNum: "",
    userName: "",
    bankName: "",
    cardNumif: false,
    userNameif: false,
    bankNameif: false,
    multiIndexif: true,
    /*multiArray: [
      ['河南省', '1'],
      ['郑州市', '2', '2', '2', '2']
    ],
    multiIndex: [0,0],*/
    user: {},
    bankImageUrl: "",
    bankImagemUrl: "",
    switch1Checked: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getProvinceByPid();
  },
  switch1Change: function (e) {
    console.log("radio发生change事件，携带value值为：", e.detail.value);
    console.log(e.detail.value);

    var check = this.data.switch1Checked;
    if (check) {
      this.data.switch1Checked = false;
      console.log("已取消选中");
      wx.showToast({
        title: "请先阅读并同意《服务协议》及《隐私政策》",
        icon: "none",
        duration: 2000,
      });
    } else {
      this.data.switch1Checked = true;
      console.log("已选中");
    }
    this.setData({
      switch1Checked: this.data.switch1Checked,
    });

    // if(!e.detail.value){

    // }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      user: app.globalData.user,
    });
  },

  onSubmit: function (e) {
    var that = this;

    if (app.isEmptyCheck(e.detail.value.cardNum)) {
      wx.showToast({
        icon: "none",
        title: "请输入银行卡号",
        duration: 3000,
      });
      return;
    }
    if (!that.data.switch1Checked) {
      wx.showToast({
        title: "请先阅读并同意《服务协议》及《隐私政策》",
        icon: "none",
        duration: 2000,
      });
      return;
    }
    var formData = {
      bankNo: e.detail.value.cardNum,
      bankImage: that.data.bankImageUrl,
      bankImagem: that.data.bankImagemUrl,
    };

    if (that.data.multiIndex[0] > 0 && that.data.multiIndex[1] > 0) {
      formData["provinceId"] = that.data.multiArrayRecord[0][that.data.multiIndex[0]].id;
      formData["cityId"] = that.data.multiArrayRecord[1][that.data.multiIndex[1]].id;
    } else {
      wx.showToast({
        icon: "none",
        title: "请选择地区",
        duration: 3000,
      });

      return;
    }

    console.log(formData);

    app.load("保存中...");
    customRequest("/user/bank/addUserBank", { header: 'headers', method: 'POST', data: formData }).then((res) => {
      wx.hideLoading();
      if (res.data.status == 200) {
        wx.redirectTo({
          url: "/pages/bindBankCardSuccess/index",
        });
      } else if (res.data.status == 9999) {
        // app.dialogNotLogin();
      } else {
        wx.showToast({
          icon: "none",
          title: res.data.msg,
          duration: 3000,
        });
      }
    })
    // wx.request({
    // 	url: app.globalData.ip + "/user/bank/addUserBank",
    // 	data: formData,
    // 	header: app.globalData.headers,
    // 	method: "POST",
    // 	success: function (res) {
    // 		console.log(res);
    // 		wx.hideLoading();
    // 		if (res.data.status == 200) {
    // 			wx.redirectTo({
    // 				url: "/pages/bindBankCardSuccess/index",
    // 			});
    // 		} else if (res.data.status == 9999) {
    // 			// app.dialogNotLogin();
    // 		} else {
    // 			wx.showToast({
    // 				icon: "none",
    // 				title: res.data.msg,
    // 				duration: 3000,
    // 			});
    // 		}
    // 	},
    // });
  },

  uploadBankImage: function () {
    var that = this;
    /*if(that.data.bankInfo.uploadBankImageTimes >= 2) {
      app.dialog("提示","不可重复绑工资卡，请联系你的打勾管家处理！","知道了");
      return
    }*/
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ["compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        app.load("识别中...");
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        // var formData = {'userInfoId': that.data.bankInfo.id};
        var formData = {};
        wx.uploadFile({
          url: baseUrl + "/user/bankCard/recognizeBankCardImage",
          filePath: res.tempFilePaths[0],
          name: "uploadFile",
          header: app.globalData.headers2, // 设置请求的 header
          formData: formData, // HTTP 请求中其他额外的 form data
          success: function (res1) {
            console.log(res1);
            if (res1.statusCode == 200) {
              var result = JSON.parse(res1.data);
              if (result.status == 200) {
                if (app.isNotEmptyCheck(result.data.bankCardNo)) {
                  that.data.cardNum = result.data.bankCardNo
                    .replace(/\s/g, "")
                    .replace(/(.{4})/g, "$1 ")
                    .replace(/(\s*$)/g, "");
                  that.data.bankImageUrl = result.data.bankImage;
                  that.data.bankImagemUrl = result.data.bankImagem;
                }

                that.setData({
                  cardNum: that.data.cardNum,
                });
              } else if (res.data.status == 9999) {
                app.dialogNotLogin();
              } else {
                wx.showToast({
                  icon: "none",
                  title: result.msg,
                  duration: 3000,
                });
              }
            }
            wx.hideLoading();
          },
          fail: function (res2) {
            wx.hideLoading();
          },
        });
      },
    });
  },
  showNotice: function () {
    this.setData({
      noticeShow: true,
    });
  },
  close: function () {
    this.setData({
      noticeShow: false,
    });
  },

  getProvinceByPid: function () {
    var that = this;
    customRequest("/city/getProvince", { header: 'headers', method: 'POST', data: {} }).then((res) => {
      if (res.data.status == 200) {
        that.data.multiArrayRecord[0] = [{ id: -1, name: "请选择" }].concat(res.data.data);

        var nameArray = ["请选择"];
        res.data.data.forEach((item, index) => {
          nameArray.push(item.name);
        });
        that.data.multiArrayTemp[0] = nameArray;
        that.setData({
          multiArrayTemp: that.data.multiArrayTemp,
        });
      } else {
        app.showTips(that, res.data.msg);
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/city/getProvince",
    //   data: {},
    //   header: app.globalData.headers,
    //   method: "POST",
    //   success: function (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       that.data.multiArrayRecord[0] = [{ id: -1, name: "请选择" }].concat(res.data.data);

    //       var nameArray = ["请选择"];
    //       res.data.data.forEach((item, index) => {
    //         nameArray.push(item.name);
    //       });
    //       that.data.multiArrayTemp[0] = nameArray;
    //       that.setData({
    //         multiArrayTemp: that.data.multiArrayTemp,
    //       });
    //     } else {
    //       app.showTips(that, res.data.msg);
    //     }
    //   },
    // });
  },
  getCityByPid: function (column, value) {
    var that = this;
    // var pid = that.data.userDetails.bankProvinceCode;
    var pid = that.data.multiArrayRecord[column][value].id;
    customRequest("/city/getCityByPid", { header: 'headers', method: 'POST', data: { pid: pid } }).then((res) => {
      if (res.data.status == 200) {
        that.data.multiArrayRecord[column + 1] = [{ id: -1, name: "请选择" }].concat(res.data.data);

        var nameArray = ["请选择"];
        res.data.data.forEach((item, index) => {
          nameArray.push(item.name);
        });
        that.data.multiArrayTemp[column + 1] = nameArray;

        that.setData({
          multiArrayTemp: that.data.multiArrayTemp,
        });
      } else {
        app.showTips(that, res.data.msg);
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/city/getCityByPid",
    //   data: {
    //     pid: pid,
    //   },
    //   header: app.globalData.headers,
    //   method: "POST",
    //   success: function (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       that.data.multiArrayRecord[column + 1] = [{ id: -1, name: "请选择" }].concat(res.data.data);

    //       var nameArray = ["请选择"];
    //       res.data.data.forEach((item, index) => {
    //         nameArray.push(item.name);
    //       });
    //       that.data.multiArrayTemp[column + 1] = nameArray;

    //       that.setData({
    //         multiArrayTemp: that.data.multiArrayTemp,
    //       });
    //     } else {
    //       app.showTips(that, res.data.msg);
    //     }
    //   },
    // });
  },
  bindMultiPickerChange: function (e) {
    console.log("picker发送选择改变，携带值为", e.detail.value);
    var that = this;
    var multiSelected = false;
    if (e.detail.value[0] > 0 && e.detail.value[1] > 0) {
      multiSelected = true;
    }

    this.setData({
      multiIndex: e.detail.value,
      multiSelected: multiSelected,
      multiArray: that.data.multiArrayTemp,
    });
  },
  bindMultiPickerColumnChange: function (e) {
    console.log("修改的列为", e.detail.column, "，值为", e.detail.value);
    var that = this;
    if (e.detail.column == 0) {
      that.data.multiIndexTemp[0] = e.detail.value;
      that.data.multiIndexTemp[1] = 0;
      // that.data.multiIndexTemp[2] = 0;
      that.data.multiArrayTemp[1] = [];
      that.data.multiArrayTemp[2] = [];
      this.setData({
        multiIndexTemp: that.data.multiIndexTemp,
        multiArrayTemp: that.data.multiArrayTemp,
      });
    }
    if (e.detail.column < 1) {
      this.getCityByPid(e.detail.column, e.detail.value);
    }
  },

  userNameFull (e) {
    this.setData({
      userName: e.detail.value,
    });
    if (this.data.userName == "") {
      this.setData({
        userNameif: false,
      });
    } else {
      this.setData({
        userNameif: true,
      });
    }
  },

  cardNumFull (e) {
    this.setData({
      cardNum: e.detail.value,
    });
    if (this.data.cardNum == "") {
      this.setData({
        cardNumif: false,
      });
    } else {
      this.setData({
        cardNumif: true,
      });
    }
  },

  bankNameFull (e) {
    this.setData({
      bankName: e.detail.value,
    });
    if (this.data.bankName == "") {
      this.setData({
        bankNameif: false,
      });
    } else {
      this.setData({
        bankNameif: true,
      });
    }
  },
  success: function () {
    wx.navigateTo({
      url: "/pages/bindBankCardSuccess/index",
    });
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return app.sharePageImage()
  },
});
