// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    apply: "-1", // 状态值
    provicevalue: "",
    cityvalue: "",
    regionvalue: "",
    selectLevel: "",
    selectLevelname: "", // 代理名字
    streetvalue: "",
    level: [],
    provinceoptions: [],
    cityoptions: [],
    regionoptions: [],
    stroptions: [],
    proviceObject: "",
    cityObject: "",
    citys: [], // 总的城市
    district: [], // 总的县
    regionObject: "",
    districtObject: "",
    // 省市区弹窗显示隐藏
    proshow: false,
    cityshow: false,
    regshow: false,
    strshow: false,
    // 代理等级显示
    selshow: false,
    prozoneshow: false,
    cityzoneshow: false,
    regzoneshow: false,
    streetshow: false,
    username: "",
    password: "",
    name: "",
    agreement: "",
    agreementShow: false,
    is_open_agreement: "0",
    is_agreement: false,
    apply_background:"",
    upload_flag:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.cheackRouter();
    this.getStatus(); // 获取当前状态
    try {
      let mailLanguage = JSON.parse(wx.getStorageSync('mailLanguage'));
      if (mailLanguage && mailLanguage.area_dividend) {
        let _title = mailLanguage.area_dividend.title || '区域代理';
        wx.setNavigationBarTitle({
          title: _title + '申请'
        });
      }
    } catch (e) {
      // console.log(e)
    }
  },
  changeAgreement(evt) {
    this.setData({
      is_agreement: evt.detail,
    });
  },
  cheackRouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == "applyRegionalAgency") {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          wx.showToast({
            title: "未开启推广权限",
            duration: 1000,
            icon: "none",
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            },
          });
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },
  setExplainFlag() {
    this.setData({
      agreementShow: true,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getAreaDividend() {
    let urlStr = app.getNetAddresss("plugin.area-dividend.api.area-dividend.getAreaDividend");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            dividend: resdata.data,
          });
          console.log(this.data.dividend, "数组");
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 获取状态
  getStatus() {
    let urlStr = app.getNetAddresss("plugin.area-dividend.api.area-dividend.apply-status");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        console.log(res.data, "数据");
        if (res.data.result === 1) {
          let res1 = res.data.data;
          this.setData({
            apply: res1.apply,
            agreement: res1.agreement,
            is_open_agreement: res1.is_open,
            tipMsg: res.data.msg,
          });
          if(res1.apply_background == ''){
            this.setData({upload_flag:false})
          }else{
            this.setData({apply_background:res1.apply_background,})
          }
         
          console.log(this.data.apply, "tplapkfkakfpo");
          this.setLogic(this.data.apply);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 分配逻辑
  setLogic(key) {
    if (key == "0") {
      console.log("错误");
    } else if (key == 1) {
      this.initLevel();
      this.initData();
    } else if (key == 2) {
      this.initLevel();
      this.initData();
    }
  },
  // 去商城
  goHome() {
    console.log("回去首页");
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },
  // 初始化等级数据
  initLevel() {
    let urlStr = app.getNetAddresss("plugin.area-dividend.api.area-dividend.can-apply-agent-level");
    let p = app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            level: resdata.data,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
    return p;
  },

  // 初始化数据
  initData() {
    console.log("5464655454");
    var province = this.data.provinceoptions;
    if (province == "") {
      this.getAddressDataInfo(); // 获取地址json数据
    } else {
      this.initProvice();
    }
  },

  // 初始化省数据
  initProvice() {
    wx.getStorage({
      key: "province",
      success: (res) => {
        this.setData({
          provinceoptions: res.data || [],
        });
      },
    });
  },
  selectProviceHandleShow() {
    this.setData({
      proshow: true,
    });
  },
  // 选择省份的响应方法
  selectProviceHandle(value) {
    // 初始化一些默认值 避免逻辑错误
    console.log(value.currentTarget.dataset.item, "items");
    this.setData({
      provicevalue: value.currentTarget.dataset.item.areaname || value.target.dataset.item.areaname,
      proshow: false,
      proviceObject: value.currentTarget.dataset.item || value.target.dataset.item,
    });
    let citys = this.data.citys;
    let arr = [];
    var id = value.currentTarget.dataset.item.id || value.target.dataset.item.id;
    console.log(id, "id878778");
    for (let i = 0; i < citys.length; i++) {
      if (citys[i].parentid == id) {
        arr.push(citys[i]);
        this.setData({
          cityoptions: arr,
        });
      }
    }
    console.log(this.data.cityoptions, "城市");
  },

  // 选择市的响应方法
  selectCityHandle(value) {
    console.log(value.currentTarget.dataset.item, "items");
    this.setData({
      cityvalue: value.currentTarget.dataset.item.areaname || value.target.dataset.item.areaname,
      cityshow: false,
      cityObject: value.currentTarget.dataset.item || value.target.dataset.item,
    });
    let citys = this.data.district;
    console.log(citys, "545454455554");
    let arr = [];
    var id = value.currentTarget.dataset.item.id || value.target.dataset.item.id;
    console.log(id, "id878778");
    for (let i = 0; i < citys.length; i++) {
      if (citys[i].parentid == id) {
        arr.push(citys[i]);
        this.setData({
          regionoptions: arr,
        });
      }
    }
    console.log(this.data.regionoptions, "64454454554城市");
  },

  // 选择县区的响应方法
  selectRegionHandle(value) {
    console.log(value.currentTarget.dataset.item, "items");
    let val = value.currentTarget.dataset.item || value.target.dataset.item;
    this.setData({
      regionvalue: val.areaname,
      regshow: false,
      regionObject: val,
    });
    this.isStreetShow(val.id);
  },

  // 获取地址信息
  getAddressDataInfo() {
    let urlStr = app.getNetAddresss("member.member-address.address");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        console.log(res.data.data, "数据844884");
        if (res.data.result === 1) {
          this.setData({
            provinceoptions: res.data.data.province,
            citys: res.data.data.city,
            district: res.data.data.district,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 是否手机
  isMoblie(value) {
    return !/^1\d{10}$/.test(value);
  },
  // 提交 isAgain是否再次申请
  submit(isAgain) {
    if (this.data.is_open_agreement == 1) {
      if (this.data.is_agreement == false) {
        wx.showToast({
          title: "请同意协议",
          icon: "none",
        });
        return;
      }
    }
    var isAgains = isAgain.currentTarget.dataset.bler || isAgain.target.dataset.bler;
    if (this.data.name == "") {
      wx.showToast({
        title: "请填写姓名",
        icon: "none",
      });
      return;
    }
    if (this.data.phone == "" || this.data.phone == undefined) {
      wx.showToast({
        title: "请填写手机号",
        icon: "none",
      });
      return;
    }
    if (this.isMoblie(this.data.phone)) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
      });
      return;
    }
    if (this.data.username == "" || this.data.username == undefined) {
      wx.showToast({
        title: "请填写登录账号",
        icon: "none",
      });
      return;
    }
    if (this.data.password == "" || this.data.password == undefined) {
      wx.showToast({
        title: "请填写登录密码",
        icon: "none",
      });
      return;
    }
    if (this.data.selectLevel == "") {
      wx.showToast({
        title: "请选择代理类型",
        icon: "none",
      });
      return;
    }
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }

    // 提交数据
    let urlStr;
    if (isAgains == "true") {
      urlStr = app.getNetAddresss("plugin.area-dividend.api.area-dividend.apply-again");
    } else {
      urlStr = app.getNetAddresss("plugin.area-dividend.api.area-dividend.set-agent-apply");
    }
    let json = {
      username: this.data.username,
      mobile: this.data.phone,
      password: this.data.password,
      real_name: this.data.name,
    };
    if (this.data.selectLevel.tag == "0") {
      json.province_id = this.data.proviceObject.id;
    } else if (this.data.selectLevel.tag == "1") {
      json.province_id = this.data.proviceObject.id;
      json.city_id = this.data.cityObject.id;
    } else if (this.data.selectLevel.tag == "2") {
      json.province_id = this.data.proviceObject.id;
      json.city_id = this.data.cityObject.id;
      json.district_id = this.data.regionObject.id;
    } else if (this.data.selectLevel.tag == "3") {
      json.province_id = this.data.proviceObject.id;
      json.city_id = this.data.cityObject.id;
      json.district_id = this.data.regionObject.id;
      json.street_id = this.data.districtObject.id;
    }
    json = {
      data: JSON.stringify(json),
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        console.log(res.data.data, "数据844884");
        if (res.data.result === 1) {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                // 要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); // 延迟时间
            },
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 弹窗显示省 市 区数据
  showCity() {
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }
    this.setData({
      cityshow: true,
    });
  },

  showReg() {
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }
    if (this.data.cityvalue == "") {
      wx.showToast({
        title: "请选择市级区域",
        icon: "none",
      });
      return;
    }
    this.setData({
      regshow: true,
    });
  },

  showStr() {
    if (this.data.provicevalue == "") {
      wx.showToast({
        title: "请选择省级区域",
        icon: "none",
      });
      return;
    }

    if (this.data.cityvalue == "") {
      wx.showToast({
        title: "请选择市级区域",
        icon: "none",
      });
      return;
    }

    if (this.data.regionvalue == "") {
      wx.showToast({
        title: "请选择区/县级区域",
        icon: "none",
      });
      return;
    }
    this.setData({
      streetshow: true,
    });
  },

  // 判断是否开启街道
  isStreetShow(value) {
    if (this.data.selectLevel.tag != 3) {
      return;
    }
    let urlStr = app.getNetAddresss("member.memberAddress.street");
    urlStr += "&district_id=" + value;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          console.log(res.data, "数据");

          this.setData({
            stroptions: res.data.data,
            strshow: true,
          });
        } else {
          this.setData({
            strshow: false,
          });
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  selectStrionHandle(value) {
    var Val = value.target.dataset.item || value.currentTarget.dataset.item;
    this.setData({
      districtObject: Val,
      streetvalue: Val.areaname,
      streetshow: false,
    });
  },
  // 等级
  onshow() {
    this.setData({
      selshow: true,
    });
  },
  onClickLeft() {
    this.setData({
      selshow: false,
      proshow: false,
      cityshow: false,
      regshow: false,
      streetshow: false,
      agreementShow: false,
    });
  },
  getlevelVal(e) {
    this.setData({
      selectLevel: e.target.dataset.item || e.currentTarget.dataset.item,
    });
    this.setData({
      selshow: false,
    });
    let name = this.data.selectLevel;
    if (name.tag == "0") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: false,
        regzoneshow: false,
        strshow: false,
      });
    } else if (name.tag == "1") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: true,
        regzoneshow: false,
        strshow: false,
      });
    } else if (name.tag == "2") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: true,
        regzoneshow: true,
        strshow: false,
      });
    } else if (name.tag == "3") {
      this.setData({
        prozoneshow: true,
        cityzoneshow: true,
        regzoneshow: true,
        strshow: false,
      });
    }
  },
  // 姓名输入框
  onChangeOne(val) {
    let val0 = val.detail.value;
    this.setData({
      name: val0,
    });
  },
  // 手机号输入框
  onChangeTwo(val) {
    let val1 = val.detail.value;
    this.setData({
      phone: val1,
    });
  },
  // 登录账号
  onChangeThree(val) {
    let val2 = val.detail.value;
    this.setData({
      username: val2,
    });
  },
  // 登录密码
  onChangeFour(val) {
    let val3 = val.detail.value;
    this.setData({
      password: val3,
    });
  },
});
