<template>
  <a-form v-if="vdata.currentConfig" ref="formRef" :model="vdata.currentConfig" layout="horizontal" :rules="vdata.formRules">
    <a-divider orientation="left">
      <a-tag color="#FF4B33">设备参数配置</a-tag>
    </a-divider>
    <a-row v-if="vdata.isAdd" justify="space-between" type="flex">
      <a-col :span="24">
        <a-form-item label="设备参数" name="deviceNo">
          <a-textarea v-model:value="vdata.currentConfig.deviceNo" placeholder="请输入" />
          <span class="jeepay-tip-text">请按照“deviceNo_deviceKey”格式填入，多个设备请以英文逗号隔开</span>
        </a-form-item>
      </a-col>
    </a-row>
    <a-row v-else justify="space-between" type="flex">
      <!-- <a-col :span="24">
        <a-form-item label="设备编号" name="deviceNo">
          <a-input v-model:value.trim="vdata.currentConfig.deviceNo" placeholder="请输入" />
          <span style="font-size: 10px; color: rebeccapurple">请按照“deviceNo_deviceKey”格式填入</span>
        </a-form-item>
      </a-col> -->
      <a-col :span="11">
        <a-form-item label="设备号" name="deviceNo">
          <a-input v-model:value.trim="vdata.currentConfig.deviceNo" placeholder="请输入" />
        </a-form-item>
      </a-col>
      <a-col :span="11">
        <a-form-item label="设备Key" name="deviceKey">
          <a-input v-model:value.trim="vdata.currentConfig.deviceKey" 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,
    isAdd: true, // 新增 or 修改页面标志
    // 表单规则
    formRules: {
      deviceNo: [{ required: true, message: '请输入设备参数', trigger: 'blur' }],
      deviceKey: [{ required: true, message: '请输入设备Key', trigger: 'blur' }]
    }
  })

  // 对外提供的页面的渲染函数 （ currentConfig = 当前配置项 ）
function pageRender(currentConfig: any, deviceType: any, isAdd:boolean){
  
  vdata.isAdd = isAdd

  // 赋值
  if (currentConfig) {
    vdata.currentConfig = currentConfig
  }

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

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

    if(vdata.isAdd) {
      return vdata.currentConfig
    }else {
      let deviceNo = vdata.currentConfig.deviceNo + '_' + vdata.currentConfig.deviceKey
      return {deviceNo : deviceNo}
    }
  })
}

defineExpose({ getConfigParams, pageRender })

</script>
