<!-- 设备参数编辑页面 -->
<template>
  <el-dialog v-model="dialog.visible" :title="dialog.title" width="70%" :draggable="draggable">
    <el-row>
      <el-col :lg="24" :xs="24">
        <el-card class="p-class h-67 w-full">
          <template #header>
            <div><span class="text-red-500 text-base">消费设备参数</span></div>
          </template>
          <el-row>
            <el-col :lg="12" :xs="24" class="h-10">
              <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="auto">
                <el-form-item label="消费工作模式" prop="workMode" class="w-3/5">
                  <el-select v-model="formData.workMode" placeholder="请选择消费工作模式">
                    <el-option v-for="dict in XF_WorkMode" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="卡上最大金额" prop="maxCardMoney" class="w-3/5">
                  <el-input
                    v-model="formData.maxCardMoney"
                    type="number"
                    :min="0"
                    placeholder="请输入卡上最大金额"
                    @blur="(e) => blurInput('maxCardMoney')"
                    @keydown="keyInput"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item label="开机模式" prop="openMode">
                  <el-radio-group v-model="formData.openMode">
                    <el-radio value="0">正常开机</el-radio>
                    <el-radio value="1">开机后需要刷营业卡方可消费</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :lg="12" :xs="24" class="bg-gray-200">
              <div><el-text type="danger"> 模式说明: </el-text></div>
              <div class="pl-15px"><el-text type="danger"> 单价：有营业员手工输入消费金额，其额度受单次最大消费限制 </el-text></div>
              <div class="pl-15px"><el-text type="danger"> 定值：每次使用固定的金额进行消费 </el-text></div>
              <div class="pl-15px"><el-text type="danger"> 编号：有营业员手工输入编号，实际消费额魏编号所代表的实际金额 </el-text></div>
              <div><el-text type="danger"> 时间段： </el-text></div>
              <div class="pl-15px"><el-text type="danger"> 仅在指定的时间段内允许消费。消费金额在营业时间段设置中单独设置。 </el-text></div>
              <div class="pl-15px">
                <el-text type="danger"> 某些机型中允许按卡类折扣。某些机型只有设置了时间段信息，则默认使用时间段消费模式。 </el-text>
              </div>
              <div><el-text type="danger"> 注：各模式下进行消费时，卡内余额均不得超过最大卡金额 </el-text></div>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :lg="16" :xs="24" class="pr-8px">
        <el-card class="h-77">
          <template #header>
            <div><span class="text-red-500 text-base">参数设置</span></div>
          </template>
          <el-form :model="formData" label-width="auto">
            <el-row>
              <el-col :span="12">
                <el-form-item label="每天最大消费次数(每日限次)" prop="dayCount" class="w-5/6">
                  <el-input
                    v-model="formData.dayCount"
                    type="number"
                    :min="0"
                    placeholder="请输入次数,0为不限"
                    @blur="(e) => blurInput('dayCount')"
                    @keydown="(e) => keyInput(e, true)"
                  >
                    <template #suffix>
                      <span>次</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="每天最大消费金额(每日限额)" prop="dayMoney" class="w-5/6">
                  <el-input
                    v-model="formData.dayMoney"
                    type="number"
                    :min="0"
                    placeholder="请输入金额"
                    @blur="(e) => blurInput('dayMoney')"
                    @keydown="keyInput"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="每餐最大消费次数(每餐限次)" prop="mealCount" class="w-5/6">
                  <el-input
                    v-model="formData.mealCount"
                    type="number"
                    :min="0"
                    placeholder="请输入次数,0为不限"
                    @blur="(e) => blurInput('mealCount')"
                    @keydown="(e) => keyInput(e, true)"
                  >
                    <template #suffix>
                      <span>次</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单次最大消费金额(单次限额)" prop="singleMoney" class="w-5/6">
                  <el-input
                    v-model="formData.singleMoney"
                    type="number"
                    :min="0"
                    placeholder="请输入单次最大消费金额"
                    @blur="(e) => blurInput('singleMoney')"
                    @keydown="keyInput"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="定值模式每次消费金额" prop="constantValue" class="w-5/6">
                  <el-input
                    v-model="formData.constantValue"
                    type="number"
                    :min="0"
                    placeholder="请输入定值消费金额"
                    @blur="(e) => blurInput('constantValue')"
                    @keydown="keyInput"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="两次刷卡间隔" prop="swipeInterval" class="w-5/6">
                  <el-input
                    v-model="formData.swipeInterval"
                    type="number"
                    :min="0"
                    placeholder="请输入,0为不限制"
                    @blur="(e) => blurInput('swipeInterval')"
                    @keydown="(e) => keyInput(e, true)"
                  >
                    <template #suffix>
                      <span>分钟</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="卡片有效限制" prop="termValidity" class="w-5/6">
                  <el-radio-group v-model="formData.termValidity">
                    <el-radio value="1">启用</el-radio>
                    <el-radio value="0">禁用</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
      <el-col :lg="8" :xs="24">
        <el-card class="h-77">
          <template #header>
            <div><span class="text-red-500 text-base">编号模式下的各编号金额</span></div>
          </template>
          <el-form :model="formData" label-width="auto">
            <el-row>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号0:" prop="rationZero" class="w-5/6">
                  <el-input-number v-model="formData.rationZero" size="small" :min="0" placeholder="请输入编号0代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号1:" prop="rationOne" class="w-5/6">
                  <el-input-number v-model="formData.rationOne" size="small" :min="0" placeholder="请输入编号1代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号2:" prop="rationTwo" class="w-5/6">
                  <el-input-number v-model="formData.rationTwo" size="small" :min="0" placeholder="请输入编号2代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号3:" prop="rationThree" class="w-5/6">
                  <el-input-number v-model="formData.rationThree" size="small" :min="0" placeholder="请输入编号3代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号4:" prop="rationFour" class="w-5/6">
                  <el-input-number v-model="formData.rationFour" size="small" :min="0" placeholder="请输入编号4代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号5:" prop="rationFive" class="w-5/6">
                  <el-input-number v-model="formData.rationFive" size="small" :min="0" placeholder="请输入编号5代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号6:" prop="rationSix" class="w-5/6">
                  <el-input-number v-model="formData.rationSix" size="small" :min="0" placeholder="请输入编号6代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号7:" prop="rationSeven" class="w-5/6">
                  <el-input-number v-model="formData.rationSeven" size="small" :min="0" placeholder="请输入编号7代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号8:" prop="rationEight" class="w-5/6">
                  <el-input-number v-model="formData.rationEight" size="small" :min="0" placeholder="请输入编号8代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="h-10 mb-2">
                <el-form-item label="编号9:" prop="rationNine" class="w-5/6">
                  <el-input-number v-model="formData.rationNine" size="small" :min="0" placeholder="请输入编号9代表的金额">
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :lg="24" :xs="24">
        <el-card>
          <template #header>
            <div><span class="text-red-500 text-base">允许使用的卡类</span></div>
          </template>
          <div class="mt-4">
            <el-checkbox-group v-model="cardTypeList" @change="handleCardTypeChange">
              <el-checkbox v-for="item in cardTypeOptions" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
            </el-checkbox-group>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :lg="16" :xs="24" class="pr-8px">
        <el-card class="h-95">
          <template #header>
            <div><span class="text-red-500 text-base">营业时间段信息</span></div>
          </template>
          <el-row :gutter="15">
            <el-col :span="24" class="mb-3">
              <el-button type="primary" class="mr-2" @click="handleGetTimePeriod">获取营业时间段</el-button>
              <el-text type="danger"> (注：请按实际营业时间段进行设置；若两个时间段的值都为00:00，则此时间段不启用)</el-text>
            </el-col>
            <el-col :span="4">
              <el-row class="text-right items-center">
                <el-col :span="24" class="mb-2 text-red-500">营业时间段(餐次)</el-col>
                <el-col :span="24" class="h-10 mb-2 items-center"><div class="py-2 font-bold" style="color: #606266">早 餐：</div></el-col>
                <el-col :span="24" class="h-10 mb-2 items-center"><div class="py-2 font-bold" style="color: #606266">午 餐：</div></el-col>
                <el-col :span="24" class="h-10 mb-2 items-center"><div class="py-2 font-bold" style="color: #606266">晚 餐：</div></el-col>
                <el-col :span="24" class="h-10 mb-2 items-center"><div class="py-2 font-bold" style="color: #606266">夜 宵：</div> </el-col>
              </el-row>
            </el-col>
            <el-col :span="6">
              <el-row>
                <el-col :span="24" class="text-center mb-2 text-red-500"><div>消费金额</div></el-col>
                <el-col :span="24" class="mb-2">
                  <div>
                    <el-input
                      v-model="formData.breakfastMoney"
                      type="number"
                      :min="0"
                      class="h-10"
                      placeholder="请输入"
                      @keydown="keyInput"
                      @blur="(e) => blurInput('breakfastMoney')"
                    >
                      <template #suffix>
                        <span>元</span>
                      </template>
                    </el-input>
                  </div>
                </el-col>
                <el-col :span="24" class="mb-2">
                  <el-input
                    v-model="formData.lunchMoney"
                    type="number"
                    :min="0"
                    class="h-10"
                    placeholder="请输入"
                    @keydown="keyInput"
                    @blur="(e) => blurInput('lunchMoney')"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-col>
                <el-col :span="24" class="mb-2">
                  <el-input
                    v-model="formData.supperMoney"
                    type="number"
                    :min="0"
                    class="h-10"
                    placeholder="请输入"
                    @keydown="keyInput"
                    @blur="(e) => blurInput('supperMoney')"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-col>
                <el-col :span="24" class="mb-2"
                  ><el-input
                    v-model="formData.nightMoney"
                    type="number"
                    :min="0"
                    class="h-10"
                    placeholder="请输入"
                    @keydown="keyInput"
                    @blur="(e) => blurInput('nightMoney')"
                  >
                    <template #suffix>
                      <span>元</span>
                    </template>
                  </el-input>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="6" class="ml-4">
              <el-row>
                <el-col :span="24" class="text-center mb-2 text-red-500">开始时间</el-col>
                <el-col :span="24" class="mb-2">
                  <el-time-picker
                    v-model="eatTimePeriod.breakfastBegin"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('breakfastBegin')"
                  />
                </el-col>
                <el-col :span="24" class="mb-2">
                  <el-time-picker
                    v-model="eatTimePeriod.lunchBegin"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('lunchBegin')"
                  />
                </el-col>
                <el-col :span="24" class="mb-2">
                  <el-time-picker
                    v-model="eatTimePeriod.supperBegin"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('supperBegin')"
                  />
                </el-col>
                <el-col :span="24" class="mb-2">
                  <el-time-picker
                    v-model="eatTimePeriod.nightBegin"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('nightBegin')"
                  />
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="6" class="ml-4">
              <el-row>
                <el-col :span="24" class="text-center mb-2 text-red-500">结束时间</el-col>
                <el-col :span="24" class="mb-2">
                  <el-time-picker
                    v-model="eatTimePeriod.breakfastEnd"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('breakfastEnd')"
                  />
                </el-col>
                <el-col :span="24" class="mb-2"
                  ><el-time-picker
                    v-model="eatTimePeriod.lunchEnd"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('lunchEnd')"
                /></el-col>
                <el-col :span="24" class="mb-2"
                  ><el-time-picker
                    v-model="eatTimePeriod.supperEnd"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('supperEnd')"
                /></el-col>
                <el-col :span="24" class="mb-2"
                  ><el-time-picker
                    v-model="eatTimePeriod.nightEnd"
                    style="width: 100%"
                    format="HH:mm"
                    value-format="HHmm"
                    @change="handleEatTimeChange('nightEnd')"
                /></el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
      <el-col :lg="8" :xs="24">
        <el-card class="h-95">
          <template #header>
            <div><span class="text-red-500 text-base">自动重启时间</span></div>
          </template>
          <el-form ref="rebootTimeForm" :model="rebootTimeClass" label-width="auto">
            <el-form-item label="重启时间1" prop="rebootTime1" class="w-3/6">
              <el-time-picker v-model="rebootTimeClass.rebootTime1" format="HH:mm" value-format="HHmm" @change="handleRebootTimeChange" />
            </el-form-item>
            <el-form-item label="重启时间2" prop="rebootTime2" class="w-3/6">
              <el-time-picker v-model="rebootTimeClass.rebootTime2" format="HH:mm" value-format="HHmm" @change="handleRebootTimeChange" />
            </el-form-item>
            <el-form-item label="重启时间3" prop="rebootTime3" class="w-3/6">
              <el-time-picker v-model="rebootTimeClass.rebootTime3" format="HH:mm" value-format="HHmm" @change="handleRebootTimeChange" />
            </el-form-item>
            <el-form-item label="重启时间4" prop="rebootTime4" class="w-3/6">
              <el-time-picker v-model="rebootTimeClass.rebootTime4" format="HH:mm" value-format="HHmm" @change="handleRebootTimeChange" />
            </el-form-item>
          </el-form>
          <div class="bg-gray-200">
            <div><el-text type="danger"> 请设置4个时间点，终端将在这些时间点各重启一次 </el-text></div>
            <div><el-text type="danger"> 如果时间点设为00:00 ，则在该时间点时不重启 </el-text></div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="formLoading" type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="dialog.visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { XfTermForm } from '@/api/consumption/xfTerm/types';
