import type { CreateCrudOptionsRet, CreateCrudOptionsProps } from '@fast-crud/fast-crud';
import { dict } from '@fast-crud/fast-crud';
import dayjs from 'dayjs';
import { search, add, edit, del, queryCateGory } from './api';
export default function createCrudOptions(crudOptionsProps: CreateCrudOptionsProps): CreateCrudOptionsRet {
  return {
    crudOptions: {
      container: {
        is: 'fs-layout-card'
      },
      request: {
        pageRequest: async ({ page, query }) => {
          const { total, data } = await search(page.offset + 1, page.limit, query);
          return { records: data, total, currentPage: page.offset, pageSize: page.limit };
        },
        addRequest: ({ form }) => {
          form.depname = form.name;
          return add(form);
        },
        editRequest: ({ form }) => {
          form.depname = form.name;
          edit(form);
        },
        delRequest: ({ row }) => {
          return del(row.id);
        }
      },
      columns: {
        id: {
          title: 'ID',
          key: 'id',
          type: 'text',
          search: { show: true },
          column: {
            width: 50,
            align: 'center'
          },
          form: {
            show: false
          }
        },
        categoryId: {
          title: '类别',
          key: 'categoryId',
          type: 'dict-select',
          dict: dict({
            async getData() {
              const result = await queryCateGory(1, 1000, {});
              return result.data?.map(r => {
                return {
                  label: r.name,
                  value: r.id
                };
              }) as any[];
            }
          }),
          search: { show: true },
          column: {
            show: false
          },
          form: {
            show: true
          }
        },
        name: {
          title: '类别名称',
          search: { show: true },
          key: 'name',
          type: 'text',
          column: {
            width: 150,
            align: 'center'
          },
          form: {
            show: true
          }
        },
        description: {
          title: '描述',
          key: 'description',
          type: 'text',
          search: { show: false },
          column: {
            align: 'center'
          }
        },
        createTime: {
          key: 'createTime',
          title: '创建时间',
          type: 'datetime',
          column: {
            width: 250,
            align: 'center'
          },
          search: { show: true },
          valueBuilder(context) {
            const { value, row, key } = context;
            if (value) {
              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: 'datetime',
          align: 'center',
          column: {
            width: 250,
            align: 'center'
          },
          valueBuilder(context) {
            const { value, row, key } = context;
            if (value) {
              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');
            }
          }
        },
        disabled: {
          key: 'disabled',
          title: '状态',
          type: 'dict-select',
          dict: dict({
            data: [
              { value: 'N', label: '启用' },
              { value: 'Y', label: '禁用' }
            ]
          }),
          search: { show: true },
          column: {
            width: 100,
            align: 'center'
          }
        }
      },
      rowHandle: {
        buttons: {
          scores: {
            text: null,
            size: 'small',
            icon: 'carbon:result-draft',
            tooltip: {
              slots: {
                default() {
                  return '录入成绩';
                }
              }
            },
            click: ({ row }) => {
              if (crudOptionsProps.context) {
                crudOptionsProps.context.importActiveFunc(row.id);
              }
            }
          },
          query: {
            text: null,
            size: 'small',
            icon: 'icon-park-outline:history-query',
            tooltip: {
              slots: {
                default() {
                  return '查看成绩';
                }
              }
            },
            click: ({ row }) => {
              if (crudOptionsProps.context) {
                crudOptionsProps.context.viewActiveFunc(row.id);
              }
            }
          }
        }
      }
    }
  };
}
