// components/region/region.js
const {
  findLinkNumberList
} = require('../../api/api')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showRegion: {
      type:Boolean,
      value:false
    },
    provinceName: {
      type:String,
      value: ''
    },
    cityName: {
      type:String,    
      value: '' 
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    capitalId: '', //资方
    bankName:'', //支行
    bankNamed: '',//总行
    bankNameList: [{
      id: 1,
      name: '中国工商银行'
    },{
      id: 2,
      name: '中国农业银行'
    },{
      id: 3,
      name: '中国银行'
    },{
      id: 4,
      name: '中国建设银行'
    },{
      id: 5,
      name: '中国邮政储蓄银行'
    },{
      id: 6,
      name: '交通银行'
    },{
      id: 7,
      name: '中信银行'
    },{
      id: 8,
      name: '兴业银行'
    },{
      id: 9,
      name: '华夏银行'
    },{
      id: 10,
      name: '招商银行'
    },{
      id: 11,
      name: '民生银行'
    },{
      id: 12,
      name: '农村商业'
    },{
      id: 13,
      name: '其他'
    }], //开户银行
    provinceList:[],
    page: 1,   
    region: [], //开户地址
    customItem: ''
  },
  ready(){
    this.setData({
      capitalId: wx.getStorageSync('capitalId')
    })
  },

  pageLifetimes: {
    show: function () {      
      // 页面被展示
      // this._bindBankChange()
    },
  }, 
  observers: {
    'cityName': function (cityName) {
      if(cityName){
        var arr = this.data.region
        arr[0] = this.data.provinceName
        arr[1] = this.data.cityName
        this.setData({
          region: arr,
        })
      }
      // console.log(333, this.data.provinceName)
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {

    /**
     * 
     * 搜索
     * 
     * @param {*} e 
     */
    _bindBankChange: function (bankName) {
      var that = this;
      var page = that.data.page;
      var list = that.data.provinceList;
      if(this.data.capitalId =='04'){
        this.setData({
          region: []
        })
      }
      findLinkNumberList({
        capitals: wx.getStorageSync('capitalId'),
        bankName: this.data.bankName?this.data.bankName:'', //总行名称
        bankBranch: this.data.bankNamed?this.data.bankNamed:'', //支行名称
        province: this.data.region[0]?this.data.region[0]:'',
        city: this.data.region[1]?this.data.region[1]:'',
        page: page,
        limit: 15
      }).then((res) => {
        // var resData = res.map(itm=>itm.columns)
        var resData = res.data
        for (var i = 0; i < resData.length; i++) {
          list.push(resData[i])
        }
        this.setData({
          provinceList: list
        })
      })
    },

    /**
     * 
     * 选择
     * 
     * @param {*} e 
     */
    _chooseBank(e) {
      const id = e.currentTarget.dataset.id
      const name = e.currentTarget.dataset.name
      var data = {
        id: id,
        name: name,
      }
      this._closeBank();
      this.triggerEvent('chooseRegion', data);
    },

    /**
     * 
     * 关闭弹框
     * 
     * @param {*} e 
     */
    _closeBank(e) {
      this.setData({
        bankNamed: '',
        // region: [],
        bankName: "",
        provinceList:[],
        showRegion: false,
      })
    },
    /**
     * 搜索查询
     */
    onSearch(e){
      // console.log("执行",e.detail);
      let bankName = e.detail;
      if(bankName.length < 2){
        wx.showToast({
          title: '请输入至少2个字符',
          icon: 'none',
          duration: 2000
        });
        return;
      }else{
        this.setData({
          page: 1,
          provinceList: [],
          bankName
        })
      }
      this._bindBankChange();
    },
    //清空搜索框
    onSearchClear(){
      this.setData({
        page: 1,
        provinceList: [],
        bankName: ''
      })
      this._bindBankChange();
    },    
    //开户银行选择
    bindBankList(e){
      this.setData({
        page: 1,
        provinceList: [],
        bankName_id: this.data.bankNameList[e.detail.value].id,
        bankNamed: this.data.bankNameList[e.detail.value].name,
        // bankNamed: this.data.bankNameList[e.detail.value].name == '其他' ? '' : this.data.bankNameList[e.detail.value].name,
      })
      this._bindBankChange();
    },
    //开户地址
    bindRegionChange(e){
      // console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        page: 1,
        provinceList: [],
        region: e.detail.value
      })
      this._bindBankChange();
    },
    more: function () {
      this.data.page++;
      this._bindBankChange()
    },
    //清空
    emptyData(e){
      this.setData({
        page: 1,
        provinceList: [],
        region: []
      })
      this._bindBankChange();
    },
  }
})