import { protocolEnum, protocolNavigateTo } from '/utils/common/protocol'
import { tokenCheck, pushThree } from '/utils/api/auth_api'

Page({
  data: {
    protocolEnum: protocolEnum,
    protocolAgree: false,
    status: {
      // 当前的申请状态: 0表示初始状态，1表示授权受限，2表示授权成功，3表示授权失败
      statusCode: 0,
      statusPrompt: ''
    },
    // 申请授权的商户信息
    formOption: {
      // 授权商户名称
      name: '',
      // 授权成功的商户服务器回调地址
      notify: null,
      // 认证分配的唯一token
      token: '',
      // 任务id
      taskId: ''
    }
  },
  protocol(e) {
    protocolNavigateTo(e.currentTarget.dataset.type)
  },
  protocolChange(e) {
    this.setData({
      protocolAgree: e.detail.value
    })
  },
  authClick() {
    if(!this.data.protocolAgree) {
      return
    }
    const authPopup = this.$selectComponent('#authPopup')
    authPopup.open(this.data.formOption.name)
  },
  // 授权获取三要素结果
  async authResult(e) {
    if(e.detail && e.detail.code === 200) {
      my.showLoading({
        content: "加载中..."
      })
      const params = {
        userId: e.detail.data.userId,
        certName: e.detail.data.username,
        certNo: e.detail.data.idNumber,
        certType: 'IDENTITY_CAR',
        phone: e.detail.data.phone,
        avatar: e.detail.data.avatar || null,
        personPictures: e.detail.data.personPictures || null,
        photoBase64: e.detail.data.photoBase64 || null,
        pushUrl: this.data.formOption.notify || null,
        taskId: this.data.formOption.taskId,
        token: this.data.formOption.token
      }
      // my.alert({
      //   title: '提示authResult',
      //   content: JSON.stringify(params)
      // })
      const result = await pushThree(params)
      if(result.data) {
        this.setData({
          status: {
            statusCode: 2,
            statusPrompt: '身份授权认证成功！'
          }
        })
      } else {
        this.setData({
          status: {
            statusCode: 1,
            statusPrompt: result.msg
          }
        })
      }
      my.hideLoading()
    } else {
      this.setData({
        status: {
          statusCode: 3,
          statusPrompt: '身份授权认证失败！'
        }
      })
    }
  },
  backClick() {
    my.navigateBackMiniProgram({
      extraData: {
          authStatus: (this.data.status && this.data.status.statusCode === 2) ? true : false
      }
    })
  },
  // 更新status状态
  updateStatus(status = null, obj = null) {
    if(status && Object.keys(status).length > 0) {
      // my.alert({
      //   title: '提示updateStatus',
      //   content: JSON.stringify(obj)
      // })
      this.setData({
        status,
        formOption: obj
      })
    } else {
      this.setData({
        status: {
          statusCode: 1,
          statusPrompt: '授权错误'
        }
      })
    }
  },
  async checkToken(options) {
    const result = await tokenCheck(options.token)
    // my.alert({
    //   title: 'tokenCheck',
    //   content: JSON.stringify(result)
    // })
    if(result.data) {
      this.updateStatus({statusCode: 0}, options)
    } else {
      this.updateStatus({statusCode: 1, statusPrompt: result.msg}, options)
    }
  },
  onLoad() {
    let launchOptions = my.getLaunchOptionsSync()
    if(!launchOptions.query) {
      launchOptions = my.getEnterOptionsSync()
    }
    // my.alert({
    //   title: 'launchOptions',
    //   content: JSON.stringify(launchOptions.query)
    // })
    if(launchOptions.query && Object.keys(launchOptions.query).length > 0 && launchOptions.query.token) {
        this.checkToken(launchOptions.query)
    } else {
      this.updateStatus()
    }
  },
  onShow: function () {
    my.hideBackHome()
  }
});
