// component/chooseCity/chooseCity.js
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    countryList: [],
    provinceList: [],
    cityList: [],
    countryID: "",
    provinceID: "",
    cityID: "",
    selectInfo: {
      name:"",
      id:"",
    },
    index:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getCountry() {
      console.log(this.data.type)
      let that = this;
      app.$api('dict_post_Destination_GetCountry', {}).then(res => {
        this.setData({
          countryList: res
        })
        if (res.length > 0) {
          let countryInfo = that.data.countryList[0];
          that.getProvince(countryInfo);
        }

      }).catch(err => { })
    },
    getProvince(countryInfo) {
      let that = this;
      app.$api('dict_post_Destination_GetChildList', { Id: countryInfo.ID }).then(res => {
        this.setData({
          provinceList: res,
          cityList: [],
        })
        if (res.length > 0) {
          let proInfo = that.data.provinceList[0];
          that.getCity(proInfo)
        } else {
          
          let index = that.data.index[0]; 
          that.data.selectInfo.name = that.data.countryList[index].Name;
          that.data.selectInfo.id = that.data.countryList[index].ID;
        }
        that.setData({
          selectInfo: that.data.selectInfo
        })

      }).catch(err => { })

    },
    getCity(proInfo) {
      let that = this;
      app.$api('dict_post_Destination_GetChildList', { Id: proInfo.ID }).then(res => {
        this.setData({
          cityList: res
        })
        if(res.length==0){
          let index = that.data.index[1]; 
          that.data.selectInfo.name = that.data.provinceList[index].Name;
          that.data.selectInfo.id = that.data.provinceList[index].ID;
          
        }else{
          that.data.selectInfo.name = that.data.cityList[0].Name;
          that.data.selectInfo.id = that.data.cityList[0].ID;
        }
        that.setData({
          selectInfo: that.data.selectInfo
        })

      }).catch(err => { })

    },
    changeCountry(e) {
      let that = this;
      const val = e.detail.value
      console.log(val);
      this.setData({
        index:val,
        countryID: this.data.countryList[val[0]]
      })
      that.getProvince(that.data.countryID);

    },
    closeCountry(e) {
      let country = e.target.dataset.self;
      if (country == "country") {
        console.log("11")
        this.triggerEvent('closeCountryView', " ")
      }

    },
    selectCity(){
      let that=this;
      // console.log("selectInfo", that.data.selectInfo);
      this.triggerEvent('closeCountryView', " ")
      this.triggerEvent('selectCity', that.data.selectInfo)
    },
  }
})
