<template>
    <view class="notice-container">
        <cwg-dropdown ref="dropdownRef" :menu-list="[]">
            <view class="pc-header-btn">
                <cwg-icon name="icon_my" color="#97A1C0" @click="openNotice" />
            </view>
            <template #btn>
                <view class="right-drawer">
                    <view class="drawer-header">
                        <image class="avatar" src="/static/images/avatars.png" mode="aspectFill" />
                        <view class="user-info">
                            <text class="name">{{ _displayName }}</text>
                            <text class="cid">CID: {{ _displayCid }}</text>
                        </view>
                    </view>
                    <view class="menu-list">
                        <view v-for="item in menuList" :key="item.id" class="menu-item"
                            :class="{ active: _activePath === item.path }" @click="handleNavigate(item.path)">
                            <cwg-icon :name="item.icon" :size="16" color="#0f172b" />
                            <text v-t="item.name"></text>
                        </view>
                    </view>
                    <view class="logout-wrap">
                        <view class="logout-btn" @click="handleLogout">
                            <cwg-icon name="logout" :size="16" color="#ff9800" />
                            <text v-t="'language.i6'"></text>
                        </view>
                    </view>
                </view>
            </template>
        </cwg-dropdown>
    </view>
</template>

<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { onLoad, onShow, onLaunch } from '@dcloudio/uni-app'
import useRoute from '@/hooks/useRoute'
import useUserStore from '@/stores/use-user-store'
import { userApi } from '@/api/user'
import { useI18n } from "vue-i18n"
import useRouter from "@/hooks/useRouter"

const { t } = useI18n()
const router = useRouter()
const dropdownRef = ref(null)
const userStore = useUserStore()
const route = useRoute()

// 强制用 ref 让插槽能渲染
const menuList = ref([])
const _displayName = ref('--')
const _displayCid = ref('--')
const _activePath = ref('')

// 初始化菜单
function initMenu() {
    menuList.value = [
        { id: 1, path: '/pages/mine/info?type=1', name: 'PersonalManagement.Title.PersonalInformation', icon: 'crm-circle-user' },
        { id: 2, path: '/pages/mine/info?type=2', name: 'PersonalManagement.Title.BankInformation', icon: 'crm-building-columns' },
        { id: 3, path: '/pages/mine/info?type=3', name: 'PersonalManagement.Title.FileManagement', icon: 'crm-file' },
        { id: 4, path: '/pages/mine/info?type=4', name: 'PersonalManagement.Title.SecurityCenter', icon: 'crm-lock' },
    ]
}

// 强制同步用户信息
function syncUserInfo() {
    const info = userStore.userInfo?.customInfo || {}
    const firstName = info.firstName || ''
    const lastName = info.lastName || ''
    _displayName.value = (firstName + ' ' + lastName).trim() || info.name || info.email || '--'
    _displayCid.value = info.cId || info.id || '--'
}

// 强制同步路径
function syncPath() {
    _activePath.value = route.path + (route.query?.type ? `?type=${route.query.type}` : '')
}

onMounted(() => {
    initMenu()
    syncUserInfo()
    syncPath()
})

onShow(() => {
    initMenu()
    syncUserInfo()
    syncPath()
})

// 监听变化自动更新
watch(() => userStore.userInfo, () => {
    syncUserInfo()
}, { deep: true })

watch(() => route, () => {
    syncPath()
}, { deep: true })

// 打开抽屉
function openNotice() {
    // dropdownRef.value?.open()
}

// 关闭
function close() {
    dropdownRef.value?.close()
}

// 跳转
function handleNavigate(path) {
    router.push({ path })
    close()
}

// 登出
async function handleLogout() {
    try {
        await userApi.logout()
    } catch (e) { }
    userStore.clearUserInfo()
    router.push('/pages/login/index')
    close()
}

defineExpose({ openNotice, close })
</script>

<style scoped lang="scss">
@import "@/uni.scss";

.notice-container {
    :deep(.cwg-dropdown-menu-container) {
        left: px2rpx(-280) !important;
        right: px2rpx(0) !important;
    }

    @media screen and (max-width: 991px) {
        :deep(.cwg-dropdown-menu-container) {
            left: px2rpx(-270) !important;
            max-width: px2rpx(400);
        }
    }

    .pc-header-btn {
        position: relative;
    }

    .right-drawer {
        width: px2rpx(300);
        background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
        display: flex;
        flex-direction: column;
        padding: 20px 16px;
        box-sizing: border-box;
    }

    .drawer-header {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 20px 16px;
        border-bottom: 1px solid #d9dde5;
    }

    .avatar {
        width: 76px;
        height: 76px;
        border-radius: 12px;
        background: #fff;
    }

    .user-info {
        display: flex;
        flex-direction: column;
        gap: 6px;
    }

    .name {
        font-size: 20px;
        font-weight: 600;
        color: #334155;
    }

    .cid {
        font-size: 14px;
        color: #ef4444;
    }

    .menu-list {
        padding: 12px 0;
    }

    .menu-item {
        height: 48px;
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 0 16px;
        color: #0f172b;
        font-size: 16px;
        font-weight: 600;

        &:hover {
            background-color: rgba(0, 0, 0, 0.05);
        }
    }

    .menu-item.active {
        background: rgba(108, 133, 149, 0.12) !important;
        border-radius: 0.125rem;
    }

    .logout-wrap {
        margin-top: auto;
        padding: 20px 16px;
        margin-bottom: 20px;
    }

    .logout-btn {
        height: 44px;
        background: #f4eadf;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        color: #ff9800;
        font-weight: 600;
        cursor: pointer;
    }
}
</style>