import { listUsePtCardType } from '@/api/basics/basicParameter/ptCardtype';
import * as XfTermApi from '@/api/consumption/xfTerm';
import { PtCardTypeVO } from '@/api/basics/basicParameter/ptCardtype/types';
import { getAllPtMealType } from '@/api/basics/basicParameter/ptMealtype';
import { t } from 'vxe-table';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

/** 数据字典 */
const { YYXT, XF_ConsumeType, XF_WorkMode, XF_TermType } = toRefs<any>(proxy?.useDict('YYXT', 'XF_ConsumeType', 'XF_WorkMode', 'XF_TermType'));
// 对话框
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 窗体是否可以拖动
const draggable = ref(true);
const formLoading = ref(false);
// 表单引用
const formRef = ref<ElFormInstance>();
const cardTypeOptions = ref<PtCardTypeVO[]>([]);
const cardTypeList = ref<number[]>([]);

type RebootTimeClass = {
  rebootTime1: string;
  rebootTime2: string;
  rebootTime3: string;
  rebootTime4: string;
};

const rebootTimeClass = reactive<RebootTimeClass>({
  rebootTime1: '0000',
  rebootTime2: '0000',
  rebootTime3: '0000',
  rebootTime4: '0000'
});

// 表单数据
const formData = ref<XfTermForm>({
  termId: undefined,
  termNo: undefined,
  termName: '',
  roomId: undefined,
  accountId: undefined,
  termType: '',
  qrCode: '',
  stationId: undefined,
  consumeType: '',
  autoDown: '',
  useType: '',
  cardType: undefined,
  termIp: '',
  termMac: '',
  commPort: undefined,
  mask: '',
  serverIp: '',
  serverPort: undefined,
  gatewayIp: '',
  beatInterval: undefined,
  timeout: undefined,
  offlineTime: undefined,
  advParam: '',
  posParam: '',
  rateParam: '',
  workMode: '',
  openMode: '0',
  maxCardMoney: undefined,
  constantValue: undefined,
  rationZero: undefined,
  rationOne: undefined,
  rationTwo: undefined,
  rationThree: undefined,
  rationFour: undefined,
  rationFive: undefined,
  rationSix: undefined,
  rationSeven: undefined,
  rationEight: undefined,
  rationNine: undefined,
  dayCount: undefined,
  dayMoney: undefined,
  mealCount: undefined,
  singleMoney: undefined,
  breakfastMoney: undefined,
  lunchMoney: undefined,
  supperMoney: undefined,
  nightMoney: undefined,
  breakfastBegin: undefined,
  breakfastEnd: undefined,
  lunchBegin: undefined,
  lunchEnd: undefined,
  supperBegin: undefined,
  supperEnd: undefined,
  nightBegin: undefined,
  nightEnd: undefined,
  swipeInterval: undefined,
  termValidity: '0',
  recordId: undefined,
  uploadTime: undefined,
  blackDownTime: undefined,
  lastCheck: undefined,
  rebootTime: '',
  remark: ''
});
// 表单校验规则
const formRules = reactive({
  termId: [{ required: true, message: '设备Id,主键不能为空', trigger: 'blur' }],
  termNo: [{ required: true, message: '设备机号不能为空', trigger: 'blur' }],
  termName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
  accountId: [{ required: true, message: '结算账户Id不能为空', trigger: 'blur' }],
  termType: [{ required: true, message: '设备类型,见XF_TermType字典类型不能为空', trigger: 'change' }],
  consumeType: [{ required: true, message: '扣费类型,见XF_ConsumeType字典类型不能为空', trigger: 'change' }],
  termIp: [{ required: true, message: '设备IP不能为空', trigger: 'blur' }],
  commPort: [{ required: true, message: '设备通讯端口不能为空', trigger: 'blur' }],
  serverIp: [{ required: true, message: '服务器IP不能为空', trigger: 'blur' }],
  serverPort: [{ required: true, message: '服务器端口不能为空', trigger: 'blur' }],
  workMode: [{ required: true, message: '消费工作模式,见XF_WorkMode字典类别不能为空', trigger: 'change' }],
  openMode: [{ required: true, message: '开机模式,0-不需要营业员开机,1-需要营业员开机不能为空', trigger: 'change' }],
  maxCardMoney: [{ required: true, message: '卡上最大金额不能为空', trigger: 'blur' }]
});

