import './head.css'
import BalanceHead from '../../components/balance/BalanceHead.tsx';
import RechargeHead from '../../components/recharge/RechargeHead.tsx';
import {Divider, Dropdown, MenuProps, message, Modal, Space, Spin} from "antd";
import {DownOutlined, UserOutlined, KeyOutlined, LogoutOutlined} from "@ant-design/icons";
import {useContext, useEffect, useState} from "react";
import {get, put} from "../../util/AjaxUtils.ts";
import {GlobalContext, GlobalDataActionType, GlobalDispatchContext} from "../../context/GlobalContext.ts";
import UserEdit from "../../route/user/UserEdit.tsx";

export default function Head() {
    const globalContext = useContext(GlobalContext);
    const globalDispatchContext = useContext(GlobalDispatchContext);
    const [messageApi, contextHolder] = message.useMessage();
    const [modal, modalHolder] = Modal.useModal();
    const [loading, setLoading] = useState<boolean>(false);
    const [isSelfModalOpen, setIsSelfModalOpen] = useState(false);

    useEffect(() => {
        get({
            messageApi,
            url: '/api/user-info/get-user-self',
            onSuccess({data}) {
                globalDispatchContext({
                    type: GlobalDataActionType.REFRESH_SELF,
                    user: {
                        balance: (Math.floor(data.accountMoney) / 100).toFixed(2),
                        nickname: data.nickname,
                        username: data.username,
                        hasUserInfo: data.hasUserInfo,
                    }
                })
                if(!data.hasUserInfo) {
                    setIsSelfModalOpen(true);
                }
            }
        })
    }, [globalContext.user]);

    const items: MenuProps['items'] = [
        {
            key: 'userinfo',
            label: (
                <div className="dropdown-item">
                    <UserOutlined />
                    <span className="title">个人信息</span>
                </div>
            ),
            onClick: () => {
                setIsSelfModalOpen(true);
            }
        },
        {
            key: 'changePass',
            label: (
                <div className="dropdown-item">
                    <KeyOutlined />
                    <span className="title">修改密码</span>
                </div>
            ),
        },
        {
            key: 'logout',
            label: (
                <div className="dropdown-item">
                    <LogoutOutlined />
                    <span className="title">退出系统</span>
                </div>
            ),
        },
    ]

    return (
        <>
            <div className="head">
                <div className="center">
                    <div className="left">
                        <span className="sys-title">AI引擎软著</span>
                        <Divider type="vertical"/>
                        <span className="sys-title-sub">软件著作权一站式服务平台</span>
                    </div>
                    <div className="right">
                        <BalanceHead/>
                        <RechargeHead/>
                        {/*<MessageHead/>*/}
                        <div style={{display: 'flex', alignContent: 'center', padding: '0 10px', cursor: 'pointer'}}>
                            <Dropdown menu={{
                                items: items
                            }}>
                                <Space>
                                    您好：{globalContext.user.nickname}
                                    <DownOutlined/>
                                </Space>
                            </Dropdown>
                        </div>
                    </div>
                </div>
            </div>
            <Modal open={isSelfModalOpen}
                   title="个人信息"
                   footer={false}
                   onCancel={() => {
                       if(!globalContext.user.hasUserInfo) {
                           messageApi.info('请完善个人信息');
                           return;
                       }
                       setIsSelfModalOpen(false)
                   }} >
                <UserEdit handleConfirm={(data) => {
                    modal.confirm({
                        title: 'Confirm',
                        content: '确定保存吗？',
                        okText: '确认',
                        cancelText: '取消',
                        okButtonProps: {
                            style: {
                                backgroundColor: 'var(--color-primary)'
                            }
                        },
                        onOk: () => {
                            setIsSelfModalOpen(false);
                            put({
                                messageApi,
                                url: '/api/user-info/update-self',
                                body: data,
                                onBefore() {
                                    setLoading(true);
                                },
                                onSuccess() {
                                    messageApi.success('修改成功');
                                },
                                onFinally() {
                                    setLoading(false);
                                }
                            })
                            console.log(data);
                        }
                    });
                }}/>
            </Modal>
            <Spin tip="正在提交..." spinning={loading} fullscreen/>
            {contextHolder}
            {modalHolder}
        </>
    )
}
