const arealist = require('../../api/store.js');

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show:{    //控制area_select显示隐藏
      type:Boolean,
      value:false
    },
    maskShow:{    //是否显示蒙层
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    provinces: [],
    citys: [],
    areas: [],
    // provinces: provinces,
    // citys: areaTool.getCitys(index[0]),
    // areas: areaTool.getAreas(index[0], index[0]),
    value:[0,0,0],
    province: '',
    city: '',
    area: '',
    resultlist:[],
    index:[0,0,0]
  },
  // 组件的生命周期
  lifetimes: {
    
    attached: function () { 
      var that = this;
      console.log(that.data.index);
      arealist.getStore({},{

      }, '加载中',
        function (res) {
          var results = res.results;
          that.setData({
            resultlist:results
          })
          var provinces = that.getProvinces();
          var citys = that.getCitys(that.data.index[0]);
          var areas = that.getAreas(that.data.index[0],that.data.index[0]);
          that.setData({
            provinces: provinces,
            citys: citys,
            areas: areas
          })
          console.log(that.data.provinces);
          console.log(that.data.citys);
          console.log(that.data.areas);
          
          
        },
        function (err) {
          wx.showToast({
            title: '失败',
          })
        }
      )
    }
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    // setTimeout(() => {
    handleNYZAreaChange:function(e){
      var provinces = this.getProvinces();
      // var citys = this.getCitys(index[0]);
      // var areas = this.getAreas(index[0], index[0]);
      var that = this;
      console.log("e:" + JSON.stringify(e));
      var value = e.detail.value;
      /**
       * 滚动的是省
       * 省改变 市、区都不变
       */
      if(that.data.index[0] != value[0]){
        that.data.index = [value[0],0,0]
        let selectCitys = that.getCitys(that.data.index[0]);
        let selectAreas = that.getAreas(that.data.index[0], 0);
        that.setData({
          citys: selectCitys,
          areas: selectAreas,
          value: [that.data.index[0],0,0],
          province: provinces[that.data.index[0]],
          city: selectCitys[0],
          area: selectAreas[0]
        })
      } else if (that.data.index[1] != value[1]){
        /**
         * 市改变了 省不变 区变
         */
        that.data.index = [value[0], value[1], 0]
        let selectCitys = that.getCitys(that.data.index[0]);
        let selectAreas = that.getAreas(that.data.index[0], value[1]);
        that.setData({
          citys: selectCitys,
          areas: selectAreas,
          value: [that.data.index[0], that.data.index[1], 0],
          province: provinces[that.data.index[0]],
          city: selectCitys[that.data.index[1]],
          area: selectAreas[0]
        })
      } else if (that.data.index[2] != value[2]){
        /**
         * 区改变了
         */
        that.data.index = [value[0], value[1], value[2]]
        let selectCitys = that.getCitys(that.data.index[0]);
        let selectAreas = that.getAreas(that.data.index[0], value[1]);
        that.setData({
          citys: selectCitys,
          areas: selectAreas,
          value: [that.data.index[0], that.data.index[1], that.data.index[2]],
          province: provinces[that.data.index[0]],
          city: selectCitys[that.data.index[1]],
          area: selectAreas[that.data.index[2]]
        })
        console.log('区'+that.data.area);
      }
    
    },
      
    /**
     * 确定按钮的点击事件
     */
    handleNYZAreaSelect:function(e){
      //console.log("e:" + JSON.stringify(e));
      // setTimeout(() => {
      var that = this;
      if (e.currentTarget.dataset.province == "") {

        e.currentTarget.dataset.province = that.data.resultlist[0].name;
        e.currentTarget.dataset.city = that.data.resultlist[0].city[0].cityName;
        e.currentTarget.dataset.area = that.data.resultlist[0].city[0].area[0];
      }
      var myEventDetail = e; // detail对象，提供给事件监听函数
      var myEventOption = {}; // 触发事件的选项
      this.triggerEvent('sureSelectArea', myEventDetail, myEventOption)
      // }, 700)
    },
    /**
     * 取消按钮的点击事件
     */
    handleNYZAreaCancle:function(e){
      var that = this;
      console.log("e:" + JSON.stringify(e))
      that.setData({
        show:false
      })
    },
    getProvinces:function(){
      var that = this;
      var provinces = [];
      for (var i = 0; i < that.data.resultlist.length;i++){
        provinces.push(that.data.resultlist[i].name);
       }
       return provinces;


  },


/**
 * 获取省对应的所有城市
 */
    getCitys:function(provinceIndex) {
      var that = this;
      var citys = [];
      for (var i = 0; i < that.data.resultlist[provinceIndex].city.length;i++){
        citys.push(that.data.resultlist[provinceIndex].city[i].cityName);
        }
        return citys;


      },

/**
 * 获取省市对应的所有地区
 */
      getAreas:function(provinceIndex, cityIndex) {
        var that = this;
        var areas = [];
        for (var i = 0; i < that.data.resultlist[provinceIndex].city[cityIndex].area.length; i++) {
          areas.push(that.data.resultlist[provinceIndex].city[cityIndex].area[i]);
            }
            return areas;

      }
  }
})