/** 提交按钮 */
const submitForm = async () => {
  // 校验表单
  if (!formRef.value) return;
  const valid = await formRef.value?.validate();
  if (!valid) return;
  // 提交请求
  formLoading.value = true;
  try {
    const data = formData.value as unknown as XfTermForm;
    await XfTermApi.updateXfTermParam(data);
    proxy?.$modal.msgSuccess('修改成功');
    dialog.visible = false;
    // 发送操作成功的事件
    // emit('success');
  } finally {
    formLoading.value = false;
  }
};
/** 表单重置 */
const resetForm = () => {
  formData.value = {
    termId: undefined,
    termNo: undefined,
    termName: '',
    roomId: undefined,
    accountId: undefined,
    termType: '',
    qrCode: '',
    stationId: undefined,
    consumeType: '',
    autoDown: '',
    useType: '',
    cardType: undefined,
    termIp: '',
    termMac: '',
    commPort: undefined,
    mask: '',
    serverIp: '',
    serverPort: undefined,
    gatewayIp: '',
    beatInterval: undefined,
    timeout: undefined,
    offlineTime: undefined,
    advParam: '',
    posParam: '',
    rateParam: '',
    workMode: '',
    openMode: '0',
    maxCardMoney: undefined,
    constantValue: undefined,
    rationZero: undefined,
    rationOne: undefined,
    rationTwo: undefined,
    rationThree: undefined,
    rationFour: undefined,
    rationFive: undefined,
    rationSix: undefined,
    rationSeven: undefined,
    rationEight: undefined,
    rationNine: undefined,
    dayCount: undefined,
    dayMoney: undefined,
    mealCount: undefined,
    singleMoney: undefined,
    breakfastMoney: undefined,
    lunchMoney: undefined,
    supperMoney: undefined,
    nightMoney: undefined,
    breakfastBegin: undefined,
    breakfastEnd: undefined,
    lunchBegin: undefined,
    lunchEnd: undefined,
    supperBegin: undefined,
    supperEnd: undefined,
    nightBegin: undefined,
    nightEnd: undefined,
    swipeInterval: undefined,
    termValidity: '0',
    recordId: undefined,
    uploadTime: undefined,
    blackDownTime: undefined,
    lastCheck: undefined,
    rebootTime: '',
    remark: ''
  };
  formRef.value?.resetFields();
};
const handleRebootTimeChange = (val: string) => {
  formData.value.rebootTime = rebootTimeClass.rebootTime1 + rebootTimeClass.rebootTime2 + rebootTimeClass.rebootTime3 + rebootTimeClass.rebootTime4;
  // console.log('rebootTimeChange', formData.value.rebootTime);
};

