// packageH/signPage/signCompany/signCompany.js
const app = getApp();
Page({
  data: {
    showTip: false,
    step: 1,
    checkboxGroup: [],
    protocolShow: false,
    agreement: {},
    CompanyData: {},
    timer: null,
    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
  //     });
  //   }
  // },
  getAgreement () {
    let that = this;
    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) {
          that.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.shop-esign.frontend.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/signIndexPlu/identifyResult/identifyResult?show=company',
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success () {
              // wx.reLaunch({
              //   url: '/packageH/signIndexPlu/signIndex/signIndex',
              // });
            }
          });
        }
      },
      fail (err) {
        console.log(err, "----失败-----");
      }
    });
  },
  closeTip () {
    this.setData({
      showTip: false
    });
  },

  checkCompanyData () {
    let that = this;
    let queryUrl = app.getNetAddresss("plugin.shop-esign.frontend.company.check-company-approve");
    app._getNetWork({
      url: queryUrl,
      success (resdata) {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            CompanyData: res.data,
          });
          that.data.status = res.data.status_code;
          if (that.data.status == '0' || that.data.status == '-1') {
            if (that.data.timer) {
              clearInterval(that.data.timer);
            }
          } else if (that.data.status == "1") {
            if (!that.data.timer) {
              that.data.timer = setInterval(() => {
                this.checkCompanyData();
              }, 8000);
            }
          } else {
            if (that.data.timer) clearInterval(that.data.timer);
            wx.redirectTo({
              url: '/packageH/signIndexPlu/identifyResult/identifyResult?show=company',
            });
          }
        } else {
          if (that.data.timer) clearInterval(that.data.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(that.data.timer);
  }
});