// packageH/signPage/identifyResult/identifyResult.js
const app = getApp();
Page({
  data: {
    show: "",
    company: {},
    person: {},
    timer: null,

    flow_id: "",
    face_flow_id: "",
  },

  onLoad: function (options) {
    let show = options.show;
    this.setNavigationBar(show);
    this.setData({
      show
    });
    if (this.data.timer) {
      clearInterval(this.data.timer);
    }
    if (show === "company") {
      this.getCompanyData();
    } else {
      if(options.auth_company == 1) {
        this.setData({
          person: {
            status: 'SUCCESSFUL'
          }
        });
      }else {
        this.checkPerson();
      }
    }
  },

  // 设置导航栏标题
  setNavigationBar (type) {
    if (type == "person") {
      wx.setNavigationBarTitle({
        title: '个人认证结果'
      });     
    } else {
      wx.setNavigationBarTitle({
        title: '企业认证结果'
      });  
    }
  },
  // methods
  toSign(event) {
    let str = '';
    let url = '';
    let eventData = event.target.dataset;
    if (eventData.str) str = eventData.str;
    if (str == 1) {
      url = '/packageH/signIndexPlu/signCompany/signCompany';
    } else if (str == "home") {
      url = '/packageH/signIndexPlu/contractList/contractList';
    } else {
      url = '/packageH/signIndexPlu/personBank/personBank';
    }
    wx.redirectTo({
      url,
    });
  },


  // 获取个人信息
  checkPerson() {
    let that = this;
    let requestUrl = app.getNetAddresss("plugin.shop-esign.frontend.person.get-person-info");
    app._getNetWork({
      url: requestUrl,
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result === 1 && res.data.info) {
          if(res.data.info.status == 1) {
            that.setData({
              person: {
                status: 'SUCCESS'
              }
            });
          }else {
            if(res.data.info.has_face == 1) {
              this.data.face_flow_id = res.data.info.face_flow_id;
              this.data.flow_id = res.data.info.flow_id;
              this.checkResult();
            }else {
              this.getPersonData();
            }
          }
        } else {
          that.setData({
            person: {
              status: 'FAIL'
            }
          });
          app.tips(res.msg);
        }
      }
    });
  },

  checkResult() {
    let that = this;
    let requestUrl = app.getNetAddresss("plugin.shop-esign.frontend.person.auth-face-result");
    app._postNetWork({
      url: requestUrl,
      data: {
        flow_id: this.data.flow_id,
        face_flow_id: this.data.face_flow_id
      },
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            person: res.data
          });
        } else {
          that.setData({
            person: {
              status: 'FAIL'
            }
          });
        }
      }
    });
  },

  getPersonData() {
    let that = this;
    let requestUrl = app.getNetAddresss("plugin.shop-esign.frontend.person.check-person-approve");
    app._getNetWork({
      url: requestUrl,
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            person: res.data
          });
          if(res.data.status_code == '99') {
            that.setData({
              person: {
                status: 'SUCCESS'
              }
            });
          }else {
            that.setData({
              person: {
                status: 'FAIL'
              }
            });
          }
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
 
  // 获取企业信息
  getCompanyData() {
    let that = this;
    let requestUrl = app.getNetAddresss("plugin.shop-esign.frontend.company.check-company-approve");
    app._getNetWork({
      url: requestUrl,
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result === 1) {
          that.setData({
            company: res.data,
            status: res.data.status_code
          });
          if (that.data.status == "1") {
            if (!that.data.timer) {
              that.data.timer = setInterval(() => {
                that.getCompanyData();
              }, 30000);
            }
          } else {
            if (that.data.timer) {
              clearInterval(that.data.timer);
            }
            setTimeout(()=>{
              wx.redirectTo({
                url: '/packageH/signIndexPlu/contractList/contractList',
              });
            },3000);
          }
        }
      },
      fial (err) {
        console.log(err, "-------err--------");
      }
    });
  },

  // 拨打电话
  freeTell () {
    wx.makePhoneCall({
      phoneNumber: '0755-66635068',
    });
  }
});