type EatTimePeriod = {
  breakfastBegin: string;
  breakfastEnd: string;
  lunchBegin: string;
  lunchEnd: string;
  supperBegin: string;
  supperEnd: string;
  nightBegin: string;
  nightEnd: string;
};

const eatTimePeriod = reactive<EatTimePeriod>({
  breakfastBegin: '0000',
  breakfastEnd: '0000',
  lunchBegin: '0000',
  lunchEnd: '0000',
  supperBegin: '0000',
  supperEnd: '0000',
  nightBegin: '0000',
  nightEnd: '0000'
});

const handleGetTimePeriod = async () => {
  const res = await getAllPtMealType();
  //早餐
  const zaoList = res.data.filter((item) => item.mealName == '早餐');
  if (zaoList.length > 0) {
    // console.log('zaoList', zaoList);
    const zao1 = zaoList[0].beginTime.substring(0, 2) + zaoList[0].beginTime.substring(3, 5);
    formData.value.breakfastBegin = parseInt(zao1);
    const zao2 = zaoList[0].endTime.substring(0, 2) + zaoList[0].endTime.substring(3, 5);
    formData.value.breakfastEnd = parseInt(zaoList[0].endTime.substring(0, 2) + zaoList[0].endTime.substring(3, 5));
    eatTimePeriod.breakfastBegin = zao1;
    eatTimePeriod.breakfastEnd = zao2;
  }
  //中餐
  const zhongList = res.data.filter((item) => item.mealName == '中餐');
  if (zhongList.length > 0) {
    // console.log('zhongList', zhongList);
    const zhong1 = zhongList[0].beginTime.substring(0, 2) + zhongList[0].beginTime.substring(3, 5);
    formData.value.lunchBegin = parseInt(zhong1);
    const zhong2 = zhongList[0].endTime.substring(0, 2) + zhongList[0].endTime.substring(3, 5);
    formData.value.lunchEnd = parseInt(zhong2);
    eatTimePeriod.lunchBegin = zhong1;
    eatTimePeriod.lunchEnd = zhong2;
  }
  //晚餐
  const wanList = res.data.filter((item) => item.mealName == '晚餐');
  if (wanList.length > 0) {
    // console.log('wanList', wanList);
    const wan1 = wanList[0].beginTime.substring(0, 2) + wanList[0].beginTime.substring(3, 5);
    formData.value.supperBegin = parseInt(wan1);
    const wan2 = wanList[0].endTime.substring(0, 2) + wanList[0].endTime.substring(3, 5);
    formData.value.supperEnd = parseInt(wan2);
    eatTimePeriod.supperBegin = wan1;
    eatTimePeriod.supperEnd = wan2;
  }
  //夜宵
  const yeList = res.data.filter((item) => item.mealName == '夜宵');
  if (yeList.length > 0) {
    // console.log('zaoList', yeList);
    const ye1 = yeList[0].beginTime.substring(0, 2) + yeList[0].beginTime.substring(3, 5);
    formData.value.nightBegin = parseInt(ye1);
    const ye2 = yeList[0].endTime.substring(0, 2) + yeList[0].endTime.substring(3, 5);
    formData.value.nightEnd = parseInt(ye2);
    eatTimePeriod.nightBegin = ye1;
    eatTimePeriod.nightEnd = ye2;
  }
};

