import type { CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import { dict } from '@fast-crud/fast-crud';
import _ from 'lodash';
import { isString } from '~/src/utils';
import type { UserList } from './api';
import { addRequest, delRequest, editRequest, pageRequest, queryAll } from './api';
const userList: UserList = await queryAll();
const dictOpt = {
  url: '/userinfo/queryAll',
  label: 'relname',
  value: 'id'
};
function curd({ context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
  const openModel = context?.openModel;
  const goToSchedule = context?.goToSchedule;
  const goToCalendar = context?.goToCalendar;
  return {
    crudOptions: {
      container: {
        is: 'fs-layout-card'
      },
      request: {
        pageRequest: async ({ page, query }) => {
          const { total, data } = await pageRequest(page.offset + 1, page.limit, query);
          return { records: data, total, currentPage: page.offset, pageSize: page.limit };
        },
        addRequest: ({ form }) => {
          return addRequest(form);
        },
        editRequest: ({ form }) => {
          editRequest(form);
        },
        delRequest: ({ row }) => {
          return delRequest(row.id);
        }
      },
      toolbar: {
        show: true
      },
      actionbar: {
        show: true,
        buttons: {
          add: {
            text: '新增班级',
            title: '使用表单新增班级',
            circle: false,
            tooltip: {
              slots: {
                default() {
                  return '使用表单新增班级';
                }
              }
            }
          }
        }
      },
      form: {
        beforeSubmit: localContext => {
          if (isString(localContext.form.createUid)) {
            localContext.form.createUid = null;
          }
          if (isString(localContext.form.manageId)) {
            localContext.form.manageId = null;
          }
          return true;
        }
      },
      columns: {
        id: {
          title: '班级ID',
          type: 'text',
          search: { show: true },
          form: {
            show: false
          },
          column: {
            resizable: true,
            width: 120,
            align: 'center',
            fixed: 'left'
          }
        },
        name: {
          title: '班级名称',
          type: 'text',
          search: {
            title: '名称',
            show: true
          },
          column: {
            resizable: true,
            width: 60,
            align: 'center',
            fixed: 'left'
          }
        },
        createUid: {
          title: '创建用户',
          type: 'text',
          valueBuilder(localContext) {
            if (userList[localContext.row.createUid]) {
              localContext.row.createUid = userList[localContext.row.createUid];
            }
          },
          form: {
            component: {
              name: 'fs-dict-select',
              dict: dict(dictOpt)
            }
          },
          search: { show: false },
          column: {
            resizable: true,
            width: 90,
            align: 'center',
            fixed: 'left'
          }
        },
        manageId: {
          title: '负责人',
          type: 'text',
          valueBuilder(localContext) {
            if (userList[localContext.row.manageId]) {
              localContext.row.manageId = userList[localContext.row.manageId];
            }
          },
          form: {
            component: {
              name: 'fs-dict-select',
              dict: dict(dictOpt)
            }
          },
          search: { show: false },
          column: {
            resizable: true,
            width: 90,
            align: 'center',
            fixed: 'left'
          }
        },
        modifyTime: {
          title: '更新时间',
          type: 'easDateTime',
          search: { show: false },
          form: { show: false },
          column: {
            resizable: true,
            width: 110,
            align: 'center',
            fixed: 'left'
          }
        },
        createTime: {
          title: '创建时间',
          type: 'easDateTime',
          search: { show: true },
          form: { show: false },
          column: {
            resizable: true,
            width: 110,
            align: 'center',
            fixed: 'left'
          }
        },
        disabled: {
          title: '状态',
          type: 'dict-select',
          dict: dict({
            data: [
              { value: 'N', label: '启用中' },
              { value: 'Y', label: '已禁用' }
            ]
          }),
          column: {
            resizable: true,
            width: 80
          }
        }
      },
      rowHandle: {
        buttons: {
          studentManager: {
            text: null,
            size: 'small',
            icon: 'raphael:people',
            tooltip: {
              slots: {
                default() {
                  return '管理学员';
                }
              }
            },
            click: ({ row }) => {
              openModel(row.id);
            }
          },
          schedule: {
            text: null,
            size: 'small',
            icon: 'uil:schedule',
            tooltip: {
              slots: {
                default() {
                  return '查看排课计划';
                }
              }
            },
            click: ({ row }) => {
              goToSchedule(row.id);
            }
          },
          calendar: {
            text: null,
            size: 'small',
            icon: 'ph:calendar-fill',
            tooltip: {
              slots: {
                default() {
                  return '查看排课日历';
                }
              }
            },
            click: ({ row }) => {
              goToCalendar(row.id);
            }
          }
        }
      }
    }
  };
}
export default curd;
