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

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { getSupplierSimpleList } from '#/api/erp/purchase/supplier';
import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';

/** 表单类型 */
export type FormType = 'create' | 'detail' | 'edit';

/** 状态常量 */
export const PURCHASE_REQUEST_STATUS = {
  DRAFT: 0, // 未提交（草稿）
  PROCESSING: 10, // 审批中
  APPROVED: 20, // 审核通过
  REJECTED: 30, // 审核不通过
  CANCELLED: 40, // 已取消
};

/** 表单的配置项 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'no',
      label: '申请编号',
      component: 'Input',
      componentProps: {
        placeholder: '系统自动生成',
        disabled: true,
      },
    },
    {
      fieldName: 'requestTime',
      label: '申请时间',
      component: 'DatePicker',
      componentProps: {
        placeholder: '选择申请时间',
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
        disabled: formType === 'detail',
      },
      rules: 'required',
    },
    {
      label: '供应商',
      fieldName: 'supplierId',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
        disabled: formType === 'detail',
      },
      rules: 'required',
    },
    {
      fieldName: 'discountPercent',
      label: '优惠率(%)',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入优惠率',
        min: 0,
        max: 100,
        precision: 2,
        disabled: formType === 'detail',
      },
    },
    {
      fieldName: 'depositPrice',
      label: '定金',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入定金',
        min: 0,
        precision: 2,
        disabled: formType === 'detail',
      },
    },
    {
      fieldName: 'requestReason',
      label: '申请理由',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入申请理由',
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'fileUrl',
      label: '附件',
      component: 'FileUpload',
      componentProps: {
        placeholder: '请上传附件',
        maxNumber: 5,
        maxSize: 20,
        accept: ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'jpg', 'png'],
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'items',
      label: '申请明细',
      component: 'Input',
      formItemClass: 'col-span-3',
    },
  ];
}

/** 表单的明细表格列 */
export function useFormItemColumns(
  disabled: boolean,
): VxeTableGridOptions['columns'] {
  return [
    { type: 'seq', title: '序号', minWidth: 50, fixed: 'left' },
    {
      field: 'productId',
      title: '产品名称',
      minWidth: 200,
      slots: { default: 'productId' },
    },
    {
      field: 'productUnitName',
      title: '单位',
      minWidth: 80,
    },
    {
      field: 'count',
      title: '数量',
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'count' },
    },
    {
      field: 'inCount',
      title: '入库数量',
      minWidth: 100,
      fixed: 'right',
      formatter: 'formatAmount6',
    },
    {
      field: 'productPrice',
      title: '单价（参考价）',
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'productPrice' },
    },
    {
      field: 'totalPrice',
      title: '预估金额',
      minWidth: 120,
      fixed: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'taxPercent',
      title: '税率(%)',
      minWidth: 100,
      fixed: 'right',
      slots: { default: 'taxPercent' },
    },
    {
      field: 'demandTime',
      title: '需求日期',
      minWidth: 140,
      fixed: 'right',
      slots: { default: 'demandTime' },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 150,
      slots: { default: 'remark' },
    },
    {
      field: 'qcCheckFlag',
      title: '是否质检',
      minWidth: 80,
      fixed: 'right',
      slots: { default: 'qcCheckFlag' },
    },
    {
      title: '操作',
      width: 50,
      fixed: 'right',
      slots: { default: 'actions' },
      visible: !disabled,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '申请编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入申请编号',
        allowClear: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'requestTime',
      label: '申请时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'requestUserId',
      label: '申请人',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择申请人',
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '未提交', value: 0 },
          { label: '审批中', value: 10 },
          { label: '审核通过', value: 20 },
          { label: '审核不通过', value: 30 },
          { label: '已取消', value: 40 },
        ],
        placeholder: '请选择状态',
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'checkbox',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: '申请编号',
      width: 200,
      fixed: 'left',
    },
    {
      field: 'supplierName',
      title: '供应商',
      minWidth: 120,
    },
    {
      field: 'requestTime',
      title: '申请时间',
      width: 160,
      formatter: 'formatDate',
    },
    {
      field: 'requestUserName',
      title: '申请人',
      minWidth: 120,
    },
    {
      field: 'requestDeptName',
      title: '申请部门',
      minWidth: 120,
    },
    {
      field: 'totalCount',
      title: '申请数量',
      minWidth: 120,
      formatter: 'formatAmount3',
    },
    {
      field: 'totalPrice',
      title: '预估金额',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'depositPrice',
      title: '定金',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'productNames',
      title: '申请产品',
      minWidth: 150,
      showOverflow: 'tooltip',
    },
    {
      field: 'orderNo',
      title: '采购订单',
      minWidth: 180,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      slots: { default: 'status' },
    },
    {
      field: 'inStatus',
      title: '入库状态',
      minWidth: 100,
      slots: { default: 'inStatus' },
    },
    {
      field: 'requestReason',
      title: '申请理由',
      minWidth: 150,
      showOverflow: 'tooltip',
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 160,
      formatter: 'formatDate',
    },
    {
      title: '操作',
      width: 240,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