function keyInput(e, type?: boolean) {
  let key = e.key;
  if (key === 'e' || key === '-') {
    e.returnValue = false;
    return false;
  }
  if (type) {
    if (key === '.') {
      e.returnValue = false;
      return false;
    }
  }
  return true;
}

function blurInput(col: string) {
  // console.log('keyInput1', formData.value[col]);
  if (formData.value[col]) {
    if (formData.value[col] != 0) {
      if (formData.value[col] != '') {
        try {
          formData.value[col] = parseFloat(formData.value[col]);
        } catch (error) {
          formData.value[col] = 0;
        }
      } else {
        formData.value[col] = 0;
      }
    } else {
      formData.value[col] = 0;
    }
  }
}

const handleEatTimeChange = (val: string) => {
  if (val == 'breakfastBegin') {
    if (eatTimePeriod.breakfastBegin != '') {
      formData.value.breakfastBegin = parseInt(eatTimePeriod.breakfastBegin);
    }
  } else if (val == 'breakfastEnd') {
    if (eatTimePeriod.breakfastEnd != '') {
      formData.value.breakfastEnd = parseInt(eatTimePeriod.breakfastEnd);
    }
  } else if (val == 'lunchBegin') {
    if (eatTimePeriod.lunchBegin != '') {
      formData.value.lunchBegin = parseInt(eatTimePeriod.lunchBegin);
    }
  } else if (val == 'lunchEnd') {
    if (eatTimePeriod.lunchEnd != '') {
      formData.value.lunchEnd = parseInt(eatTimePeriod.lunchEnd);
    }
  } else if (val == 'supperBegin') {
    if (eatTimePeriod.supperBegin != '') {
      formData.value.supperBegin = parseInt(eatTimePeriod.supperBegin);
    }
  } else if (val == 'supperEnd') {
    if (eatTimePeriod.supperEnd != '') {
      formData.value.supperEnd = parseInt(eatTimePeriod.supperEnd);
    }
  } else if (val == 'nightBegin') {
    if (eatTimePeriod.nightBegin != '') {
      formData.value.nightBegin = parseInt(eatTimePeriod.nightBegin);
    }
  } else if (val == 'nightEnd') {
    if (eatTimePeriod.nightEnd != '') {
      formData.value.nightEnd = parseInt(eatTimePeriod.nightEnd);
    }
  }
};
/** 窗体打开事件 */
const open = async (type: string, id: number) => {
  dialog.visible = true;
  dialog.title = type;
  resetForm();
  // 修改时，设置数据
  if (!id) {
    proxy?.$modal.msgError('缺少设备id');
    return;
  }
  formLoading.value = true;
  try {
    const res = await XfTermApi.getXfTermParam(id);
    Object.assign(formData.value, res.data);
    initNums();
    await getCardType();
    // 查询卡类 初始卡类
    initCardTypeList();
    // 初始化重启时间
    initRebootTime();
    //初始化餐类时间
    initEatTime();
  } finally {
    formLoading.value = false;
  }
};

