import Toast from '@vant/weapp/toast/toast';
import { API } from './../../../utils/network/api'
const api = new API()
const app = getApp()
Page({

  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '绑定银行卡', //导航栏 中间的标题
    },
    height: app.globalData.height,
    realname: "",
    idcard: "",
    bankcard: "",
    city_show: false,
    bank_show: false,
    bank_note: "请选择开户行",
    city_note: "请选择开户行所在城市",
    areaList: [],
    bankList: [],
    select_province_id: "",
    select_city_id: "",
    select_county_id: "",
    select_bank_index: "",
    select_bank_id: "",
    area_sub_titles: ["省份","市区","区县"],
    areaprovince_data_ready: false,
    banklist_data_ready: false,
    bank_card_id: "",
    static_host: api.getStaticHost()
  },
  onLoad(options){
    console.log("options: ", options);
    let bank_card_id = options.bank_card_id;
    if(bank_card_id && bank_card_id != "undefined"){
      console.log("options bank_card_id: ", bank_card_id);
      this.setData({
        bank_card_id: bank_card_id
      })
    }
    let _this = this;
    //加载省份列表
    api.getAreaProvince().then(response => {
      console.log(response);
      const list = _this.formatArea(response, -1, true);
      _this.setData({
        areaprovince_data_ready: true,
        areaList: list
      })
    }).catch(errors => {console.error(errors); })

    //加载银行卡列表
    api.getBasicBank().then(response => {
      console.log(response);
      const list = _this.formatBankList(response, -1, true);
      _this.setData({
        banklist_data_ready: true,
        bankList: list
      })
    }).catch(errors => {console.error(errors); })


  },
  onShow(){
    //获取实名认证信息
    api.getDoctorAuthReal().then(response => {
      console.log(response);
      if(response.data){
        this.setData({
          realname:response.data.card_name_mask,
          idcard:response.data.card_num_mask
        });
      }
    }).catch(errors => {
      console.error(errors);
    })
  },
  onCityShow(e) {
    console.log("onCityShow");
    this.setData({ city_show: true });
  },
  onBankShow(e) {
    console.log("onBankShow");
    if(this.data.select_county_id == ""){
      wx.showToast({
        title: '请选择城市',
        icon: "error"
      })
      return;
    }
    this.setData({ bank_show: true });
  },
  onBankClose(e) {
    console.log("onBankShow");
    this.setData({ bank_show: false });
  },
  onBankCancel(event) {
    console.log("onBankCancel");
    const { picker, value, index } = event.detail;
    Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({ 
      bank_show: false
    });

  },
  onBankConfirm(event) {
    console.log("onBankConfirm");
    const { picker, value, index } = event.detail;
    console.log(event.detail);
    this.setData({ 
      bank_show: false,
      select_bank_id: `${value.value}`,
      bank_note: `${value.text}`
    });
  },
  onBankChange(event) {
    console.log("onYiYuanChange");
    const { picker, value, index } = event.detail;
    Toast(`当前值：${value}, 当前索引：${index}`);
  },
  onChangeCity(e) {
    this.setData({
      select_county_id: "",
      select_yiyuan_id: ""
    })
    console.log("onChangeCity");
    const { selectedOptions } = e.detail;
    console.log(e.detail)
    let area = selectedOptions[selectedOptions.length-1];
    if(!area){
      area = selectedOptions[selectedOptions.length-2];
    }
    const type = area.type;
    const value = area.value;
    const index = area.index;
    const p_index = area.p_index;
    if(type == 2){//省份
      api.getAreaCity({area_id: value}).then(response => {// 请求市区列表
        const list = this.formatArea(response, index, true);
        const _city = "areaList[" + index + "].children";
        this.setData({
          [_city]: list,
          select_province_id: value
        })
      }).catch(errors => {console.error(errors); })
    }
    if(type == 3){//市区
      api.getAreaCounty({area_id: value}).then(response => {// 请求市区列表
        const list = this.formatArea(response, index, false);
        const _county = "areaList[" + p_index + "].children[" +index+ "].children";
        this.setData({
          [_county]: list,
          select_city_id: value
        })
      }).catch(errors => {console.error(errors); })
    }

    console.log("type: " + type);
    if(type == 4){//区县
      this.setData({
        select_county_id: value,
        bank_show: true,
        city_note: this.getCityNote(selectedOptions)
      });

    }else{
      this.setData({
        city_show: true
      })
    }
  },
  getCityNote(selectedOptions){
    let txt = [];
    selectedOptions.forEach(item => {
      if(item){
        txt.push(item.label);
      }
    })
    return txt.join("/");
  },
  formatArea(response, p_index, has_children){
    var list = []
    let index = 0;
    response.data.forEach(data => {
      let area = {};
      area.index = index++;
      area.p_index = p_index;
      area.type = data.area_type;
      area.label = data.area_name;
      area.value = data.area_id;
      if(has_children){
        area.children = [];
      }
      list.push(area)
    });
    return list;
  },
  formatBankList(response){
    let list = [];
    response.data.forEach(item => {
      let data = {};
      data.text = item.bank_name;
      data.value = item.bank_id;
      list.push(data)
    });
    return list;
  },
  addBank(e){
    console.log("addbank, ", e);

    
    //绑定医生银行卡
    let params = {};
    params.county_id = this.data.select_county_id;
    params.city_id = this.data.select_city_id;
    params.province_id = this.data.select_province_id;
    params.bank_card_code = this.data.bankcard;
    params.bank_id = this.data.select_bank_id;
    console.log("params.county_id: ", params.county_id)
    if(params.county_id == ""){
      wx.showToast({
        title: '请选择城市',
        icon: "error"
      })
      return;
    }
    console.log(params);
    let bank_card_id = this.data.bank_card_id;
    if(bank_card_id){
      params.bank_card_id = bank_card_id;
      //更换医生银行卡
      api.putDoctorBank(params).then(response => {
        console.log(response);
        wx.showToast({
          title: '更换成功',
          icon: "success"
        })
        wx.navigateBack();
      }).catch(errors => {console.error(errors); })
    }else{
      //绑定医生银行卡
      api.postDoctorBank(params).then(response => {
        console.log(response);
        wx.showToast({
          title: '绑定成功',
          icon: "success"
        })
        wx.navigateBack();
      }).catch(errors => {console.error(errors); })
    }
  }
})