<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="code">
          <a-input v-model:value.trim="vdata.currentConfig['code']" placeholder="请输入激活码code" @change="changeFun('code')" />
        </a-form-item>
      </a-col>
      <a-col span="12">
        <a-form-item label=" ">
          <a-button type="primary" @click="activationFun()">激活</a-button>
        </a-form-item>
      </a-col>
      <a-col span="24">
        <a-alert message="点击激活将生成终端信息。注：该商户在对应服务商下仅可生成一次终端信息。" type="warning" />
        <a-divider />
      </a-col>
      <a-col span="12">
        <a-form-item label="终端号" name="terminalSn">
          <a-input v-model:value.trim="vdata.currentConfig['terminalSn']" placeholder="请输入终端号" @change="changeFun('terminalSn')" />
        </a-form-item>
      </a-col>
      <a-col span="12">
        <a-form-item label="终端秘钥" name="terminalKey">
          <a-input v-model:value.trim="vdata.currentConfig['terminalKey']" placeholder="请输入终端秘钥" @change="changeFun('terminalKey')" />
        </a-form-item>
      </a-col>
    </a-row>
  </a-form>
</template>
<script setup lang="ts">
import { upload, $sqbActivationConfig } from '@/api/manage'
import { ref, reactive, defineExpose, getCurrentInstance, inject } from 'vue'
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

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

})

//表单规则
const ifParamsRules = reactive({
    terminalSn: [{ required: true, message: '请输入终端号', trigger: 'blur' }],
    terminalKey: [{ required: true, message: '请输入终端秘钥', trigger: 'blur' }],
})

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

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

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.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 activationFun(){

  if(!vdata.currentConfig.code){
    $infoBox.message.error('请先输入激活码')
    return Promise.reject()
  }

  return $sqbActivationConfig({mchAppId: injectInfoId, code: vdata.currentConfig.code}).then((res) => {
    vdata.currentConfig.terminalSn = res.terminalSn
    vdata.currentConfig.terminalKey = res.terminalKey
    $infoBox.modalSuccess('激活成功')
  })

}

// 消除参数空格
function changeFun(value) {
  if(vdata.currentConfig[value]) {
    vdata.currentConfig[value] = vdata.currentConfig[value].trim()
  }
}

defineExpose({ getConfigParams, pageRender })

</script>
