import { MovableModalForm } from '@/components';
import { FormInstance, ProFormRadio, ProFormTextArea } from '@ant-design/pro-components';
import { App } from 'antd';
import { useRef, useState } from 'react';

/** 审核数据类型 */
export type AuditFormValueType = {
    isApproved: boolean;
    remark?: string;
};

/** 审核对话框属性 */
export type AuditModalProps = {
    title?: string;
    alert?: React.ReactNode;
    width?: number;
    onAudit: (values: AuditFormValueType) => Promise<boolean>;
    onClose: () => void;
};

/** 通用审核对话框 */
const AuditModal: React.FC<AuditModalProps> = ({ title, alert, width, onAudit, onClose }) => {
    const [open, setOpen] = useState(true);
    const handleClose = () => { setOpen(false); setTimeout(() => onClose?.(), 300); };

    const formRef = useRef<FormInstance>();
    const [isApproved, setIsApproved] = useState();

    const { message } = App.useApp();

    return (
        <MovableModalForm<AuditFormValueType>
            title={title ?? '审核'}
            width={width ?? 560}
            open={open}
            formRef={formRef}
            modalProps={{
                centered: true,
                maskClosable: false,
                onCancel: () => {
                    formRef?.current?.resetFields();
                    handleClose();
                },
            }}
            onFinish={async (values: any) => {
                try {
                    const res = await onAudit(values);
                    if (res) {
                        handleClose();
                        return true;
                    }
                    return false;
                } catch {
                    message.error('操作失败！');
                    return false;
                }
            }}
        >
            {alert}
            <ProFormRadio.Group
                label="审核类型"
                name="isApproved"
                options={[
                    { label: '通过', value: true },
                    { label: '驳回', value: false },
                ]}
                required
                rules={[{ required: true, message: '${label}必须选择' }]}
                fieldProps={{ onChange: (e) => { setIsApproved(e.target.value) } }}
            />
            <ProFormTextArea
                label="审核意见"
                tooltip="审核类型为【驳回】时，必须输入审核意见"
                name="remark"
                dependencies={['isApproved']}
                fieldProps={{ rows: 6 }}
                required={isApproved === false}
                rules={[
                    ({ getFieldValue }) => ({
                        validator: (_, value) => {
                            const iad = getFieldValue('isApproved');
                            if (iad === false && !value) {
                                return Promise.reject('驳回必须输入审核意见！');
                            }
                            return Promise.resolve();
                        },
                    }),
                ]}
            />
        </MovableModalForm>
    );
};

export default AuditModal;

