import type { 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(): CreateCrudOptionsRet {
  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: context => {
          if (isString(context.form.createUid)) {
            context.form.createUid = null;
          }
          if (isString(context.form.managerId)) {
            context.form.managerId = null;
          }
          return true;
        }
      },
      columns: {
        id: {
          title: '教室ID',
          type: 'text',
          search: { show: true },
          form: {
            show: false
          },
          column: {
            resizable: true,
            width: 60,
            align: 'center',
            fixed: 'left'
          }
        },
        name: {
          title: '教室名称',
          type: 'text',
          search: {
            title: '名称',
            show: true
          },
          column: {
            resizable: true,
            width: 60,
            align: 'center',
            fixed: 'left'
          }
        },
        address: {
          title: '教室地址',
          type: 'text',
          search: {
            title: '地址',
            show: true
          },
          column: {
            resizable: true,
            width: 110,
            align: 'center',
            fixed: 'left'
          }
        },
        floor: {
          title: '楼层',
          type: 'text',
          search: {
            title: '楼层',
            show: true
          },
          column: {
            resizable: true,
            width: 30,
            align: 'center',
            fixed: 'left'
          }
        },
        capacity: {
          title: '容量',
          type: 'number',
          search: {
            title: '容量',
            show: false
          },
          column: {
            resizable: true,
            width: 30,
            align: 'center',
            fixed: 'left'
          }
        },
        comment: {
          title: '备注信息',
          type: 'text',
          search: {
            title: '容量',
            show: false
          },
          column: {
            resizable: true,
            width: 130,
            align: 'center',
            fixed: 'left'
          }
        },
        createUid: {
          title: '创建用户',
          type: 'text',
          valueBuilder(context) {
            if (userList[context.row.createUid]) {
              context.row.createUid = userList[context.row.createUid];
            }
          },
          form: {
            component: {
              name: 'fs-dict-select',
              dict: dict(dictOpt)
            }
          },
          search: { show: false },
          column: {
            resizable: true,
            width: 90,
            align: 'center',
            fixed: 'left'
          }
        },
        managerId: {
          title: '负责人',
          type: 'text',
          valueBuilder(context) {
            if (userList[context.row.managerId]) {
              context.row.managerId = userList[context.row.managerId];
            }
          },
          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
          }
        }
      }
    }
  };
}
export default curd;
