<template>
    <AppLayout back-gorund-color="#F4F4F4">
        <view class="mine-content">
        <!-- 自定义tabbar -->
        <CustomTabBar :currentPage="4" />
        <!-- 企业用户信息卡片 -->
        <view v-if="userType === 0" class="company-info-card btn-feel" @click="seeDetail">
            <view class="company-avatar">
                <image class="company-avatar-img" :src="companyInfo.avatar || '/static/icon/company-default.png'"></image>
            </view>
            <view class="company-details">
                <view class="company-name">{{ companyInfo.name || '科里喀什分公司' }}</view>
                <view class="company-completeness">信息完整度 {{ companyInfo.completeness || '100%' }}</view>
            </view>
            <view class="company-arrow">
                <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
            </view>
        </view>

        <!-- 求职者用户信息卡片 -->
        <view v-else class="mine-userinfo btn-feel" @click="seeDetail">
            <view class="userindo-head">
                <image class="userindo-head-img" v-if="userInfo.sex === '0'" src="/static/icon/boy.png"></image>
                <image class="userindo-head-img" v-else src="/static/icon/girl.png"></image>
            </view>
            <view class="userinfo-ls">
                <view class="userinfo-ls-name">{{ userInfo.name || '暂无用户名' }}</view>
                <view class="userinfo-ls-resume" v-if="isAbove90(Completion)">
                    简历完成度 {{ Completion }}，建议优化
                </view>
                <view class="userinfo-ls-resume" v-else>简历完成度 {{ Completion }}</view>
            </view>
        </view>
        <view class="mini-number">
            <view class="numbe-item button-click" @click="navTo('/packageA/pages/Intendedposition/Intendedposition')">
                <view class="mini-num">{{ counts.applyCount }}</view>
                <view class="mini-text">投递</view>
            </view>
            <view class="numbe-item button-click" @click="navTo('/packageA/pages/collection/collection')">
                <view class="mini-num">{{ counts.collectionCount }}</view>
                <view class="mini-text">收藏</view>
            </view>
            <view class="numbe-item button-click" @click="navTo('/packageA/pages/browseJob/browseJob')">
                <view class="mini-num">{{ counts.jobReviewCount }}</view>
                <view class="mini-text">足迹</view>
            </view>
            <view class="numbe-item button-click" @click="navTo('/packageA/pages/reservation/reservation')">
                <view class="mini-num">{{ counts.fairCollecitonCount }}</view>
                <view class="mini-text">预约</view>
            </view>
            <view class="numbe-item button-click" @click="navTo('/packageA/pages/cancelApplication/cancelApplication')">
                <view class="mini-num">{{ counts.applyCencalCount || 0 }}</view>
                <view class="mini-text">取消投递</view>
            </view>
        </view>
        <view class="mini-cards">
            <view class="card-top btn-feel">
                <view class="top-title line_1">
                    <text>{{ userInfo.name || '暂无用户名' }}</text>
                    &nbsp;|&nbsp;
                    <dict-Label dictType="education" :value="userInfo.education"></dict-Label>
                </view>
                <view class="top-subTitle line_1">
                    <text v-for="(item, index) in userInfo.jobTitle" :key="index">
                        &nbsp;{{ item }}&nbsp;
                        <text v-if="userInfo.jobTitle.length - 1 !== index">|</text>
                    </text>
                </view>
                <view class="top-btn button-click" @click="navTo('/packageA/pages/personalInfo/personalInfo')">
                    修改简历
                </view>
            </view>
            <view class="card-main">
                <view class="main-title">服务专区</view>
                <view class="main-row btn-feel">
                    <view class="row-left">
                        <image class="left-img" src="@/static/icon/server1.png"></image>
                        <text class="left-text">实名认证</text>
                    </view>
                    <view class="row-right">已认证</view>
                </view>
                <view class="main-row btn-feel" @click="goCa()">
                    <view class="row-left">
                        <image class="left-img" src="@/static/icon/server2.png"></image>
                        <text class="left-text">素质测评</text>
                    </view>
                    <view class="row-right">
                        <uni-icons color="#909090" type="right" size="14"></uni-icons>
                    </view>
                </view>
                <view class="main-row btn-feel" @click="goCaAI()">
                    <view class="row-left">
                        <image class="left-img" src="@/static/icon/server3.png"></image>
                        <text class="left-text">AI面试</text>
                    </view>
                    <view class="row-right">
                        <uni-icons color="#909090" type="right" size="14"></uni-icons>
                    </view>
                </view>
                <view class="main-row btn-feel" @click="goToMessage()">
                    <view class="row-left">
                        <image class="left-img" src="@/static/tabbar/chat4.png"></image>
                        <text class="left-text">消息</text>
                        <text v-if="unreadNoticeCount > 0" class="unread-badge">{{ unreadNoticeCount > 99 ? '99+' : unreadNoticeCount }}</text>
                    </view>
                    <view class="row-right">
                        <uni-icons color="#909090" type="right" size="14"></uni-icons>
                    </view>
                </view>
                <view class="main-row btn-feel" @click="openReminderSettings">
                    <view class="row-left">
                        <image class="left-img" src="@/static/icon/server4.png"></image>
                        <text class="left-text">通知与提醒</text>
                    </view>
                    <view class="row-right">
                        <switch class="reminder-switch" :checked="reminderEnabled" @change="toggleReminder"></switch>
                    </view>
                </view>
                <view class="main-row btn-feel" @click="openFeedbackPopup">
                    <view class="row-left">
                        <image class="left-img" src="@/static/icon/feedback.png"></image>
                        <text class="left-text">评论与反馈</text>
                    </view>
                    <view class="row-right">
                        <uni-icons color="#909090" type="right" size="14"></uni-icons>
                    </view>
                </view>
            </view>
            <view v-if="userType === 2" class="card-help button-click" @click="goToJobHelper">求职帮</view>
             <view class="card-back button-click" @click="logOut">退出登录</view>
            <uni-popup ref="popup" type="dialog">
                <uni-popup-dialog
                    mode="base"
                    title="确定退出登录吗？"
                    type="info"
                    :duration="2000"
                    :before-close="true"
                    @confirm="confirm"
                    @close="close"
                ></uni-popup-dialog>
            </uni-popup>
            <!-- 提醒设置弹窗 -->
            <uni-popup ref="reminderPopup" type="center">
                <view class="reminder-popup">
                    <view class="reminder-popup-title">提醒设置</view>
                    <view class="reminder-popup-content">
                        <view class="reminder-item" v-for="(item, index) in reminderOptions" :key="index">
                            <view class="reminder-item-label">{{ item.label }}</view>
                            <radio :value="item.value" :checked="reminderFrequency === item.value" @change="handleFrequencyChange"></radio>
                        </view>
                    </view>
                    <view class="reminder-popup-footer">
                        <button class="reminder-popup-btn" @click="closeReminderPopup">确定</button>
                    </view>
                </view>
            </uni-popup>
            <!-- 评论与反馈弹窗 -->
            <uni-popup ref="feedbackPopup" type="center">
                <view class="feedback-popup">
                    <view class="feedback-popup-title">评论与反馈</view>
                    <view class="feedback-popup-content">
                        <textarea class="feedback-textarea" v-model="feedbackContent" placeholder="请输入您的评论或反馈..."></textarea>
                        <view class="feedback-rating">
                            <text class="feedback-rating-label">满意度评分：</text>
                            <view class="feedback-stars">
                                <text class="feedback-star" v-for="i in 5" :key="i" @click="setRating(i)" :class="{ 'active': rating >= i }">★</text>
                            </view>
                        </view>
                    </view>
                    <view class="feedback-popup-footer">
                        <button class="feedback-popup-btn" @click="submitFeedback">提交</button>
                    </view>
                </view>
            </uni-popup>
        </view>
        <template #footer>
            <!-- 统一使用系统tabBar -->
        </template>
    </view>
    </AppLayout>
