<template>
  <a-form v-if="vdata.currentConfig" ref="formRef" :model="vdata.currentConfig" layout="vertical" :rules="vdata.formRules">
    <a-divider orientation="left">
      <a-tag color="#FF4B33">智谷联参数配置</a-tag>
    </a-divider>
    <a-row justify="space-between" type="flex">
      <a-col :span="11">
        <a-form-item label="AccessKeyId" name="accessKeyId">
          <a-input v-model:value.trim="vdata.currentConfig.accessKeyId" placeholder="请输入" />
        </a-form-item>
      </a-col>
      <a-col :span="11">
        <a-form-item label="AccessKeySecret" name="accessKeySecret">
          <a-input v-model:value.trim="vdata.currentConfig.accessKeySecret" :placeholder="vdata.configDataDesen.accessKeySecret || '请输入'" />
        </a-form-item>
      </a-col>
      <a-col :span="11">
        <a-form-item label="实例ID" name="instanceId">
          <a-input v-model:value.trim="vdata.currentConfig.instanceId" placeholder="请输入" />
        </a-form-item>
      </a-col>
      <a-col :span="11">
        <a-form-item label="公网接入点" name="endPoint">
          <a-input v-model:value.trim="vdata.currentConfig.endPoint" placeholder="请输入" />
        </a-form-item>
      </a-col>
      <a-col :span="11">
        <a-form-item label="Topic" name="topic">
          <a-input v-model:value.trim="vdata.currentConfig.topic" placeholder="请输入" />
        </a-form-item>
      </a-col>
      <a-col :span="11">
        <a-form-item label="GroupId" name="groupId">
          <a-input v-model:value.trim="vdata.currentConfig.groupId" placeholder="请输入" />
        </a-form-item>
      </a-col>
    </a-row>
  </a-form>
</template>

<script lang="ts" setup>
import {reactive, ref, defineExpose} from 'vue'
 
  // 当前的form
  const formRef = ref()

  const  vdata = reactive({
    // 配置对象
    currentConfig: {} as any,
    configDataDesen: {
      accessKeySecret: ''
    },
    // 表单规则
    formRules: {
      accessKeyId: [{ required: true, message: '请输入accessKeyId', trigger: 'blur' }],
      accessKeySecret: [{ required: false, trigger: 'blur' }, {
        validator: (rule, value) => {
          if (!vdata.configDataDesen.accessKeySecret && !value) {
            return Promise.reject('请输入accessKeySecret')
          }
          return Promise.resolve()
        }
      }],
      instanceId: [{ required: true, message: '请输入实例ID', trigger: 'blur' }],
      endPoint: [{ required: true, message: '请输入公网接入点', trigger: 'blur' }],
      topic: [{ required: true, message: '请输入topic', trigger: 'blur' }],
      groupId: [{ required: true, message: '请输入groupId', trigger: 'blur' }]
    }
  })

  // 对外提供的页面的渲染函数 （ ifDefineArray = 接口的配置定义项数组，  currentConfig = 当前配置项 ）
function pageRender(currentConfig: any){
  // 赋值
  if (currentConfig) {
    vdata.currentConfig = currentConfig
  }

  vdata.configDataDesen.accessKeySecret = vdata.currentConfig.accessKeySecret
  vdata.currentConfig.accessKeySecret = ''

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

// 对外提供的获取配置参数函数 （返回JSON类型）
function getConfigParams(){
  return formRef.value.validate().then( () => {

  // 处理加密数据字段
  Object.keys(vdata.configDataDesen).forEach(key => {
    // 加密数据 && 空串
    if (!vdata.currentConfig[key]) {
      delete vdata.currentConfig[key]
    }
  })

    return vdata.currentConfig
  })
}

defineExpose({ getConfigParams, pageRender })

</script>
