<!-- 用户编辑弹窗 -->
<template>
  <a-drawer
    :width="880"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '编辑设备' : '添加设备'"
    :body-style="{ paddingBottom: '8px' }"
    :footer-style="{ textAlign: 'right' }"
    @update:visible="updateVisible"
    :maskClosable="isUpdate"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :label-col="{ md: { span: 8 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 24 }, sm: { span: 24 } }"
      layout="vertical"
      :model="form"
      :rules="rules"
    >
      <a-form-item label="设备编码" name="equipmentCode">
        <a-input
          allow-clear
          placeholder="请输入设备编码"
          :disabled="isUpdate"
          v-model:value="form.equipmentCode"
        />
      </a-form-item>
      <a-form-item label="所属档口" name="gear">
        <a-radio-group v-model:value="form.gear">
          <a-radio :value="10">食堂档口</a-radio>
          <a-radio :value="20">物品档口</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="设备描述" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入描述内容"
          v-model:value="form.comments"
        />
      </a-form-item>
      <!--      <a-form-item label="启用状态" name="status">-->
      <!--        <a-switch :checked="form.status === 0" @change="editStatus" />-->
      <!--      </a-form-item>-->
    </a-form>
    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="save">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import type { BcEquipment } from '@/api/apps/bc/equipment/model';
  import { addBcEquipment, updateBcEquipment } from '@/api/apps/bc/equipment';
  import { FormInstance, Rule } from 'ant-design-vue/es/form';
  import useFormData from '@/utils/use-form-data';

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: BcEquipment | null;
  }>();
  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 是否是修改
  const isUpdate = ref(false);

  // 提交状态
  const loading = ref(false);

  const formRef = ref<FormInstance | null>(null);

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<BcEquipment>({
    bcEquipmentId: undefined,
    equipmentName: undefined,
    equipmentCode: '',
    gear: 10,
    createTime: '',
    comments: '',
    sortNumber: 100,
    status: 0
  });

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    equipmentName: [
      {
        required: true,
        message: '请输入设备名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    equipmentCode: [
      {
        required: true,
        type: 'string',
        message: '请填写设备编号',
        trigger: 'blur'
      }
    ],
    status: [
      {
        required: true,
        type: 'number',
        message: '请选择设备状态',
        trigger: 'blur'
      }
    ],
    sortNumber: [
      {
        required: true,
        type: 'number',
        message: '请输入排序号',
        trigger: 'blur'
      }
    ]
  });

  /* 控制放店开关 */
  const editStatus = () => {
    if (form.status == 0) {
      form.status = 1;
    } else {
      form.status = 0;
    }
    updateBcEquipment(form)
      .then(() => {
        message.success('操作成功');
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  const onClose = () => {
    updateVisible(false);
  };

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const equipmentForm = {
          ...form
        };
        console.log(equipmentForm, 'equipmentForm');
        const saveOrUpdate = isUpdate.value
          ? updateBcEquipment
          : addBcEquipment;
        saveOrUpdate(equipmentForm)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          assignFields({
            ...props.data
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate();
      }
    }
  );
</script>
<style lang="less">
  .tab-pane {
    min-height: 300px;
  }
  .ml-10 {
    margin-left: 5px;
  }
</style>
