<?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>
