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

  /**
   * 组件的初始数据
   */
  data: {
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this._bindRegionChange()
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {

    /**
     * 
     * 选择用户地址
     * 
     * @param {*} e 
     */
    _bindRegionChange: function (e) {
      getProvinceList({
        traderId: this.data.traderId,
      }).then((res) => {
        this.setData({
          provinceList: res
        })
      })

      this.setData({
        provinceShow: true
      })
    },

    /**
     * 
     * 选择省查找市
     * 
     * @param {*} e 
     */
    _chooseProvince(e) {
      const ProvinceID = e.currentTarget.dataset.id
      const provinceName = e.currentTarget.dataset.name
      const traderId = e.currentTarget.dataset.tid
      this.setData({
        provinceShow: false,
        cityShow: true,
        backShow: true,
        provinceName: provinceName,
      })
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      getCitysByProId({
        province: ProvinceID,
        traderId: traderId?traderId:'',
      }).then((res) => {
        wx.hideLoading()
        this.setData({
          cityList: res
        })
      })
    },


    /**
     * 
     * 选择市查找区
     * 
     * @param {*} e 
     */
    _chooseCity(e) {
      const city = e.currentTarget.dataset.id
      const cityName = e.currentTarget.dataset.name
      const traderId = e.currentTarget.dataset.tid
      this.setData({
        cityShow: false,
        areaShow: true,
        cityName: cityName
      })
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      getAreaByCityId({
        city: city,
        traderId: traderId?traderId:'',
      }).then((res) => {
        wx.hideLoading()
        this.setData({
          areaList: res
        })
      })
    },


    /**
     * 
     * 选择区
     * 
     * @param {*} e 
     */
    _chooseArea(e) {
      const areaName = e.currentTarget.dataset.name
      this.setData({
        showRegion: false,
      })
      setTimeout(()=>{
        this.setData({
          provinceShow: true,
          areaShow: false,
          backShow: false,
          areaName: areaName
        })
        console.log("选择的省市区===》》》", this.data.provinceName, this.data.cityName, this.data.areaName)
        var data = {
          provinceName: this.data.provinceName,
          cityName: this.data.cityName,
          areaName: this.data.areaName,
        }
        this.triggerEvent('chooseRegion', data)
      },200)
    },



    /**
     * 
     * 市区后退
     * 
     * @param {*} e 
     */
    _backChooseRegion(e) {
      if (this.data.cityShow) {
        this.setData({
          cityShow: false,
          provinceShow: true,
          backShow: false,
        })
      } else if (this.data.areaShow) {
        this.setData({
          areaShow: false,
          cityShow: true
        })
      }
    },

    /**
     * 
     * 关闭弹框
     * 
     * @param {*} e 
     */
    _closeRegion(e) {
      this.setData({
        provinceName: "",
        cityName: "",
        areaName: "",
        addressName: "",
        provinceShow: true,
        showRegion: false,
        cityShow: false,
        areaShow: false,
        backShow: false,
      })
    },


  }
})