<template>
    <AppLayout back-gorund-color="#F4F4F4">
        <!-- 自定义tabbar -->
        <CustomTabBar :currentPage="4" />
        
        <!-- 企业信息卡片 -->
        <view class="company-info-card btn-feel" @click="goToCompanyInfo">
            <view class="company-avatar">
                <image class="company-avatar-img" :src="companyInfo.avatar || '/static/imgs/avatar.jpg'"></image>
            </view>
            <view class="company-details">
                <view class="company-name">{{ companyInfo.name || '暂无公司名称' }}</view>
                <view class="company-completeness">
                    信息完整度 {{ companyInfo.completeness || '100%' }}
                    <text class="verification-status" :class="{ 'verified': companyInfo.isVerified, 'unverified': !companyInfo.isVerified }">
                        {{ companyInfo.isVerified ? '已实名' : '未实名' }}
                    </text>
                </view>
            </view>
            <view class="company-arrow">
                <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
            </view>
        </view>

        <!-- 服务专区 -->
        <view class="service-zone-card">
            <view class="service-title">服务专区</view>
            <view class="service-item btn-feel">
                <view class="service-left">
                    <uni-icons type="contact" size="20" color="#256BFA"></uni-icons>
                    <text class="service-text">实名认证</text>
                </view>
                <view class="service-status" :class="{ 'verified': companyInfo.isVerified, 'unverified': !companyInfo.isVerified }">
                    {{ companyInfo.isVerified ? '已通过' : '未认证' }}
                </view>
            </view>
            <view class="service-item btn-feel" @click="goToMessage">
                <view class="service-left">
                    <uni-icons type="chat" size="20" color="#256BFA"></uni-icons>
                    <text class="service-text">消息</text>
                </view>
                <view class="service-status">
                    <uni-icons type="right" size="14" color="#909090"></uni-icons>
                </view>
            </view>
            <view class="service-item btn-feel" @click="openReminderSettings">
                <view class="service-left">
                    <uni-icons type="notification" size="20" color="#256BFA"></uni-icons>
                    <text class="service-text">通知与提醒</text>
                </view>
                <view class="service-status">
                    <switch class="reminder-switch" :checked="reminderEnabled" @change="toggleReminder"></switch>
                </view>
            </view>
            <view class="service-item btn-feel" @click="openFeedbackPopup">
                <view class="service-left">
                    <uni-icons type="chat" size="20" color="#256BFA"></uni-icons>
                    <text class="service-text">评论与反馈</text>
                </view>
                <view class="service-status">
                    <uni-icons type="right" size="14" color="#909090"></uni-icons>
                </view>
            </view>
			<view class="service-item btn-feel" @click="handleInstitutionClick(1)">
			    <view class="service-left">
			        <uni-icons type="compose" size="20" color="#256BFA"></uni-icons>
			        <text class="service-text">培训机构维护</text>
			    </view>
			    <view class="service-status">修改</view>
			</view>
			<view class="service-item btn-feel" @click="handleInstitutionClick(2)">
			    <view class="service-left">
			        <uni-icons type="compose" size="20" color="#256BFA"></uni-icons>
			        <text class="service-text">评价机构维护</text>
			    </view>
			    <view class="service-status">修改</view>
			</view>
			<view class="service-item btn-feel" @click="handleInstitutionClick(3)">
			    <view class="service-left">
			        <uni-icons type="paperplane" size="20" color="#256BFA"></uni-icons>
			        <text class="service-text">培训公告发布</text>
			    </view>
			    <view class="service-status">发布</view>
			</view>
			<view class="service-item btn-feel" @click="handleInstitutionClick(4)">
			    <view class="service-left">
			        <uni-icons type="paperplane" size="20" color="#256BFA"></uni-icons>
			        <text class="service-text">评价公告发布</text>
			    </view>
			    <view class="service-status">发布</view>
			</view>
        </view>

        <!-- 退出登录按钮 -->
        <view class="logout-btn btn-feel" @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>
    </AppLayout>
</template>

<script setup>
import { reactive, inject, ref, onMounted, onUnmounted } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import useUserStore from '@/stores/useUserStore';

