<!--
  * 容器信息
  *
  * @Author:    霍锦
  * @Date:      2024-11-26 11:37:48
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.stockId ? '编辑容器' : '添加容器'"
      :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="stockCode">
        <a-input style="width: 100%" v-model:value="form.stockCode" placeholder="容器编码"/>
      </a-form-item>

      <a-form-item label="类型" name="stockType">
        <DictSelect width="100%" key-code="STOCK_TYPE" v-model:value="form.stockType"/>
      </a-form-item>
      <a-form-item label="状态" name="status">
        <SmartEnumSelect style="width: 100%" enum-name="USAGE_STATUS_ENUM" v-model:value="form.status"/>
      </a-form-item>

      <a-form-item label="库位" name="locationId" v-show="form.stockId" >
        <LocationSelect style="width: 100%" v-model:value="form.locationId" :disabled-flag="true" @change="changeLocationSelect"/>
      </a-form-item>

      <a-form-item label="是否启用" name="disabledFlag">
        <a-switch
            v-model:checked="form.disabledFlag"
            checked-children="启用"
            un-checked-children="禁用"
        />
      </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 {stockApi} from '/@/api/business/base/stock/stock-api';
import {smartSentry} from '/@/lib/smart-sentry';
import DictSelect from '/@/components/support/dict-select/index.vue';
import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
import {USAGE_STATUS_ENUM} from "/@/constants/business/base/usagestatus-const";
import LocationSelect from "/@/views/business/base/location/location-select.vue";

//选择库区
function changeLocationSelect(selectValue: any) {
  if(selectValue){
    form.locationId = selectValue.locationId;
  }

}

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

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 = {
  stockId: undefined, //容器id
  stockCode: undefined, //容器编码
  status: USAGE_STATUS_ENUM.FREE.value, //状态
  disabledFlag: true, //是否禁用
  stockType: [], //容器类型
  locationId: undefined,//库位
};

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

const rules = {
  stockCode: [{required: true, message: '容器编码 必填'}],
  status: [{required: true, message: '状态 必填'}],
  disabledFlag: [{required: true, message: '启用状态 必填'}],
  stockType: [{required: true, message: '容器类型 必填'}],
};

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

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

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