import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { HrmTaxRateConfigApi } from '#/api/hrm/salary/tax-rate-config';

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

/** 新增/修改个税税率配置的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'level',
      label: '税率级数',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入税率级数',
        min: 1,
        max: 7,
        precision: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'minAmount',
      label: '应纳税所得额下限',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入下限（含）',
        min: 0,
        precision: 2,
      },
      rules: 'required',
    },
    {
      fieldName: 'maxAmount',
      label: '应纳税所得额上限',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入上限（不含），0表示无上限',
        min: 0,
        precision: 2,
      },
      help: '上限不含，0表示无上限',
    },
    {
      fieldName: 'taxRate',
      label: '税率',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入税率',
        min: 0,
        max: 1,
        step: 0.01,
        precision: 4,
        formatter: (value: number) => `${(value * 100).toFixed(0)}%`,
        parser: (value: string) => parseFloat(value.replace('%', '')) / 100,
      },
      rules: 'required',
    },
    {
      fieldName: 'quickDeduction',
      label: '速算扣除数',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入速算扣除数',
        min: 0,
        precision: 2,
      },
      rules: 'required',
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<HrmTaxRateConfigApi.TaxRateConfig>['columns'] {
  return [
    { field: 'level', title: '级数', width: 80 },
    {
      field: 'amountRange',
      title: '应纳税所得额',
      minWidth: 200,
      slots: { default: 'amountRange' },
    },
    {
      field: 'taxRate',
      title: '税率',
      width: 100,
      slots: { default: 'taxRate' },
    },
    { field: 'quickDeduction', title: '速算扣除数', width: 120, formatter: 'formatAmount2' },
    { field: 'effectiveDate', title: '生效日期', width: 110 },
    {
      title: '操作',
      width: 150,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}