/* eslint-disable @typescript-eslint/no-unused-vars */
import './card-proj.css';
import {
    CheckOutlined,
    ClockCircleOutlined,
    CloseCircleOutlined, DeleteOutlined,
    DownloadOutlined, DownOutlined,
    EditOutlined,
    EyeOutlined,
    LoadingOutlined,
    SearchOutlined,
    WarningOutlined,
    FolderOutlined
} from '@ant-design/icons';
import { Button, Dropdown, Tag, Modal, Carousel } from 'antd';
import { GenerateStatus, ProjChargeType } from "../../interfaces/proj/IProj.ts";
import { useNavigate } from "react-router-dom";
import { Axios, post, downloadUrl, get } from "../../util/AjaxUtils.ts";
import { useContext, useEffect, useState } from "react";
import { IndexListContext } from "../../context/IndexListContext.ts";
import useMessage from "antd/es/message/useMessage";
import setImg from '../../static/right/set.png'
import orderImg from '../../static/right/order.png'
import menuImg from '../../static/right/menu.png'
// import LoginPage from '../../components/LoginPage/LoginPage.tsx'
import LoginPage from '../../route/proj/edit/ProjConfigLoginpage.tsx'
// import LoginPageShow from '../../components/LoginPageShow/LoginPageShow.tsx'
import LoginPageShow from '../../route/proj/edit/ProjConfigLoginpageShow.tsx'

