import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { z } from '#/adapter/form';

import {
  SUPPLIER_STATUS_OPTIONS,
  SUPPLIER_STATUS_CELLTAG,
  SUPPLIER_TYPE_OPTIONS,
  SUPPLIER_TYPE_CELLTAG,
} from '../enums';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '供应商编码',
      component: 'Input',
      rules: 'required',
      componentProps: { placeholder: '请输入供应商编码' },
    },
    {
      fieldName: 'name',
      label: '供应商名称',
      component: 'Input',
      rules: 'required',
      componentProps: { placeholder: '请输入供应商名称' },
    },
    {
      fieldName: 'type',
      label: '供应商类型',
      component: 'Select',
      rules: 'required',
      componentProps: {
        placeholder: '请选择供应商类型',
        options: SUPPLIER_TYPE_OPTIONS,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: SUPPLIER_STATUS_OPTIONS,
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(1),
    },
    {
      fieldName: 'contactName',
      label: '联系人',
      component: 'Input',
      componentProps: { placeholder: '请输入联系人' },
    },
    {
      fieldName: 'contactMobile',
      label: '联系电话',
      component: 'Input',
      componentProps: { placeholder: '请输入联系电话' },
    },
    {
      fieldName: 'contactEmail',
      label: '邮箱',
      component: 'Input',
      componentProps: { placeholder: '请输入邮箱' },
    },
    {
      fieldName: 'address',
      label: '地址',
      component: 'Input',
      componentProps: { placeholder: '请输入地址' },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'bankName',
      label: '开户银行',
      component: 'Input',
      componentProps: { placeholder: '请输入开户银行' },
    },
    {
      fieldName: 'bankAccount',
      label: '银行账号',
      component: 'Input',
      componentProps: { placeholder: '请输入银行账号' },
    },
    {
      fieldName: 'taxNo',
      label: '税号',
      component: 'Input',
      componentProps: { placeholder: '请输入税号' },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: { placeholder: '请输入备注', rows: 3 },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '编码',
      component: 'Input',
      componentProps: { placeholder: '请输入编码', allowClear: true },
    },
    {
      fieldName: 'name',
      label: '供应商名称',
      component: 'Input',
      componentProps: { placeholder: '请输入供应商名称', allowClear: true },
    },
    {
      fieldName: 'type',
      label: '类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择类型',
        allowClear: true,
        options: SUPPLIER_TYPE_OPTIONS,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        options: SUPPLIER_STATUS_OPTIONS,
      },
    },
  ];
}

/** 表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'code', title: '编码', minWidth: 120 },
    { field: 'name', title: '供应商名称', minWidth: 150 },
    {
      field: 'type',
      title: '类型',
      minWidth: 100,
      cellRender: { name: 'CellTag', props: SUPPLIER_TYPE_CELLTAG },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      cellRender: { name: 'CellTag', props: SUPPLIER_STATUS_CELLTAG },
    },
    { field: 'contactName', title: '联系人', minWidth: 100 },
    { field: 'contactMobile', title: '联系电话', minWidth: 120 },
    { field: 'contactEmail', title: '邮箱', minWidth: 150 },
    { field: 'createTime', title: '创建时间', minWidth: 160 },
    {
      title: '操作',
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
