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

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

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

// 更新所有证件状态
checkAndUpdateAllDocumentStatus($conn);

// 获取统计数据
$expiringCount = getExpiringDocumentsCount($conn);
$expiredCount = getExpiredDocumentsCount($conn);
$totalDocuments = getTotalDocumentsCount($conn);
$personnelCount = getPersonnelCount($conn);

// 获取即将到期的证件
$expiringDocuments = getAllExpiringDocuments($conn);
// 获取已过期的证件
$expiredDocuments = getAllExpiredDocuments($conn);
?>

<!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" class="active">仪表盘</a></li>
            <li><a href="personnel.php">人员信息管理</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="stats-container">
            <div class="stat-card primary">
                <div class="stat-title">人员总数</div>
                <div class="stat-value"><?php echo $personnelCount; ?></div>
            </div>
            <div class="stat-card success">
                <div class="stat-title">证件总数</div>
                <div class="stat-value"><?php echo $totalDocuments['total']; ?></div>
            </div>
            <div class="stat-card warning">
                <div class="stat-title">即将到期证件</div>
                <div class="stat-value"><?php echo $expiringCount['total']; ?></div>
            </div>
            <div class="stat-card danger">
                <div class="stat-title">已过期证件</div>
                <div class="stat-value"><?php echo $expiredCount['total']; ?></div>
            </div>
        </div>
        
        <!-- 即将到期证件 -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">即将到期证件</h3>
            </div>
            <div class="card-body">
                <?php if (count($expiringDocuments) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>证件类型</th>
                            <th>人员姓名</th>
                            <th>证件号码/卡号</th>
                            <th>到期日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($expiringDocuments as $doc): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($doc['type']); ?></td>
                            <td>
                                <a href="personnel_view.php?id=<?php echo $doc['personnel_id']; ?>" title="查看人员详情">
                                    <?php echo htmlspecialchars($doc['chinese_name'] ?: $doc['english_name']); ?>
                                </a>
                            </td>
                            <td>
                                <?php 
                                $idField = '';
                                switch($doc['table']) {
                                    case 'visitor_permits':
                                        echo '系统名称: ' . htmlspecialchars($doc['system_name']);
                                        $idField = 'visitor_permits';
                                        break;
                                    case 'entry_permits':
                                        echo htmlspecialchars($doc['permit_number'] ?: '未分配');
                                        $idField = 'entry_permits';
                                        break;
                                    case 'access_cards':
                                        echo htmlspecialchars($doc['card_number']);
                                        $idField = 'access_cards';
                                        break;
                                    case 'airport_passes':
                                        echo htmlspecialchars($doc['airport_number']);
                                        $idField = 'airport_passes';
                                        break;
                                    case 'restricted_area_passes':
                                        echo htmlspecialchars($doc['pass_number']);
                                        $idField = 'restricted_area_passes';
                                        break;
                                    case 'vehicle_restricted_passes':
                                        echo htmlspecialchars($doc['pass_number']) . ' / ' . htmlspecialchars($doc['vehicle_number']);
                                        $idField = 'vehicle_restricted_passes';
                                        break;
                                }
                                ?>
                            </td>
                            <td><?php echo formatDate($doc['expiry_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($doc['status']); ?>">
                                    <?php echo htmlspecialchars($doc['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="<?php echo str_replace('_passes', '', $idField); ?>.php?action=edit&id=<?php echo $doc['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>
            </div>
            <div class="card-body">
                <?php if (count($expiredDocuments) > 0): ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>证件类型</th>
                            <th>人员姓名</th>
                            <th>证件号码/卡号</th>
                            <th>到期日期</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($expiredDocuments as $doc): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($doc['type']); ?></td>
                            <td>
                                <a href="personnel_view.php?id=<?php echo $doc['personnel_id']; ?>" title="查看人员详情">
                                    <?php echo htmlspecialchars($doc['chinese_name'] ?: $doc['english_name']); ?>
                                </a>
                            </td>
                            <td>
                                <?php 
                                $idField = '';
                                switch($doc['table']) {
                                    case 'visitor_permits':
                                        echo '系统名称: ' . htmlspecialchars($doc['system_name']);
                                        $idField = 'visitor_permits';
                                        break;
                                    case 'entry_permits':
                                        echo htmlspecialchars($doc['permit_number'] ?: '未分配');
                                        $idField = 'entry_permits';
                                        break;
                                    case 'access_cards':
                                        echo htmlspecialchars($doc['card_number']);
                                        $idField = 'access_cards';
                                        break;
                                    case 'airport_passes':
                                        echo htmlspecialchars($doc['airport_number']);
                                        $idField = 'airport_passes';
                                        break;
                                    case 'restricted_area_passes':
                                        echo htmlspecialchars($doc['pass_number']);
                                        $idField = 'restricted_area_passes';
                                        break;
                                    case 'vehicle_restricted_passes':
                                        echo htmlspecialchars($doc['pass_number']) . ' / ' . htmlspecialchars($doc['vehicle_number']);
                                        $idField = 'vehicle_restricted_passes';
                                        break;
                                }
                                ?>
                            </td>
                            <td><?php echo formatDate($doc['expiry_date']); ?></td>
                            <td>
                                <span class="status-tag <?php echo getStatusClass($doc['status']); ?>">
                                    <?php echo htmlspecialchars($doc['status']); ?>
                                </span>
                            </td>
                            <td>
                                <a href="<?php echo str_replace('_passes', '', $idField); ?>.php?action=edit&id=<?php echo $doc['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>