const initNums = () => {
  const cols = ['dayMoney', 'nightMoney', 'lunchMoney', 'supperMoney', 'breakfastMoney', 'maxCardMoney', 'dayMoney', 'singleMoney', 'constantValue'];
  cols.forEach((col) => {
    initNum(col);
  });
};

const initNum = (col: string) => {
  const regx = /\.0*$/;
  if (formData.value[col]) {
    let num = formData.value[col].toString();
    if (num.match(regx)) {
      formData.value[col] = parseFloat(num);
    }
  }
};

const initEatTime = () => {
  eatTimePeriod.breakfastBegin = '0000';
  eatTimePeriod.breakfastEnd = '0000';
  eatTimePeriod.lunchBegin = '0000';
  eatTimePeriod.lunchEnd = '0000';
  eatTimePeriod.supperBegin = '0000';
  eatTimePeriod.supperEnd = '0000';
  eatTimePeriod.nightBegin = '0000';
  eatTimePeriod.nightEnd = '0000';

  if (formData.value.breakfastBegin) {
    eatTimePeriod.breakfastBegin = formData.value.breakfastBegin.toString().padStart(4, '0');
  }
  if (formData.value.breakfastEnd) {
    eatTimePeriod.breakfastEnd = formData.value.breakfastEnd.toString().padStart(4, '0');
  }
  if (formData.value.lunchBegin) {
    eatTimePeriod.lunchBegin = formData.value.lunchBegin.toString().padStart(4, '0');
  }
  if (formData.value.lunchEnd) {
    eatTimePeriod.lunchEnd = formData.value.lunchEnd.toString().padStart(4, '0');
  }
  if (formData.value.supperBegin) {
    eatTimePeriod.supperBegin = formData.value.supperBegin.toString().padStart(4, '0');
  }
  if (formData.value.supperEnd) {
    eatTimePeriod.supperEnd = formData.value.supperEnd.toString().padStart(4, '0');
  }
  if (formData.value.nightBegin) {
    eatTimePeriod.nightBegin = formData.value.nightBegin.toString().padStart(4, '0');
  }
  if (formData.value.nightEnd) {
    eatTimePeriod.nightEnd = formData.value.nightEnd.toString().padStart(4, '0');
  }
};

