// packageD/energy/powerAddMeb/powerAddMeb.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    array: ["男", "女"],
    sex: "0",
    pickerValue: [0, 0, 0],
    form: {
      sex: "1",
      user_name: "",
      phone: "",
      address: "",
      dealer_user_id: "",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.dealer_uid) {
      this.setData({
        dealer_uid: options.dealer_uid,
      });
    }
    this._initAddressInfo();
    this.getData();
  },
  phoneBtn(e) {
    wx.navigateTo({
      url: "/packageA/member/editmobile/editmobile",
    });
  },
  bindPickerChange(e) {
    console.log(e);
    let key = e.detail.value;
    this.setData({
      sex: key,
    });
  },
  addressShowbtn() {
    this.setData({
      addressShow: true,
    });
  },
  // 收货地址滚动事件
  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();
  },
  // 关闭选择收货地址
  _closeDateLw: function () {
    this.setData({
      addressShow: false,
    });
  },
  //城市结果
  resultAdd: function () {
    let seleData = this.data.selectAddressData;
    let retCity =
      seleData[0].name + " " + seleData[1].name + " " + seleData[2].name;
    this.setData({
      addressName: retCity,
      provice: seleData[0].name,
      city: seleData[1].name,
      district: seleData[2].name,
    });
    this.setData({
      "applyModel.provinceId": seleData[0].id,
      "applyModel.cityId": seleData[1].id,
      "applyModel.districtId": seleData[2].id,
      addressShow: false,
    });

    console.log(this.data.applyModel, this.data.addressName);
    // this.getStreet(seleData[2].id);
  },
  //地址选择器数据初始化
  _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", _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", _data);
    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]);
          }
        }
        console.log("地区数据筛选", _json);
        that.setData({
          districtData: _json,
        });
        console.log("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);
      },
    });
  },
  iuser_name(e) {
    console.log(e.detail.value);
    let val = e.detail.value;
    this.setData({
      "form.user_name": val,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  confirm() {
    let from = this.data.form;
    from.address = this.data.addressName;
    from.sex = this.data.form.sex == 1 ? "男" : "女";
    console.log(from);
    if (app._isTextEmpty(this.data.form.user_name)) {
      wx.showToast({
        title: "请输入姓名",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.addressName)) {
      wx.showToast({
        title: "请选择地区",
        icon: "none",
        duration: 1000,
      });
      return;
    }

    let urlStr = app.getNetAddresss(
      "plugin.energy-cabin.frontend.dealer-terminal.add-member-info"
    );
    app._getNetWork({
      url: urlStr,
      data: from,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result === 1) {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
            success: function () {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500);
            },
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getData() {
    let that = this;
    let json = {
      dealer_uid: this.data.dealer_uid,
    };
    let urlStr = app.getNetAddresss(
      "plugin.energy-cabin.frontend.dealer-terminal.show-member-page"
    );
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result === 1) {
          that.setData({
            "form.phone": res.data.member.mobile,
            "form.dealer_user_id": res.data.dealer_member.uid,
            "form.user_name": res.data.member.nickname,
            "form.name": res.data.dealer_member.nickname,
            "form.invite_mobile": res.validate_page.invite_mobile,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
