import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DatabaseApi, TenantApi } from '#/api';
import { dataTypeOptions } from '#/api/model';

export const searchFormOptions: VbenFormProps = {
  showCollapseButton: false,
  schema: [
    {
      component: 'Input',
      fieldName: 'name',
      label: '表名',
    },
    {
      component: 'Input',
      fieldName: 'description',
      label: '说明',
    },
  ],
};

export const gridOptions: VxeGridProps<DatabaseApi.RecordItem> = {
  toolbarConfig: {
    refresh: true,
    print: false,
    export: false,
    zoom: true,
    custom: true,
  },
  columns: [
    { title: '序号', type: 'seq', width: 50 },
    {
      align: 'left',
      field: 'name',
      title: '表名',
      width: 200,
    },
    { align: 'left', field: 'description', title: '备注' },
    {
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      title: '操作',
      width: 170,
    },
  ],
  height: 'auto',
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await DatabaseApi.getPage({
          pageIndex: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
};

export const formOptions: VbenFormProps = {
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  schema: [
    {
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请输入',
        api: {
          url: TenantApi.getOptions,
        },
        showSearch: true,
        numberToString: true,
      },
      fieldName: 'configId',
      label: '数据库',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入表名',
      },
      fieldName: 'name',
      label: '表名',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入说明',
      },
      fieldName: 'description',
      label: '说明',
      rules: 'required',
    },
  ],
  wrapperClass: 'grid-cols-1',
};

export const gridColumnOptions: VxeGridProps<DatabaseApi.BasicColumnRecord> = {
  rowConfig: {
    isCurrent: true,
    isHover: true,
    keyField: 'columnName',
    drag: true,
    useKey: true,
  },
  columnConfig: {
    useKey: true,
  },
  columns: [
    { title: '序号', type: 'seq', width: 50 },
    {
      align: 'left',
      field: 'columnName',
      title: '列名',
      width: 200,
      dragSort: true,
    },
    { align: 'left', field: 'dataType', title: '类型', width: 100 },
    { align: 'left', field: 'description', title: '说明' },
    {
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      title: '操作',
      width: 110,
    },
  ],
  editConfig: {
    mode: 'row',
    trigger: 'click',
    // showStatus: true,
  },
  height: 'auto',
  keepSource: true,
  pagerConfig: {
    enabled: false,
  },
  dragConfig: {
    rowIcon: 'vxe-icon-sort',
  },
};

export const formColumnOptions: VbenFormProps = {
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入字段名',
      },
      fieldName: 'columnName',
      label: '字段名',
      rules: 'required',
    },
    {
      component: 'Select',
      componentProps: {
        placeholder: '请输入数据类型',
        options: dataTypeOptions,
        showSearch: true,
      },
      fieldName: 'dataType',
      label: '数据类型',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入说明',
      },
      fieldName: 'description',
      label: '说明',
      rules: 'required',
    },
    {
      component: 'Switch',
      defaultValue: false,
      componentProps: {
        placeholder: '请输入主键',
        class: 'w-auto',
      },
      fieldName: 'isPrimarykey',
      label: '主键',
    },
    {
      component: 'Switch',
      defaultValue: false,
      componentProps: {
        placeholder: '请输入自动增加',
        class: 'w-auto',
      },
      fieldName: 'isIdentity',
      label: '自动增加',
      dependencies: {
        show(values) {
          return values.isPrimarykey === true;
        },
        triggerFields: ['isPrimarykey'],
      },
    },
    {
      component: 'Switch',
      defaultValue: true,
      componentProps: {
        placeholder: '请输入允许为空',
        class: 'w-auto',
      },
      fieldName: 'isNullable',
      label: '允许为空',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入长度',
      },
      fieldName: 'length',
      label: '长度',
      rules: 'required',
      dependencies: {
        show(values) {
          return ['char', 'decimal', 'nchar', 'nvarchar', 'varchar'].includes(
            values.dataType,
          );
        },
        triggerFields: ['dataType'],
      },
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入小数位数',
      },
      fieldName: 'decimalDigits',
      label: '小数位数',
      rules: 'required',
      dependencies: {
        show(values) {
          return ['decimal'].includes(values.dataType);
        },
        triggerFields: ['dataType'],
      },
    },
  ],
  wrapperClass: 'grid-cols-1',
};
