<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="750"
    :visible="visible"
    :confirm-loading="loading"
    :maxable="maxAble"
    :title="isUpdate ? '编辑临时报餐人员' : '添加临时报餐人员'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    :maskClosable="false"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 8 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 24 }, sm: { span: 24 } }"
      layout="vertical"
    >
      <a-form-item label="姓名" name="userId">
        <a-input
          allow-clear
          placeholder="请输入姓名ID"
          :disabled="isUpdate"
          v-model:value="form.userId"
        />
      </a-form-item>
      <a-form-item label="代报餐人员姓名ID" name="parentId">
        <a-input
          allow-clear
          placeholder="请输入代报餐人员姓名ID"
          :disabled="isUpdate"
          v-model:value="form.parentId"
        />
      </a-form-item>
      <a-form-item label="排序号" name="sortNumber">
        <a-input-number
          :min="0"
          :max="99999"
          class="ele-fluid"
          placeholder="请输入排序号（数字越小越靠前）"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

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

  // 是否是修改
  const isUpdate = ref(false);
  // 是否显示最大化切换按钮
  const maxAble = ref(true);
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: BCAgent | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  const formRef = ref<FormInstance | null>(null);

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

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<BCAgent>({
    userId: undefined,
    parentId: undefined,
    sortNumber: 100
  });

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    userId: [
      {
        required: true,
        message: '请输入姓名ID',
        type: 'string',
        trigger: 'blur'
      }
    ],
    parentId: [
      {
        required: true,
        type: 'string',
        message: '请输入代报餐人员姓名ID',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const merchantForm = {
          ...form
        };
        const saveOrUpdate = isUpdate.value
          ? updateBCTemporary
          : addBCTemporary;
        saveOrUpdate(merchantForm)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            resetFields();
            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;
  }
  .flex-sb {
    display: flex;
    justify-content: space-between;
  }
</style>
