<!--
  * 物料信息
  *
  * @Author:    霍锦
  * @Date:      2024-11-25 17:08:18
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.itemId ? '编辑' : '添加'"
      :width="600"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-form-item label="物料id" name="itemId">
        <a-input-number style="width: 100%" v-model:value="form.itemId" placeholder="物料id"/>
      </a-form-item>
      <a-form-item label="物料编码" name="itemCode">
        <a-input style="width: 100%" v-model:value="form.itemCode" placeholder="物料编码"/>
      </a-form-item>
      <a-form-item label="物料名称" name="itemName">
        <a-input style="width: 100%" v-model:value="form.itemName" placeholder="物料名称"/>
      </a-form-item>
      <a-form-item label="是否禁用" name="disabledFlag">
        <BooleanSelect v-model:value="form.disabledFlag" style="width: 100%"/>
      </a-form-item>
      <a-form-item label="物料类型" name="itemType">
        <SmartEnumSelect width="100%" v-model:value="form.itemType" enumName="" placeholder="物料类型"/>
      </a-form-item>
      <a-form-item label="创建人ID" name="createUserId">
        <a-input-number style="width: 100%" v-model:value="form.createUserId" placeholder="创建人ID"/>
      </a-form-item>
      <a-form-item label="创建人" name="createUserName">
        <a-input style="width: 100%" v-model:value="form.createUserName" placeholder="创建人"/>
      </a-form-item>
      <a-form-item label="创建时间" name="createTime">
        <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.createTime" style="width: 100%"
                       placeholder="创建时间"/>
      </a-form-item>
      <a-form-item label="更新时间" name="updateTime">
        <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.updateTime" style="width: 100%"
                       placeholder="更新时间"/>
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup lang="ts">
import {reactive, ref, nextTick} from 'vue';
import _ from 'lodash';
import {message} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import {itemApi} from '/@/api/business/base/item/item-api.js';
import {smartSentry} from '/@/lib/smart-sentry';
import BooleanSelect from '/@/components/framework/boolean-select/index.vue';
import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData: object) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  itemId: undefined, //物料id
  itemCode: undefined, //物料编码
  itemName: undefined, //物料名称
  disabledFlag: undefined, //是否禁用
  itemType: undefined, //物料类型
  createUserId: undefined, //创建人ID
  createUserName: undefined, //创建人
  createTime: undefined, //创建时间
  updateTime: undefined, //更新时间
};

let form = reactive({...formDefault});

const rules = {
  itemId: [{required: true, message: '物料id 必填'}],
  itemCode: [{required: true, message: '物料编码 必填'}],
  itemName: [{required: true, message: '物料名称 必填'}],
  disabledFlag: [{required: true, message: '是否禁用 必填'}],
  itemType: [{required: true, message: '物料类型 必填'}],
  createUserId: [{required: true, message: '创建人ID 必填'}],
  createUserName: [{required: true, message: '创建人 必填'}],
  createTime: [{required: true, message: '创建时间 必填'}],
  updateTime: [{required: true, message: '更新时间 必填'}],
};

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    if (form.itemId) {
      await itemApi.update(form);
    } else {
      await itemApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});
</script>
