<!--
  * 入库明细
  *
  * @Author:    霍锦
  * @Date:      2025-03-26 15:16:28
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.asnDetailId ? '编辑明细' : '添加明细'"
      :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="物料" name="itemId">
        <ItemSelect style="width: 100%" v-model:value="form.itemId" :disabledFlag="true" @change="changeItemSelect"/>
      </a-form-item>

      <a-form-item label="数量" name="orderQuantity">
        <a-input-number style="width: 100%" v-model:value="form.orderQuantity" :min="1" :max="9999"/>
      </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 {asnDetailApi} from '/@/api/business/receive/asnDetail/asn-detail-api';
import {smartSentry} from '/@/lib/smart-sentry';
import ItemSelect from "/@/views/business/base/item/item-select.vue";

// ------------------------ 事件 ------------------------
const emits = defineEmits(['reloadList']);

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

function show(rowData: object, asnId: number) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  //将入库单id赋值
  form.asnId = asnId;
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

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

//选择物料
function changeItemSelect(selectValue: any) {
  if (selectValue) {
    form.itemId = selectValue.itemId;
  }
}

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

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

const formDefault = {
  asnDetailId: undefined, //入库明细id
  asnId: undefined,//入库单id
  itemId: undefined, //物料id
  orderQuantity: 1, //订单数量
};

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

const rules = {
  itemId: [{required: true, message: '物料id 必填'}],
  orderQuantity: [{required: true, message: '订单数量 必填'}],
};

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

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

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