import type { AddReq, CreateCrudOptionsRet, DelReq, EditReq } from '@fast-crud/fast-crud';
import { dict } from '@fast-crud/fast-crud';
import dayjs from 'dayjs';
import * as api from './api';
import { selectAll_1 } from './api';
export default function createCrudOptions(): CreateCrudOptionsRet {
  const pageRequest = async (query: any): Promise<any> => {
    // 调用你自己的函数从接口获取数据
    const data = await selectAll_1(query);
    return {
      rows: data,
      total: data.length
    };
  };
  const editRequest = async (ctx: EditReq) => {
    const { form, row } = ctx;
    form.id = row.id;
    return api.UpdateObj(form);
  };
  const delRequest = async (ctx: DelReq) => {
    const { row } = ctx;
    return api.DelObj(row.id);
  };

  const addRequest = async (req: AddReq) => {
    const { form } = req;
    return api.AddObj(form);
  };
  return {
    crudOptions: {
      container: {
        is: 'fs-layout-card'
      },
      request: {
        pageRequest,
        addRequest,
        editRequest,
        delRequest
      },
      columns: {
        id: {
          title: 'ID',
          key: 'id',
          type: 'number',
          search: { show: true },
          column: {
            width: 50,
            align: 'center'
          },
          form: {
            show: false
          }
        },
        name: {
          title: '部门名称',
          search: { show: true },
          key: 'name',
          type: 'text',
          column: {
            width: 150,
            align: 'center'
          },
          form: {
            show: true
          }
        },
        // select: {
        //   title: '状态',
        //   search: { show: true },
        //   type: 'dict-select',
        // 	align: 'center',
        //   // dict: dict({
        //   //   url: '/mock/crud/demo/dict'
        //   // })
        // },
        description: {
          title: '部门地址',
          key: 'description',
          type: 'text',
          search: { show: true },
          column: {
            width: 200,
            align: 'center'
          }
        },
        createTime: {
          key: 'createTime',
          title: '创建时间',
          type: 'text',
          column: {
            width: 250,
            align: 'center'
          },
          search: { show: true },
          // naive 默认仅支持数字类型时间戳作为日期输入与输出
          // 字符串类型的时间需要转换格式
          valueBuilder(context) {
            const { value, row, key } = context;
            if (value) {
              // naive 默认仅支持时间戳作为日期输入与输出
              row[key] = dayjs(value).valueOf();
            }
          },
          valueResolve(context) {
            const { value, form, key } = context;
            if (value) {
              form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
            }
          }
        },
        modifyTime: {
          title: '修改时间',
          key: 'modifyTime',
          type: 'text',
          align: 'center',
          column: {
            width: 250,
            align: 'center'
          },
          // naive 默认仅支持数字类型时间戳作为日期输入与输出
          // 字符串类型的时间需要转换格式
          valueBuilder(context) {
            const { value, row, key } = context;
            if (value) {
              // naive 默认仅支持时间戳作为日期输入与输出
              row[key] = dayjs(value).valueOf();
            }
          },
          valueResolve(context) {
            const { value, form, key } = context;
            if (value) {
              form[key] = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
            }
          }
        },
        createUid: {
          title: '创建用户ID',
          key: 'createUid',
          type: 'file-uploader',
          column: {
            width: 250,
            align: 'center'
          }
        },
        disabled: {
          key: 'disabled',
          title: '状态',
          type: 'dict-select',
          column: {
            width: 100,
            align: 'center'
          },
          dict: dict({
            url: '/mock/crud/demo/dict'
          }),
          search: { show: true }
        }
        // richtext: {
        //   title: '富文本',
        //   type: 'editor-wang5',
        //   column: {
        //     // cell中不显示
        //     show: false
        //   },
        //   form: {
        //     col: {
        //       // 横跨两列
        //       span: 24
        //     },
        //     component: {
        //       style: {
        //         height: '300px'
        //       }
        //     }
        //   }
        // }
      }
    }
  };
}
