import './proj-edit.css';
import {  useNavigate, useParams } from "react-router-dom";
import {  Button, message } from "antd";
import StepProjEdit from "../../components/step/StepProjEdit.tsx";
import CardProjEdit from "../../components/card/CardProjEdit.tsx";
import { Process } from "../../interfaces/step/IStepProj.ts";
import CardProjDownload from "../../components/card/CardProjDownload.tsx";
import CardProjJump from "../../components/card/CardProjJump.tsx";
import { useEffect, useState } from "react";
import { Axios, get } from "../../util/AjaxUtils.ts";
import { EditStepEnum, IProjEdit } from "../../interfaces/card/ICardProj.ts";
import { MAX_MOD_SIZE_FREE } from "./edit/ProjConfigModList.tsx";
import { GenerateStatus } from "../../interfaces/proj/IProj.ts";

export default function ProjEdit() {
    const nav = useNavigate();
    const pathParams = useParams();

    const [messageApi, contextHolder] = message.useMessage();
    const [editStepArray, setEditStepArray] = useState<IProjEdit[]>([]);
    const [configArray, setConfigArray] = useState<IProjEdit[]>([]);
    const [isEditStepEdited, setIsEditStepEdited] = useState(false);
    const [isConfigEdited, setIsConfigEdited] = useState(false);
    const [canGenerate, setCanGenerate] = useState(false);
    const [generateStatus, setGenerateStatus] = useState(GenerateStatus.NONE);
    const [previewUrl, setPreviewUrl] = useState('');

    // const height = window.innerHeight - 240;

    const renderEditStep = (data: any, isEdited: boolean, isGenerateSuccess: boolean) => {
        const editStepArray: IProjEdit[] = [];
        editStepArray.push(
            {
                title: '标题简介',
                desc: '完善代码、样式类型和详细介绍等内容',
                step: 1,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: data.projIntroduction?EditStepEnum.EDITED:EditStepEnum.UN_EDIT,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/step1/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/step1-show/${pathParams.projId}`)
                    }
                }
            },
            {
                title: '基本信息',
                desc: '完善项目简介、编程语言、版本、公司等内容',
                step: 2,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: data.apply.projDevCompleteDate?EditStepEnum.EDITED:EditStepEnum.UN_EDIT,
                canBtnClick: false,
                handleEdit() {
                    if (generateStatus != GenerateStatus.SUCCESS) {
                        nav(`/proj-edit/step2/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/step2-show/${pathParams.projId}`)
                    }
                }
            },
            {
                title: '软件功能特点',
                desc: '请完善软件功能特点',
                step: 3,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: data.apply.envHardDev?EditStepEnum.EDITED:EditStepEnum.UN_EDIT,
                canBtnClick: false,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/step3/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/step3-show/${pathParams.projId}`)
                    }
                }
            },
            {
                title: '登录界面设置',
                desc: '请对登录界面完成个性化设置',
                step: 1,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                canBtnClick: false,

                // 状态判断lyp
                status:data.loginpage.loginpageId?EditStepEnum.EDITED:EditStepEnum.UN_EDIT,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/config-loginpage/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/config-loginpage-show/${pathParams.projId}`)
                    }
                }
            },
            // {
            //     title: '著作人信息',
            //     desc: '请完善著作人相关信息',
            //     step: 4,
            //     btnName: !isGenerateSuccess ? '设置' : '查看',
            //     status: editSteps[3].editStatus,
            //     canBtnClick: false,
            //     handleEdit() {
            //         if (!isGenerateSuccess) {
            //             nav(`/proj-edit/step4/${pathParams.projId}`)
            //         } else {
            //             nav(`/proj-edit/step4-show/${pathParams.projId}`)
            //         }
            //     }
            // },
            // {
            //     title: '申请人信息',
            //     desc: '请完善申请人信息',
            //     step: 5,
            //     btnName: !isGenerateSuccess ? '设置' : '查看',
            //     status: editSteps[4].editStatus,
            //     canBtnClick: false,
            //     handleEdit() {
            //         if (!isGenerateSuccess) {
            //             nav(`/proj-edit/step5/${pathParams.projId}`)
            //         } else {
            //             nav(`/proj-edit/step5-show/${pathParams.projId}`)
            //         }
            //     }
            // },
            // {
            //     title: '选择登录页面',
            //     desc: '选择软件的登录页面模板',
            //     step: 6,
            //     btnName: !isGenerateSuccess ? '设置' : '查看',
            //     status: editSteps[5].editStatus,
            //     handleEdit() {
            //         if (!isGenerateSuccess) {
            //             nav(`/proj-edit/step6/${pathParams.projId}`)
            //         } else {
            //             nav(`/proj-edit/step6-show/${pathParams.projId}`)
            //         }
            //     }
            // }
        )
        setEditStepArray(editStepArray);
        setIsEditStepEdited(isEdited);
    }

    const renderSetting = (data: any, isConfig: boolean, isGenerateSuccess: boolean) => {
        const configArray: IProjEdit[] = [];
        configArray.push(
            // {
            //     title: '登录界面设置',
            //     desc: '请对登录界面完成个性化设置',
            //     step: 1,
            //     btnName: !isGenerateSuccess ? '设置' : '查看',
            //     status: data.loginpage.loginpageId ? EditStepEnum.EDITED : EditStepEnum.UN_EDIT,
            //     handleEdit() {
            //         if (!isGenerateSuccess) {
            //             nav(`/proj-edit/config-loginpage/${pathParams.projId}`)
            //         } else {
            //             nav(`/proj-edit/config-loginpage-show/${pathParams.projId}`)
            //         }
            //     }
            // },
            {
                title: '系统菜单管理',
                desc: '请对系统菜单进行设置',
                step: 2,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: data.projModCount > MAX_MOD_SIZE_FREE ? EditStepEnum.EDITED : EditStepEnum.UN_EDIT,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/config-mod-flist/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/config-mod-flist-show/${pathParams.projId}`)
                    }
                }
            },
            {
                title: '菜单顺序',
                desc: '调整菜单顺序',
                step: 3,
                btnName: !isGenerateSuccess ? '设置' : '查看',
                status: data.projModCount > MAX_MOD_SIZE_FREE ? EditStepEnum.EDITED : EditStepEnum.UN_EDIT,
                handleEdit() {
                    if (!isGenerateSuccess) {
                        nav(`/proj-edit/config-menu-list/${pathParams.projId}`)
                    } else {
                        nav(`/proj-edit/config-menu-list-show/${pathParams.projId}`)
                    }
                }
            }
        )
        setConfigArray(configArray);
        setIsConfigEdited(isConfig);
    }

    const renderData = () => {
        get<any>({
            messageApi: messageApi,
            url: `/api/proj/get/${pathParams.projId}`,
            onSuccess({ data }) {
                // const isEdited = data.editSteps[0].editStatus == EditStepEnum.EDITED
                //     && data.editSteps[5].editStatus == EditStepEnum.EDITED;
                // console.log(data);
                
                const isEdited = Boolean(data.projIntroduction) && Boolean(data.loginpage.loginpageId) == true
                // console.log('isEdited',isEdited);
                const isConfig = data.loginpage.loginpageId && data.projModCount > MAX_MOD_SIZE_FREE;
                const isGenerate = isEdited && isConfig;
                const isGenerateSuccess: boolean = data.generate.generateStatus == GenerateStatus.SUCCESS;
                renderEditStep(data, isEdited, isGenerateSuccess);
                renderSetting(data, isConfig, isGenerateSuccess);
                setCanGenerate(isGenerate);
                setGenerateStatus(data.generate.generateStatus);
                setPreviewUrl(data.previewUrl);
            }
        })
    }

    useEffect(() => {
        renderData();
    }, [])

    return (
        <>
            {contextHolder}
            {/* <Breadcrumb
                items={[
                    { title: <Link to={'/'}>首页</Link> },
                    { title: <Link to={'/proj-create'}>创建项目</Link> },
                    { title: '编辑项目' },
                ]}
            /> */}
            <div className="proj-edit" style={{ marginTop:'21px' }}>
                <StepProjEdit step={1} process={isEditStepEdited ? Process.COMPLETE : Process.PROCESSING}
                    descTitle="完善信息"
                    descDetail="完善项目的基本信息"
                    hasNext={true}>
                    {
                        editStepArray.map((item, index) => {
                            return <CardProjEdit key={`editStep-${index}`}
                                title={item.title}
                                desc={item.desc}
                                status={item.status}
                                btnName={item.btnName}
                                canBtnClick={item.canBtnClick}
                                handleEdit={item.handleEdit}
                                background={item.status == EditStepEnum.EDITED ? '#F5FAFE' : '#f3f3f3'}
                                shadow={item.status == EditStepEnum.EDITED ? '0px 3px 0px 0px rgba(55,144,215,0.18)' : ''}

                            />
                        })
                    }

                </StepProjEdit>
                <StepProjEdit step={2}
                    process={!isEditStepEdited ? Process.PENDING : (isConfigEdited ? Process.COMPLETE : Process.PROCESSING)}
                    descTitle="功能设置"
                    descDetail="设置系统的菜单功能" hasNext={true}>
                    {
                        configArray.map((item, index) => {
                            return <CardProjEdit key={`config-${index}`}
                                title={item.title}
                                desc={item.desc}
                                btnName={item.btnName}
                                canBtnClick={isEditStepEdited}
                                status={item.status}
                                handleEdit={item.handleEdit}
                                background={item.status == EditStepEnum.EDITED ? '#F5FEFA' : '#f3f3f3'}
                                shadow={item.status == EditStepEnum.EDITED ? ' 0px 3px 0px 0px rgba(55,215,138,0.18)' : ''}

                            />
                        })
                    }
                    {
                        isEditStepEdited && isConfigEdited ? (
                            <CardProjJump title="预览系统"
                                desc="点击查看预览系统"
                                handleJump={() => {
                                    window.open(`${Axios.defaults?.baseURL}/${previewUrl}`, '_blank')
                                }}
                            />
                        ) : <></>
                    }
                </StepProjEdit>
                <StepProjEdit step={3}
                    process={generateStatus == GenerateStatus.SUCCESS ? Process.COMPLETE : (canGenerate ? Process.PROCESSING : Process.PENDING)}
                    descTitle="资料生成"
                    hasNext={true}>
                    {
                        generateStatus == GenerateStatus.NONE || generateStatus == GenerateStatus.FAILED ? (
                            <CardProjEdit title="资料生成"
                                desc="生成软著所需要的资料，此操作后项目无法再次编辑"
                                btnName="生成"
                                status={EditStepEnum.UN_EDIT}
                                canBtnClick={false}
                                handleEdit={() => { }}
                                background='#f3f3f3'

                            />
                        ) : <></>
                    }


                </StepProjEdit>
                <StepProjEdit step={4}
                    process={generateStatus == GenerateStatus.SUCCESS ? Process.PROCESSING : Process.PENDING}
                    descTitle="资料下载">
                    <CardProjDownload title="申请表"
                        desc="点击下载申请表"
                        canBtnClick={false}
                        handleDownload={() => { }}
                    />
                    <CardProjDownload title="操作手册"
                        desc="点击下载操作手册"
                        canBtnClick={false}
                        handleDownload={() => { }}
                    />
                    <CardProjDownload title="代码压缩包"
                        desc="点击下载代码压缩包"
                        canBtnClick={false}
                        handleDownload={() => { }}
                    />
                    <CardProjDownload title="代码文档"
                        desc="点击下载代码文档"
                        canBtnClick={false}
                        handleDownload={() => { }}
                    />
                    <CardProjJump title="找代理"
                        desc="到软著代理完成软著申请"
                        canBtnClick={false}
                        handleJump={() => { }}
                    />
                </StepProjEdit>
            </div>
            <div className="btn-container">
                <Button size="large" style={{
                    width: '200px',
                    height:'40px',
                    fontSize: '16px',
                    backgroundColor: '#e9e7e7',
                    color: '#A0A0A0',
                    border:'none',
                }} onClick={() => {
                    nav(-1);
                }}>返回</Button>
            </div>
        </>
    )
}