import SoftwareManagement from '../../route/proj/edit/ProjConfigModList.tsx'
import SoftwareManagementShow from '../../route/proj/edit/ProjConfigModListShow.tsx'
import DisplayOrder from '../../route/proj/edit/ProjConfigMenuList.tsx'
import DisplayOrderShow from '../../route/proj/edit/ProjConfigMenuListShow.tsx'
export default function CardProj(props: any) {
    // // 选项数组
    // const [tagArray, setTagArray] = useState([])
    // // 获取标签
    // const getTag = () => {
    //     get({
    //         messageApi,
    //         url: `/api/proj/tag/list-tag`,
    //         onBefore() {

    //         },
    //         onSuccess(data: any) {
    //             // console.log('标签信息', data);
    //             const newarrty: any = (data.data).map((item: any) => ({
    //                 value: item.key,
    //                 label: item.value
    //             }));
    //             // console.log('标签信息', newarrty);

    //             setTagArray(newarrty)

    //         },
    //         onFinally() {

    //         }
    //     })
    // }
    // 更新标签状态
    const upTag = (dataId:string,projId:string) => {
        post<any>({
            messageApi,
            url: `/api/proj/tag/save-or-delete`,
            body: {
                dataId,
                projId
            },
            onBefore() {
               
            },
            onSuccess() {
            //   console.log(data);
            //   props.upData
            // props.updata()
            },
            onFinally() {
              
            }
        })
    }
    // 第四步 登陆页面设置
    const [loginPageOpne, setLoginPageOpne] = useState(false)
    const [loginPageShowOpne, setLoginPageShowOpne] = useState(false)
    const nav = useNavigate();

    // 第五步 软件功能管理
    const [softwareManagementOpen, setSoftwareManagementOpen] = useState(false)
    const [softwareManagementShowOpen, setSoftwareManagementShowOpen] = useState(false)
    // const [showZpi,setShowZpi] = useState(false)
    // 第六步 显示顺序
    const [displayOrderOpen, setDisplayOrderOpen] = useState(false)
    const [displayOrderShowOpen, setDisplayOrderShowOpen] = useState(false)

    const data = props.item;
    const pkg = data.pay.chargeAdditionals.match(/PKG:(\d+)/);
    const video = data.pay.chargeAdditionals.match(/VIDEO_DEMO:(\d+)/);
    const URGENT = data.pay.chargeAdditionals.match(/URGENT:(\d+)/);
    // 基础服务费
    // const basics = data.pay.charge.match(/:(\d+)/);

    const pkgValue: number | null = pkg ? parseInt(pkg[1], 10) / 100 : null;
    const videoDemoValue: number | null = video ? parseInt(video[1], 10) / 100 : null;
    const URGENTvalue: number | null = URGENT ? parseInt(URGENT[1], 10) / 100 : null;
    // const basicsValue: number | null = basics ? parseInt(basics[1], 10)/100 : null;
    const [projId] = useState(data.projId)
    const isShow = data.pay.chargeAdditionals.includes('PKG') || data.pay.charge.includes('ALL')

    const [messageApi, messageContext] = useMessage();
    // const [projCategoryId, setProjCategoryId] = useState(data.projCategoryId);
    // const [projCategoryName, setProjCategoryName] = useState(data.projCategoryName);
    const [charge, setCharge] = useState('');
    const [payCharge, setPayCharge] = useState('');
    // const indexListContext = useContext(IndexListContext);
    /**
     * 生成状态
     */
    const renderGenerateStatus = () => {
        if (data.generate.generateStatus == GenerateStatus.PENDING) {
            return <Tag color="cyan"><ClockCircleOutlined /> 等待生成</Tag>
        }
        if (data.generate.generateStatus == GenerateStatus.GENERATING) {
            return <Tag color="magenta"><LoadingOutlined /> 正在生成</Tag>
        }
        if (data.generate.generateStatus == GenerateStatus.SUCCESS) {
            return <Tag color="green"><CheckOutlined /> 生成成功</Tag>
        }
        if (data.generate.generateStatus == GenerateStatus.FAILED) {
            return <Tag color="red"><WarningOutlined /> 生成失败</Tag>
        }
        if (data.generate.generateStatus == GenerateStatus.NONE) {
            return <Tag color="cyan">未生成</Tag>
        }
        return <Tag color="red"><CloseCircleOutlined /> 错误</Tag>
    }

    const goEdit = () => {
        if (charge == ProjChargeType.ALL) {
            nav(`/proj-eall/${data.projId}`);
        } else if (charge == ProjChargeType.FREE) {
            nav(`/proj-efree/${data.projId}`);
        } else {
            nav(`/proj-edit/${data.projId}`);
        }
    }

    useEffect(() => {
        const charge = props.item.pay.charge.split(':')[0];
        let chargeName = '';
        if (charge == ProjChargeType.ALL) {
            chargeName = '全托管';
        } else if (charge == ProjChargeType.MATERIAL_AGENT) {
            chargeName = '写材料+代理';
        } else if (charge == ProjChargeType.MATERIAL_AGENT_URGENT) {
            chargeName = '写材料+代理(加急)';
        } else if (charge == ProjChargeType.MATERIAL) {
            chargeName = '写材料';
        } else {
            chargeName = '免费试用';
        }
        setCharge(charge)
        setPayCharge(chargeName);
        // console.log('额外收费安装包',pkgValue);
        // console.log('额外收费视频',videoDemoValue);
        // console.log('额外收费加急',URGENTvalue);
        // console.log('基础服务费',basicsValue);
        // getTag()
    }, [])

    return (
        <>
            <div className="card-proj">
                <div className='cp-top'>
                    <div className='cpt-left'>
                        <div className='dottedLine'></div>
                        <div className='cpt-title' onClick={goEdit}>{data.projName}</div>
                        <div className='cp-tag' style={{ background: payCharge == '全托管' ? '#6bd2b4' : payCharge == '免费试用' ? '#ff9226' : payCharge == '写材料' ? '#5279f4' : '', color: '#fff' }}>
                            {payCharge}
                        </div>
                        <div className='cp-num'>
                            编号：{data.projContext}
                        </div>
                        <div className='cp-smallLine'>
                        </div>
                        <div className='cp-time'>
                            {data.gmtCreate}
                        </div>
                        {props.tagArray.map((item:any) => {
                            return (
                                <div className='proj-progress' key={item.value} style={{
                                    background:(data.tagDataIds).includes(item.value)?'#ff7b00':'',
                                    color:(data.tagDataIds).includes(item.value)?'#ffffff':'#5a5a5a',
                                    border:(data.tagDataIds).includes(item.value)?'none':' 1px solid #5a5a5a',
                                }}
                                onClick={()=>{
                                    upTag(item.value,data.projId)
                                    props.updata()
                                }}
                                >
                                    {item.label}
                                </div>
                                
                            )
                        })}
                        

                    </div>




                    {/* <div style={{ display: payCharge !== '全托管' ? 'block' : 'none' }}>
                        <div className='proj-progress' style={{display:data.progress?'block':'none'}}>

                            {data.progress == 'DONE' ? '已完成' : data.progress == 'SUBMIT_FOR_REVIEW' ? '已提交版权中心' : data.progress == 'PRODUCTION' ? '正在制作中' : '等待制作中'}

                        </div>
                    </div> */}
                    <div className='cpt-right'>

                        {
                            data.generate.generateStatus == GenerateStatus.SUCCESS ? (
                                <span className="cpt-text-btn">
                                    <SearchOutlined />
                                    <span className='text-btn' onClick={goEdit}>查看</span>
                                </span>
                            ) : (
                                <span className="cpt-text-btn">
                                    <EditOutlined />
                                    <span className='text-btn' onClick={goEdit}>编辑</span>
                                </span>
                            )
                        }
                        <span className='orignLine'>|</span>
                        <span className="cpt-text-btn">
                            <EyeOutlined />
                            <span className='text-btn' onClick={() => {
                                window.open(`${Axios.defaults?.baseURL}/${data.previewUrl}`, '_blank')
                            }}>预览</span>
                        </span>

                    </div>
                </div>
                <div className='cp-bot'>
                    <div className='cpb-left'>
                        {/* lyp */}
                        <div className='cpb-left-img'>
                            <Carousel autoplay dots={false}>

                                <div className='cpb-left-imgX'>
                                    <img src={downloadUrl(data.loginpage.loginpagePreviewImgs, true)} height={'143px'} alt="" />

                                </div>
                                <div className='cpb-left-imgX'>

                                    <img src={downloadUrl(data.codeTypePage.previewImgs, true)} height={'143px'} alt="" />

                                </div>

                            </Carousel>
                        </div>
                        {/* <img src={downloadUrl(data.codeTypePage.previewImgs)} className='cpb-left-img' alt="" /> */}
                        <div className='cpbl-right'>
                            <div style={{ display: 'flex' }}>
                                <div className='cpbl-money'>
                                    <span className='money'>金额(￥) : </span>
                                    <span className='num'>{data.pay.payment / 100}</span>
                                </div>
                                <div className='otherMoney'>
                                    {/* <div>基础费用:{basicsValue}</div> */}
                                    <Tag color="#f50" style={{ display: URGENTvalue ? 'block' : 'none' }}>加急:{URGENTvalue}</Tag>
                                    <Tag color="#2db7f5" style={{ display: pkgValue ? 'block' : 'none' }}>安装包:{pkgValue}</Tag>
                                    <Tag color="#87d068" style={{ display: videoDemoValue ? 'block' : 'none' }}>演示视频:{videoDemoValue}</Tag>
                                </div>
                            </div>
                            <div className='cpbl-btn'>
                                <div className='threeBtn' onClick={() => {
                                    if (data.generate.generateStatus == GenerateStatus.SUCCESS || payCharge == '全托管') {
                                        // nav(`/proj-edit/config-loginpage-show/${data.projId}`)
                                        setLoginPageShowOpne(true);

                                    } else {
                                        // nav(`/proj-edit/config-loginpage/${data.projId}`)
                                        setLoginPageOpne(true)
                                    }
                                }}>
                                    <img src={setImg} alt="" />
                                    <div>登录界面设置</div>
                                </div>
                                <div className='threeBtn' onClick={() => {
                                    if (data.generate.generateStatus == GenerateStatus.SUCCESS || payCharge == '全托管') {
                                        // nav(`/proj-edit/config-mod-list-show/${data.projId}`)
                                        setSoftwareManagementShowOpen(true)
                                    } else {
                                        setSoftwareManagementOpen(true)
                                        // nav(`/proj-edit/config-mod-list/${data.projId}`)

                                    }
                                }}>
                                    <img src={menuImg} alt="" />
                                    <div>系统菜单管理({data.projModCount})</div>
                                </div>
                                <div className='threeBtn rightBit' onClick={() => {
                                    if (data.generate.generateStatus == GenerateStatus.SUCCESS || payCharge == '全托管') {
                                        // nav(`/proj-edit/config-menu-list-show/${data.projId}`)
                                        setDisplayOrderShowOpen(true);
                                    } else {
                                        // nav(`/proj-edit/config-menu-list/${data.projId}`)
                                        setDisplayOrderOpen(true);


                                    }
                                }}>
                                    <img src={orderImg} alt="" />
                                    <div>菜单排序({data.projModCount})</div>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div className='cline'></div>
                    <div className='cpb-center'>
                        <div className='cpbc-top'>
                            {
                                data.generate.generateStatus == GenerateStatus.SUCCESS ? (
                                    <div className="option">
                                        <Button size="small" type="text" onClick={() => {
                                            window.open(`${Axios.defaults?.baseURL}/route/proj/download/apply/${data.projId}`)
                                        }}><DownloadOutlined /> 申请表</Button>
                                        <Button size="small" type="text" onClick={() => {
                                            window.open(`${Axios.defaults?.baseURL}/route/proj/download/manual/${data.projId}`)
                                        }}><DownloadOutlined /> 操作手册</Button>
                                        {
                                            isShow && <Button size="small" type="text" onClick={() => {
                                                window.open(`${Axios.defaults?.baseURL}/route/proj/download/code-zip/${data.projId}`)
                                            }}><DownloadOutlined /> 代码压缩包</Button>
                                        }
                                        {/* <Button size="small" type="text" onClick={() => {
                                            window.open(`${Axios.defaults?.baseURL}/route/proj/download/code-zip/${data.projId}`)
                                        }}><DownloadOutlined /> 代码压缩包</Button> */}
                                        <Button size="small" type="text" onClick={() => {
                                            window.open(`${Axios.defaults?.baseURL}/route/proj/download/code/${data.projId}`)
                                        }}><DownloadOutlined /> 源代码</Button>
                                    </div>
                                ) : <></>
                            }

                        </div>
                        {/* <div className='cpbc-bot'>
                            <div>
                                <FolderOutlined />
                            </div>
                            <Dropdown menu={{
                                items: indexListContext.categorys,
                                onClick: (e) => {
                                    const span = e.domEvent.target as HTMLSpanElement;
                                    put<any>({
                                        messageApi,
                                        url: `/api/proj/update-category/${data.projId}/${e.key}`,
                                        onSuccess() {
                                            messageApi.success('修改成功');
                                            setProjCategoryId(e.key);
                                            setProjCategoryName(span.innerText);
                                        }
                                    });
                                }
                            }}>
                                <span>
                                    <span className="text-btn">{projCategoryId ? projCategoryName : '无目录'}</span>
                                    <DownOutlined />
                                </span>
                            </Dropdown>
                            {
                                projCategoryId ? (
                                    <DeleteOutlined title="移除目录" onClick={() => {
                                        put<any>({
                                            messageApi,
                                            url: `/api/proj/cancel-category/${data.projId}`,
                                            onSuccess() {
                                                messageApi.success('取消成功');
                                                setProjCategoryId('');
                                                setProjCategoryName('');
                                            }
                                        });
                                    }} />
                                ) : <></>
                            }
                        </div> */}
                        <div className='cpbc-bot' style={{ color: '#FFA415' }}>
                            产权所属者:{data.apply.authorName}
                        </div>
                    </div>
                    {/* <div className='cline'></div> */}
                    <div className='cpb-right'>
                        <div className='cpbr-top'>
                            -当前状态-
                        </div>
                        <div className='cpbr-bot'>
                            {renderGenerateStatus()}
                        </div>
                    </div>
                </div>


                {/* <div className="card-proj-title">
                    <div className='titleLift'>


                        <div className="left">
                            <div className='boxline'></div>
                            <span className="text-btn" onClick={goEdit}>{data.projName}</span>
                        </div>
                        <div className="right">
                            <span className="context">编号：{data.projContext}</span>
                            <span className='line'></span>
                            <span className="date">{data.gmtCreate}</span> */}
                {/* 状态标签 */}
                {/* <span className="status">{renderGenerateStatus()}</span> */}
                {/* </div>
                    </div>
                    <div className="titleRight">
                        {
                            data.generate.generateStatus == GenerateStatus.SUCCESS ? (
                                <span>
                                    <SearchOutlined />
                                    <span className="text-btn" onClick={goEdit}>查看</span>
                                </span>
                            ) : (
                                <span>
                                    <EditOutlined />
                                    <span className="text-btn" onClick={goEdit}>编辑</span>
                                </span>
                            )
                        }
                        <span className='orignLine'>|</span>
                        <span>
                            <EyeOutlined />
                            <span className="text-btn" onClick={() => {
                                window.open(`${Axios.defaults?.baseURL}/${data.previewUrl}`, '_blank')
                            }}>预览</span>
                        </span>
                    </div>
                </div> */}
                {/* <hr/> */}
                {/* <div className="body">
                    <div className="line">
                        <div className="left"> */}
                {/* <Tag color="magenta">{payCharge}</Tag> */}
                {/* 金额 */}
                {/* <Tag color="gold">￥{data.pay.payment / 100}</Tag> */}
                {/* </div> */}
                {/* <div className='centerl'>
                            <div className='c-top'>
                                <span className='money'>金额(￥) : </span>
                                <span className='num'>{data.pay.payment / 100}</span>
                            </div>
                            <div className='c-bot'>
                                <div className='set' onClick={() => {
                                    if (data.generate.generateStatus == GenerateStatus.SUCCESS) {
                                        nav(`/proj-edit/config-loginpage-show/${data.projId}`)
                                    } else {
                                        nav(`/proj-edit/config-loginpage/${data.projId}`)
                                    }
                                }}>
                                    <img src={setImg} alt="" />
                                    <div>登录界面设置</div>
                                </div>
                                <div className='set' onClick={() => {
                                    if (data.generate.generateStatus == GenerateStatus.SUCCESS) {
                                        nav(`/proj-edit/config-mod-list-show/${data.projId}`)
                                    } else {
                                        nav(`/proj-edit/config-mod-list/${data.projId}`)
                                    }
                                }}>
                                    <img src={menuImg} alt="" />
                                    <div>系统菜单管理({data.projModCount})</div>
                                </div>
                                <div className='set' onClick={() => {
                                    if (data.generate.generateStatus == GenerateStatus.SUCCESS) {
                                        nav(`/proj-edit/config-menu-list-show/${data.projId}`)
                                    } else {
                                        nav(`/proj-edit/config-menu-list/${data.projId}`)

                                    }
                                }}>
                                    <img src={orderImg} alt="" />
                                    <div>菜单排序({data.projModCount})</div>
                                </div>
                            </div>
                        </div> */}
                {/* 目录层级结构*/}
                {/* <div className='centerr'>

                        </div>
                        <div className='right'>
                            <div className='rigth-t'>
                                - 当前状态 -
                            </div>
                            <div className="status">{renderGenerateStatus()}</div>
                        </div> */}





                {/* <div className="right">
                        {
                                data.generate.generateStatus == GenerateStatus.SUCCESS ? (
                                    <span>
                                        <SearchOutlined/>
                                        <span className="text-btn" onClick={goEdit}>查看</span>
                                    </span>
                                ) : (
                                    <span>
                                        <EditOutlined/>
                                        <span className="text-btn" onClick={goEdit}>编辑</span>
                                    </span>
                                )
                            }
                            <span>
                                <EyeOutlined/>
                                <span className="text-btn" onClick={() => {
                                    window.open(`${Axios.defaults?.baseURL}/${data.previewUrl}`, '_blank')
                                }}>预览</span>
                            </span>
                        </div> */}
                {/* </div> */}
                {/* <div className="line">
                        <div className="left">
                            {
                                data.generate.generateStatus == GenerateStatus.SUCCESS ? (
                                    <span>
                                        <SearchOutlined/>
                                        <span className="text-btn" onClick={(e) => {
                                            e.preventDefault();
                                            nav(`/agent-select/${data.projId}`);
                                        }}>找代理</span>
                                    </span>
                                ) : <></>
                            }
                        </div>
                        <div className="right">
                            <Dropdown menu={{
                                items: indexListContext.categorys,
                                onClick: (e) => {
                                    const span = e.domEvent.target as HTMLSpanElement;
                                    put<any>({
                                        messageApi,
                                        url: `/api/proj/update-category/${data.projId}/${e.key}`,
                                        onSuccess() {
                                            messageApi.success('修改成功');
                                            setProjCategoryId(e.key);
                                            setProjCategoryName(span.innerText);
                                        }
                                    });
                                }
                            }}>
                                <span>
                                    <span className="text-btn">{projCategoryId ? projCategoryName : '无目录'}</span>
                                    <DownOutlined/>
                                </span>
                            </Dropdown>
                            {
                                projCategoryId ? (
                                    <DeleteOutlined title="移除目录" onClick={() => {
                                        put<any>({
                                            messageApi,
                                            url: `/api/proj/cancel-category/${data.projId}`,
                                            onSuccess() {
                                                messageApi.success('取消成功');
                                                setProjCategoryId('');
                                                setProjCategoryName('');
                                            }
                                        });
                                    }}/>
                                ) : <></>
                            }
                        </div>
                    </div> */}
                {/* </div> */}
                {/* <hr/> */}
                {/* <div className="foot">
                    <ConfigProvider theme={{
                        components: {
                            Button: {
                                contentFontSizeSM: 12,
                            }
                        }
                    }}>
                        {renderOption()}
                    </ConfigProvider>
                </div> */}
            </div >
            {/* 第四步 设置 */}
            < Modal open={loginPageOpne}
                title="登陆页面设置"
                width={1700}
                destroyOnClose={true}

                onCancel={() => {
                    setLoginPageOpne(false);
                    // window.location.reload(); // 在取消时刷新页面
                }
                }

                footer={null}
            >
                <LoginPage projId={projId} closeModal={() => { setLoginPageOpne(false) }}></LoginPage>
            </ Modal>
            {/* 第四步 查看 */}
            < Modal open={loginPageShowOpne}
                title="登陆页面查看"
                width={1700}
                destroyOnClose={true}

                onCancel={() => {
                    setLoginPageShowOpne(false);

                }}

                footer={null}
            >
                <LoginPageShow projId={projId}></LoginPageShow>
            </Modal >
            {/* 第五步 设置 */}
            < Modal open={softwareManagementOpen}
                title="软件功能管理设置"
                width={1500}
                destroyOnClose={true}

                onCancel={() => {
                    // props.upreqData()
                    setSoftwareManagementOpen(false);
                    // props.getreqData()
                    // window.location.reload(); // 在取消时刷新页面
                }}
                footer={null}
            >
                <SoftwareManagement projId={projId}></SoftwareManagement>
            </Modal >
            {/* 第五步 查看 */}
            < Modal open={softwareManagementShowOpen}
                title="软件功能管理查看"
                width={1500}
                destroyOnClose={true}

                onCancel={() => {
                    setSoftwareManagementShowOpen(false);
                }}
                footer={null}
            >
                <SoftwareManagementShow projId={projId}></SoftwareManagementShow>
            </Modal >
            {/* 第六步 设置 */}
            < Modal open={displayOrderOpen}
                title="设置功能列表显示顺序"
                width={1500}
                destroyOnClose={true}

                onCancel={() => {
                    // props.upreqData()
                    setDisplayOrderOpen(false);
                }}
                footer={null}
            >
                <DisplayOrder projId={projId}></DisplayOrder>
            </Modal >
            {/* 第六步 查看 */}
            < Modal open={displayOrderShowOpen}
                title="查看功能列表显示顺序"
                width={1500}
                destroyOnClose={true}

                onCancel={() => {
                    setDisplayOrderShowOpen(false);
                }}
                footer={null}
            >
                <DisplayOrderShow projId={projId}></DisplayOrderShow>
            </Modal >
            {messageContext}
        </>
    )
}