const { $api, navTo } = inject('globalFunction');
const popup = ref(null);
const reminderPopup = ref(null);
const feedbackPopup = ref(null);

// 提醒设置
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 companyInfo = reactive({
    name: '',
    avatar: '/static/imgs/avatar.jpg',
    completeness: '100%',
    isVerified: false // 实名状态
});

function goToCompanyInfo() {
    navTo('/packageA/pages/company-mine/company-info');
}
function handleInstitutionClick(i){
	if(i==1){
		navTo('/packageB/institution/trainingInstitutionMaintenance');
	}else if(i==2){
		navTo('/packageB/institution/evaluationAgencyMaintenance');
	}else if(i==3){
		navTo('/packageB/notice/trainingAnnouncement/postedList');
	}else if(i==4){
		navTo('/packageB/notice/evaluateAnnouncement/evaluateList');
	}
}
// 跳转到消息页面
function goToMessage() {
    navTo('/pages/msglog/msglog');
}

function logOut() {
    popup.value.open();
}

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

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

onShow(() => {
    // 获取企业信息
    getCompanyInfo();
});

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

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

// 从缓存获取公司信息
function getCompanyInfo() {
    try {
        const cachedUserInfo = uni.getStorageSync('userInfo') || {};
        // console.log('缓存中的userInfo:', cachedUserInfo);
        
        // 检查是否有company字段
        if (cachedUserInfo.company) {
            companyInfo.name = cachedUserInfo.company.name || '';
            // 判断是否实名：legalIdCard字段有值则表示已实名
            companyInfo.isVerified = !!(cachedUserInfo.company.legalIdCard && cachedUserInfo.company.legalIdCard.trim());
            console.log('公司名称:', companyInfo.name);
            console.log('实名状态:', companyInfo.isVerified);
            console.log('legalIdCard值:', cachedUserInfo.company.legalIdCard);
        } else {
            console.log('缓存中没有company字段');
            companyInfo.name = '';
            companyInfo.isVerified = false;
        }
    } catch (error) {
        console.error('获取公司信息失败:', error);
        companyInfo.name = '';
        companyInfo.isVerified = false;
    }
}

// 切换提醒开启/关闭状态
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>
.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;
            display: flex;
            align-items: center;
            gap: 16rpx;
            
            .verification-status {
                font-size: 24rpx;
                padding: 4rpx 12rpx;
                border-radius: 12rpx;
                
                &.verified {
                    background-color: #E8F5E8;
                    color: #52C41A;
                }
                
                &.unverified {
                    background-color: #FFF2E8;
                    color: #FA8C16;
                }
            }
        }
    }
    
    .company-arrow {
        margin-left: 20rpx;
    }
}

.service-zone-card {
    background: #FFFFFF;
    margin: 0 20rpx 20rpx;
    border-radius: 20rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    padding: 32rpx;
    
    .service-title {
        font-size: 32rpx;
        font-weight: 600;
        color: #000000;
        margin-bottom: 32rpx;
    }
    
    .service-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 24rpx 0;
        border-bottom: 1rpx solid #F5F5F5;
        
        &:last-child {
            border-bottom: none;
        }
        
        .service-left {
            display: flex;
            align-items: center;
            
            .service-text {
                font-size: 28rpx;
                color: #333333;
                margin-left: 16rpx;
            }
        }
        
        .service-status {
            font-size: 28rpx;
            color: #6E6E6E;
            
            &.verified {
                color: #52C41A;
            }
            
            &.unverified {
                color: #FA8C16;
            }
        }
    }
}

.logout-btn {
    height: 96rpx;
    background: #FFFFFF;
    margin: 0 20rpx 40rpx;
    border-radius: 20rpx;
    text-align: center;
    line-height: 96rpx;
    font-size: 28rpx;
    color: #256BFA;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.btn-feel {
    transition: transform 0.2s ease;
    
    &:active {
        transform: scale(0.98);
    }
}

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

// 提醒设置弹窗样式
.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;
        }
    }
}

// 评论与反馈弹窗样式
.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>