</template>

<script setup>
import { reactive, inject, watch, ref, onMounted, onUnmounted, computed } from 'vue';
import { storeToRefs } from 'pinia';
import { onLoad, onShow } from '@dcloudio/uni-app';
const { $api, navTo } = inject('globalFunction');
import useUserStore from '@/stores/useUserStore';
import { tabbarManager } from '@/utils/tabbarManager';
const popup = ref(null);
const reminderPopup = ref(null);
const feedbackPopup = ref(null);
const { userInfo, Completion } = storeToRefs(useUserStore());
const counts = ref({});

// 计算未读消息数量
const unreadNoticeCount = computed(() => counts.value.unreadNoticeCount || 0);

// 提醒设置
const reminderEnabled = ref(true);
const reminderFrequency = ref('realtime');
const reminderOptions = ref([
    { label: '实时提醒', value: 'realtime' },
    { label: '每小时提醒', value: 'hourly' },
    { label: '每天提醒', value: 'daily' }
]);

// 评论与反馈
const feedbackContent = ref('');
const rating = ref(0);

// 获取用户类型，参考首页的实现方式
const userType = computed(() => {
    // 优先从store获取，如果为空则从缓存获取
    const storeIsCompanyUser = userInfo.value?.isCompanyUser;
    const cachedUserInfo = uni.getStorageSync('userInfo') || {};
    const cachedIsCompanyUser = cachedUserInfo.isCompanyUser;
    
    // 获取用户类型：优先使用store中的isCompanyUser，如果store中没有，使用缓存中的isCompanyUser
    // 缓存中的值可能是字符串，需要转换为数值类型
    return storeIsCompanyUser !== undefined ? Number(storeIsCompanyUser) : Number(cachedIsCompanyUser);
});

