<!--
  * 库位信息
  *
  * @Author:    霍锦
  * @Date:      2024-11-18 14:17:31
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.locationId ? '编辑' : '添加'"
      :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="areaId">
        <AreaSelect style="width: 100%" v-model:value="form.areaId" @change="changeAreaSelect"/>
      </a-form-item>
      <a-form-item label="库位" name="locationCode">
        <a-input style="width: 100%" v-model:value="form.locationCode" placeholder="库位编码"/>
      </a-form-item>
      <a-form-item label="类型" name="locationType">
        <DictSelect width="100%" key-code="LOC_TYPE" v-model:value="form.locationType" />
      </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="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 {locationApi} from '/@/api/business/base/location/location-api';
import {smartSentry} from '/@/lib/smart-sentry';
import {USAGE_STATUS_ENUM} from '/@/constants/business/base/usagestatus-const';
import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
import AreaSelect from "/@/views/business/base/area/area-select.vue";
import DictSelect from "/@/components/support/dict-select/index.vue";

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

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

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

function show(rowData: any) {
  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 = {
  locationId: undefined,//库位ID
  locationCode: undefined, //库位编码
  locationName: undefined, //库位名称
  status: USAGE_STATUS_ENUM.FREE.value, //状态
  areaId: undefined, //库区ID
  disabledFlag: true, //是否禁用
  locationType: [] //库位类型
};

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

const rules = {
  locationCode: [{required: true, message: '库位编码 必填'}],
  status: [{required: true, message: '状态 必填'}],
  areaId: [{required: true, message: '库区 必填'}],
  disabledFlag: [{required: true, message: '是否禁用 必填'}],
  locationType: [{required: true, message: '库位类型不能为空'}],
};


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

}

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

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    //库位名称和编码一致
    form.locationName = form.locationCode;
    if (form.locationId) {
      await locationApi.update(form);
    } else {
      await locationApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

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