// packageH/signPage/signCompany/signCompany.js
const app = getApp();
Page({
  data: {
    showTip: false,
    step: 1,
    checkboxGroup: [],
    protocolShow: false,
    agreement: {},
    CompanyData: {},
    form: {
      name: "",
      org_code: "",
      legal_name: "",
      legal_idNo: "",
      org_type: "1"
    }
  },

  onLoad: function (options) {
  },

  onReady: function () {

  },

  onShow: function () {
    this.getAgreement();
    this.checkCompanyData();
  },

  toStep () {
    if (this.data.checkboxGroup.length >= 1) {
      this.getCompanyData();
      this.setData({
        step: 2
      });
    } else {
      wx.showModal({
        content: "请先同意并勾选协议",
        showCancel: false
      });
    }
  },
  checkoutInfo() {
    if(!this.data.form.name) {
      app.tips("请填写组织名称");
      return;
    }
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.company.qccCheck");
    app._postNetWork({
      url: queryUrl,
      data: {name: this.data.form.name},
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result === 1) {
          this.setData({
            'form.org_code': res.data.org_code
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false
          });
        }
      },
      fail (err) {
        console.log(err, "----失败-----");
      }
    });
  },
  getAgreement () {
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.company.get-agreement");
    app._postNetWork({
      url: queryUrl,
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result === 1) {
          this.setData({
            agreement: res.data
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false
          });
        }
      },
      fail (err) {
        console.log(err, "----失败-----");
      }
    });
  },

  // 获取企业信息
  getCompanyData () {
    let that = this;
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.company.get-company-auth-info");
    app._postNetWork({
      url: queryUrl,
      success (resdata) {
        let res = resdata.data;
        console.log(res.data);
        if (res.result === 1) {
          that.setData({
            CompanyData: res.data,
            ['form.name']: res.data.name,
            ['form.org_code']: res.data.org_code,
            ['form.legal_name']: res.data.legal_name,
            ['form.legal_idNo']: res.data.legal_idNo
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false
          });
        }
      },
      fail (err) {
        console.log(err, "----失败-----");
      }
    });
  },

  sureIdentify () {
    if(!this.data.form.legal_name) {
      app.tips("请填写姓名");
      return;
    }
    if(!this.data.form.legal_idNo) {
      app.tips("请选择身份证");
      return;
    }

    if(!this.data.form.org_type) {
      app.tips("请选择证件号类型");
      return;
    }

    if(!this.data.form.name) {
      app.tips("请填写组织名称");
      return;
    }

    if(!this.data.form.org_code) {
      app.tips("请填写证件号");
      return;
    }

    this.authRequest();
  },

  authRequest () {
    let that = this;
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.company.auth-company-v2");
    app._postNetWork({
      url: queryUrl,
      data: that.data.form,
      success (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          // that.setData({
          //   showTip: true
          // });
          // that.checkCompanyData();
          wx.redirectTo({
            url: '/packageH/signPage/identifyResult/identifyResult?show=company',
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success () {
              // wx.reLaunch({
              //   url: '/packageH/signPage/signIndex/signIndex',
              // });
            }
          });
        }
      },
      fail (err) {
        console.log(err, "----失败-----");
      }
    });
  },
  closeTip () {
    this.setData({
      showTip: false
    });
  },

  checkCompanyData () {
    let that = this;
    let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.company.check-company-approve");
    app._getNetWork({
      url: queryUrl,
      success (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          that.status = res.data.status;
          if (that.status == 'NO_INIT' || that.status == 'FAIL') {
            if (that.timer) {
              clearInterval(that.timer);
            }
          } else if (that.status == "ING") {
            if (!that.timer) {
              that.timer = setInterval(() => {
                this.checkCompanyData();
              }, 8000);
            }
          } else {
            if (that.timer) clearInterval(this.timer);
            wx.redirectTo({
              url: '/packageH/signPage/identifyResult/identifyResult?show=company',
            });
          }
        } else {
          if (that.timer) clearInterval(this.timer);
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false
          });
        }
      },
      fail (err) {
        console.log(err, "----失败-----");
      }
    });
  },




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

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

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


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