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 { erpPriceInputFormatter } from '@vben/utils';

import { getAccountSimpleList } from '#/api/erp/finance/account';
import { getPurchaseInvoiceSimpleList } from '#/api/erp/purchase/invoice';
import { getSupplierSimpleList } from '#/api/srm/supplier';
import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';

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

/** 表单的配置项 */
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: 'paymentTime',
      label: '付款时间',
      component: 'DatePicker',
      componentProps: {
        disabled: formType === 'detail',
        placeholder: '选择付款时间',
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
      rules: 'required',
    },
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      componentProps: (_values, form) => ({
        disabled: formType === 'detail',
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
        onChange: () => {
          // 供应商变化后，清空已选的关联来票
          form.setFieldValue('invoiceId', undefined);
        },
      }),
      rules: 'required',
    },
    {
      fieldName: 'invoiceId',
      label: '关联来票',
      component: formType === 'detail' ? 'Input' : 'Select',
      componentProps:
        formType === 'detail'
          ? {
              disabled: true,
              placeholder: '--',
            }
          : undefined,
      rules: 'required',
      dependencies: {
        // 依赖 invoiceId 自身，确保编辑回显时重新加载选项以匹配已选来票
        triggerFields: ['supplierId', 'invoiceId'],
        disabled: (values) => formType === 'detail' || !values.supplierId,
        async componentProps(values) {
          if (formType === 'detail') {
            return {};
          }
          if (!values.supplierId) {
            return {
              options: [],
              placeholder: '请先选择供应商',
            };
          }
          const invoices = await getPurchaseInvoiceSimpleList(
            values.supplierId,
          );
          return {
            options: invoices.map((item) => {
              const remain = item.remainingInvoicePrice ?? item.price ?? 0;
              return {
                label: `${item.no} / ${item.invoiceNo ?? '-'} / ¥${item.price ?? 0} / 剩余可付 ¥${remain.toFixed(2)}`,
                value: item.id,
              };
            }),
            placeholder: '请选择关联来票',
          } as any;
        },
      },
    },
    {
      fieldName: 'financeUserId',
      label: '财务人员',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择财务人员',
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'blobIds',
      label: '附件',
      component: 'FileUpload',
      componentProps: {
        valueKey: 'id',
        maxNumber: 10,
        multiple: true,
        showDescription: formType !== 'detail',
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-3',
    },
    {
      fieldName: 'items',
      label: '采购入库、退货单',
      component: 'Input',
      formItemClass: 'col-span-3',
    },
    {
      fieldName: 'accountId',
      label: '付款账户',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择付款账户',
        allowClear: true,
        showSearch: true,
        api: getAccountSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
      help: '付款账户数据来自【财务管理 → 结算账户】菜单维护的开启状态账户，无选项时请先在结算账户页面新增。',
    },
    {
      fieldName: 'totalPrice',
      label: '合计付款',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '合计付款',
        precision: 2,
        formatter: erpPriceInputFormatter,
        disabled: true,
      },
    },
    {
      fieldName: 'discountPrice',
      label: '优惠金额',
      component: 'InputNumber',
      defaultValue: 0,
      componentProps: {
        class: '!w-full',
        disabled: formType === 'detail',
        placeholder: '请输入优惠金额',
        precision: 2,
        formatter: erpPriceInputFormatter,
      },
    },
    {
      fieldName: 'paymentPrice',
      label: '实际付款',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '实际付款',
        precision: 2,
        formatter: erpPriceInputFormatter,
        disabled: true,
      },
      dependencies: {
        triggerFields: ['totalPrice', 'discountPrice'],
        componentProps: (values) => {
          const totalPrice = values.totalPrice || 0;
          const discountPrice = values.discountPrice || 0;
          values.paymentPrice = totalPrice - discountPrice;
          return {};
        },
      },
    },
  ];
}

