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} from "./edit/ProjConfigModList.tsx";
import {GenerateStatus} from "../../interfaces/proj/IProj.ts";

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

    const [messageApi, contextHolder] = message.useMessage();
    const [editStepArray, setEditStepArray] = useState<IProjEdit[]>([]);
    const [isEditStepEdited, setIsEditStepEdited] = useState(false);
    const [isConfigEdited, setIsConfigEdited] = 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: 4,
            //     btnName: !isGenerateSuccess ? '设置' : '查看',
            //     status: editSteps[3].editStatus,
            //     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,
            //     handleEdit() {
            //         if (!isGenerateSuccess) {
            //             nav(`/proj-edit/step5/${pathParams.projId}`)
            //         } else {
            //             nav(`/proj-edit/step5-show/${pathParams.projId}`)
            //         }
            //     }
            // },
        )
        setEditStepArray(editStepArray);
        setIsEditStepEdited(isEdited);
    }

    const renderData = () => {
        get<any>({
            messageApi: messageApi,
            url: `/api/proj/get/${pathParams.projId}`,
            onSuccess({data}) {
                const isEdited = Boolean(data.projIntroduction) == true  && Boolean(data.apply.projDevCompleteDate) == true && Boolean(data.apply.envHardDev) == true && Boolean(data.loginpage.loginpageId) == true
                const isConfig = data.loginpage.loginpageId && MAX_MOD_SIZE > 0;
                const isGenerateSuccess: boolean = data.generate.generateStatus == GenerateStatus.SUCCESS;
                renderEditStep(data, isEdited, isGenerateSuccess);
                setIsConfigEdited(isConfig);
                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', padding: '15px 175px'}}>
                <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}
                                                 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}>
                    {
                        <CardProjJump title="预览系统"
                                      desc={isEditStepEdited && isConfigEdited ? "点击查看预览系统" : "制作中"}
                                      canBtnClick={isEditStepEdited && isConfigEdited}
                                      handleJump={() => {
                                          window.open(`${Axios.defaults?.baseURL}/${previewUrl}`, '_blank')
                                      }}
                        />
                    }
                </StepProjEdit>
                <StepProjEdit step={3}
                              process={generateStatus == GenerateStatus.SUCCESS ? Process.PROCESSING : Process.PENDING}
                              descTitle="资料下载">
                    <CardProjDownload title="申请表"
                                      desc="点击下载申请表"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/apply/${pathParams.projId}`)
                                      }}
                    />
                    <CardProjDownload title="操作手册"
                                      desc="点击下载操作手册"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/manual/${pathParams.projId}`)
                                      }}
                    />
                    <CardProjDownload title="代码压缩包"
                                      desc="点击下载代码压缩包"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/code-zip/${pathParams.projId}`)
                                      }}
                    />
                    <CardProjDownload title="代码文档"
                                      desc="点击下载代码文档"
                                      canBtnClick={generateStatus == GenerateStatus.SUCCESS}
                                      handleDownload={() => {
                                          window.open(`${Axios.defaults?.baseURL}/route/proj/download/code/${pathParams.projId}`)
                                      }}
                    />
                </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>
        </>
    )
}