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