const initCardTypeList = () => {
  cardTypeList.value = [];
  if (formData.value.cardType && formData.value.cardType != 0) {
    // 十进制 转 二进制
    let num: string = formData.value.cardType.toString(2);
    num = num.padStart(cardTypeOptions.value.length, '0');
    for (let i = 0; i < num.length; i++) {
      if (num.charAt(i) == '1') {
        const e: PtCardTypeVO = cardTypeOptions.value[num.length - i - 1];
        if (e) {
          cardTypeList.value.push(e.typeId as number);
        }
      }
    }
  }
};

const initRebootTime = () => {
  rebootTimeClass.rebootTime1 = '0000';
  rebootTimeClass.rebootTime2 = '0000';
  rebootTimeClass.rebootTime3 = '0000';
  rebootTimeClass.rebootTime4 = '0000';
  if (formData.value.rebootTime && formData.value.rebootTime.length == 16) {
    rebootTimeClass.rebootTime1 = formData.value.rebootTime.substring(0, 4);
    rebootTimeClass.rebootTime2 = formData.value.rebootTime.substring(4, 8);
    rebootTimeClass.rebootTime3 = formData.value.rebootTime.substring(8, 12);
    rebootTimeClass.rebootTime4 = formData.value.rebootTime.substring(12, 16);
  }
};

//查询卡类
const getCardType = async () => {
  const res = await listUsePtCardType();
  cardTypeOptions.value = res.data;
};

const handleCardTypeChange = (val: number[]) => {
  // console.log('val', val);
  // 拼接二进制后转成10进制
  let str = '';
  cardTypeOptions.value.forEach((item) => {
    if (val.includes(item.typeId as number)) {
      str = '1' + str;
    } else {
      str = '0' + str;
    }
  });

  //二进制转十进制
  formData.value.cardType = parseInt(str, 2);
};

// 提供 open 方法，用于打开弹窗
defineExpose({ open });
</script>
<style lang="scss" scoped>
.el-row {
  margin-bottom: 8px;
}
::deep .p-class ::deep div.el-card__body {
  padding: 10px !important;
}
</style>
