<template>
  <a-form ref="formRef" :model="vdata.currentConfig" layout="vertical" :rules="ifParamsRules">
    <a-row :gutter="16">
      <a-col span="12">
        <a-form-item label="签约协议编号" name="agreementNo">
          <a-input v-model:value.trim="vdata.currentConfig['agreementNo']" :disabled="true" placeholder="签约协议编号" style="width: 330px" />
          <a-button type="primary" style="margin-left:20px" @click="pageSign(injectInfoId)">
            1>签约开通
          </a-button>
          <a-button type="primary" style="margin-left:20px" @click="queryPageSign(injectInfoId)">
            2>签约查询
          </a-button>
        </a-form-item>
      </a-col>
      
      <a-col span="12">
        <a-form-item label="记账本ID" name="accountBookId">
          <a-input v-model:value.trim="vdata.currentConfig['accountBookId']" :disabled="true" placeholder="记账本ID" style="width: 330px" />
        </a-form-item>
      </a-col>
    </a-row>
  </a-form>
</template>
<script setup lang="ts">
import { upload, $alipayAgreementPageSign, $queryUserAgreementPageSign } from '@/api/manage'
import { ref, reactive, defineExpose, inject, getCurrentInstance } from 'vue'
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

const vdata = reactive ({
  mchType: 1, //商户类型
  state: '',
  isAdd: true,
  ifDefineArray: [], // 接口的配置定义项数组
  currentConfig: {} as any, // 当前配置项
  liteConfig: {} as any, // 小程序配置项
  activeKey: ['1'], // 折叠默认展开
  action: upload.cert, // 上传文件地址
  encryptionField : { // 定义加密的字段
    privateKey: '',
    alipayPublicKey: ''
  },

})

// 获取上级注入的infoId
const injectInfoId = inject('infoId')

const alipayAuthRef = ref()

// 获取上级注入的func
const initPageFunc : any = inject('initPageFunc')

//表单规则
const ifParamsRules = reactive({
})

// 当前的form
const formRef = ref()

function getConfigParams(){
  return formRef.value.validate().then( () => {
    return convertEncryData()
  })

}

// 转换加密数据
function convertEncryData(){

  // 对象克隆
  let result = Object.assign({}, vdata.currentConfig)
  Object.keys(vdata.encryptionField).forEach(key => {
    // 加密数据 && 空串
    if (!result[key]) {
      delete result[key]
    }
  })

  // 转换小程序参数
  result.liteParams = vdata.liteConfig

  return result
}

// 对外提供的页面的渲染函数 （ ifDefineArray = 接口的配置定义项数组，  currentConfig = 当前配置项,mchType=商户类型 ）
// 20230918 新增： isProcessStarField = true ( 是否处理 脱敏数据， 默认为 true  )
function pageRender(ifDefineArray, currentConfig, mchType, isProcessStarField = true){  
  
  // 赋值
  vdata.mchType = mchType
  vdata.currentConfig = currentConfig
  if(currentConfig.liteParams) {
    vdata.liteConfig = currentConfig.liteParams
  }

  // 处理加密数据字段
  if(isProcessStarField){
    Object.keys(vdata.encryptionField).forEach(key => {
      vdata.encryptionField[key] = vdata.currentConfig[key]
      delete vdata.currentConfig[key]
    })
  }

  // 重置form验证
  if (formRef.value !== undefined && formRef.value !== null) {
    formRef.value.resetFields()
  }

}

// 签约页面
function pageSign(appId) {
  $alipayAgreementPageSign(appId).then(res => {
    const winOpen =  window.open('', '_blank')
    winOpen?.document.write(res)
    winOpen?.focus()
  })
}

// 查询签约结果
function queryPageSign(appId) {
  $queryUserAgreementPageSign(appId).then(res => {
    if (!res.agreementNo) {
      return $infoBox.message.error('请先扫码签约，再查询结果')
    }
    vdata.currentConfig.agreementNo = res.agreementNo
    vdata.currentConfig.accountBookId = res.accountBookId
    $infoBox.message.success('查询成功')
  })
}

defineExpose({ getConfigParams, pageRender })

</script>
