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

/** 状态选项：BI 模块 1=启用 0=禁用（与 common_status 字典值相反） */
const STATUS_OPTIONS = [
  { label: '启用', value: 1 },
  { label: '禁用', value: 0 },
];

/** 图表类型选项 */
const CHART_TYPE_OPTIONS = [
  { label: '柱状图 (bar)', value: 'bar' },
  { label: '折线图 (line)', value: 'line' },
  { label: '饼图 (pie)', value: 'pie' },
  { label: '散点图 (scatter)', value: 'scatter' },
  { label: '仪表盘 (gauge)', value: 'gauge' },
  { label: '表格 (table)', value: 'table' },
  { label: '数字卡 (number)', value: 'number' },
];

/** 新增/修改表单 */
export function useFormSchema(
  dashboardOptions: { label: string; value: number }[],
): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'dashboardId',
      label: '所属仪表盘',
      component: 'Select',
      componentProps: {
        options: dashboardOptions,
        placeholder: '请选择仪表盘',
      },
      rules: 'required',
    },
    {
      fieldName: 'name',
      label: '图表名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入图表名称',
      },
      rules: 'required',
    },
    {
      fieldName: 'chartType',
      label: '图表类型',
      component: 'Select',
      componentProps: {
        options: CHART_TYPE_OPTIONS,
        placeholder: '请选择图表类型',
      },
      defaultValue: 'bar',
      rules: 'required',
    },
    {
      fieldName: 'dataSourceId',
      label: '数据源ID',
      component: 'InputNumber',
      componentProps: {
        placeholder: '数据源配置ID',
      },
    },
    {
      fieldName: 'querySql',
      label: '查询SQL',
      component: 'Textarea',
      componentProps: {
        placeholder: 'SELECT ...',
        rows: 3,
      },
    },
    {
      fieldName: 'position',
      label: '布局位置',
      component: 'Input',
      componentProps: {
        placeholder: '{"x":0,"y":0,"w":12,"h":6}',
      },
    },
    {
      fieldName: 'chartOptions',
      label: '图表配置',
      component: 'Textarea',
      componentProps: {
        placeholder: 'JSON 配置（可选）',
        rows: 3,
      },
    },
    {
      fieldName: 'refreshInterval',
      label: '刷新间隔(秒)',
      component: 'InputNumber',
      componentProps: {
        placeholder: '0 表示不自动刷新',
        min: 0,
      },
      defaultValue: 0,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
      },
      defaultValue: 0,
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: STATUS_OPTIONS,
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: 1,
      rules: 'required',
    },
  ];
}

/** 列表搜索表单 */
export function useGridFormSchema(
  dashboardOptions: { label: string; value: number }[],
): VbenFormSchema[] {
  return [
    {
      fieldName: 'dashboardId',
      label: '所属仪表盘',
      component: 'Select',
      componentProps: {
        options: dashboardOptions,
        placeholder: '请选择仪表盘',
        allowClear: true,
      },
    },
    {
      fieldName: 'name',
      label: '图表名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入图表名称',
        allowClear: true,
      },
    },
  ];
}

/** 列表字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      width: 80,
    },
    {
      field: 'name',
      title: '图表名称',
      minWidth: 150,
    },
    {
      field: 'chartType',
      title: '图表类型',
      width: 120,
      cellRender: {
        name: 'CellSelect',
        props: {
          options: CHART_TYPE_OPTIONS,
        },
      },
    },
    {
      field: 'refreshInterval',
      title: '刷新间隔(s)',
      width: 100,
    },
    {
      field: 'sort',
      title: '排序',
      width: 70,
    },
    {
      field: 'status',
      title: '状态',
      width: 80,
      slots: { default: 'status' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 170,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
