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: 'Switch',
      fieldName: 'no_offline',
      label: '不支持下架',
      defaultValue: false,
      help: '勾选后该模块不开放下架功能，提交时白名单价格传 -1',
    },
    {
      component: 'InputNumber',
      fieldName: 'whitelist_price',
      label: '白名单屏蔽价格（元）',
      componentProps: {
        min: 0,
        precision: 2,
        placeholder: '0=免费下架，>0=付费下架；勾选「不支持下架」时此项忽略',
      },
      dependencies: {
        triggerFields: ['no_offline'],
        if(values) {
          return !values?.no_offline;
        },
      },
    },
    {
      component: 'InputNumber',
      componentProps: { min: 0, precision: 2 },
      fieldName: 'cost_price',
      label: '成本价（元）',
      rules: 'required',
    },
  ];
}

// 搜索表单配置
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: 'whitelist_price',
      title: '白名单屏蔽价格（元）',
      minWidth: 150,
      cellRender: {
        name: 'VxeCellRender',
        props: {
          render: ({ row }: { row: FeatureApi.FeatureItem }) => {
            const price = (row as FeatureApi.FeatureItem).whitelist_price ?? 0;
            if (price < 0) return '不支持下架';
            if (price === 0) return '免费下架';
            return `¥${price.toFixed(2)}`;
          },
        },
      },
    },
    {
      field: 'cost_price',
      formatter: ({ cellValue }) => `¥${(cellValue || 0).toFixed(2)}`,
      title: '成本价（元）',
      minWidth: 120,
    },
    {
      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,
    },
  ];
}