// 企业信息数据
const companyInfo = reactive({
    name: '科里喀什分公司',
    avatar: '/static/icon/company-avatar.png',
    completeness: '100%'
});
function logOut() {
    popup.value.open();
}
onShow(() => {
    getUserstatistics();
    // 更新自定义tabbar选中状态
    tabbarManager.updateSelected(4);
});

// 监听退出登录事件，显示微信登录弹窗
onMounted(() => {
    uni.$on('showLoginModal', () => {
        // 这里可以显示微信登录弹窗
        // 跳转到微信登录页面
        uni.navigateTo({
            url: '/packageA/pages/login/wx-login'
        });
    });
});

onUnmounted(() => {
    uni.$off('showLoginModal');
});

function close() {
    popup.value.close();
}

function confirm() {
    useUserStore().logOut(false); // 不显示登录弹窗
    // 跳转到首页
    uni.reLaunch({
        url: '/pages/index/index'
    });
}

const isAbove90 = (percent) => parseFloat(percent) < 90;

function getUserstatistics() {
    $api.createRequest('/app/user/statistics').then((resData) => {
        console.log(resData);
        counts.value = resData.data;
    });
}
function seeDetail() {
    if (userType === 0) {
        // 企业用户跳转到企业信息页面
        navTo('/packageA/pages/company-mine/company-info');
    } else {
        // 求职者用户跳转到简历页面
        navTo('/packageA/pages/myResume/myResume');
    }
}

function goToJobHelper() {
    // 跳转到求职者信息补全页面
    navTo('/packageA/pages/complete-info/complete-info');
}
// 跳转到素质测评
function goCa(){
	let userInfo = uni.getStorageSync('userInfo')
	navTo(`/packageCa/search/search?name=${userInfo.name}&userId=${userInfo.idCard}`);
}
// 跳转到AI面试
function goCaAI(){
	let userInfo = uni.getStorageSync('userInfo')
	navTo(`/packageCa/search/AIAudition?name=${userInfo.name}&userId=${userInfo.idCard}`);
}

// 跳转到消息页面
function goToMessage(){
	navTo('/pages/msglog/msglog');
}

// 切换提醒开启/关闭状态
function toggleReminder(e) {
    reminderEnabled.value = e.detail.value;
}

// 打开提醒设置弹窗
function openReminderSettings() {
    reminderPopup.value.open();
}

// 关闭提醒设置弹窗
function closeReminderPopup() {
    reminderPopup.value.close();
}

// 处理提醒频率变化
function handleFrequencyChange(e) {
    reminderFrequency.value = e.detail.value;
}

// 打开评论与反馈弹窗
function openFeedbackPopup() {
    feedbackPopup.value.open();
}

// 关闭评论与反馈弹窗
function closeFeedbackPopup() {
    feedbackPopup.value.close();
}

// 设置评分
function setRating(score) {
    rating.value = score;
}

// 提交反馈
function submitFeedback() {
    // 模拟提交成功
    uni.showToast({
        title: '反馈提交成功',
        icon: 'success'
    });
    // 清空表单
    feedbackContent.value = '';
    rating.value = 0;
    // 延迟关闭弹窗，确保用户能看到成功提示
    setTimeout(() => {
        closeFeedbackPopup();
    }, 1000);
}

</script>

<style lang="stylus" scoped>

.card-top {
  transition: transform 0.3s ease;
  transform-style: preserve-3d;
}

