import { MovableModalForm } from '@/components';
import ExamPaperController from '@/services/apis/ExamPaperController';
import SysUserController from '@/services/apis/SysUserController';
import { ExamPaperWriterType } from '@/services/enums';
import { ProFormSelect } from '@ant-design/pro-components';
import { App, Typography } from 'antd';
import { useState } from 'react';

/** 分配编制人 */
const AssignWriterModal: React.FC<{
    ids: number[];
    type: ExamPaperWriterType;
    onFinish: () => void;
    onClose?: () => void;
}> = ({ ids, type, onFinish, onClose }) => {
    const [open, setOpen] = useState<boolean>(true);
    const handleClose = () => { setOpen(false); setTimeout(() => onClose?.(), 300); };

    const { message } = App.useApp();

    return (
        <MovableModalForm<{
            examPaperQuestionMajorId?: number;
            score: number;
        }>
            title={type === ExamPaperWriterType.TWCL ? '分配双向细目表编制人' : '分配问题建议撰写人'}
            width={480}
            open={open}
            modalProps={{
                centered: true,
                maskClosable: false,
                onCancel: handleClose,
            }}
            onFinish={async (values: any) => {
                try {
                    const p: API.AssignExamPaperWriterInput = {
                        ...values,
                        ids,
                    };
                    if (type === ExamPaperWriterType.TWCL) {
                        await ExamPaperController.assignTwclWriter(p);
                    }
                    else {
                        await ExamPaperController.assignSuggestionWriter(p);
                    }
                    message.success('操作成功！');
                    onFinish();
                    handleClose();
                    return true;
                } catch {
                    message.error('操作失败！');
                    return false;
                }
            }}
        >
            <ProFormSelect
                label="用户（输入关键字搜索用户）"
                name="writerSysUserId"
                showSearch
                debounceTime={200}
                fieldProps={{ filterOption: false }}
                request={async ({ keyWords }) => {
                    if (!keyWords) {
                        return [];
                    }

                    const list = await SysUserController.queryUserSimplePageList({ searchValue: keyWords, pageIndex: 1, pageSize: 20 });
                    const v = list?.items?.map(t => ({
                        value: t.id,
                        // label: t.name,
                        label: (
                            <Typography.Text>
                                {t.name} [{t.account}] -
                                <Typography.Text type="secondary">{t.sysOrg.name}</Typography.Text>
                            </Typography.Text>
                        ),
                    })) ?? [];
                    return v;
                }}
            />
        </MovableModalForm>
    );
};

export default AssignWriterModal;
