<template>
    <AppLayout back-gorund-color="#F4F4F4">
        <!-- 编辑头像 -->
        <view class="avatar-section btn-feel" @click="editAvatar">
            <view class="avatar-label">编辑信息</view>
            <view class="avatar-container">
                <image class="company-avatar" :src="companyInfo.avatar || '/static/imgs/avatar.jpg'"></image>
                <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
            </view>
        </view>

        <!-- 企业详细信息 -->
        <view class="info-section">
            <view class="info-item btn-feel" @click="editInfo('name')">
                <view class="info-label">企业名称</view>
                <view class="info-content">
                    <text class="info-value">{{ companyInfo.name || '暂无公司名称' }}</text>
                    <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                </view>
            </view>

            <view class="info-item btn-feel" @click="editInfo('code')">
                <view class="info-label">统一社会代码</view>
                <view class="info-content">
                    <text class="info-value">{{ companyInfo.socialCode || '暂无统一社会代码' }}</text>
                    <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                </view>
            </view>

            <view class="info-item btn-feel" @click="editInfo('location')">
                <view class="info-label">企业注册地点</view>
                <view class="info-content">
                    <text class="info-value">{{ companyInfo.location || '暂无注册地点' }}</text>
                    <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                </view>
            </view>

            <view class="info-item btn-feel" @click="editInfo('description')">
                <view class="info-label">企业信息介绍</view>
                <view class="info-content">
                    <text class="info-value">{{ companyInfo.description || '暂无企业介绍' }}</text>
                    <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                </view>
            </view>

            <view class="info-item btn-feel" @click="editInfo('legalPerson')">
                <view class="info-label">企业法人姓名</view>
                <view class="info-content">
                    <text class="info-value">{{ companyInfo.legalPerson || '暂无法人信息' }}</text>
                    <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                </view>
            </view>

            <!-- 企业联系人列表 -->
            <view v-for="(contact, index) in companyInfo.companyContactList" :key="contact.id || index" class="contact-group">
                <view class="contact-group-title">企业联系人{{ index + 1 }}</view>
                
                <view class="info-item btn-feel" @click="editContact(index, 'name')">
                    <view class="info-label">联系人姓名</view>
                    <view class="info-content">
                        <text class="info-value">{{ contact.contactPerson || '暂无联系人' }}</text>
                        <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                    </view>
                </view>

                <view class="info-item btn-feel" @click="editContact(index, 'phone')">
                    <view class="info-label">联系电话</view>
                    <view class="info-content">
                        <text class="info-value">{{ contact.contactPersonPhone || '暂无联系电话' }}</text>
                        <uni-icons color="#A2A2A2" type="right" size="16"></uni-icons>
                    </view>
                </view>
            </view>
        </view>
    </AppLayout>
</template>

<script setup>
import { reactive, inject, onMounted } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import AppLayout from '@/components/AppLayout/AppLayout.vue';

const { $api, navTo } = inject('globalFunction');

// 企业信息数据
const companyInfo = reactive({
    name: '',
    avatar: '/static/imgs/avatar.jpg',
    completeness: '65%',
    socialCode: '',
    location: '',
    description: '',
    legalPerson: '',
    companyContactList: [], // 企业联系人列表
    isVerified: false // 实名状态
});

function editAvatar() {
    // 编辑头像逻辑
    // uni.chooseImage({
    //     count: 1,
    //     success: (res) => {
    //         // 上传头像
    //         uploadAvatar(res.tempFilePaths[0]);
    //     }
    // });
}

function uploadAvatar(filePath) {
    // 上传头像到服务器
    uni.uploadFile({
        url: '/api/upload/avatar',
        filePath: filePath,
        name: 'avatar',
        success: (res) => {
            const data = JSON.parse(res.data);
            if (data.success) {
                companyInfo.avatar = data.data.url;
                uni.showToast({
                    title: '头像更新成功',
                    icon: 'success'
                });
            }
        }
    });
}

function editInfo(type) {
    // 根据类型跳转到不同的编辑页面
    const editPages = {
        name: '/pages/mine/edit-company-name',
        code: '/pages/mine/edit-company-code',
        location: '/pages/mine/edit-company-location',
        description: '/pages/mine/edit-company-description',
        legalPerson: '/pages/mine/edit-legal-person'
    };
    
    if (editPages[type]) {
        navTo(editPages[type]);
    }
}

function editContact(contactIndex, field) {
    // 编辑联系人信息
    const contact = companyInfo.companyContactList[contactIndex];
    if (!contact) return;
    
    // 跳转到联系人编辑页面，传递联系人索引和字段类型
    const editPages = {
        name: '/pages/mine/edit-contact-name',
        phone: '/pages/mine/edit-contact-phone'
    };
    
    if (editPages[field]) {
        // 传递联系人索引和联系人ID
        const params = {
            contactIndex: contactIndex,
            contactId: contact.id
        };
        navTo(editPages[field], params);
    }
}

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

// 从缓存获取公司信息
function getCompanyInfo() {
    try {
        const cachedUserInfo = uni.getStorageSync('userInfo') || {};
        console.log('缓存中的userInfo:', cachedUserInfo);
        
        // 检查是否有company字段
        if (cachedUserInfo.company) {
            const company = cachedUserInfo.company;
            
            // 基本信息
            companyInfo.name = company.name || '';
            companyInfo.socialCode = company.code || '';
            companyInfo.location = company.registeredAddress || '';
            companyInfo.description = company.description || '';
            companyInfo.legalPerson = company.legalPerson || '';
            
            // 联系人信息 - 直接使用companyContactList数组
            companyInfo.companyContactList = company.companyContactList || [];
            
            // 判断是否实名：legalIdCard字段有值则表示已实名
            companyInfo.isVerified = !!(company.legalIdCard && company.legalIdCard.trim());
            
            console.log('公司名称:', companyInfo.name);
            console.log('实名状态:', companyInfo.isVerified);
            console.log('legalIdCard值:', company.legalIdCard);
        } else {
            console.log('缓存中没有company字段');
            // 保持默认值
        }
    } catch (error) {
        console.error('获取公司信息失败:', error);
        // 保持默认值
    }
}

function goBack() {
    uni.navigateBack();
}
</script>

<style lang="stylus" scoped>

.avatar-section {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 30rpx;
    background: #FFFFFF;
    margin: 20rpx;
    border-radius: 20rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    
    .avatar-label {
        font-size: 28rpx;
        color: #333333;
    }
    
    .avatar-container {
        display: flex;
        align-items: center;
        
        .company-avatar {
            width: 80rpx;
            height: 80rpx;
            border-radius: 50%;
            margin-right: 16rpx;
        }
    }
}

.info-section {
    background: #FFFFFF;
    margin: 0 20rpx 40rpx;
    border-radius: 20rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    
    .info-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 30rpx;
        border-bottom: 1rpx solid #F5F5F5;
        
        &:last-child {
            border-bottom: none;
        }
        
        .info-label {
            font-size: 28rpx;
            color: #6C7282;
            min-width: 200rpx;
        }
        
        .info-content {
            display: flex;
            align-items: center;
            flex: 1;
            
            .info-value {
                font-size: 28rpx;
                color: #333333;
                flex: 1;
                text-align: right;
                margin-right: 16rpx;
                word-break: break-all;
            }
        }
    }
}

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

.contact-group {
    border-top: 20rpx solid #F4F4F4;
    
    .contact-group-title {
        padding: 20rpx 30rpx 10rpx;
        font-size: 24rpx;
        color: #999999;
        background: #F8F8F8;
        border-bottom: 1rpx solid #F5F5F5;
    }
}
</style>
