<!--
  * 物料信息
  *
  * @Author:    霍锦
  * @Date:      2024-11-25 17:08:18
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.itemId ? '编辑物料' : '添加物料'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules">

      <a-row>
        <a-col :span="24">
          <a-form-item label="物料名称" name="itemName">
            <a-input style="width: 100%" v-model:value="form.itemName" placeholder="物料名称"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="12">
          <a-form-item label="物料编码" name="itemCode">
            <a-input style="width: 100%" v-model:value="form.itemCode" placeholder="物料编码"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="包装系数" name="packFactor">
            <a-input-number style="width: 100%" v-model:value="form.packFactor" min="1" max="99999"
                            placeholder="包装系数"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="12">
          <a-form-item label="包装单位" name="unit">
            <DictSelect width="100%" key-code="ITEM_UNIT" v-model:value="form.unit"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="物料类型" name="itemType">
            <DictSelect width="100%" key-code="	ITEM_TYPE" v-model:value="form.itemType"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="24">
          <a-form-item label="是否启用" name="disabledFlag">
            <a-switch
                style="width: 80px"
                v-model:checked="form.disabledFlag"
                checked-children="启用"
                un-checked-children="禁用"
            />
          </a-form-item>
        </a-col>
      </a-row>


    </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';
import DictSelect from "/@/components/support/dict-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: true, //是否禁用
  itemType: undefined, //物料类型
  unit: undefined, //包装单位
  packFactor: 1,//包装系数
};

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

const rules = {
  itemId: [{required: true, message: '物料id 必填'}],
  itemCode: [{required: true, message: '物料编码 必填'}],
  itemName: [{required: true, message: '物料名称 必填'}],
  itemType: [{required: true, message: '物料类型 必填'}],
  unit: [{required: true, message: '包装单位 必填'}],
  packFactor: [{required: true, message: '包装系数 必填'}],
  disabledFlag: [{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>
