import { downloadFileByBlob } from "@/common/net/download";
import { CardStepTitle, SuperTable } from "@/components";
import ExamSampleController from "@/services/apis/ExamSampleController";
import { DataPublishType } from "@/services/enums";
import { DownloadOutlined } from "@ant-design/icons";
import { ActionType, PageContainer, ProColumns } from "@ant-design/pro-components";
import { useEmotionCss } from "@ant-design/use-emotion-css";
import { history, useParams } from "@umijs/max";
import { useRequest } from "ahooks";
import { App, Button, Spin, theme } from "antd";
import { useRef } from "react";

/** 监测抽样数量统计 */
const OrgExamSampleCountList: React.FC = () => {
    const reqParams = useParams() as unknown as { examDataPublishId: number };

    const { token } = theme.useToken();

    const actionRef = useRef<ActionType>();
    const { message } = App.useApp();

    const countRowClassName = useEmotionCss(({ token }) => {
        return {
            backgroundColor: token.colorBgLayout,
            fontWeight: 'bold',
            fontStyle: 'italic',
        };
    });

    const { data, loading } = useRequest(async () => {
        const sampleRes = await ExamSampleController.getByExamDataPublishId({ examdatapublishid: reqParams.examDataPublishId, type: DataPublishType.STUDENT_SAMPLE_COUNT_LIST });
        return {
            examSample: sampleRes,
        };
    });

    // 明细表列定义
    const columns: ProColumns<API.ExamSampleCountOutput>[] = [
        {
            title: '学校',
            dataIndex: 'sysOrgName',
            width: 280,
            align: 'center',
            onCell: (r) => {
                if (r.gradeId === 9999) {
                    return { colSpan: 3 };
                }
                return { colSpan: 1 };
            },
        },
        {
            title: '年级',
            dataIndex: 'gradeName',
            width: 96,
            align: 'center',
            onCell: (r) => {
                if (r.gradeId === 9999) {
                    return { colSpan: 0 };
                }
                if (r.classNumber === 9999) {
                    return { colSpan: 2 };
                }
                return { colSpan: 1 };
            },
        },
        {
            title: '班级',
            dataIndex: 'classNumber',
            width: 96,
            align: 'center',
            renderText: (v) => `${v}班`,
            onCell: (r) => {
                if (r.classNumber === 9999 || r.gradeId === 9999) {
                    return { colSpan: 0 };
                }
                return {};
            },
        },
        {
            title: '区级监测',
            dataIndex: 'centerStudentCount',
            width: 96,
            align: 'center',
        },
        {
            title: '校内考试',
            dataIndex: 'schoolStudentCount',
            width: 96,
            align: 'center',
        },
        {
            title: '合计',
            dataIndex: 'totalStudentCount',
            width: 96,
            align: 'center',
        },
        {},
    ];

    return (
        <PageContainer
            title={`${data?.examSample?.examPlan?.fullName ?? '监测计划'} - 监测抽样统计表`}
            onBack={() => history.back()}
        >
            <Spin spinning={loading}>
                {data?.examSample?.id &&
                    <>
                        <SuperTable<API.ExamSampleCountOutput>
                            headerTitle={<CardStepTitle>监测抽样统计表</CardStepTitle>}
                            style={{ marginTop: token.margin }}
                            actionRef={actionRef}
                            scroll={{ x: 'max-content' }}
                            search={false}
                            rowKey="index"
                            columnEmptyText=""
                            columns={columns}
                            pagination={false}
                            toolbar={{
                                actions: [
                                    <Button
                                        key="export"
                                        icon={<DownloadOutlined />}
                                        disabled={!data?.examSample?.id}
                                        type="primary"
                                        onClick={async () => {
                                            try {
                                                message.loading('正在生成文件，请稍侯');
                                                if (!data?.examSample?.id) {
                                                    return;
                                                }
                                                const res = await ExamSampleController.exportSampleCountToOrg({ id: data?.examSample?.id });
                                                if (res) {
                                                    downloadFileByBlob(res.data, res.fileName);
                                                }
                                            }
                                            catch { }
                                            finally {
                                                message.destroy();
                                            }
                                        }}
                                    >下载统计表</Button>,
                                ],
                            }}
                            request={async () => {
                                const res = await ExamSampleController.getOrgSampleCountListById({ id: data.examSample?.id ?? 0 })
                                return {
                                    data: res,
                                    success: true,
                                };
                            }}
                            rowClassName={(r) => {
                                if (r.classNumber === 9999 || r.gradeId === 9999) {
                                    return countRowClassName;
                                }
                                return '';
                            }}
                        />
                    </>
                }
            </Spin>
        </PageContainer>
    );
}

export default OrgExamSampleCountList;
