// mycomponent/yz_area/yz_area.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,//弹窗是否显示，默认不显示
      value: false
    },
  },
  pageLifetimes: {
    show: function() {
      // 页面被展示
      //加载地址数据初始化
      this._initAddressInfo();
    }
  },
  observers: {
    show: function (e) {
      console.log(e);
      if(e){
        this.setData({
          showArea:e
        });
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    showArea: false,
    pickerValue: [0, 0, 0],
    provinceData: [],
    cityData: [],
    districtData: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //地址选择器数据初始化
    _initAddressInfo: function () {
      var that = this;
      var _keyName = "provinceData";
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          console.log("地址信息准备就绪");
          that._getProvinceData();
        },
        fail: function (res) {
          console.log("省fail");
          app._getNetWork({
            url: app.getNetAddresss("member.member-address.address"),
            success: function (res) {
              var response = res.data;
              var province = response.data.province;
              var city = response.data.city;
              var district = response.data.district;
              wx.setStorage({
                key: "provinceData",
                data: province,
              });
              wx.setStorage({
                key: "cityData",
                data: city,
              });
              wx.setStorage({
                key: "districtData",
                data: district,
              });
              that._initAddressInfo();
            },
            fail: function (res) {},
            state: true,
          });
        },
      });
    },
    // 读取省key
    _getProvinceData: function () {
      var that = this;
      var _keyName = "provinceData";
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          // console.log("省读取成功", res.data);
          that.setData({
            provinceData: res.data,
          });
          that.setData({
            "selectAddressData[0].name": res.data[that.data.pickerValue[0]].areaname,
            "selectAddressData[0].id": res.data[that.data.pickerValue[0]].id,
            "selectAddressData[0].index": that.data.pickerValue[0],
          });
          that._getCityData();
        },
      });
    },
    // 读取城市key，筛选城市data
    _getCityData: function () {
      var _data = this.data.selectAddressData[0];
      // console.log("城市", _data);
      var that = this;
      var _keyName = "cityData";
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          // console.log("城市读取成功", res);

          var _json = [];
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].parentid == _data.id) {
              _json.push(res.data[i]);
            }
          }
          // console.log("城市数据筛选", _json);
          that.setData({
            cityData: _json,
          });
          that.setData({
            "selectAddressData[1].name": _json[that.data.pickerValue[1]].areaname,
            "selectAddressData[1].id": _json[that.data.pickerValue[1]].id,
            "selectAddressData[1].index": that.data.pickerValue[1],
          });
          that._getDistrictData();
        },
      });
    },
    // 读取地区key，筛选地区data
    _getDistrictData: function () {
      var _data = this.data.selectAddressData[1];
      // console.log("地区", _data);
      var that = this;
      var _keyName = "districtData";
      wx.getStorage({
        key: _keyName,
        success: function (res) {
          // console.log("地区读取成功");
          var _json = [];
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].parentid == _data.id) {
              _json.push(res.data[i]);
            }
          }
          that.setData({
            districtData: _json,
          });
          that.setData({
            "selectAddressData[2].name": _json[that.data.pickerValue[2]].areaname,
            "selectAddressData[2].id": _json[that.data.pickerValue[2]].id,
            "selectAddressData[2].index": that.data.pickerValue[2],
          });
          console.log("over", that.data.selectAddressData);
        },
      });
    },
    // 收货地址滚动事件
    bindChange: function (e) {
      var val = e.detail.value;
      if (this.data.pickerValue[0] != val[0]) {
        val[1] = 0;
        val[2] = 0;
      }
      if (this.data.pickerValue[1] != val[1]) {
        val[2] = 0;
      }
      this.setData({
        "pickerValue[0]": val[0],
        "pickerValue[1]": val[1],
        "pickerValue[2]": val[2],
      });
      this._initAddressInfo();
    },
    // 关闭选择收货地址
    onClickLeft: function () {
      this.setData({
        showArea: false,
      });
    },
    // 确定选择收货地址
    onClickRight: function () {
      let seleData = this.data.selectAddressData;
      this.onClickLeft();
      // 传递给父组件
      console.log("确定选择收货地址", seleData);
      this.triggerEvent('confirm', seleData);
    },
  }
});