import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { FeatureApi } from '#/api/product-manage';

// 表单配置
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'api_id',
      label: '模块编号',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '描述',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      fieldName: 'cost_price',
      label: '成本价',
      rules: 'required',
      componentProps: {
        min: 0,
        precision: 2,
        step: 0.01,
        formatter: (value: number) => {
          // 格式化为带千分位分隔符的货币
          const parts = value.toString().split('.');
          parts[0] = (parts[0] || '0').replace(/\B(?=(\d{3})+(?!\d))/g, ',');
          return `¥ ${parts.join('.')}`;
        },
        parser: (value: string) => {
          // 移除货币符号和千分位分隔符
          return value.replace(/[¥,\s]/g, '');
        },
      },
    },
  ];
}

// 搜索表单配置
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'api_id',
      label: '模块编号',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '描述',
    },
  
  ];
}

// 表格列配置
export function useColumns<T = FeatureApi.FeatureItem>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'api_id',
      title: '模块编号',
      minWidth: 150,
    },
    {
      field: 'name',
      title: '描述',
      minWidth: 200,
    },
    {
      field: 'cost_price',
      title: '成本价',
      minWidth: 120,
      formatter: ({ cellValue }) => {
        // 格式化为带千分位分隔符的货币
        const value = cellValue?.toFixed(2) || '0.00';
        const parts = value.split('.');
        parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
        return `¥ ${parts.join('.')}`;
      },
    },
    {
      field: 'create_time',
      title: '创建时间',
      minWidth: 180,
    },
    {
      field: 'update_time',
      title: '更新时间',
      minWidth: 180,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '模块',
          onClick: onActionClick,
        },
        options: [
          {
            code: 'edit',
            text: '编辑',
          },
          {
            code: 'delete',
            text: '删除',
          },
          {
            code: 'example',
            text: '示例配置',
          },
        ],
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 180,
    },
  ];
}
