<template>
  <a-form
    v-if="vdata.currentConfig"
    ref="formRef"
    :model="vdata.currentConfig"
    layout="horizontal"
    :label-col="{ span: 6 }"
    :rules="vdata.formRules"
  >
    <a-divider orientation="left">
      <a-tag color="#FF4B33">设备参数配置</a-tag>
    </a-divider>
    <a-row justify="space-between" type="flex">
      <a-col v-if="vdata.isAdd" :span="24">
        <a-form-item label="设备编号" name="deviceNo">
          <a-input v-model:value="vdata.currentConfig.deviceNo" placeholder="请输入设备编号" />
        </a-form-item>
      </a-col>
    </a-row>
    <a-row justify="space-between" type="flex">
      <a-col v-if="!vdata.isAdd" :span="24">
        <a-form-item label="设备编号" name="deviceNo">
          <a-input v-model:value="vdata.currentConfig.deviceNo" placeholder="请输入设备编号" />
        </a-form-item>
      </a-col>
    </a-row>

    <div v-if="!vdata.isAdd">
      <a-row justify="space-between" type="flex">
        <a-col :span="24">
          <a-form-item label="是否配置打印机参数" name="configFlag">
            <a-radio-group v-model:value="vdata.currentConfig.configFlag">
              <a-radio :value="1">是</a-radio>
              <a-radio :value="0">否</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row v-if="vdata.currentConfig.configFlag==1" justify="space-between" type="flex">
        <a-divider orientation="left">
          <a-tag color="#FF4B33">支付配置信息</a-tag>
        </a-divider>
        <a-col :span="24">
          <a-form-item label="是否截获收银小票金额" name="billCap">
            <a-radio-group v-model:value="vdata.currentConfig.billCap">
              <a-radio :value="1">是</a-radio>
              <a-radio :value="0">否</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="收银小票打印标志" name="billPrint">
            <a-radio-group v-model:value="vdata.currentConfig.billPrint">
              <a-radio :value="2">支付后打印</a-radio>
              <a-radio :value="1">支付前打印</a-radio>
              <a-radio :value="0">不打印</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="支付超时时间" name="billTimeout">
            <a-input-number v-model:value="vdata.currentConfig.billTimeout" :min="1" :step="1" :precision="0" addonAfter="秒" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="交易结果查询时间间隔" name="queryInterval">
            <a-input-number v-model:value="vdata.currentConfig.queryInterval" :min="1" :step="1" :precision="0" addonAfter="秒" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="最大交易结果查询次数" name="queryNum">
            <a-input-number v-model:value="vdata.currentConfig.queryNum" :min="1" :step="1" :precision="0" addonAfter="次" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="支付错误信息打印标志" name="errInfoPrint">
            <a-radio-group v-model:value="vdata.currentConfig.errInfoPrint">
              <a-radio :value="false">不打印</a-radio>
              <a-radio :value="true">打印</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="退款码打印标志" name="refundCodePrint">
            <a-radio-group v-model:value="vdata.currentConfig.refundCodePrint">
              <a-radio :value="false">不打印</a-radio>
              <a-radio :value="true">打印</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="收银系统厂商" name="providerCode">
            <a-select v-model:value="vdata.currentConfig.providerCode" placeholder="请选择厂商" @change="providerCheck">
              <a-select-option v-for="item in vdata.providerList" :key="item.code" :value="item.code">{{ item.name }}</a-select-option>
              <a-select-option value="-1">其他</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col v-if="vdata.currentConfig.providerCode != '-1'" :span="24">
          <a-form-item label="收银软件版本" name="softCode">
            <a-select v-model:value="vdata.currentConfig.softCode" placeholder="请选择软件" @change="softCheck">
              <a-select-option v-for="item in vdata.softList" :key="item.code" :value="item.code">{{ item.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col v-if="vdata.currentConfig.providerCode != '-1'" :span="24">
          <a-form-item label="金额关键字" name="presetPrefix">
            <a-input v-model:value="vdata.currentConfig.presetPrefix" placeholder="收银小票金额拦截关键字" />
            <span class="jeepay-tip-text">收银小票金额拦截关键字，只允许一个关键字</span>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
            <a-form-item label="配置参数" name="presetData">
              <a-textarea
                v-model:value="vdata.currentConfig.presetData"
                disabled="disabled"
                style="height: 120px"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="时间戳" name="timestamp">
              <span>{{ vdata.currentConfig.timestamp }}</span>
            </a-form-item>
          </a-col> -->
        <a-row v-else justify="space-between" type="flex">
          <a-col :span="24">
            <a-form-item label="金额关键字" name="keywordCap">
              <a-input v-model:value="vdata.currentConfig.keywordCap" placeholder="获取收银小票支付金额关键字" />
              <span class="jeepay-tip-text">获取收银小票支付金额关键字，每个关键字直接用逗号分隔，不能带空格 (实收,应收,总价,总计,合计)</span>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="放行关键字" name="keywordSkip">
              <a-input v-model:value="vdata.currentConfig.keywordSkip" placeholder="放弃支付（小票放行关键字）" />
              <span class="jeepay-tip-text">放弃支付（小票放行关键字），每个关键字直接用逗号分隔，不能带空格 (店名,销售商品报表)</span>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="收银单号前缀" name="orderPrefix">
              <a-input v-model:value="vdata.currentConfig.orderPrefix" placeholder="收银单号前缀" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否跳过订单编号" name="orderSkip">
              <a-radio-group v-model:value="vdata.currentConfig.orderSkip">
                <a-radio :value="true">是</a-radio>
                <a-radio :value="false">否</a-radio>
              </a-radio-group>
              <br>
              <span class="jeepay-tip-text">重复的订单编号，是否跳过</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-col :span="24">
          <a-form-item label="验证码authCode" name="authCode">
            <a-input v-model:value="vdata.currentConfig.authCode" placeholder="请输入验证码authCode" />
            <span class="jeepay-tip-text">从开机时打印的参数配置码中提取（二维码中","后的参数）</span>
          </a-form-item>
        </a-col>
      </a-row>
    </div>
  </a-form>
</template>

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

  const vdata = reactive({
    // 配置对象
    currentConfig: {
      configFlag: 0,
      billCap: 1,
      billPrint: 2,
      billTimeout: 180,
      queryInterval: 5,
      queryNum: 18,
      errInfoPrint: false,
      refundCodePrint: false,
      orderSkip: true,
    } as any,
    deviceType: null, // 设备类型： 1-云喇叭, 2-云打印
    isAdd: true, // 新增 or 修改页面标志
    // 表单规则
    formRules: {
      deviceNo: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
      configFlag: [{ required: true, message: '请选择是否配置打印机参数', trigger: 'blur' }],
      billCap: [{ required: true, message: '请选择是否截获收银小票金额', trigger: 'blur' }],
      billPrint: [{ required: true, message: '请选择收银小票打印标志', trigger: 'blur' }],
      billTimeout: [{ required: true, message: '请输入支付超时时间', trigger: 'blur' }],
      queryInterval: [{ required: true, message: '请输入交易结果查询时间间隔', trigger: 'blur' }],
      queryNum: [{ required: true, message: '请输入最大交易结果查询次数', trigger: 'blur' }],
      authCode: [{ required: true, message: '请输入验证码authCode', trigger: 'blur' }],
      errInfoPrint: [{ required: true, message: '请选择支付错误信息打印标志', trigger: 'blur' }],
      refundCodePrint: [{ required: true, message: '请选择退款码打印标志', trigger: 'blur' }],
      providerCode: [{ required: true, message: '请选择厂商', trigger: 'blur' }],
      softCode: [{ required: true, message: '请选择软件', trigger: 'blur' }],
      presetPrefix: [{ required: true, message: '请输入收银小票金额拦截关键字', trigger: 'blur' }],
      timestamp: [{ required: true, message: '请选择软件', trigger: 'blur' }],
      presetData: [{ required: true, message: '请选择软件', trigger: 'blur' }],
      keywordCap: [{ required: true, message: '请输入获取收银小票支付金额关键字', trigger: 'blur' }],
      keywordSkip: [{ required: true, message: '请输入放行关键字', trigger: 'blur' }],
    },
    printConfig: {} as any,
    providerList: [] as any,
    softList: [] as any,
  })

// 对外提供的页面的渲染函数 （ ifDefineArray = 接口的配置定义项数组，  currentConfig = 当前配置项 ）
function pageRender(currentConfig: any, deviceType: any, isAdd:boolean){

  vdata.deviceType = deviceType
  vdata.isAdd = isAdd

  if (currentConfig) {
    vdata.currentConfig = currentConfig
    vdata.currentConfig.authCode = ''
    nextTick(() => {
      if (vdata.currentConfig.providerCode != -1) {
        getSoftList(vdata.currentConfig.providerCode)
      }
    })
  }
  getCompanyList()

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

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

function providerCheck(value) {

  vdata.currentConfig.timestamp = ''
  vdata.currentConfig.softName = ''
  vdata.currentConfig.presetData = ''
  vdata.currentConfig.softCode = ''

  getSoftList(value)
}

function softCheck(value) {
  // 参数重置
  vdata.currentConfig.timestamp = ''
  vdata.currentConfig.softName = ''
  vdata.currentConfig.presetData = ''
  getSoftConfig(value)
}

// 厂商列表
function getCompanyList() {
  $getProviderParams('/getZwCompanyList', {}).then((res: any) => {
    vdata.providerList = res
  }).catch ((error) =>{console.log(error)} )
}

// 软件列表
function getSoftList(value) {
  vdata.softList = []
  $getProviderParams('/getZwSoftCodeList/' + value, {}).then((res: any) => {
    vdata.softList = res
  }).catch ((error) =>{console.log(error)})
}

// 软件列表
function getSoftConfig(value) {
  $getProviderParams('/getZwConfigInfo/' + value, {}).then((res: any) => {
    vdata.currentConfig.timestamp = res.timestamp
    vdata.currentConfig.softName = res.name
    vdata.currentConfig.presetData = res.preset_data
  }).catch ((error) =>{console.log(error)})
}

defineExpose({ getConfigParams, pageRender })

</script>
