<!--
  * 客户管理
  *
  * @Author:    霍锦
  * @Date:      2025-03-25 10:42:33
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="form.customerId ? '编辑客户' : '添加客户'"
      :width="600"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules">
      <a-row>
        <a-col :span="12">
          <a-form-item label="代码" name="customerCode">
            <a-input style="width:  100%" v-model:value="form.customerCode" placeholder="客户代码" disabled/>
          </a-form-item>

        </a-col>
        <a-col :span="12">
          <a-form-item label="名称" name="customerName">
            <a-input style="width: 100%" v-model:value="form.customerName" placeholder="客户名称"
                     @input="generateCustomerCode"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item label="联系人" name="person">
            <a-input style="width: 100%" v-model:value="form.person" placeholder="联系人"/>
          </a-form-item>

        </a-col>
        <a-col :span="12">
          <a-form-item label="电话" name="telephone">
            <a-input style="width: 100%" v-model:value="form.telephone" placeholder="电话"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="24">
          <a-form-item label="地址" name="address">
            <a-textarea
                v-model:value="form.address"
                style="width: 100%; height: 100px"
                placeholder="地址"
            />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="24">
          <a-form-item label="是否启用" name="disabledFlag">
            <a-switch
                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 pinyin from 'pinyin';
import {message} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import {customerApi} from '/@/api/business/base/customer/customer-api';
import {smartSentry} from '/@/lib/smart-sentry';

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

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 = {
  customerId: undefined, //客户id
  customerCode: undefined, //客户代码
  customerName: undefined, //客户名称
  person: undefined, //联系人
  telephone: undefined, //电话
  address: undefined, //地址
  disabledFlag: true //是否禁用
};

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

const rules = {
  customerId: [{required: true, message: '客户id 必填'}],
  customerCode: [{required: true, message: '客户代码 必填'}],
  customerName: [{required: true, message: '客户名称 必填'}],
  createTime: [{required: true, message: '创建时间 必填'}],
  createUserId: [{required: true, message: '创建人ID 必填'}],
  createUserName: [{required: true, message: '创建人 必填'}],
  updateTime: [{required: true, message: '更新时间 必填'}],
};

// 自动生成客户代码
function generateCustomerCode() {
  form.customerCode = form.customerName
      ? pinyin(form.customerName, {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.customerId) {
      await customerApi.update(form);
    } else {
      await customerApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

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