import { downloadFileByBlob } from "@/common/net/download";
import { CardStepTitle, FileLink } from "@/components";
import ExamOrgResultController from "@/services/apis/ExamOrgResultController";
import { DataPublishType, ExamStatus, PublishStatus } from "@/services/enums";
import { ActionType, ProTable } from "@ant-design/pro-components";
import { history } from "@umijs/max";
import { Space, theme } from "antd";
import { useRef } from "react";

/** 监测发布内容管理 */
const OrgExamDataPublishList: React.FC<{ examPlanId: number, examPlanStatus?: ExamStatus }> = ({ examPlanId }) => {
    const { token } = theme.useToken();

    const actionRef = useRef<ActionType>();
    const dataRef = useRef<API.ExamDataPublishOrgResultOutput[]>();

    // const { getDictValueEnum } = useModel('useDict');

    return (
        <>
            <ProTable<API.ExamDataPublishOrgResultOutput>
                headerTitle={<CardStepTitle>结果反馈</CardStepTitle>}
                style={{ marginTop: token.margin }}
                search={false}
                size="small"
                bordered
                scroll={{ x: 'max-content' }}
                sticky={{ offsetHeader: 56 }}
                actionRef={actionRef}
                options={{ density: false, fullScreen: false, setting: false }}
                pagination={false}
                columns={[
                    {
                        title: '序',
                        valueType: 'option',
                        fixed: 'left',
                        width: 32,
                        align: 'center',
                        render: (_, r, ri) => ri + 1,
                    },
                    // {
                    //     title: '类型',
                    //     dataIndex: 'type',
                    //     valueEnum: getDictValueEnum('data_publish_type'),
                    //     width: 80,
                    //     align: 'center',
                    //     fixed: 'left',
                    // },
                    {
                        title: '名称',
                        dataIndex: 'name',
                        width: 320,
                        render: (v, r) => {
                            if (r.status !== PublishStatus.PUBLISHED) {
                                return v;
                            }
                            if (r.type === DataPublishType.QUESTIONNAIRE_PROGRESS) {
                                return <a onClick={() => history.push(`/exam-s/questionnaire/patriarch/progress/${examPlanId}`)}>{v}</a>
                            }
                            return v;
                        },
                    },
                    {
                        title: '备注说明',
                        dataIndex: 'remark',
                        width: 480,
                    },
                    // {
                    //     title: '发布状态',
                    //     dataIndex: 'status',
                    //     valueEnum: getDictValueEnum('publish_status', true),
                    //     width: 88,
                    //     align: 'center',
                    // },
                    {
                        title: '反馈时间',
                        dataIndex: 'publishTime',
                        width: 144,
                        align: 'center',
                    },
                    {
                        title: '文件列表',
                        valueType: 'option',
                        className: 'minw-80',
                        render: (_, r) => {
                            if (r.status !== PublishStatus.PUBLISHED) {
                                return null;
                            }
                            const li = r.examOrgResultList?.map((t, i) => {
                                return (
                                    <FileLink
                                        key={i}
                                        fileExtName={t.fileExtName}
                                        fileName={t.fileName}
                                        fileSize={t.fileSize}
                                        onDownload={async () => {
                                            const res = await ExamOrgResultController.download({ id: t.id });
                                            if (res) {
                                                downloadFileByBlob(res.data, res.fileName);
                                            }
                                        }}
                                    />
                                );
                            });
                            return (
                                <Space direction="vertical" style={{ width: '100%' }}>
                                    {li}
                                </Space>
                            );
                        },
                    },
                    {
                        title: '操作',
                        valueType: 'option',
                        width: 64,
                        align: 'center',
                        render: (_, r) => {
                            if (r.status !== PublishStatus.PUBLISHED) {
                                return null;
                            }
                            if (r.type === DataPublishType.QUESTIONNAIRE_PROGRESS) {
                                return (<a onClick={() => history.push(`/exam-s/questionnaire/patriarch/progress/${examPlanId}`)}>查看</a>);
                            }
                            if (r.type === DataPublishType.STUDENT_SAMPLE_LIST) {
                                return (<a onClick={() => history.push(`/exam-s/plan/sample-list/${r.id}`)}>查看</a>);
                            }
                            if (r.type === DataPublishType.STUDENT_SAMPLE_COUNT_LIST) {
                                return (<a onClick={() => history.push(`/exam-s/plan/sample-count/${r.id}`)}>查看</a>);
                            }
                            return null;
                        },
                    },
                ]}
                rowKey="id"
                request={async () => {
                    const res = await ExamOrgResultController.getByExamPlanId({ examplanid: examPlanId });
                    dataRef.current = res ?? [];
                    return {
                        data: res,
                        success: true,
                    };
                }}
            />
        </>
    );
}

export default OrgExamDataPublishList;