.card-top.tilt {
  transform: perspective(600px) rotateY(8deg) rotateX(4deg);
}
.mine-content{
    padding: 28rpx 28rpx 90rpx 28rpx;
    height: 100%;
    overflow-y: auto;
}
.mini-cards{
    padding: 28rpx
    .card-top{
        background-image: url('@/static/icon/backAI2.png'), linear-gradient(to left, #9E74FD 0%, #256BFA 63%, #4180FF 100%);
        background-repeat: no-repeat, no-repeat;
        background-position: right top, center;
        background-size: 396rpx 212rpx, cover;
        padding: 36rpx 36rpx 64rpx 36rpx
        border-radius: 20rpx 20rpx 0rpx 0rpx;
        position: relative
        font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
        .top-title{
            font-weight: 500;
            font-size: 32rpx;
            color: #FFFFFF;
            width: auto;
			max-width: 60%;
			white-space: nowrap
			overflow:visible;
        }
        .top-subTitle{
            margin-top: 8rpx;
            font-weight: 500;
            font-size: 32rpx;
            color: #FFFFFF;
            width: auto;
			max-width: 60%;
			white-space: nowrap
			overflow:hidden;
			text-overflow: ellipsis;
        }
        .top-btn{
            width: 164rpx;
            height: 64rpx;
            background: radial-gradient( 0% 24% at 57% 42%, rgba(255,255,255,0) 0%, rgba(255,255,255,0.27) 100%);
            box-shadow: 0rpx 8rpx 40rpx 0rpx rgba(210,210,210,0.14);
            border-radius: 80rpx 80rpx 80rpx 80rpx;
            border: 2rpx solid #D2D2D2;
            text-align: center
            line-height: 64rpx
            font-weight: 500;
            font-size: 28rpx;
            color: #FFFFFF;
            position: absolute
            right: 36rpx
            top: 50rpx
        }
    }
    .card-main{
        background: #FFFFFF;
        border-radius: 20rpx 20rpx 20rpx 20rpx;
        position: relative;
        top: -28rpx
        padding: 26rpx 60rpx 0 32rpx
        .main-title{
            font-weight: 600;
            font-size: 32rpx;
            color: #000000;
        }
        .main-row{
            display: flex
            align-items: center
            justify-content: space-between
            .row-left{
                display: flex
                align-items: center
                .left-img{
                    width: 44rpx;
                    height: 44rpx;
                    margin: 32rpx 16rpx 32rpx 10rpx
                }
                .left-text{
                    font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
                    font-weight: 500;
                    font-size: 28rpx;
                    color: #333333;
                }
                .unread-badge{
                    display: inline-flex;
                    align-items: center;
                    justify-content: center;
                    min-width: 32rpx;
                    height: 32rpx;
                    padding: 0 10rpx;
                    margin-left: 12rpx;
                    background: linear-gradient(135deg, #FF4757, #FF6B81);
                    border-radius: 16rpx;
                    font-size: 20rpx;
                    font-weight: 500;
                    color: #FFFFFF;
                    line-height: 32rpx;
                    text-align: center;
                    box-shadow: 0 2rpx 8rpx rgba(255, 71, 87, 0.4);
                }
            }
            .row-right{
                color: #6E6E6E;
                font-size: 28rpx;
            }
        }
        .main-row:last-child{
            margin: 0
        }
    }
    .card-help{
        height: 96rpx;
        background: #FFFFFF;
        border-radius: 20rpx 20rpx 20rpx 20rpx;
        text-align: center;
        line-height: 96rpx;
        font-size: 28rpx;
        color: #256BFA;
        margin-bottom: 20rpx;
    }
    .card-back{
        height: 96rpx;
        background: #FFFFFF;
        border-radius: 20rpx 20rpx 20rpx 20rpx;
        text-align: center;
        line-height: 96rpx
        font-size: 28rpx;
        color: #256BFA;
    }
}
.mini-number{
    padding: 24rpx
    display: flex
    justify-content: space-around
    .numbe-item{
        display: flex
        flex-direction: column
        justify-content: center
        align-items: center
        .mini-num{
            font-family: DIN-Medium;
            font-weight: 500;
            font-size: 46rpx;
            color: #333333;
        }
        .mini-text{
            font-weight: 400;
            font-size: 28rpx;
            color: #6C7282;
        }
    }
}
.mine-userinfo
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 30rpx;
    position: relative;
    .userindo-head
        width: 101rpx;
        height: 101rpx;
        overflow: hidden
        border-radius: 50%
        margin-right: 22rpx;
        .userindo-head-img
            width: 100%;
            height: 100%;
    .userinfo-ls
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        .userinfo-ls-name
            font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            font-weight: 600;
            font-size: 40rpx;
            color: #333333;
            border-radius: 50%
        .userinfo-ls-resume
            font-weight: 400;
            font-size: 28rpx;
            color: #6C7282;
.mine-userinfo::before
    position: absolute;
    right: 40rpx;
    top: 50%;
    content: '';
    width: 4rpx;
    height: 18rpx;
    border-radius: 2rpx
    background: #A2A2A2;
    transform: translate(0, -50%) rotate(-45deg);
.mine-userinfo::after
    position: absolute;
    right: 40rpx;
    top: calc(50% + 1rpx);
    content: '';
    width: 4rpx;
    height: 18rpx;
    border-radius: 2rpx
    background: #A2A2A2;
    transform: rotate(45deg)

// 企业信息卡片样式
.company-info-card {
    display: flex;
    align-items: center;
    padding: 30rpx;
    background: #FFFFFF;
    margin: 20rpx;
    border-radius: 20rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    
    .company-avatar {
        width: 100rpx;
        height: 100rpx;
        border-radius: 50%;
        overflow: hidden;
        margin-right: 24rpx;
        
        .company-avatar-img {
            width: 100%;
            height: 100%;
        }
    }
    
    .company-details {
        flex: 1;
        
        .company-name {
            font-size: 36rpx;
            font-weight: 600;
            color: #333333;
            margin-bottom: 8rpx;
        }
        
        .company-completeness {
            font-size: 28rpx;
            color: #6C7282;
        }
    }
    
    .company-arrow {
        margin-left: 20rpx;
    }
}

// 提醒设置弹窗样式
.reminder-popup {
    width: 600rpx;
    background: #FFFFFF;
    border-radius: 20rpx;
    padding: 32rpx;
    
    .reminder-popup-title {
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
        text-align: center;
        margin-bottom: 32rpx;
    }
    
    .reminder-popup-content {
        margin-bottom: 40rpx;
        
        .reminder-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 24rpx 0;
            border-bottom: 1rpx solid #F0F0F0;
            
            &:last-child {
                border-bottom: none;
            }
            
            .reminder-item-label {
                font-size: 28rpx;
                color: #333333;
            }
        }
    }
    
    .reminder-popup-footer {
        display: flex;
        justify-content: center;
        
        .reminder-popup-btn {
            width: 100%;
            height: 80rpx;
            background: #256BFA;
            color: #FFFFFF;
            font-size: 28rpx;
            font-weight: 500;
            border-radius: 10rpx;
            border: none;
        }
    }
}

// 提醒开关样式
.reminder-switch {
    transform: scale(0.8);
}

// 评论与反馈弹窗样式
.feedback-popup {
    width: 600rpx;
    background: #FFFFFF;
    border-radius: 20rpx;
    padding: 32rpx;
    
    .feedback-popup-title {
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
        text-align: center;
        margin-bottom: 32rpx;
    }
    
    .feedback-popup-content {
        margin-bottom: 40rpx;
        
        .feedback-textarea {
            width: 100%;
            height: 200rpx;
            border: 1rpx solid #E5E5E5;
            border-radius: 10rpx;
            padding: 20rpx;
            font-size: 28rpx;
            color: #333333;
            resize: none;
            margin-bottom: 32rpx;
            box-sizing: border-box;
        }
        
        .feedback-rating {
            display: flex;
            align-items: center;
            
            .feedback-rating-label {
                font-size: 28rpx;
                color: #333333;
                margin-right: 20rpx;
            }
            
            .feedback-stars {
                display: flex;
                
                .feedback-star {
                    font-size: 40rpx;
                    color: #E5E5E5;
                    margin-right: 16rpx;
                    cursor: pointer;
                    
                    &.active {
                        color: #FFD700;
                    }
                }
            }
        }
    }
    
    .feedback-popup-footer {
        display: flex;
        justify-content: center;
        
        .feedback-popup-btn {
            width: 100%;
            height: 80rpx;
            background: #256BFA;
            color: #FFFFFF;
            font-size: 28rpx;
            font-weight: 500;
            border-radius: 10rpx;
            border: none;
        }
    }
}
</style>
