<?php
// 开启会话
session_start();

// 引入必要的文件
require_once '../includes/config.php';
require_once '../includes/functions.php';

// 检查用户是否已登录，如果没有则重定向到登录页面
if (!isLoggedIn()) {
    redirect("../index.php");
}

// 获取即将到期的证件数量
$expiringCount = getExpiringDocumentsCount($conn);

// 检查是否提供了ID
if (!isset($_GET['id'])) {
    redirect("personnel.php");
}

$id = (int)$_GET['id'];

// 获取人员信息
$personnel = getPersonnelById($conn, $id);
if (!$personnel) {
    redirect("personnel.php");
}

// 获取人员的所有证件
$documents = getPersonnelDocuments($conn, $id);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查看人员信息 - 证件到期提醒系统</title>
    <link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
    <!-- 头部 -->
    <header class="header">
        <div class="container header-content">
            <div class="logo">证件到期提醒系统</div>
            <div class="user-info">
                <span>欢迎, <?php echo htmlspecialchars($_SESSION["admin_username"]); ?></span>
                <a href="logout.php" class="logout-btn">退出</a>
            </div>
        </div>
    </header>
    
    <!-- 侧边栏 -->
    <aside class="sidebar">
        <ul class="sidebar-menu">
            <li><a href="dashboard.php">仪表盘</a></li>
            <li><a href="personnel.php" class="active">人员信息管理</a></li>
            <li><a href="visitor_permits.php">访客许可证<?php if($expiringCount['visitor_permits'] > 0): ?><span class="badge"><?php echo $expiringCount['visitor_permits']; ?></span><?php endif; ?></a></li>
            <li><a href="entry_permits.php">进入许可证<?php if($expiringCount['entry_permits'] > 0): ?><span class="badge"><?php echo $expiringCount['entry_permits']; ?></span><?php endif; ?></a></li>
            <li><a href="access_cards.php">门禁卡<?php if($expiringCount['access_cards'] > 0): ?><span class="badge"><?php echo $expiringCount['access_cards']; ?></span><?php endif; ?></a></li>
            <li><a href="airport_passes.php">机场证<?php if($expiringCount['airport_passes'] > 0): ?><span class="badge"><?php echo $expiringCount['airport_passes']; ?></span><?php endif; ?></a></li>
            <li><a href="restricted_passes.php">禁区证<?php if($expiringCount['restricted_area_passes'] > 0): ?><span class="badge"><?php echo $expiringCount['restricted_area_passes']; ?></span><?php endif; ?></a></li>
            <li><a href="vehicle_passes.php">车辆禁区证<?php if($expiringCount['vehicle_restricted_passes'] > 0): ?><span class="badge"><?php echo $expiringCount['vehicle_restricted_passes']; ?></span><?php endif; ?></a></li>
            <li><a href="settings.php">管理员设置</a></li>
        </ul>
    </aside>
    
    <!-- 主要内容区域 -->
    <main class="main-content">
        <h1 class="page-title">查看人员信息</h1>
        
        <!-- 人员基本信息 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">基本信息</h3>
                <div>
                    <a href="personnel_form.php?id=<?php echo $id; ?>" class="btn btn-primary">编辑信息</a>
                    <a href="personnel.php" class="btn btn-secondary">返回列表</a>
                </div>
            </div>
            <div class="card-body">
                <div class="form-row">
                    <div class="form-col">
                        <?php if (!empty($personnel['photo']) && file_exists("../" . $personnel['photo'])): ?>
                        <div style="margin-bottom: 20px;">
                            <strong>照片：</strong>
                            <div class="photo-preview" style="width: 150px; height: 150px;">
                                <img src="<?php echo "../" . htmlspecialchars($personnel['photo']); ?>" alt="照片">
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <p><strong>英文姓名：</strong> <?php echo htmlspecialchars($personnel['english_name'] ?: '-'); ?></p>
                        <p><strong>中文电码：</strong> <?php echo htmlspecialchars($personnel['chinese_code'] ?: '-'); ?></p>
                        <p><strong>中文姓名：</strong> <?php echo htmlspecialchars($personnel['chinese_name'] ?: '-'); ?></p>
                        <p><strong>乳名：</strong> <?php echo htmlspecialchars($personnel['nickname'] ?: '-'); ?></p>
                        <p><strong>职位：</strong> <?php echo htmlspecialchars($personnel['position'] ?: '-'); ?></p>
                    </div>
                    <div class="form-col">
                        <p><strong>电话：</strong> <?php echo htmlspecialchars($personnel['phone'] ?: '-'); ?></p>
                        <p><strong>邮箱：</strong> <?php echo htmlspecialchars($personnel['email'] ?: '-'); ?></p>
                        <p><strong>身份证或护照：</strong> <?php echo htmlspecialchars($personnel['id_passport'] ?: '-'); ?></p>
                        <p><strong>性别：</strong> <?php echo htmlspecialchars($personnel['gender'] ?: '-'); ?></p>
                        <p><strong>雇佣类型：</strong> <?php echo htmlspecialchars($personnel['employment_type'] ?: '-'); ?></p>
                        <p><strong>公司/家：</strong> <?php echo htmlspecialchars($personnel['company_home'] ?: '-'); ?></p>
                        <p><strong>是否可以进入数据中心：</strong> <?php echo $personnel['data_center_access'] ? '是' : '否'; ?></p>
                        <p><strong>执行组：</strong> <?php echo $personnel['execution_team'] ? '是' : '否'; ?></p>
                        <p><strong>保养组：</strong> <?php echo $personnel['maintenance_team'] ? '是' : '否'; ?></p>
                        <p><strong>工作承诺书：</strong> <?php echo $personnel['work_commitment'] ? '是' : '否'; ?></p>
                        <p><strong>罪行核查：</strong> <?php echo $personnel['criminal_check'] ? '是' : '否'; ?></p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 访客许可证 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">访客许可证</h3>
                <a href="visitor_permits_form.php?personnel_id=<?php echo $id; ?>" class="btn btn-success">添加访客许可证</a>
            </div>
            <div class="card-body">
                <?php if (count($documents['visitor_permits']) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>系统名称</th>
                            <th>分判商名称</th>
                            <th>工作日期</th>
                            <th>有效期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents['visitor_permits'] as $permit): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($permit['system_name']); ?></td>
                            <td><?php echo htmlspecialchars($permit['subcontractor_name']); ?></td>
                            <td><?php echo formatDate($permit['work_date']); ?></td>
                            <td><?php echo formatDate($permit['expiry_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($permit['status']); ?>">
                                    <?php echo htmlspecialchars($permit['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="visitor_permits_form.php?id=<?php echo $permit['id']; ?>" class="btn btn-primary action-btn">编辑</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>暂无访客许可证记录</p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 进入许可证 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">进入许可证</h3>
                <a href="entry_permits_form.php?personnel_id=<?php echo $id; ?>" class="btn btn-success">添加进入许可证</a>
            </div>
            <div class="card-body">
                <?php if (count($documents['entry_permits']) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>许可证号码</th>
                            <th>已申请</th>
                            <th>已提交照片</th>
                            <th>有效期</th>
                            <th>归还日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents['entry_permits'] as $permit): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($permit['permit_number'] ?: '-'); ?></td>
                            <td><?php echo $permit['application_submitted'] ? '是' : '否'; ?></td>
                            <td><?php echo $permit['photo_submitted'] ? '是' : '否'; ?></td>
                            <td><?php echo formatDate($permit['expiry_date']); ?></td>
                            <td><?php echo formatDate($permit['return_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($permit['status']); ?>">
                                    <?php echo htmlspecialchars($permit['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="entry_permits_form.php?id=<?php echo $permit['id']; ?>" class="btn btn-primary action-btn">编辑</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>暂无进入许可证记录</p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 门禁卡 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">门禁卡</h3>
                <a href="access_cards_form.php?personnel_id=<?php echo $id; ?>" class="btn btn-success">添加门禁卡</a>
            </div>
            <div class="card-body">
                <?php if (count($documents['access_cards']) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>卡号码</th>
                            <th>卡晶片号码</th>
                            <th>发出日期</th>
                            <th>有效期</th>
                            <th>归还日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents['access_cards'] as $card): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($card['card_number']); ?></td>
                            <td><?php echo htmlspecialchars($card['chip_number'] ?: '-'); ?></td>
                            <td><?php echo formatDate($card['issue_date']); ?></td>
                            <td><?php echo formatDate($card['expiry_date']); ?></td>
                            <td><?php echo formatDate($card['return_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($card['status']); ?>">
                                    <?php echo htmlspecialchars($card['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="access_cards_form.php?id=<?php echo $card['id']; ?>" class="btn btn-primary action-btn">编辑</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>暂无门禁卡记录</p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 机场证 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">机场证</h3>
                <a href="airport_passes_form.php?personnel_id=<?php echo $id; ?>" class="btn btn-success">添加机场证</a>
            </div>
            <div class="card-body">
                <?php if (count($documents['airport_passes']) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>机场号码</th>
                            <th>证件类别</th>
                            <th>有效期</th>
                            <th>归还日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents['airport_passes'] as $pass): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($pass['airport_number']); ?></td>
                            <td><?php echo htmlspecialchars($pass['pass_type']); ?></td>
                            <td><?php echo formatDate($pass['expiry_date']); ?></td>
                            <td><?php echo formatDate($pass['return_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($pass['status']); ?>">
                                    <?php echo htmlspecialchars($pass['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="airport_passes_form.php?id=<?php echo $pass['id']; ?>" class="btn btn-primary action-btn">编辑</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>暂无机场证记录</p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 禁区证 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">禁区证</h3>
                <a href="restricted_passes_form.php?personnel_id=<?php echo $id; ?>" class="btn btn-success">添加禁区证</a>
            </div>
            <div class="card-body">
                <?php if (count($documents['restricted_area_passes']) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>禁区证号码</th>
                            <th>禁区区域</th>
                            <th>有效期</th>
                            <th>归还日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents['restricted_area_passes'] as $pass): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($pass['pass_number']); ?></td>
                            <td><?php echo htmlspecialchars($pass['restricted_area']); ?></td>
                            <td><?php echo formatDate($pass['expiry_date']); ?></td>
                            <td><?php echo formatDate($pass['return_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($pass['status']); ?>">
                                    <?php echo htmlspecialchars($pass['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="restricted_passes_form.php?id=<?php echo $pass['id']; ?>" class="btn btn-primary action-btn">编辑</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>暂无禁区证记录</p>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 车辆禁区证 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">车辆禁区证</h3>
                <a href="vehicle_passes_form.php?personnel_id=<?php echo $id; ?>" class="btn btn-success">添加车辆禁区证</a>
            </div>
            <div class="card-body">
                <?php if (count($documents['vehicle_restricted_passes']) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>禁区证号码</th>
                            <th>车辆号码</th>
                            <th>禁区区域</th>
                            <th>有效期</th>
                            <th>归还日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents['vehicle_restricted_passes'] as $pass): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($pass['pass_number']); ?></td>
                            <td><?php echo htmlspecialchars($pass['vehicle_number']); ?></td>
                            <td><?php echo htmlspecialchars($pass['restricted_area']); ?></td>
                            <td><?php echo formatDate($pass['expiry_date']); ?></td>
                            <td><?php echo formatDate($pass['return_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($pass['status']); ?>">
                                    <?php echo htmlspecialchars($pass['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="vehicle_passes_form.php?id=<?php echo $pass['id']; ?>" class="btn btn-primary action-btn">编辑</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php else: ?>
                <p>暂无车辆禁区证记录</p>
                <?php endif; ?>
            </div>
        </div>
    </main>
</body>
</html>