/** 表单的明细表格列（付款基于来票，明细由来票自动生成，不可删除） */
export function useFormItemColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'seq', title: '序号', minWidth: 50, fixed: 'left' },
    {
      field: 'bizNo',
      title: '来票编号',
      minWidth: 200,
    },
    {
      field: 'totalPrice',
      title: '应付金额',
      minWidth: 100,
      formatter: 'formatAmount2',
    },
    {
      field: 'paidPrice',
      title: '已付金额',
      minWidth: 100,
      formatter: 'formatAmount2',
    },
    {
      field: 'paymentPrice',
      title: '本次付款',
      minWidth: 115,
      fixed: 'right',
      slots: { default: 'paymentPrice' },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 150,
      slots: { default: 'remark' },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '付款单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入付款单号',
        allowClear: true,
      },
    },
    {
      fieldName: 'paymentTime',
      label: '付款时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'creator',
      label: '创建人',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择创建人',
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
      },
    },
    {
      fieldName: 'financeUserId',
      label: '财务人员',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择财务人员',
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
      },
    },
    {
      fieldName: 'accountId',
      label: '付款账户',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择付款账户',
        allowClear: true,
        showSearch: true,
        api: getAccountSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ERP_AUDIT_STATUS, 'number'),
        placeholder: '请选择状态',
        allowClear: true,
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注',
        allowClear: true,
      },
    },
    {
      fieldName: 'bizNo',
      label: '采购单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入采购单号',
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'checkbox',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: '付款单号',
      width: 180,
      fixed: 'left',
    },
    {
      field: 'supplierName',
      title: '供应商',
      minWidth: 120,
    },
    {
      field: 'invoice',
      title: '关联来票',
      minWidth: 200,
      slots: { default: 'invoice' },
    },
    {
      field: 'paymentTime',
      title: '付款时间',
      width: 160,
      formatter: 'formatDate',
    },
    {
      field: 'creatorName',
      title: '创建人',
      minWidth: 120,
    },
    {
      field: 'financeUserName',
      title: '财务人员',
      minWidth: 120,
    },
    {
      field: 'accountName',
      title: '付款账户',
      minWidth: 120,
    },
    {
      field: 'totalPrice',
      title: '合计付款',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'discountPrice',
      title: '优惠金额',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'paymentPrice',
      title: '实际付款',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 90,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_AUDIT_STATUS },
      },
    },
    {
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 采购入库单选择表单的配置项 */
export function usePurchaseInGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '入库单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入入库单号',
        allowClear: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'Input',
      componentProps: {
        disabled: true,
        placeholder: '已自动选择供应商',
      },
    },
    {
      fieldName: 'paymentStatus',
      label: '付款状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '未付款', value: 0 },
          { label: '部分付款', value: 1 },
          { label: '全部付款', value: 2 },
        ],
        placeholder: '请选择付款状态',
        allowClear: true,
      },
    },
  ];
}

/** 采购入库单选择列表的字段 */
export function usePurchaseInGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'checkbox',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: '入库单号',
      width: 200,
      fixed: 'left',
    },
    {
      field: 'supplierName',
      title: '供应商',
      minWidth: 120,
    },
    {
      field: 'inTime',
      title: '入库时间',
      width: 160,
      formatter: 'formatDate',
    },
    {
      field: 'totalPrice',
      title: '应付金额',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'paymentPrice',
      title: '已付金额',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'unPaymentPrice',
      title: '未付金额',
      formatter: ({ row }) => {
        return erpPriceInputFormatter(row.totalPrice - row.paymentPrice || 0);
      },
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_AUDIT_STATUS },
      },
    },
  ];
}

/** 采购退货单选择表单的配置项 */
export function useSaleReturnGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '退货单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入退货单号',
        allowClear: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'Input',
      componentProps: {
        disabled: true,
        placeholder: '已自动选择供应商',
      },
    },
    {
      fieldName: 'refundStatus',
      label: '退款状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '未退款', value: 0 },
          { label: '部分退款', value: 1 },
          { label: '全部退款', value: 2 },
        ],
        placeholder: '请选择退款状态',
        allowClear: true,
      },
    },
  ];
}

/** 采购退货单选择列表的字段 */
export function useSaleReturnGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'checkbox',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: '退货单号',
      width: 200,
      fixed: 'left',
    },
    {
      field: 'supplierName',
      title: '供应商',
      minWidth: 120,
    },
    {
      field: 'returnTime',
      title: '退货时间',
      width: 160,
      formatter: 'formatDate',
    },
    {
      field: 'totalPrice',
      title: '应退金额',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'refundPrice',
      title: '已退金额',
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'unRefundPrice',
      title: '未退金额',
      formatter: ({ row }) => {
        return erpPriceInputFormatter(row.totalPrice - row.refundPrice || 0);
      },
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_AUDIT_STATUS },
      },
    },
  ];
}
