var app = getApp();
const config = require('../../../../config/config.js')
const util = require('../../../../utils/util.js')
const Http = require('../../../../utils/HttpBasics.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    organizationType: '',
    accountType: "",
    bankingList: [],
    bankBoj: null, //存放当前选中银行
    tempArr: [], //临时存放查询数据
    province: [], //省份数组
    provinceIndex: 0,
    city: [], //市
    cityIndex: 0,
    branch: [], //支行
    branchIndex: 0,
    accountList: [{
      id: 74,
      name: "对公银行账户"
    },
    {
      id: 75,
      name: "个人银行账户"
    },
    ],
    accountInfo: {
      bankAlias: "",
      bank_account_type: 74, //账户类型 74对公75对私
      accountIndex: 0,
      account_name: "", //开户名称
      account_bank: "", //开户银行
      need_bank_branch: "", //是否有支行
      bank_address_code: "", //开户银行省市编码
      bank_branch_id: "", //开户银行联行号
      bank_name: "", //开户银行全称（含支行）
      account_number: "", //银行账号
    },
  },
  setBranch(e) {
    console.log('setBranch')
    let value = e.detail.value
    console.log(value);
    let tempObj = this.data.accountInfo
    tempObj.bank_branch_id = this.data.branch[value].bankBranchId
    tempObj.bank_name = this.data.branch[value].bankBranchName
    this.setData({
      accountInfo: tempObj,
      branchIndex: value
    })
  },
  setAccount_name(e) {
    let value = e.detail.value
    let tempObj = this.data.accountInfo
    tempObj.account_name = value
    this.setData({
      accountInfo: tempObj,
    })
  },
  getBankBranches(e) {
    wx.showLoading({
      title: '加载中...',
    })
    console.log('getBankBranches')
    // if (!e.detail.ins) {
    //   this.data.accountInfo.bankAlias = ''
    //   this.data.accountInfo.bank_name = ''
    // }
    let value = e.detail.value
    let tempObj = this.data.accountInfo
    tempObj.bank_address_code = this.data.city[value].regionCode
    this.data.accountInfo.cityName = this.data.city[value].cityName
    this.setData({
      cityIndex: value,
      accountInfo: tempObj
    })

    if (this.data.bankBoj && this.data.bankBoj.needBankBranch) {
      Http.get({
        url: config.api.bankBranches,
        data: {
          bankAliasCode: this.data.bankBoj.bankAliasCode,
          cityCode: this.data.city[value].cityCode
        }
      }).then(res => {
        this.setData({
          branch: res.data
        })
        wx.hideLoading();
        this.setBranch({
          detail: {
            value: 0
          }
        })
      }).catch(err => {
        wx.hideLoading();
        wx.showToast({
          title: err.message,
          icon: "none",
          duration: 2000
        })
      })
    } else {
      wx.hideLoading();
    }
  },
  getAreasCities(e) {
    console.log('getAreasCities');
    wx.showLoading({
      title: '加载中...',
    })
    if (!e.detail.ins) {
      // this.data.accountInfo.bankAlias = ''
      // this.data.accountInfo.bank_name = ''
    }
    let value = e.detail.value

    console.log(e.detail, 'e.detail');
    this.setData({
      provinceIndex: value,
    })

    Http.get({
      url: config.api.areasCities,
      data: {
        provinceId: this.data.province[value].id
      }
    }).then(res => {
      this.setData({
        city: res.data
      })
      let tempObj = this.data.accountInfo
      tempObj.bank_address_code = this.data.city[0].regionCode
      tempObj.region_code = this.data.province[value].regionCode
      this.setData({
        cityIndex: 0,
        accountInfo: tempObj
      })
      wx.hideLoading();
      const index = this.data.accountInfo.cityIndex
      this.getBankBranches({
        detail: {
          value: index || 0,
          ins: 1
        }
      })
    }).catch(err => {
      wx.hideLoading();
      // wx.showToast({
      //   title: err.message,
      //   icon: "none",
      //   duration: 2000
      // })
    })
  },

  getAreasProvinces() {
    console.log('getAreasProvinces');
    Http.get({
      url: config.api.areasProvinces,
      data: {}
    }).then(res => {
      this.setData({
        province: res.data
      })
      //选择市区 
      console.log(this.data.province, 'province');
      if (this.data.accountInfo.region_code) {
        let tempArr = res.data.map((item, index) => {
          if (item.regionCode == this.data.accountInfo.region_code) {
            this.setData({
              provinceIndex: index
            })
          }
        })
        // console.log(this.data.provinceIndex,"tempArr888  provinceIndex")

        Http.get({
          url: config.api.areasCities,
          data: {
            provinceId: this.data.province[this.data.provinceIndex].id
          }
        }).then(res => {
          this.setData({
            city: res.data
          })
          // return
          let tempObj = this.data.accountInfo
          res.data.map((item, index) => {
            if (item.regionCode == this.data.accountInfo.bank_address_code) {
              this.setData({
                cityIndex: index
              })
            }
          })
          // tempObj.bank_address_code = this.data.city[0].regionCode
          // tempObj.region_code = this.data.province[value].regionCode
          // this.setData({
          //   cityIndex: value,
          //   accountInfo: tempObj
          // })
          // console.log(res.data)
          // wx.hideLoading();

        }).catch(err => {
          wx.hideLoading();
          wx.showToast({
            title: err.message,
            icon: "none",
            duration: 2000
          })
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.message,
        icon: "none",
        duration: 2000
      })
    })
  },
  setBankObj(e) {
    let item = e.currentTarget.dataset.item
    let tempBoj = this.data.accountInfo
    tempBoj.bankAlias = item.bankAlias
    tempBoj.need_bank_branch = item.needBankBranch
    tempBoj.account_bank = item.accountBank
    this.setData({
      bankBoj: item,
      tempArr: [],
      branch: [],
      accountInfo: tempBoj,
    })


    Http.get({
      url: config.api.areasCities,
      data: {
        provinceId: this.data.province[0].id
      }
    }).then(res => {
      this.setData({
        // city: res.data,
      })

      let tempObj = this.data.accountInfo
      tempBoj.bank_address_code = this.data.city[0].regionCode
      tempObj.region_code = this.data.province[0].regionCode
      this.setData({
        accountInfo: tempBoj
      })
      console.log(res.data)
      wx.hideLoading();
      this.getBankBranches({
        detail: {
          value: 0,
          ins: 1
        }
      })
    }).catch(err => {
      wx.hideLoading();
      // wx.showToast({
      //   title: err.message,
      //   icon: "none",
      //   duration: 2000
      // })
    })
  },

  inquireBank(e) {
    let mytext = e.detail.value;
    console.log(mytext)
    let arr = this.data.bankingList.filter((item) => {
      // indexOf()报告指定字符在此实例中的第一个匹配项的索引
      // 需要输入字符为首字母时候改'>-1'为'==0'
      return item.bankAlias.indexOf(mytext) > -1
    })
    if (arr.length > 0) {
      this.setData({
        tempArr: arr
      })
    } else {
      this.setData({
        tempArr: []
      })
    }
  },

  getBanking() {
    Http.get({
      url: config.api.banking,
      data: {
        bankAccountType: this.data.accountInfo.bank_account_type
      }
    }).then(res => {
      this.setData({
        bankingList: res.data
      })
      wx.hideLoading();

    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.message,
        icon: "none",
        duration: 2000
      })
    })
  },

  // setBank_address_code(e) {
  //   let tempObj = this.data.accountInfo
  //   tempObj.bank_address_code = e.detail.value
  //   this.setData({
  //     accountInfo: tempObj
  //   })
  // },

  setAccount_number(e) {
    let tempObj = this.data.accountInfo
    tempObj.account_number = e.detail.value
    this.setData({
      accountInfo: tempObj
    })
  },

  getReceiverApply() {
    wx.showLoading({
      title: '加载中...',
    })
    Http.get({
      url: config.api.getReceiverApply,
      data: {
        merchantId: app.globalData.merchant.merchant_id
      }
    }).then(res => {
      if (res.data && res.data.organizationType) {
        this.setData({
          organizationType: res.data.organizationType
        })
      }

      if (res.data && res.data.id) {
        this.setData({
          id: res.data.id
        })
      }
      if (res.data && res.data.accountInfo) {
        let obj = JSON.parse(res.data.accountInfo)
        this.setData({
          accountInfo: {
            ...this.data.accountInfo,
            ...obj
          }
        })
        console.log(this.data.accountInfo, 'obj');
      }
      this.getBanking()
      this.getAreasProvinces()
      if (res.data && res.data.accountInfo) {
        setTimeout(() => {
          this.getAreasCities({
            detail: {
              value: this.data.accountInfo.provinceIndex,
              ins: 1
            }
          })
        }, 1000);
      }
    }).catch(err => {
      wx.hideLoading();
      // wx.showToast({
      //   title: err.message,
      //   icon: "none",
      //   duration: 2000
      // })
    })
  },

  bindDateChange1: function (e) {
    let id = this.data.accountList[e.detail.value].id
    let tempObj = this.data.accountInfo
    tempObj.bank_account_type = id
    tempObj.accountIndex = e.detail.value
    this.setData({
      accountInfo: tempObj
    })
    console.log(id)
  },

  bindDateChange2: function (e) {
    let id = this.data.bankingList[e.detail.value].id
    let tempObj = this.data.accountInfo
    tempObj.bank_account_type = id
    tempObj.accountIndex = e.detail.value
    this.setData({
      accountInfo: tempObj
    })
    console.log(id)
  },

  save(e) {
    let flag = e.currentTarget.dataset.flag

    let errMeg = "";
    let errFlag = false;

    if (!this.data.accountInfo.account_name) {
      errMeg = "请输入开户名称"
      errFlag = true
    } else if (!this.data.accountInfo.bankAlias) {
      errMeg = "请输入开户行"
      errFlag = true
    } else if (!this.data.accountInfo.bank_address_code) {
      errMeg = "请选着开户行省市"
      errFlag = true
    } else if (!this.data.accountInfo.account_number) {
      errMeg = "请输入开户银行账号"
      errFlag = true
    }

    if (errFlag) {
      wx.showToast({
        title: errMeg,
        icon: "none",
        duration: 2000
      })
      return
    }

    let tempAccountInfo = {
      bank_account_type: this.data.accountInfo.bank_account_type, //账户类型 74对公75对私*
      accountIndex: this.data.accountInfo.accountIndex,

      account_name: this.data.accountInfo.account_name, //开户名称*
      account_bank: this.data.accountInfo.account_bank, //开户银行*
      need_bank_branch: this.data.accountInfo.need_bank_branch, //是否有支行
      bankAlias: this.data.accountInfo.bankAlias,
      bank_address_code: this.data.accountInfo.bank_address_code, //开户银行省市编码*
      // bank_branch_id: this.data.accountInfo.bank_branch_id, //开户银行联行号(支行)*
      // bank_name: this.data.accountInfo.bank_name, //开户银行全称（支行）*
      account_number: this.data.accountInfo.account_number, //银行账号*
      provinceIndex: this.data.provinceIndex,
      cityIndex: this.data.cityIndex
      // region_code: this.data.accountInfo.region_code,//选择省编码
    }
    if (this.data.accountInfo.need_bank_branch) {
      tempAccountInfo.bank_branch_id = this.data.accountInfo.bank_branch_id
      tempAccountInfo.bank_name = this.data.accountInfo.bank_name
    }
    console.log(tempAccountInfo)
    if (this.data.organizationType == 2) {
      tempAccountInfo.bank_account_type = 74
    }

    let parame = {
      id: this.data.id,
      organizationType: this.data.organizationType,
      accountInfo: JSON.stringify(tempAccountInfo)
    };

    console.log(parame, "parame")

    Http.post({
      url: config.api.receiverAdd,
      data: parame
    }).then(res => {
      console.log(res, "res")
      if (flag == "save") {
        wx.reLaunch({
          url: '/pages/warehouse/warehouse',
        })
      } else if (flag == "next") {
        wx.navigateTo({
          url: '/pages/warehouse/detail/adminFirm/adminFirm',
        })
      }

    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: "none",
        duration: 2000
      })
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getReceiverApply()
  },

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

  },


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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})