<!--
  * 库区信息
  *
  * @Author:    hj
  * @Date:      2025-03-11 11:12:36
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.areaId ? '编辑库区' : '添加库区'"
      :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="areaCode">
        <a-input style="width: 100%" v-model:value="form.areaCode" placeholder="库区编码" disabled/>
      </a-form-item>
      <a-form-item label="库区名称" name="areaName">
        <a-input style="width: 100%" v-model:value="form.areaName" placeholder="库区名称" @input="generateAreaCode"/>
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <textarea v-model="form.remark" style="width: 100%; height: 100px; outline: none"></textarea>
      </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 {areaApi} from '/@/api/business/base/area/area-api';
import {smartSentry} from '/@/lib/smart-sentry';
import pinyin from 'pinyin';

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

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

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

function show(rowData: Object) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
  // if (form.status && form.status.length > 0) {
  //   form.status = form.status.map((e) => e.valueCode);
  // }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

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

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

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

const formDefault = {
  areaId: undefined, //库区id
  areaCode: undefined, //库区编码
  areaName: undefined, //库区名称
  disabledFlag: true, //是否禁用
  remark: undefined,//备注
};

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

const rules = {
  areaCode: [{required: true, message: '库区编码 必填'}],
  areaName: [{required: true, message: '库区名称 必填'}],
  disabledFlag: [{required: true, message: '是否禁用 必填'}],
};

// 生成库区编码
function generateAreaCode() {
  form.areaCode = form.areaName
      ? pinyin(form.areaName, {style: pinyin.STYLE_FIRST_LETTER})
          .map((item: string []) => item[0].toUpperCase())
          .join('')
      : undefined;
}

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

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

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