import { toSelectOptions } from '@/common/converter';
import { SuperTable } from '@/components';
import ExamOrgDataReportController from '@/services/apis/ExamOrgDataReportController';
import { DataReportType, ExamStatus } from '@/services/enums';
import { ActionType, PageContainer, ProColumns } from '@ant-design/pro-components';
import { history, useModel } from '@umijs/max';
import { Tag } from 'antd';
import { useRef } from 'react';

/** 监测教师管理 */
const OrgExamTeacherList: React.FC = () => {
    const actionRef = useRef<ActionType>();

    const { getDictValueEnum, getKeyDict } = useModel('useDict');
    const { baseData } = useModel('useBaseData');
    const dataReportStatusDict = getKeyDict('data_report_status');

    const columns: ProColumns<API.ExamPlanOrgDataReportOutput>[] = [
        {
            title: '监测名称',
            dataIndex: 'examPlanFullName',
            renderText: (v, r) => <a onClick={() => history.push(`/exam-s/teacher/report/${r.examPlanId}`)}>{v}</a>,
            hideInDescriptions: true,
        },
        {
            title: '监测状态',
            dataIndex: 'examStatus',
            valueEnum: getDictValueEnum('exam_status', true, [ExamStatus.READY, ExamStatus.CANCELLED]),
            width: 80,
            align: 'center',
            hideInDescriptions: true,
        },
        {
            title: '监测学期',
            dataIndex: ['semester', 'nickShortName'],
            search: {
                transform: (v) => v ? ({ semesterId: JSON.parse(v) }) : v,
            },
            valueType: 'select',
            fieldProps: {
                options: toSelectOptions(baseData?.semesters ?? [], (item) => ({ key: item.id, label: item.nickShortName, value: item.id })),
            },
            hideInTable: true,
        },
        {
            title: '截止时间',
            dataIndex: 'endTime',
            hideInSearch: true,
            width: 144,
            align: 'center',
        },
        {
            title: '上报人',
            dataIndex: 'reportSysUserName',
            width: 88,
            align: 'center',
        },
        {
            title: '上报时间',
            dataIndex: 'reportTime',
            width: 144,
            align: 'center',
        },
        {
            title: '上报状态',
            dataIndex: 'status',
            valueEnum: getDictValueEnum('data_report_status', true),
            width: 88,
            align: 'center',
            render: (v, r) => {
                if (!r.status) {
                    return null;
                }
                const s = dataReportStatusDict[r.status]
                return (<Tag color={s.antStatus} style={{ marginRight: 0 }}>{s.name}</Tag>);
            },
        },
    ];

    return (
        <PageContainer title={false}>
            <SuperTable<API.ExamPlanOrgDataReportOutput>
                headerTitle="教师信息上报列表"
                actionRef={actionRef}
                columns={columns}
                scroll={{ x: 'max-content' }}
                rowKey="rowNumber"
                request={async (params, sort) => {
                    return SuperTable.requestPageAgent({ params, sort }, async (p) => {
                        const res = await ExamOrgDataReportController.queryPageList({ ...p, type: DataReportType.TEACHER });
                        return res;
                    });
                }}
            />
        </PageContainer>
    );
};

export default OrgExamTeacherList;
