// packageH/Advertising/advertisingApply/advertisingApply.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    strShow: false,
    provinceData: [],
    cityData: [],
    districtData: [],
    pickerValue: [0, 0, 0],
    selectAddressData: [],
    districtVal: [],
    streetShow: false,
    addressShow: false,
    addressName: '',
    provice: '',
    // districtVal: [],

    configInfo: {
      // status: 0
    }, //顶部banner图,申请协议,审核状态
    apply: {
      realname: "",
      mobile: "",
      company_name: "",

      province_id: "",
      city_id: "",
      district_id: "",
      company_address: "",
      remark: "",
      address: "",
      street_id: "" //街道
    },
    showSex: false, //显示性别
    showProtocol: false, //申请协议

    FrontIDcard: [],
    backIDcard: [],
    businessFlie: [],
    GenerateFlie: [],
    otherFlie: [],

    sexId: 0, //（男1女2）
    checked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载地址数据初始化
    this.getData();
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.ad-serving.frontend.advertisers.apply.index');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            configInfo: res.data
          });
          if(res.data.status == 1){
            that._initAddressInfo();
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  submitApply(){
    // let that = this;
    if (app._isTextEmpty(this.data.apply.realname)) {
      wx.showToast({
        title: '请填写姓名！',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.apply.mobile)) {
      wx.showToast({
        title: '请填写联系方式！',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    if (app._isMoblie(this.data.apply.mobile)) {
      wx.showToast({
        icon: "none",
        title: "请输入正确的手机号",
        duration: 1500,
      });
      return;
    }
    if (app._isTextEmpty(this.data.apply.province_id) || app._isTextEmpty(this.data.apply.city_id) || app._isTextEmpty(this.data.apply.district_id)) {
      wx.showToast({
        icon: "none",
        title: "请选择省市区",
        duration: 1500,
      });
      return;
    }

    if (app._isTextEmpty(this.data.apply.street_id)) {
      wx.showToast({
        icon: "none",
        title: "请在后台系统-交易设置-开启街道地址",
        duration: 1500,
      });
      return;
    }
    if (app._isTextEmpty(this.data.FrontIDcard) || app._isTextEmpty(this.data.backIDcard)) {
      wx.showToast({
        icon: "none",
        title: "请上传身份证正反照！",
        duration: 1500,
      });
      return;
    }
    if (app._isTextEmpty(this.data.businessFlie)) {
      wx.showToast({
        icon: "none",
        title: "请上传经营许可证！",
        duration: 1500,
      });
      return;
    }
    if (!this.data.checked) {
      wx.showToast({
        icon: "none",
        title: "请勾选申请协议！",
        duration: 1500,
      });
      return;
    }

    let json = {
      username: this.data.apply.realname,
      mobile: this.data.apply.mobile,
      sex: this.data.sexId,
      province_id: this.data.apply.province_id,
      city_id: this.data.apply.city_id,
      district_id: this.data.apply.district_id,
      street_id: this.data.apply.street_id,
      address: this.data.apply.address,
      id_card_front: this.data.FrontIDcard[0],
      id_card_side: this.data.backIDcard[0],
      business_certificate: this.data.businessFlie[0],
      production_licence: this.data.GenerateFlie[0],
      other_certificates: this.data.otherFlie
    };
    let urlStr = "";
    urlStr = app.getNetAddresss("plugin.ad-serving.frontend.advertisers.apply.apply");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500
          });
          this.getData();
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
  realnameinp(e) {
    let val = e.detail.value;
    this.setData({
      "apply.realname": val,
    });
  },
  mobileinp(e) {
    let val = e.detail.value;
    this.setData({
      "apply.mobile": val,
    });
  },
  updateFrontIDcard(e) {
    this.setData({
      FrontIDcard: e.detail
    });
  },
  updatebackIDcard(e) {
    this.setData({
      backIDcard: e.detail
    });
  },
  updatebusinessFlie(e) {
    this.setData({
      businessFlie: e.detail
    });
  },
  updateGenerateFlie(e) {
    this.setData({
      GenerateFlie: e.detail
    });
  },
  updateotherFlie(e) {
    this.setData({
      otherFlie: e.detail
    });
  },
  showProtocols(){
    this.setData({
      showProtocol: true
    });
  },
  showSexs(){
    this.setData({
      showSex: true
    });
  },
  onCloseSex(){
    this.setData({
      showSex: false
    });
  },
  onCloseProtocol(){
    this.setData({
      showProtocol: false
    });
  },
  onChangechecked(event) {
    this.setData({
      checked: event.detail,
    });
  },
  selcetSex(e){
    let tag = e.currentTarget.dataset.tag;
    this.setData({
      sexId: tag,
      'apply.gender': tag == 1 ? "男" : "女",
      showSex: false
    });
  },
  addressShowbtn() {
    this.setData({
      addressShow: true
    });
  },
  //所在地选择显示
  showAddBtn(e) {
    let idx = e.currentTarget.dataset.idex;
    this.setData({
      ["diydata[" + idx + "].showAdd"]: true
    });
  },
  // 关闭选择收货地址
  _closeDateLw: function () {
    this.setData({
      addressShow: false
    });
  },
  //地址选择器数据初始化
  _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);
      }
    });
  },
  // 收货地址滚动事件
  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();
  },
  //城市结果
  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({
      'apply.province_id': seleData[0].id,
      'apply.city_id': seleData[1].id,
      'apply.district_id': seleData[2].id,
      'addressShow': false,
    });
    this.getStreet(seleData[2].id);
  },
  //获取街道
  getStreet(param) {
    let that = this;
    let urlStr = "";
    urlStr += app.getNetAddresss("member.memberAddress.street");
    urlStr += '&district_id=' + param;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (!app._isTextEmpty(res.data) && res.data.length > 0) {
            that.setData({
              districtVal: res.data,
              strShow: true,
            });
          } else {
            that.setData({
              strShow: false
            });
          }
        } else {
          that.setData({
            strShow: false
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  streetShowbtn() {
    this.setData({
      streetShow: true
    });
  },
  treetShowLeft() {
    this.setData({
      streetShow: false
    });
  },
  streetConfirm(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      street: item.areaname,
      'apply.street_id': item.id,
      'streetShow': false
    });
  },
  addressinp(e) {
    let val = e.detail.value;
    this.setData({
      'apply.address': val
    });
  },
  gotoHome(){
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});