import type { CreateCrudOptionsRet, CreateCrudOptionsProps } from '@fast-crud/fast-crud';
import dayjs from 'dayjs';
import { queryStudentScores, addStudentScore } from '../api';
export default function createCrudOptions(crudOptionsProps: CreateCrudOptionsProps): CreateCrudOptionsRet {
  return {
    crudOptions: {
      request: {
        pageRequest: async ({ page, query }) => {
          query.subjectId = crudOptionsProps.context?.subjectId;
          const { total, data } = await queryStudentScores(page.offset + 1, page.limit, query);
          return { records: data, total, currentPage: page.offset, pageSize: page.limit };
        },
        addRequest: ({ form, row }) => {
          return addStudentScore({
            studentNumber: form.studentNumber,
            categoryId: row.categoryId,
            subjectId: row.subjectId,
            testDate: form.testDate,
            score: form.score,
            comment: form.comment,
            id: row.id
          });
        },
        editRequest: ({ form, row }) => {
          return addStudentScore({
            studentNumber: form.studentNumber,
            categoryId: row.categoryId,
            subjectId: row.subjectId,
            testDate: form.testDate,
            score: form.score,
            comment: form.comment,
            id: row.id
          });
        },
        delRequest: () => {
          return Promise.resolve(true);
        }
      },
      rowHandle: {
        show: true
      },
      toolbar: {
        show: false
      },
      actionbar: {
        show: false
      },
      search: {
        show: true,
        buttons: {
          search: {
            show: true
          },
          reset: {
            show: true
          },
          custom: {
            show: true,
            text: '关闭',
            click: () => {
              crudOptionsProps.context?.viewActiveCloseFunc();
            }
          }
        }
      },
      columns: {
        studentName: {
          title: '学员姓名',
          type: 'text',
          search: {
            show: true
          }
        },
        categoryName: {
          title: '类别名称',
          type: 'text',
          search: {
            show: true
          },
          form: {
            show: false
          }
        },
        subjectsName: {
          title: '科目名称',
          type: 'text',
          search: {
            show: true
          },
          form: {
            show: false
          }
        },
        studentNumber: {
          title: '学员编号',
          search: {
            show: true
          },
          type: 'text',
          column: {
            align: 'center',
            width: 240
          },
          form: {
            show: false
          }
        },
        testDate: {
          title: '考试日期',
          type: 'datetime',
          search: { show: false },
          column: {
            align: 'center'
          },
          form: {
            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');
            }
          }
        },
        score: {
          title: '成绩',
          type: 'number',
          search: {
            show: true
          },
          form: {
            show: true
          }
        },
        createTime: {
          key: 'createTime',
          title: '创建时间',
          type: 'datetime',
          column: {
            width: 250,
            align: 'center'
          },
          form: {
            show: false
          },
          search: { show: false },
          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'
          },
          form: {
            show: false
          },
          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');
            }
          }
        },
        passRate: {
          title: '及格率',
          type: 'text',
          search: { show: false },
          column: {
            align: 'center'
          },
          form: {
            show: false
          }
        },
        excelRate: {
          title: '通过率',
          type: 'text',
          search: { show: false },
          column: {
            align: 'center'
          },
          form: {
            show: false
          }
        },
        comment: {
          title: '评语',
          type: 'textarea',
          search: { show: false },
          column: {
            align: 'center',
            width: 200
          }
        }
      }
    }
  };
}
