// packageI/member_cancel/member_cancel.js
const app = getApp();
Page({

  data: {
    cancelInfo: {},
    isSubmitted: false, // 是否已提交
    checked: false, // 同意协议
    showConfirm: false, // 确认弹框
    showMobileVerification: false,
    bindMobile: false,
    imgCaptcha: false, 
    country: "", // 区号
    captcha: "", // 图形验证码

    mobile: "", // 手机号
    code: "", // 手机验证码
    count: "", // 验证码时间
  },

  onLoad: function (options) {

  },

  onShow: function () {
    this.getData();
  },

  getData() {
    let urlStr = app.getNetAddresss('member.cancel.getSet');
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
        if (res.data.data.set && res.data.data.set.content) {
          res.data.data.set.content = this.replaceDetail(res.data.data.set.content);
        }
        this.setData({
          cancelInfo: res.data.data.set,
          isSubmitted: res.data.data.set ? res.data.data.set.is_record : false,
          bindMobile: res.data.data.set ? res.data.data.set.is_bind_mobile : false,
        });
      }
    });
  },

  // 去除富文本中图片样式
  replaceDetail(details) {
    var texts = '';
    while (details.indexOf('<img') != -1) {
      // 截取到<img前面的内容
      texts += details.substring('0', details.indexOf('<img') + 4);
      // <img 后面的内容
      details = details.substring(details.indexOf('<img') + 4);
      if (details.indexOf('style=') != -1 && details.indexOf('style=') < details.indexOf('>')) {
        texts += details.substring(0, details.indexOf('style="') + 7) + "max-width:100%;height:auto;margin:0 auto;";
        details = details.substring(details.indexOf('style="') + 7);
      } else {
        texts += ' style="max-width:100%;height:auto;margin:0 auto;" ';
      }
    }
    texts += details; //最后拼接的内容
    return texts;
  },

  // 提交按钮
  submit() {
    if (!this.data.checked) {
      wx.showToast({
        title: '请先阅读申请协议并确认',
        icon: 'none'
      });
      return;
    }
    // 未绑定手机号
    if (this.data.cancelInfo.tel_status == 1 && !this.data.bindMobile ) {
      wx.showModal({
        title: "提示",
        content: "暂未绑定手机号, 请先绑定手机号",
        success: (res) => {
          if (res.confirm) {

            wx.navigateTo({
              url:'/packageA/member/editmobile/editmobile?num=' + this.data.mobile + '&fromApp=2&editType=0'
            });
            wx.setStorage({
              key: 'isset',
              data: 1
            });
          }
        }
      });
      return;
    }
    if (this.data.cancelInfo.tel_status == 1) {
      this.getCaptcha();
      this.setData({
        showMobileVerification: true
      });
    } else {
      this.setData({
        showConfirm: true
      });
    }
  },

  // 获取图形验证码
  getCaptcha () {
    let urlStr = app.getNetAddresss("home-page.getCaptcha");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        this.setData({
          imgCaptcha: res.data.data.captcha.img
        });
      }
    });
  },



  // 获取手机验证码
  sendCode() {
    if (this.data.cancelInfo.country_code && app._isTextEmpty(this.data.country)) {
      wx.showModal({
        content: "请输入国际区号",
        showCancel: false,
      });
      return;
    }

    if (app._isTextEmpty(this.data.mobile)) {
      wx.showModal({
        content: "请填写手机号",
        showCancel: false,
      });
      return;
    }

    if (this.data.imgCaptcha && app._isTextEmpty(this.data.captcha)) {
      wx.showModal({
        content: "请填图形验证码",
        showCancel: false,
      });
      return;
    }

    wx.showLoading({
      title: 'loading',
    });
    let urlStr = app.getNetAddresss(
      "member.register.sendCodeV2"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        reset: 1,
        sms_type: 0,
        mobile: this.data.mobile,
        state: this.data.country,
        captcha: this.data.captcha
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.result === 1) {
          wx.showToast({
            title: '发送成功',
          });
          let time_count = 60;
          if (!this.timer) {
            this.setData({
              count: time_count
            });
            this.timer = setInterval(() => {
              if (this.data.count > 0 && this.data.count <= time_count) {
                this.setData({
                  count: (--time_count)
                });
              } else {
                clearInterval(this.timer);
                this.timer = null;
              }
            }, 1000);
          }
        } else {
          this.getCaptcha();
          wx.hideLoading();
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
          });
        }
      },
      fail(err) {
        wx.hideLoading();
      }
    });
  },

  // 校验手机验证码
  checkCode() {
    if (app._isTextEmpty(this.data.mobile)) {
      wx.showModal({
        content: "请填写手机号",
        showCancel: false
      });
      return;
    }
    if (app._isTextEmpty(this.data.code)) {
      wx.showModal({
        content: "请填写验证码",
        showCancel: false
      });
      return;
    }
    let urlStr = app.getNetAddresss(
      "member.register.checkCode"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: this.data.mobile,
        code: this.data.code
      },
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showModal({
            content: res.data.msg,
            showCancel: false
          });
        } else {
          this.setData({
            showConfirm: true,
          });
        }
      }
    });
  },

  // 注销请求
  cancelPost() {
    let urlStr = app.getNetAddresss("member.cancel.index");
    wx.showLoading({ title: 'loading', });
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        wx.hideLoading();
        if (res.data.result !== 1) {
          wx.showModal({
            content: res.data.msg,
            showCancel: false
          });
        } else {
          wx.showToast({ title: res.data.msg,});
          this.setData({ isSubmitted: true });
        }
      },
      fail () {
        wx.hideLoading();
      }
    });
  },

  confirmCancel() {
    this.cancelPost();
    this.closeConfirmPopup();
  },

  // 关闭确认弹框
  closeConfirmPopup() {
    this.setData({
      showConfirm: false
    });
  },
  back() {
    wx.navigateBack({
      delta: 1,
    });
  },

  // 撤销申请
  revoke () {
    let urlStr = app.getNetAddresss("member.cancel.cancel");
    wx.showLoading({ title: 'loading', });
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        wx.hideLoading();
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          return;
        }
        wx.showToast({
          title: '撤销成功',
          icon: 'none'
        });
        this.initData();
        this.getData();
      },
      fail () {
        wx.hideLoading();
      }
    });
  },

  initData () {
    this.setData({
      cancelInfo: {},
      isSubmitted: false,
      checked: false,
      showConfirm: false,
      showMobileVerification: false,
      bindMobile: false,
      country: "",
      captcha: "",
  
      mobile: "",
      code: "",
      count: "",
    });
  },

  changeInput (event) {
    let param = event.currentTarget.dataset.key;
    this.setData({
      [`${param}`]: event.detail,
    });
  },
  onAgree(event) {
    this.setData({
      checked: event.detail,
    });
  },
  inpMobile(event) {
    this.setData({
      mobile: event.detail
    });
  },
  inpCode(event) {
    this.setData({
      code: event.detail
    });
  }
});