import * as React from 'react';
import { useLocation } from 'react-router-dom';
import { observer } from 'mobx-react';
import './style.less';
import {
    Button, Input, Form, Divider, Splitter, Select, InputNumber, InputNumberProps,
    Radio, Switch, Row, Col, Slider, Space, RadioChangeEvent,
    Spin, message, Typography, Tooltip,
    Cascader,
    Tag, Modal, Table,TablePaginationConfig,Drawer
} from 'antd';
import type { TableProps } from 'antd';

import { PlusCircleOutlined, MinusCircleOutlined, ArrowLeftOutlined, InfoCircleOutlined,CloseCircleOutlined,LinkOutlined } from '@ant-design/icons';
import { apis } from '@/apis';
import router from '@/router';
import LocalStorage from '@/LocalStorage';
import Chat from '@/components/chat';
import store from './store';
import DrawerIndex from '@/pages/deepseek/knowledgeLib/detail/drawerIndex'

const { TextArea } = Input;
const FormItem = Form.Item;
const { Option } = Select;
const MAX_COUNT = 5;
const Index = 1;

const QuestionAnswerInfo: React.FC = () => {

    const { state,onChangePagination,onFetchUserListApi } = store;
    const { page,sourceData } = state;


    const [form] = Form.useForm();

    // top_p
    const [topPValue, setTopPValue] = React.useState(0.1);

    const TopPDecimalStep: React.FC = () => {

        const onChange: InputNumberProps['onChange'] = (value) => {
            if (Number.isNaN(value)) {
                return;
            }
            setTopPValue(value as number);
        };

        return (
            <Row>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={1}
                        onChange={onChange}
                        // value={typeof topPValue === 'number' ? topPValue : 0}
                        value={topPValue}
                        step={0.1}
                    />
                </Col>
                <Col span={4}>
                    <InputNumber
                        min={0}
                        max={1}
                        className='form-input-number-small'
                        step={0.01}
                        value={topPValue}
                        onChange={onChange}
                    />
                </Col>
            </Row>
        );
    };
    const [tempValue, setTempValue] = React.useState(0.01);
    // temperature
    const TempDecimalStep: React.FC = () => {
        const onChange: InputNumberProps['onChange'] = (value) => {
            if (Number.isNaN(value)) {
                return;
            }
            setTempValue(value as number);
        };

        return (
            <Row>
                <Col span={12}>
                    <Slider
                        min={0}
                        max={1}
                        onChange={onChange}
                        // value={typeof tempValue === 'number' ? tempValue : 0}
                        value={tempValue}
                        step={0.01}
                    />
                </Col>
                <Col span={4}>
                    <InputNumber
                        min={0}
                        max={1}
                        className='form-input-number-small'
                        step={0.01}
                        value={tempValue}
                        onChange={onChange}
                    />
                </Col>
            </Row>
        );
    };

    type ModelList = {
        label: string,
        value: string,
    }[];

    type KnowledgeList = {
        label: string,
        value: string,
        createBy: string,
    }[];

    type AppTypeList = {
        label: string,
        value: string,
        children: {
            label: string,
            value: string,
        }[],
    }[];
    const tagRender = (props:any) => {
        const { label, value, closable, onClose } = props;
        return (
            <Tag
                color="blue"
                closable={closable}
                onClose={onClose} // 保留原有的删除事件
                style={{ marginRight: 4 }}
            >
                {label}
                {/* 在删除按钮后添加自定义图标 */}
                <LinkOutlined style={{
                    marginLeft: 4,
                    fontSize: 12,
                    cursor: 'pointer',
                }}
                 onMouseDown={(e) => {
                    e.stopPropagation();
                    e.preventDefault();
                 }}
                 onMouseUp={(e) => {
                    e.stopPropagation();
                    e.preventDefault();
                 }}
                // 自定义图标点击事件
                onClick={(e) => {
                    // 阻止事件冒泡到Tag，避免触发删除
                    e.stopPropagation();
                    e.preventDefault();
                    knowledgeList.forEach((item) => {
                        if (item.value === value) {
                            // router.navigate({ pathname: `/deepseek/knowledgeLib/${value}/${item.createBy}`,},);
                            setDrawerItem(item);
                            setOpenDrawer(true)
                            e.stopPropagation();
                        }
                    });
                    // console.log('点击了额外图标，当前选项值：', value,props);
                    // 这里可以添加你的业务逻辑，如：打开详情、编辑等
                }} />
            </Tag>
        );
    };

    const [step, setStep] = React.useState(1);
    const [pageLoading, setPageLoading] = React.useState(false);
    const [modelList, setModelList] = React.useState<ModelList>([]);
    const [knowledgeList, setKnowledgeList] = React.useState<KnowledgeList>([]);
    const [isVisible, setIsVisible] = React.useState(true);
    const [isVisibleCus, setIsVisibleCus] = React.useState(false);
    const [isVisibleSlice, setIsVisibleSlice] = React.useState(false);
    const [isVisibleRerank, setIsVisibleRerank] = React.useState(true);
    const [isDeepThinkVisible, setIsDeepThinkVisible] = React.useState(true);
    const [name, setName] = React.useState('');
    const [appTypeList, setAppTypeList] = React.useState<AppTypeList>([]);
    const [appVisibleList, setAppVisibleList] = React.useState<AppTypeList>([]); // 是否公开
    const [visibleFlag, setVisibleFlag] = React.useState<string|number>(0); // 是否公开用来判断是否展示VIP用户
    const [updateFlag, setUpdateFlag] = React.useState<boolean>();
    const [createFlag, setCreateFlag] = React.useState<boolean>();
    const [appProjectList, setAppProjectList] = React.useState<AppTypeList>([]);
    const [isAppPro, setIsAppPro] = React.useState<boolean>(false);
    const [appId, setAppId] = React.useState<string>('');
    const [fetchUserTypeList, setFetchUserTypeList] = React.useState<AppTypeList>([]); // 用户类型
    const [userName, setUserName] = React.useState<string>(''); // 用户名
    const [userNickName, setUserNickName] = React.useState<string>(''); // 用户昵称
    const [userType, setUserType] = React.useState<string>(''); // 用户类型
    const [vipList, setVipList] = React.useState<any>([]); // 用户列表
    const style: React.CSSProperties = {
        display: 'flex',
        flexDirection: 'column',
        gap: 8,
        width: 300,
    };


    const location = useLocation();

    const init = async (id: string) => {
        await Promise.all([
            api.fetchKnowlegde(),
            api.fetchAppType(),
            // api.fetchModelList(),
            api.fetchAppProType(),
            api.fetchAppVisible(id)
        ])
        if (id) {
            await api.fetchDetail(id);
        }
        onFetchUserListApi('','','');
        await api.fetchUserType();
    }
    React.useEffect(() => {
        const id = location?.state?.id;
        init(id);
        const uFlag = LocalStorage.getStatusFlag('deepseek:application:update');
        setUpdateFlag(uFlag);
        const cFlag = LocalStorage.getStatusFlag('deepseek:application:create');
        setCreateFlag(cFlag);
    }, []);

    // 定义一个状态来存储输入框数组
    const [inputs, setInputs] = React.useState([{ id: 1, value: '' }]);

    // 添加新输入框的函数
    const addInput = () => {
        const newId = inputs.length + 1; // 生成新的唯一ID
        setInputs([...inputs, { id: newId, value: '' }]);
    };

    // 删除输入框（按id删除+最少数量限制）
    const delInput = (id: number) => {
        if (inputs.length <= 1) {
            message.warning("至少保留1个预设问题");
            return;
        }
        setInputs(inputs.filter(input => input.id !== id));
    };

    // 处理输入变更的函数
    const handleChange = (id: number, value: string) => {
        setInputs(inputs.map(input => (input.id === id ? { ...input, value } : input)));
    };

    const handleAppChange = (typeId: number) => {
        if (typeId === 41) { // 根据实际值进行判断
            setIsAppPro(true);
        } else {
            setIsAppPro(false);
        }
    };


    const onChangeShow = (checked: boolean) => {
        console.log(`switch to ${checked}`);
    };

    const onChangeModel = (checked: boolean) => {
        setIsVisibleRerank(!isVisibleRerank);
    };

    const onChangeCount = (value: string) => {
        if (value === 'fixed') {
            setIsVisibleSlice(!isVisibleSlice);
        } else {
            setIsVisibleSlice(false);
        }
    };

    // 召回方式
    const onChangeRecallMethod = (e: RadioChangeEvent) => {

    };

    // 获取应用详情
    const api = {
        fetchDetail: async (app_id: string) => {
            setPageLoading(true);
            try {
                const res = await apis.fetchTakaiApplicationDetail(app_id);
                const sd = res.data.questionlist.map((item: any, index: number) => {
                    return {
                        "id": index + 1,
                        "value": item.question,
                    }
                });

                const info = res.data.detail;

                setAppId(info.appId);

                setTopPValue(info.topP as number);
                setTempValue(info.temperature as number);
                setName(info.name);

                interface Item2 {
                    index_type_id: number,
                    knowledge_id: string
                }

                interface Item {
                    show_recall_result: boolean,
                    recall_method: string,
                    rerank_status: boolean,
                    slice_config_type: string,
                    slice_count: number,
                    recall_slice_splicing_method: string,
                    param_desc: string,
                    rerank_model_name: string,
                    rerank_index_type_list: [Item2],
                    recall_index_type_list: [Item2]
                }

                const data_info: Item = JSON.parse(info.knowledgeInfo);

                if (data_info.param_desc === 'custom') {

                    setIsVisibleCus(!isVisibleCus);    //自定义回答风格
                }
                if (data_info.rerank_status === true) {
                    setIsVisibleRerank(data_info.rerank_status) //模型
                }
                //召回切片数量
                if (data_info.slice_config_type === 'fixed') {
                    setIsVisibleSlice(!isVisibleSlice);
                } else {
                    setIsVisibleSlice(false);
                }

                if (info.typeId === 41) {
                    setIsAppPro(true);
                } else {
                    setIsAppPro(false);
                }

                // if (info.model === 'Qwen3-30B') {
                //     setIsDeepThinkVisible(true);
                // } else {
                    setIsDeepThinkVisible(false);
                // }

                form.setFieldsValue({
                    id: info.id,
                    name: info.name,  //应用名称
                    desc: info.desc,  //应用描述
                    prompt: info.prompt, //应用提示语
                    top_p: info.topP as number, //topP
                    temperature: info.temperature as number, //温度
                    knowledge_ids: info.knowledgeIds,
                    model: info.model,
                    // isDeepThink: info.isDeepThink,
                    isDeepThink: 'N',
                    icon_color: info.icon_color,
                    icon_type: info.icon_type,
                    questionList: sd, //问题列表
                    max_token: info.maxToken, //应用最大token
                    updateDate: info.updateDate, // 更新时间
                    appProId: info.appProId?.join('-'),// 项目
                    typeId: info.typeId, //应用类型
                    visible: info.visible || '0', //是否公开
                    sort: info.sort || null, //显示顺序
                    param_desc: data_info.param_desc, //回答风格
                    show_recall_result: data_info.show_recall_result, //是否展示召回结果
                    recall_method: data_info.recall_method, //召回方式
                    rerank_status: data_info.rerank_status, //开启rerank
                    rerank_model_name: 'rerank', //模型名称
                    slice_config_type: data_info.slice_config_type, // 召回切片数量
                    slice_count: data_info.slice_count, // 切片数量
                    recall_slice_splicing_method: data_info.recall_slice_splicing_method, // 切片内容
                    // rerank_status = 1 rerank_index_type_list
                    // recall_method = 'embedding' || 'mixed'  recall_index_type_list
                    //recall_index_type_list: info.recall_index_type_list, //知识库id
                    //rerank_index_type_list: info.rerank_index_type_list, //知识库id
                })
                setVisibleFlag(info.visible || '0')
                if(info.vipList&&info.vipList.length>0){
                    setVipList(info.vipList);
                }
                if (sd.length > 0) {
                    setInputs(sd);
                }
            } catch (error) {
                console.error(error);
            } finally {
                setPageLoading(false);
            }
        },

        //获取知识库列表
        fetchKnowlegde: async () => {
            try {
                const res = await apis.fetchTakaiKnowledgeList();
                const list = res.data.map((item: any) => {
                    return {
                        label: item.name,
                        value: item.knowledgeId,
                        createBy: item.createBy,
                    }
                });

                setKnowledgeList(list);
            } catch (error: any) {
                console.error(error);
            }
        },

        // 获取应用类型
        fetchAppType: async () => {
            try {
                const res = await apis.fetchTakaiAppTypeList('app_type');
                const list = res.data.map((item: any) => {
                    return {
                        label: item.dictLabel,
                        value: item.dictCode,
                    }
                });
                setAppTypeList(list);
            } catch (error: any) {
                console.error(error);
            }
        },
        // 获取是否公开类型
        fetchAppVisible: async (id: string) => {
            try {
                const res = await apis.fetchTakaiAppTypeList('app_visible');
                const list = res.data.map((item: any) => {
                    return {
                        label: item.dictLabel,
                        value: item.dictValue,
                    }
                });
                setAppVisibleList(list);
                if (!id) {
                    form.setFieldsValue({
                        visible: list[0]?.value
                    });
                    setVisibleFlag(list[0]?.value);
                }
            } catch (error: any) {
                console.error(error);
            }
        },
        // 获取用户类型
        fetchUserType: async () => {
            try {
                const res = await apis.fetchTakaiAppTypeList('sys_user_type');
                const list = res.data.map((item: any) => {
                    return {
                        label: item.dictLabel,
                        value: item.dictValue,
                    }
                });
                setFetchUserTypeList(list);

            } catch (error: any) {
                console.error(error);
            }
        },

        // 获取模型列表
        fetchModelList: async () => {
            try {
                const res = await apis.fetchModelList();
                const list = res.data.data.map((item: any) => {
                    return {
                        label: item.modelName,
                        value: item.modelCode,
                    }
                });
                setModelList(list);
            } catch (error: any) {
                console.error(error);
            }
        },
        // 项目级应用下的类型
        fetchAppProType: async () => {
            try {
                const res = await apis.fetchTakaiAppTypeList('projectTree');
                console.log('res.data',res.data)
                const list: AppTypeList = res.data?.reduce((acc:any,item: any) => {
                    if(item.children.length>0){
                        item.children.forEach((val:any)=>{
                            acc.push({
                                label: val.label,
                                value: `${item.value}-${val.value}`,
                            })
                        })
                    }
                    return acc;
                },[]);
                setAppProjectList(list);
            } catch (error: any) {
                console.error(error);
            }
        },
        // 获取用户列表信息
        fetchUserListApi: async () => {
            try {
                const res = await apis.fetchUserListApi({
                    pageNum:page.pageNumber,
                    pageSize: page.pageSize,
                    userName:userName,
                    nickName:userNickName,
                    userType:userType
                });
                // setSourceData(res.rows)
            } catch (error) {
                console.error(error);
            }
        }
    }

    const handleRedioClick = (value: string) => {
        setIsVisibleCus(false);
        if (value === 'strict') {
            setTopPValue(0.5);
            setTempValue(0.01);
        } else if (value === 'moderate') {
            setTopPValue(0.7);
            setTempValue(0.50);
        } else if (value === 'flexib') {
            setTopPValue(0.9);
            setTempValue(0.90);
        }
    }

    const saveConfig = (type: 'SAVE' | 'SUBMIT') => {
        form.validateFields().then(async (values) => {
            const data = values;
            console.log(values, 'values----');
            // 问题列表
            const question: string[] = [];
            if (inputs) {
                inputs.map((item, index) => {
                    question.push(item.value);
                });
            }
            console.log(question, 'question');
            interface Item {
                index_type_id: number,
                knowledge_id: string
            }
            const indexTypeList: Item[] = [];
            if (values.knowledge_ids && values.knowledge_ids.length > 0) {
                console.log("knowledge_ids", values.knowledge_ids);
                const index_type: Item = {
                    index_type_id: 0,
                    knowledge_id: values.knowledge_ids
                };
                indexTypeList.push(index_type);
            }
            const userInfo = LocalStorage.getUserInfo();
            const userId = (userInfo?.id ?? '').toString();

            const data_info = {
                param_desc: values.param_desc, //回答风格
                show_recall_result: values.show_recall_result, //是否展示召回结果
                recall_method: values.recall_method, //召回方式
                rerank_status: values.rerank_status, //开启rerank
                rerank_model_name: values.rerank_model_name, //模型名称
                slice_config_type: values.slice_config_type, // 召回切片数量
                slice_count: values.slice_count, // 切片数量
                recall_slice_splicing_method: values.recall_slice_splicing_method, // 切片内容
                rerank_index_type_list: values.rerank_status === true ? indexTypeList : [], //知识库id
                recall_index_type_list: values.recall_method === 'embedding' || 'mixed' ? indexTypeList : []
                // rerank_status = 1 rerank_index_type_list
                // recall_method = 'embedding' || 'embedding'  recall_index_type_list
            };
            // const knowledgeIds: string[] = [];
            // knowledgeIds.push(values.knowledge_ids);
            const info = {
                id: values.id,
                name: values.name,  //应用名称
                desc: values.desc,  //应用描述
                prompt: btoa(unescape(encodeURIComponent(values.prompt))), //应用提示语
                top_p: topPValue.toString(), //topP
                temperature: tempValue.toString(), //温度
                knowledge_ids: values.knowledge_ids,
                slice_count: values.slice_count,
                model: values.model,
                // isDeepThink: values.isDeepThink,
                isDeepThink: 'N',
                icon_color: values.icon_color,
                icon_type: values.icon_type,
                questionList: question,
                knowledge_info: JSON.stringify(data_info),
                max_token: values.max_token, //应用最大token
                typeId: values.typeId, // 应用类型
                visible: values.visible, // 是否公开
                sort: values.sort||null, // 显示顺序
                vipList: vipList, // vip用户列表
                appProId: Array.isArray(values?.appProId) ? values.appProId?.[0]?.split('-') : values?.appProId?.split('-')?? null, // 项目
                userId: userId, // 用户id
            };
            // console.log(info, 'info data----');
            const id = location?.state?.id;
            let res = null;
            try {
                if (id) {
                    // 编辑应用
                    res = await apis.modifyTakaiApplicationApi(id, info);
                } else {
                    // 创建应用
                    res = await apis.createTakaiApplicationApi(info);
                }
                console.log(res, 'create or modify');
                if (res.data === 9) {
                    message.error('没有配置审核人，请联系管理员');
                } else if (res.data === 1) {
                    message.success('操作成功')
                    if (type === 'SUBMIT') {
                        router.navigate({ pathname: '/deepseek/questionAnswer' });
                    }
                } else {
                    message.error('操作失败');
                }
            } catch (error: any) {
                // 检查是否是403错误
                if (error?.response?.status === 403) {
                    message.error('检测到您的此次提交请求未能通过安全验证, 请调整后重新提交。');
                } else {
                    console.error('提交失败:', error);
                    // 其他错误会由axios拦截器处理，这里不再重复显示错误信息
                }
            }
        }).catch((error) => {
            console.error(error);
            error.errorFields && error.errorFields.map((item: any) => {
                console.log(item, 'item');
                message.error(`字段 ${item.name} ${item.errors[0]}`);
            });
        });
    }
    /*
        选择VIP用户弹窗start
    */
    const [isModalOpen, setIsModalOpen] = React.useState(false);
    let falgVipList:any = [];
    const handleOk = () => {
        setIsModalOpen(false);
        let vipListFalg:any = [...vipList];
        const vipIds = new Set(vipListFalg.map((vip:any) => vip.userId));
        const merged = [...vipListFalg];

        falgVipList.forEach((item:any) => {
            if (!vipIds.has(item.userId)) {
                merged.push(item);
            }
        });
        setVipList([...merged]);
    };

    const handleCancel = async () => {
        setIsModalOpen(false);
    };
    interface DataType {
        name: string;
        key: string;
        nickName: string;
        userName: string;
        deptName: string;
        userTypeName: string;
    }
    // const [sourceData, setSourceData] = React.useState<DataType[]>([]);
    const paginationConfig: TablePaginationConfig = {
        // 显示数据总量
        showTotal: (total: number) => {
            return `共 ${total} 条`;
        },
        // 展示分页条数切换
        showSizeChanger: false,
        // 指定每页显示条数
        // 快速跳转至某页
        showQuickJumper: false,
        current: page.pageNumber,
        pageSize: page.pageSize,
        total: page.total,
        onChange: async (page, pageSize) => {
            await onChangePagination(page, pageSize,userName,userNickName,userType);
        },
    };

    const vipModal = () => {
        const columns: TableProps<DataType>['columns'] = [
            {
                title: '昵称',
                dataIndex: 'nickName',
                render: (text) => <p>{text}</p>,
            },
            {
                title: '用户名称',
                dataIndex: 'userName',
                render: (text) => <p>{text}</p>,
            },
            {
                title: '部门',
                dataIndex: 'deptName',
            },
            {
                title: '用户类型',
                dataIndex: 'userTypeName',
            },
        ];
        const rowSelection: TableProps<DataType>['rowSelection'] = {
            type: 'checkbox',
            onChange: (selectedRowKeys: React.Key[], selectedRows: DataType[]) => {
                console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
                falgVipList = selectedRows
            },
            getCheckboxProps: (record: DataType) => ({
                disabled: record.name === 'Disabled User', // Column configuration not to be checked
                name: record.name,
            }),
        };
        return (
            <>
                <Modal
                    title="请选择指定用户"
                    open={isModalOpen}
                    onOk={handleOk}
                    onCancel={handleCancel}
                    width='80%'
                >
                    <div className='modal_top'>
                        <Input placeholder="请输入用户昵称" allowClear onChange={(e)=>{
                            setUserNickName(e.target.value)
                        }} />
                        <Input placeholder="请输入用户名称" allowClear onChange={(e)=>{
                            setUserName(e.target.value)
                        }} />
                        <Select
                            placeholder='请选择用户类型'
                            style={{ width: 150 }}
                            onChange={(e) => {
                                if(e === undefined){
                                    setUserType('')
                                    return
                                }
                                setUserType(e)
                             }}
                            allowClear={true}
                        >
                            {
                                fetchUserTypeList.map((item, index) => {
                                    return <Option value={item.value} key={index}>
                                        {item.label}
                                    </Option>
                                })
                            }
                        </Select>
                        <Button value="large" style={{
                            background: 'transparent',
                            border: '1px solid #1677ff',
                            color: '#1677ff'
                        }}
                        onClick={() => { onFetchUserListApi(userName,userNickName,userType) }}
                        > 搜索 </Button>
                        {/* <Button value="large"
                            onClick={() => { api.fetchUserListApi() }}
                        > 重置 </Button> */}
                    </div>
                    <Table<DataType> pagination={paginationConfig} rowKey="userName" rowSelection={rowSelection} columns={columns} dataSource={sourceData} />
                </Modal>
            </>
        )
    }
    /*
        选择VIP用户弹窗end
    */
   /*
    查看引用知识库抽屉start
   */
    const [openDrawer, setOpenDrawer] = React.useState(false);
    const [drawerItem,setDrawerItem] = React.useState<any>({});
    const onCloseDrawer = () => {
        setOpenDrawer(false);
    }

    const DrawerDetail = ()=>{
        return (
             <Drawer
                title={drawerItem?.label}
                width={'80%'}
                closable={{ 'aria-label': 'Close Button' }}
                onClose={onCloseDrawer}
                open={openDrawer}
                style={{zIndex:11111}}
            >
                <DrawerIndex drawerItem={drawerItem}></DrawerIndex>
            </Drawer>
        )
    }

   /*
    查看引用知识库抽屉end
   */
    return (
        <>
            <div className='questionAnswerInfo'>
                <Spin spinning={pageLoading}>
                    <Form
                        form={form}
                        layout='vertical'
                        initialValues={{
                            isDeepThink: 'N',
                            max_token: 4096,
                            model:'Qwen3-30B-vl-chat',
                            show_recall_result: true,
                            rerank_model_name:'rerank',
                            slice_config_type:'customized',
                            rerank_status: true,
                            param_desc:'strict',
                            recall_method:'mixed'
                        }}
                    >
                        <div style={{ display: step === 1 ? 'block' : 'none' }} className='questionAnswerInfo-content'>
                            <FormItem
                                label='问答应用名称'
                                name='name'
                                rules={[{ required: true, message: '问答应用名称不能为空' }]}
                            >
                                <Input placeholder="请输入问答应用名称" className='form-element-standard' style={{ height: '48px'}}/>
                            </FormItem>
                            <FormItem
                                label='应用类型'
                                name='typeId'
                            >
                                <Select
                                    className='form-element-select'
                                    style={{ height: '48px'}}
                                    placeholder='请选择问答应用类型'
                                    onChange={handleAppChange}
                                    allowClear={true}
                                >
                                    {
                                        appTypeList.map((item, index) => {
                                            return <Option value={item.value} key={index}>
                                                {item.label}
                                            </Option>
                                        })
                                    }
                                </Select>
                            </FormItem>
                            {
                                isAppPro &&
                                <>
                                    <FormItem
                                        label='项目'
                                        name='appProId'
                                        rules={[{ required: true, message: '项目不能为空' }]}
                                    >
                                        <Cascader
                                            options={appProjectList}
                                            placeholder="请选择项目"
                                            showSearch
                                            className="form-element-select"
                                            style={{ height: '48px'}}
                                        />
                                    </FormItem>
                                </>
                            }
                            <FormItem
                                label='是否公开'
                                name='visible'
                            >
                                <Select
                                    className='form-element-select'
                                    style={{ height: '48px'}}
                                    placeholder='请选择是否公开'
                                    allowClear={true}
                                    onChange={(e) => {
                                        setVisibleFlag(e)
                                    }}
                                >
                                    {
                                        appVisibleList.map((item, index) => {
                                            return <Option value={item.value} key={index}>
                                                {item.label}
                                            </Option>
                                        })
                                    }
                                </Select>
                            </FormItem>
                            <FormItem
                                label='显示顺序'
                                name='sort'
                            >
                                <InputNumber placeholder="请输入显示顺序" value={''} className='form-element-standard' style={{ height: '48px',lineHeight:'48px'}}/>
                            </FormItem>
                            {/* VIP用户 */}
                            {visibleFlag==1&&<FormItem
                                label='指定用户'
                            >
                                <div className='tags-info'>
                                    <p className='tags-list'>
                                        {vipList.map((item: any) =>
                                        (<Tag key={item.userId} color="blue" closeIcon onClose={(e)=>{
                                            const newVipList = vipList.filter((vip:any) => vip.userId !== item.userId);
                                            setVipList(newVipList);
                                            e.preventDefault();
                                        }}>
                                            {item.userName}
                                        </Tag>)
                                        )}
                                    </p>
                                    <p>
                                        {vipList.length>0&&<CloseCircleOutlined className='cup' onClick={()=>{
                                            setVipList([]);
                                        }} />}
                                        <Button style={{
                                            background: 'transparent',
                                            border: '1px solid #1677ff',
                                            color: '#1677ff'
                                        }} type="primary" variant="outlined" onClick={() => { setIsModalOpen(true) }}>选择</Button>
                                    </p>
                                </div>
                            </FormItem>}
                            <FormItem
                                label='问答应用描述'
                                name='desc'
                                rules={[{ required: true, message: '问答应用描述不能为空' }]}
                            >
                                <TextArea
                                    showCount
                                    maxLength={500}
                                    placeholder="请输入当前应用的描述"
                                    className='form-textarea-large'
                                />
                            </FormItem>

                            <div className='preset-questions'>
                                <h4>添加引导问题</h4>
                                <div>
                                    {
                                        inputs.map(input => (
                                            <div key={input.id} className='question-item'>
                                                <label>引导问题 {input.id}</label>
                                                <Input
                                                    className='question-input'
                                                    type="text"
                                                    value={input.value}
                                                    onChange={e => handleChange(input.id, e.target.value)}
                                                />
                                                <div className='question-actions'>
                                                    <PlusCircleOutlined className='question-icon' onClick={addInput} />
                                                    <MinusCircleOutlined className='question-icon' onClick={() => delInput(input.id)} />
                                                </div>
                                            </div>
                                        ))}
                                </div>
                            </div>

                            <div style={{ display: 'flex', gap: '12px', marginTop: '24px', paddingTop: '24px', borderTop: '1px solid #f0f0f0' }}>
                                <Button
                                    className='btn-cancel'
                                    onClick={() => {
                                        router.navigate({ pathname: '/deepseek/questionAnswer' });
                                    }}
                                >
                                    返回
                                </Button>
                                <Button
                                    type='primary'
                                    onClick={() => {
                                        form.validateFields(['name', 'desc', 'appProId']).then(async (values) => {
                                            setStep(2);
                                            setInputs(inputs);
                                        }).catch((error) => {
                                            console.error(error);
                                        });
                                    }}
                                >
                                    下一步
                                </Button>
                            </div>
                        </div>
                        <div style={{ display: step === 2 ? 'block' : 'none' }} className='questionAnswerInfo-content'>
                            <div className='flex-between padding-bottom-16'>
                                <div>
                                    <Button
                                        className='btn-back'
                                        icon={<ArrowLeftOutlined />}
                                        onClick={() => {
                                            setStep(1);
                                        }}
                                    >
                                        上一步
                                    </Button>
                                </div>
                                <div style={{ display: 'flex', gap: '12px' }}>
                                    <Button
                                        className='btn-cancel'
                                        onClick={() => {
                                            router.navigate({ pathname: '/deepseek/questionAnswer' });
                                        }}
                                    >
                                        取消
                                    </Button>
                                    {
                                        appId && (<Button
                                            type='primary'
                                            className='btn-cancel'
                                            onClick={() => {
                                                saveConfig('SAVE');
                                            }}
                                        >
                                            保存
                                        </Button>)
                                    }

                                    {createFlag && (
                                        <Button
                                            type='primary'
                                            onClick={() => {
                                                saveConfig('SUBMIT');
                                            }}
                                        >
                                            提交应用
                                        </Button>
                                    )}
                                </div>
                            </div>
                            <div className='section-title'>
                                Prompt编写与参数配置
                                <Tooltip
                                    title="Prompt用于对大模型的回复做出一些列指令和约束。这段Prompt不会被用户看到。"
                                    placement="right"
                                    overlayStyle={{ fontSize: '12px' }}
                                >
                                    <InfoCircleOutlined style={{ marginLeft: '8px', color: '#999', fontSize: '14px' }} />
                                </Tooltip>
                            </div>
                            <Splitter style={{ border: '1px solid #f0f0f0', borderRadius: '6px', height: 550 }}>
                                <Splitter.Panel defaultSize="35%">
                                    <div className='prompt'>
                                        <div className='prompt-info'>
                                            <div className='prompt-info-text'>
                                                <Typography.Paragraph style={{ fontSize: '12px', lineHeight: '1.6', color: '#999', margin: 0 }}>
                                                    编写Prompt过程中可以引入2项变量：
                                                    <span className='variable-highlight'>{'{{知识}}'}</span>
                                                    代表知识库中检索到的知识内容，
                                                    <span className='variable-highlight'>{'{{用户}}'}</span>
                                                    代表用户输入的内容。您可以在编写Prompt过程中将变量拼接在合适的位置。
                                                    {/* <br />
                                                    插入：
                                                    <span className='variable-highlight'>{'{{知识}}'}</span>，
                                                    插入：
                                                    <span className='variable-highlight'>{'{{用户}}'}</span>。 */}
                                                </Typography.Paragraph>
                                            </div>
                                        </div>
                                        {/* 移除 Divider，使用 CSS 边框替代 */}
                                        <div className='prompt-editor-area'>
                                            <FormItem name='prompt'
                                                initialValue={
                                                    `你是一位基于DeepSeek和Qwen的预训练模型，是结合公司多种领域的专业知识训练的AI文档处理专家。你必须并且只能从我发送的众多知识片段中请进行归纳总结并生成对话式回答，并且在执行任务的过程中严格执行规定的要求。

知识片段如下:
{{知识}}

规定要求：

一、 核心回答原则
你的回答必须基于上方提供的知识片段进行归纳总结，并以对话形式呈现。
如果在知识片段中找不到答案，你可以运用自身知识回答，但必须明确告知用户“此信息未在知识库中明确记载，仅供参考”。
如果用户问题模糊，你必须根据知识片段内容，主动询问用户可能想问的具体问题。例如，用户提问“标准”，你可以回复“您是否想了解‘高处作业安全’相关的法规与标准？”

二、示意图占位符处理规则
- 触发条件与精确复制：仅当所依据的知识片段中明确包含符合 【示意图序号_xxxxxxxxxxxxxxxxxxxxx】格式的占位符时，才可在回答中引用。引用时必须将占位符及其在源片段中的直接上下文描述文字一并原样复制，严禁任何修改、概括或截断。
正确示例：若知识片段为“...施工流程如下【示意图序号_a29375108162406318082】...”，则回答中应为“...施工流程如下【示意图序号_a29375108162406318082】”。
- 严禁虚构：严禁生成任何知识片段中不存在的示意图占位符或描述性文字。如果回答内容所依据的知识片段内没有示意图占位符，则整个回答中不得出现任何形式的 【示意图序号_xxxxxxxxxxxxxxxxxxxxx】格式的占位符。
清理无关标记：从最终回答中删除所有来自知识片段的、与示意图占位符无关的图注、图表序号（如“（图1.1）”）等信息。

三、 针对URL信息来源的引用规则（修订版）
1. 引用前提：
   - 仅当知识片段中已存在原始URL链接，且回答内容直接引用该片段时，方可标注来源。
   - 若知识片段无URL，则回答中禁止出现任何形式的链接或引用标记（如[1]）。
2. 合规操作示例：
   - 知识片段包含："混凝土强度要求：... [原始链接：http://真实链接]"
   - 合规输出："混凝土强度需≥C30[1]。\\n信息来源：\\n[1] http://真实链接"
3. 违规操作示例：
   - 知识片段无链接时输出："依据《规范》[1] http://虚构链接" ❌
   - 修改原始链接格式 ❌
4. 无URL时的替代方案：
   - 若需引用无URL的知识片段，直接注明："根据知识片段中《XX规范》第X条..."

四、LaTeX公式处理规则
公式代码保护：知识片段中如出现以美元符号包裹（例如 公式或 双美元符号包裹）或其他数学标记的LaTeX公式代码，你必须将这些代码视为纯文本并完整地、一字不差地输出在你的回答中。
禁止修改：严禁对任何公式代码进行修改、转义、截断、简化或使用自然语言进行解释。你的目标是确保这些代码块在传递至前端时，能与原始知识片段中的内容完全一致。
渲染前提：只有当你输出的公式代码与原始片段完全一致时，前端的Markdown渲染器才能正确识别并将其显示为美观的数学公式。任何微小的改动都可能导致公式渲染失败。

五、 格式与内容规范
文档中的表格以图片标识符呈现，若表格数据缺失则返回空单元格。
如需使用表格数据，以markdown格式输出。
回复开头避免使用“我想”、“我认为”等词语。
回答中若出现网页链接，务必在链接后换行。
注意区分不同系统的概念，如“掌监APP”和“慧项管平台”不能混淆。
注意“模型”或“大模型”与“机器人”是不同的概念。
请严格执行以上要求，不要复述用户问题，直接开始回答。

用户输入问题：

{{用户}}`
                                                }
                                                rules={[{ required: true, message: '提示词不能为空' }]}>
                                                <TextArea
                                                    placeholder="提示词"
                                                    rows={50}
                                                />
                                            </FormItem>
                                        </div>
                                    </div>
                                </Splitter.Panel>
                                <Splitter.Panel defaultSize="30%">
                                    <div className='flex-center-container'>
                                        <div className='half-width'>
                                            <div className='flex-center-top'>
                                                欢迎使用 {name || '问答应用'}
                                            </div>
                                            <div className='flex-start pl-20 padding-top-20'>
                                                <FormItem
                                                    label='引用知识库'
                                                    name='knowledge_ids'
                                                    rules={[{ required: true, message: '知识库不能为空' }]}>
                                                    <Select
                                                        mode='multiple'
                                                        maxCount={MAX_COUNT}
                                                        showSearch={true}
                                                        className='form-element-select'
                                                        placeholder='请选择需要引用的知识库'
                                                        tagRender={tagRender}
                                                    >
                                                        {
                                                            knowledgeList.map((item, index) => {
                                                                return <Option value={item.value} key={index}>
                                                                    {item.label}
                                                                </Option>
                                                            })
                                                        }
                                                    </Select>
                                                </FormItem>
                                            </div>
                                            <div className='flex-start pl-20'>
                                                <FormItem
                                                    label='调用模型'
                                                    name="model"
                                                    rules={[{ required: true, message: '模型不能为空' }]}>
                                                    <Select
                                                        placeholder='请选择模型'
                                                        allowClear={true}
                                                        className='form-element-select'
                                                        onChange={(value) => {
                                                            // if (value === 'Qwen3-30B') {
                                                            //     setIsDeepThinkVisible(false);
                                                            // } else {
                                                                setIsDeepThinkVisible(false);
                                                            // }
                                                        }}
                                                    >
                                                        <Option value='Qwen3-30B-vl-chat'>Qwen3-30B-vl-chat</Option>
                                                        {/* <Option value='Qwen2-72B'>Qwen2-72B</Option> */}
                                                    </Select>

                                                </FormItem>
                                            </div>
                                            {/* <div className='flex-start pl-20' style={{
                                                display: isDeepThinkVisible ? 'flex' : 'none'
                                            }}>
                                                <FormItem
                                                    label='深度思考'
                                                    name='isDeepThink'
                                                    // rules={[{ required: true, message: '请选择是否深度思考' }]}
                                                    >
                                                    <Radio.Group buttonStyle="solid" className='form-element-button-group'>
                                                        <Radio.Button value='Y'>是</Radio.Button>
                                                        <Radio.Button value='N'>否</Radio.Button>
                                                    </Radio.Group>
                                                </FormItem>
                                            </div> */}
                                            <div className='flex-start pl-20'>
                                                <FormItem
                                                    label='max token'
                                                    name='max_token'
                                                    rules={[{ required: true, message: 'max token不能为空' }]}>
                                                    <InputNumber
                                                        className='form-element-input-number'
                                                    />
                                                </FormItem>
                                            </div>

                                            {
                                                !isVisible &&
                                                <div className='flex-start pl-20'>
                                                    <a onClick={() => {
                                                        setIsVisible(!isVisible);
                                                    }} className='link-more-settings'>
                                                        更多设置
                                                    </a>
                                                </div>

                                            }

                                            {/* {isVisible && */}
                                            <div style={{ display: isVisible ? 'block' : 'none', paddingTop: '20px' }}>
                                                {isVisibleCus &&
                                                    <Space style={{ width: '100%' }} direction="vertical">
                                                        <div className='flex-start pl-20'>
                                                            <FormItem
                                                                label='Top-p'
                                                                name='topP'
                                                                className='form-element-standard'
                                                            >
                                                                <TopPDecimalStep />
                                                            </FormItem>
                                                        </div>
                                                        <div className='flex-start pl-20'>
                                                            <FormItem
                                                                label='Temperature'
                                                                name='temperature'
                                                                className='form-element-standard'
                                                            >
                                                                <TempDecimalStep />
                                                            </FormItem>
                                                        </div>
                                                    </Space>
                                                }

                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <FormItem
                                                        label='回答风格'
                                                        name='param_desc'
                                                        rules={[{ required: true, message: '回答风格不能为空' }]}>
                                                        <Radio.Group buttonStyle="solid"
                                                            className='form-element-button-group'>
                                                            <Radio.Button onClick={() => {
                                                                handleRedioClick('strict')
                                                            }} value='strict'>严谨</Radio.Button>
                                                            <Radio.Button onClick={() => {
                                                                handleRedioClick('moderate')
                                                            }} value='moderate'>适中</Radio.Button>
                                                            <Radio.Button onClick={() => {
                                                                handleRedioClick('flexib')
                                                            }} value='flexib'>发散</Radio.Button>
                                                            <Radio.Button value='custom'
                                                                onClick={() => {
                                                                    setIsVisibleCus(!isVisibleCus);
                                                                    setTopPValue(0.1);
                                                                    setTempValue(0.01);
                                                                }}
                                                            >自定义
                                                            </Radio.Button>
                                                        </Radio.Group>
                                                    </FormItem>
                                                </div>
                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <FormItem
                                                        label='展示引用知识'
                                                        name='show_recall_result'
                                                        className='form-element-standard'>
                                                        <Switch onChange={onChangeShow} />
                                                    </FormItem>
                                                </div>
                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <FormItem
                                                        label='召回方式'
                                                        name='recall_method'
                                                        rules={[{ required: true, message: '召回方式不能为空' }]}>

                                                        <Radio.Group
                                                            style={style}
                                                            onChange={onChangeRecallMethod}
                                                            options={[
                                                                { value: 'embedding', label: '向量化检索' },
                                                                { value: 'keyword', label: '关键词检索' },
                                                                { value: 'mixed', label: '混合检索' },
                                                            ]}
                                                        />
                                                    </FormItem>
                                                </div>
                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <div className='section-title'>重排方式</div>
                                                </div>
                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <FormItem
                                                        label='Rerank模型'
                                                        name='rerank_status'
                                                        valuePropName='checked'
                                                        className='form-control-width'
                                                    >
                                                        <Switch onChange={onChangeModel} />
                                                    </FormItem>
                                                </div>
                                                {isVisibleRerank &&
                                                    <div style={{
                                                        display: 'flex',
                                                        justifyContent: 'flex-start',
                                                        alignItems: 'center'
                                                    }} className='pl-20'>
                                                        <FormItem
                                                            label='模型选择'
                                                            name='rerank_model_name'
                                                        >
                                                            <Select
                                                                className='questionAnswerInfo-content-title'
                                                                placeholder='请选择模型'
                                                            // defaultValue={'rerank'}
                                                            >
                                                                <Option value='rerank'>默认Rerank模型</Option>
                                                            </Select>
                                                        </FormItem>
                                                    </div>
                                                }
                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <FormItem
                                                        label='召回切片数量'
                                                        name='slice_config_type'
                                                        rules={[{ required: true, message: '召回方式不能为空' }]}>
                                                        <Select
                                                            // className='questionAnswerInfo-content-title'
                                                            className='form-element-select'
                                                            placeholder='请选择'
                                                            onChange={onChangeCount}>
                                                            <Option value="fixed">手动设置</Option>
                                                            <Option value="customized">自动设置</Option>
                                                        </Select>
                                                    </FormItem>
                                                </div>

                                                {isVisibleSlice &&
                                                    <div style={{
                                                        display: 'flex',
                                                        justifyContent: 'flex-start',
                                                        alignItems: 'center'
                                                    }} className='pl-20'>
                                                        <FormItem
                                                            label='召回切片数'
                                                            name='slice_count'
                                                            rules={[{ required: true, message: '切片数不能为空' }]}>
                                                            <InputNumber max={1024} changeOnWheel
                                                                // className='questionAnswerInfo-content-title'
                                                                className='form-element-standard'
                                                            />
                                                        </FormItem>
                                                    </div>
                                                }
                                                <div style={{
                                                    display: 'flex',
                                                    justifyContent: 'flex-start',
                                                    alignItems: 'center'
                                                }} className='pl-20'>
                                                    <FormItem
                                                        label='召回切片拼接方式'
                                                        name='recall_slice_splicing_method'
                                                    >
                                                        <TextArea
                                                            rows={4}
                                                            className='form-textarea-large'
                                                            placeholder="请输入内容"
                                                        />
                                                    </FormItem>
                                                </div>
                                            </div>
                                            {/* } */}
                                        </div>
                                    </div>
                                </Splitter.Panel>
                                {appId && (<Splitter.Panel defaultSize="35%">
                                    <Chat appId={appId} />
                                </Splitter.Panel>)}
                            </Splitter>
                        </div>
                    </Form>
                </Spin>
            </div >
            {isModalOpen && vipModal()}
            {DrawerDetail()}
        </>
    );
};

export default observer(QuestionAnswerInfo);
