// packageH/signPage/personBank/personBank.js
const app = getApp();
let that;
Page({
  data: {
    activeName: "",
    step: 1,
    checkboxGroup: [],
    protocolShow: false,
    agreement: {},
    form: {
      name: "",
      id_no: "",
      tel: "",
      face_auth_mode: "",
    },
    authcode: "",
    bank_no: "",
    flow_id: "",
    count: "",
    timer: null,
  },

  onLoad: function (options) {
    that = this;
    this.getPersonData();
    // this.getAgreement();
  },
  onReady: function () {},

  // request

  // 人员数据
  getPersonData() {
    let queryUrl = app.getNetAddresss("plugin.shop-esign.frontend.person.check-person-approve");
    app._getNetWork({
      url: queryUrl,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          // 已经认证成功
          that.setData({
            person: res.data,
          });
          if (res.status == "SUCCESS") {
            wx.navigateTo({
              url: `/packageH/signIndexPlu/identifyResult/identifyResult?show=person`,
            });
          }
        } else {
          console.log(res.msg);
        }
      },
      fail: function (err) {
        console.log(err, "失败-----------");
      },
    });
  },

  // 获取认证协议内容
  getAgreement() {
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.person.get-agreement");
    app._postNetWork({
      url: queryUrl,
      showToastIn: true,
      success(resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            agreement: res.data,
          });
        } else {
          app.tips(res.msg);
        }
      },
    });
  },

  // 第一步---> 开始认证按钮
  getMemberData() {
    that.setData({
      step: 2,
    });
    // if (this.data.checkboxGroup.length < 1) {
    //   wx.showModal({
    //     content: "请先同意并勾选协议",
    //     showCancel: false
    //   });
    //   return;
    // }
    // let queryUrl = app.getNetAddresss('plugin.yun-sign.frontend.h5.person.get-person-auth-info');
    // app._postNetWork({
    //   url: queryUrl,
    //   success: function(resdata) {
    //     that.setData({
    //       step: 2
    //     });
    //     let res = resdata.data;
    //     if (res.result === 1) {
    //       that.setData({
    //         ['form.name']: resdata.data.name,
    //         ['form.tel']: resdata.data.tel
    //       })
    //     } else {
    //       app.tips(res.msg)
    //     }
    //   },
    //   fail: function(err) {
    //     console.log(err, "失败-----------")
    //   }
    // })
  },

  // 第二步 ---> 进行认证按钮
  toStep() {
    if (!this.data.form.name) {
      app.tips("请先填写姓名");
      return;
    }
    if (!this.data.form.id_no) {
      app.tips("请先填写身份证号");
      return;
    }
    if (!this.data.form.tel) {
      app.tips("请先填写手机号");
      return;
    }
    if (!this.data.activeName) {
      app.tips("请先选择认证方式");
      return;
    }

    that.setData({
      step: 3,
    });
    // this.checkPersonData();
  },
  checkPersonData() {
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.person.check-person-approve-by-idNo");
    app._getNetWork({
      url: queryUrl,
      showToastIn: true,
      data: {
        name: that.data.form.name,
        idNo: that.data.form.id_no,
        tel: that.data.form.tel,
      },
      success: function (resdata) {
        if (resdata.data.result === 1) {
          let res = resdata.data;
          that.setData({
            person: res.data,
          });
          if (res.data.status_code == 99) {
            wx.showModal({
              title: "提示",
              content: "您在认证中心已经完成认证，无需重复认证",
              showCancel: false,
              success() {
                wx.navigateTo({
                  url: `/packageH/signIndexPlu/identifyResult/identifyResult?show=person`,
                });
              },
            });
          } else {
            that.setData({
              step: 3,
            });
          }
        } else {
          wx.showModal({
            content: resdata.data.msg,
            showCancel: false,
          });
        }
      },
      fail: function (err) {
        console.log(err, "失败-----------");
      },
    });
  },

  // 获取短信验证码
  sureIdentify() {
    if (this.data.activeName == 2 && !this.data.bank_no) {
      app.tips("请先填写银行卡");
      return;
    }
    let queryUrl = "";
    let json = {};
    const TIME_COUNT = 60;
    if (this.data.activeName == 1) {
      queryUrl = app.getNetAddresss("plugin.shop-esign.frontend.person.auth-tel-v2");
      json = this.data.form;
    } else if (this.data.activeName == 2) {
      queryUrl = app.getNetAddresss("plugin.shop-esign.frontend.person.auth-bank-card-v2");
      this.setData({
        ["form.bank_no"]: this.data.bank_no,
      });
      json = this.data.form;
    }
    app._postNetWork({
      url: queryUrl,
      data: json,
      success(resdata) {
        that.data.timer = null;
        if (resdata.data.result === 1) {
          let res = resdata.data;
          app.tips("已发送");
          that.setData({
            flow_id: res.data.flow_id,
          });
          if (!that.data.timer) {
            that.countDown = TIME_COUNT;
            that.data.timer = setInterval(() => {
              if (that.countDown > 0 && that.countDown <= TIME_COUNT) {
                that.setData({
                  count: --that.countDown,
                });
              } else {
                clearInterval(that.data.timer);
                that.data.timer = null;
              }
            }, 1000);
          }
        } else {
          wx.showModal({
            title: "提示",
            content: resdata.data.msg,
            showCancel: false,
          });
        }
      },
    });
  },

  // 第三步----> 立即认证按钮
  sendCode() {
    if (this.data.activeName == 2 && !this.data.bank_no) {
      app.tips("请先填写银行卡");
      return;
    }
    if (this.data.activeName != 3 && !this.data.flow_id) {
      app.tips("请先获取验证码");
      return;
    }
    console.log(this.data.authcode);
    if (this.data.activeName != 3 && !this.data.authcode) {
      app.tips("请先填写验证码");
      return;
    }
    if (this.data.activeName == 3 && !this.data.form.face_auth_mode) {
      console.log(typeof this.data.activeName, this.data.form);
      app.tips("请先选择识别方式");
      return;
    }

    this.sendAuthRequest();
  },
  sendAuthRequest() {
    let url = "";
    let json = {};
    if (this.data.activeName == 1) {
      url = app.getNetAddresss("plugin.shop-esign.frontend.person.check-tel-sms-code");
      json = {
        flow_id: this.data.flow_id,
        authcode: this.data.authcode,
      };
    } else if (this.data.activeName == 2) {
      url = app.getNetAddresss("plugin.shop-esign.frontend.person.check-bank-sms-code");
      json = {
        flow_id: this.data.flow_id,
        authcode: this.data.authcode,
      };
    } else {
      url = app.getNetAddresss("plugin.shop-esign.frontend.person.auth-face");
      json = {
        name: this.data.form.name,
        id_no: this.data.form.id_no,
        tel: this.data.form.tel,
        face_auth_mode: this.data.form.face_auth_mode,
        callback_url: "",
      };
    }

    app._postNetWork({
      url,
      data: json,
      success(resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          if (that.data.activeName == 3) {
            wx.redirectTo({
              url: `/packageH/signIndexPlu/signPage?url=${res.data.data.authUrl}`,
            });
          } else {
            app.tips(res.msg);
            if (res.data.auth_company == 1) {
              wx.redirectTo({
                url: "/packageH/signIndexPlu/identifyResult/identifyResult?show=person&auth_company=1",
              });
            } else {
              wx.redirectTo({
                url: "/packageH/signIndexPlu/identifyResult/identifyResult?show=person",
              });
            }
          }
        } else {
          wx.showModal({
            title: "提示",
            content: resdata.data.msg,
            showCancel: false,
          });
        }
      },
      fail(err) {
        app.tips(err.msg);
      },
    });
  },

  // methodes

  // 协议阅读情况
  onAgree(event) {
    this.setData({
      checkboxGroup: event.detail,
    });
  },

  // 显示 popup
  showProtocol() {
    this.setData({
      protocolShow: true,
    });
  },

  // 关闭popup
  closePopup() {
    console.log(this.data.checkboxGroup);
    this.setData({
      protocolShow: false,
    });
  },

  // 表单修改
  handlerInput(event) {
    let type = event.target.dataset.type;
    this.setData({
      [`${type}`]: event.detail,
    });
  },

  /**
   * 隐藏
   */
  onHide() {
    clearInterval(this.data.timer);
  },
});
