<template>
  <my-dialog
    :visible="visible"
    :header="title"
    :width="600"
    attach="body"
    :closeOnOverlayClick="false"
    @close="emit('update:visible', false)"
  >
    <t-form ref="formRef" :data="formData" :rules="rules" labelAlign="top">
      <t-row :gutter="[20, 20]">
        <t-col :span="6">
          <t-form-item label="供应商编号" name="code">
            <t-input v-model="formData.code" clearable />
          </t-form-item>
        </t-col>
        <t-col :span="6">
          <t-form-item label="供应商名称" name="name">
            <t-input v-model="formData.name" clearable />
          </t-form-item>
        </t-col>
        <t-col :span="6">
          <t-form-item label="类型" name="type">
            <t-select v-model="formData.type">
              <t-option
                v-for="(val, key) in SUPPLIER_TYPE"
                :key="key"
                :label="val"
                :value="key"
              />
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="6">
          <t-form-item label="启用/禁用" name="enable">
            <select-enable v-model="formData.enable"></select-enable>
          </t-form-item>
        </t-col>
        <t-col :span="12">
          <t-form-item label="备注">
            <t-textarea
              v-model="formData.remark"
              :autosize="{ minRows: 3, maxRows: 5 }"
              :maxlength="50"
            />
          </t-form-item>
        </t-col>
      </t-row>
    </t-form>
    <template #foot>
      <t-button theme="default" @click="emit('update:visible', false)"
        >取消</t-button
      >
      <t-button theme="primary" @click="save">保存</t-button>
    </template>
  </my-dialog>
</template>
<script setup name="EditSupplierDialog">
import { ref, computed } from 'vue';
import { MessagePlugin } from 'tdesign-vue-next';
import useClearDialog from '@/hooks/useClearDialog';
import { SUPPLIER_TYPE } from '@/config/constants';
import { supplierEditApi } from '@/api/system';
const emit = defineEmits(['update:visible', 'success']);
const formRef = ref(null);

const props = defineProps({
  visible: Boolean,
  curRow: Object,
});

const title = computed(() => {
  return (isEdit.value ? '编辑' : '新增') + '供应商';
});

const { formData, isEdit } = useClearDialog(
  {
    id: null,
    code: '',
    name: '',
    type: '',
    remark: '',
    sync: true,
    enable: true,
  },
  props,
  formRef,
  () => {
    for (let key in formData) {
      formData[key] = props.curRow[key];
    }
  }
);
const rules = {
  name: [
    { required: true, message: '名称必填', type: 'error', trigger: 'change' },
    {
      max: 100,
      message: '至多需要100个字',
      type: 'error',
      trigger: 'change',
    },
  ],
  type: [
    { required: true, message: '类型必选', type: 'error', trigger: 'change' },
  ],
  code: [
    { required: true, message: '编码必填', type: 'error', trigger: 'change' },
    {
      max: 50,
      message: '至多需要50个字',
      type: 'error',
      trigger: 'change',
    },
  ],
};

const save = async () => {
  const valid = await formRef.value.validate();
  if (valid !== true) return;

  const res = await supplierEditApi(formData).catch(() => {});
  if (!res) return;

  MessagePlugin.success('保存成功');
  emit('update:visible', false);
  emit('success');
};
</script>
