<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { ElMessage } from "element-plus";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { updateSmartCabinet } from "@/api/cabinet/smart-cabinet";
import Confirm from "@iconify-icons/ep/check";
import type { FormRules } from 'element-plus';

export interface FormDTO {
  cabinetId: number;
  cabinetName: string;
  cabinetType: number;
  templateNo: string;
  lockControlNo: number;
  location: number;
}

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  row: {
    type: Object,
    default: null
  }
});

const emit = defineEmits(["update:visible", "refresh"]);

const formRef = ref();
const formData = reactive<FormDTO>({
  cabinetId: 0,
  cabinetName: "",
  cabinetType: 1,
  templateNo: "",
  lockControlNo: 0,
  location: 0
});

const rules = reactive<FormRules>({
  cabinetName: [{ required: true, message: "柜体名称必填", trigger: "blur" }],
  cabinetType: [{ required: true, message: "请选择柜体类型", trigger: "change" }],
  templateNo: [{ required: true, message: "请选择模板编号", trigger: "change" }],
  lockControlNo: [
    { required: true, message: "锁控板序号", trigger: "blur" }
  ],
  location: [
    { required: true, message: "位置信息必填", trigger: "blur" },
    { type: 'number', min: 0, message: '位置编号不能为负数' }
  ]
});

const handleConfirm = async () => {
  try {
    await formRef.value.validate();
    await updateSmartCabinet(formData.cabinetId!, formData);
    ElMessage.success("修改成功");
    emit("refresh");
    closeDialog();
  } catch (error) {
    console.error("表单提交失败", error);
  }
};

const closeDialog = () => {
  formRef.value.resetFields();
  emit("update:visible", false);
};

// 初始化表单数据
watch(() => props.row, (val) => {
  if (val) {
    Object.assign(formData, val);
  }
}, { immediate: true });
</script>

<template>
  <el-dialog title="编辑智能柜" :model-value="visible" width="600px" @close="closeDialog">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="柜体名称" prop="cabinetName">
        <el-input v-model="formData.cabinetName" placeholder="请输入柜体名称" />
      </el-form-item>

      <el-form-item label="柜体类型" prop="cabinetType">
        <el-select v-model="formData.cabinetType" placeholder="请选择类型">
          <el-option label="主柜" :value="0" />
          <el-option label="副柜" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="模板编号" prop="templateNo">
        <el-select v-model="formData.templateNo" placeholder="请选择模板编号">
          <el-option label="模板一" :value="1" />
          <el-option label="模板二" :value="2" />
          <el-option label="模板三" :value="3" />
          <el-option label="模板四" :value="4" />
        </el-select>
      </el-form-item>

      <el-form-item label="锁控板序号" prop="lockControlNo">
        <el-input v-model="formData.lockControlNo" placeholder="请输入锁控板序号" />
      </el-form-item>

      <el-form-item label="位置" prop="location">
        <el-input-number v-model="formData.location" :min="0" controls-position="right" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" :icon="useRenderIcon(Confirm)" @click="handleConfirm">
        确认
      </el-button>
    </template>
  </el-dialog>
</template>