<?php
// pages/roster_print.php
require_once '../config/database.php';
require_once '../includes/functions.php';
require_once '../includes/role_config.php';

$service_id = isset($_GET['service_id']) ? (int)$_GET['service_id'] : 0;

if (!$service_id) die("Service ID missing");

// Fetch Service
$stmt = $pdo->prepare("SELECT * FROM services WHERE id = ?");
$stmt->execute([$service_id]);
$service = $stmt->fetch();

if (!$service) die("Service not found");

// Setup Date Format
$dateStr = date("F d, Y", strtotime($service['service_date']));

// Fetch Roster ID
$r_stmt = $pdo->prepare("SELECT id FROM rosters WHERE service_id = ?");
$r_stmt->execute([$service_id]);
$roster_id = $r_stmt->fetchColumn();

// Fetch Assignments
$assignments = [];
if ($roster_id) {
    $a_stmt = $pdo->prepare("SELECT role_name, person_name FROM roster_assignments WHERE roster_id = ?");
    $a_stmt->execute([$roster_id]);
    while ($row = $a_stmt->fetch()) {
        $assignments[$row['role_name']] = $row['person_name'];
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WCS Assignment Schedule</title>
    <style>
        body {
            font-family: "Calibri", "Arial", sans-serif;
            color: #000;
            background: #fff;
            padding: 20px;
            max-width: 900px;
            margin: 0 auto;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #000;
            padding-bottom: 10px;
        }
        .header h1 {
            font-size: 18pt;
            margin: 0;
            text-transform: uppercase;
        }
        .header h2 {
            font-size: 14pt;
            margin: 5px 0 0 0;
        }
        .meta {
            margin-bottom: 20px;
            font-weight: bold;
            font-size: 12pt;
        }
        
        .section-title {
            background-color: #333; /* Dark header like sample? Or just bold text */
            /* User sample is simple text. Let's make it bold and distinct */
            font-size: 14pt;
            font-weight: bold;
            margin-top: 20px;
            margin-bottom: 5px;
            padding: 5px;
            border: 1px solid #000;
            background: #eee;
        }
        
        .sub-section-title {
            font-size: 12pt;
            font-weight: bold;
            margin-top: 10px;
            margin-bottom: 5px;
            text-decoration: underline;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
            font-size: 11pt;
        }
        
        th, td {
            border: 1px solid #000;
            padding: 4px 8px;
            vertical-align: top;
        }
        
        th {
            background-color: #f0f0f0;
            text-align: left;
            width: 40%;
        }
        
        /* Specific Styles to match the sample */
        .personnel {
            white-space: pre-wrap; /* Handle newlines for multiple people */
        }
    </style>
</head>
<body onload="window.print()">

    <div class="header">
        <h1>Apostolic Faith Church, WECA HQ., Anthony</h1>
        <h2>WCS Assignment Schedule (Draft 01)</h2>
    </div>

    <div class="meta">
        Date: <?php echo $dateStr; ?> | <?php echo htmlspecialchars($service['type']); ?>
    </div>

    <!-- 1. Tabernacle Operations: Audio & Video -->
    <div class="section-title">1. Tabernacle Operations: Audio & Video</div>

    <!-- Audio -->
    <div class="sub-section-title">Tabernacle Audio Operations</div>
    <table>
        <thead>
            <tr>
                <th>Assignment</th>
                <th>Assigned Personnel</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($role_categories['Tabernacle Audio Operations'] as $role): ?>
            <tr>
                <td><?php echo $role; ?></td>
                <td class="personnel"><?php echo isset($assignments[$role]) ? nl2br(htmlspecialchars($assignments[$role])) : ''; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Video -->
    <div class="sub-section-title">Tabernacle Video Operations</div>
    <table>
        <thead>
            <tr>
                <th>Assignment</th>
                <th>Assigned Personnel</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($role_categories['Tabernacle Video Operations'] as $role): ?>
            <tr>
                <td><?php echo $role; ?></td>
                <td class="personnel"><?php echo isset($assignments[$role]) ? nl2br(htmlspecialchars($assignments[$role])) : ''; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- 2. Additional Operations -->
    <div class="section-title">2. Additional Operations & Technical Support</div>
    <table>
        <thead>
            <tr>
                <th>Assignment</th>
                <th>Assigned Personnel</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($role_categories['Additional Operations & Technical Support'] as $role): ?>
            <tr>
                <td><?php echo $role; ?></td>
                <td class="personnel"><?php echo isset($assignments[$role]) ? nl2br(htmlspecialchars($assignments[$role])) : ''; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- 3. Basement and Other Centres -->
    <div class="section-title">3. Basement and Other Centres</div>
    <table>
        <thead>
            <tr>
                <th>Assignment</th>
                <th>Assigned Personnel</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($role_categories['Basement and Other Centres'] as $role): ?>
            <tr>
                <td><?php echo $role; ?></td>
                <td class="personnel"><?php echo isset($assignments[$role]) ? nl2br(htmlspecialchars($assignments[$role])) : ''; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

</body>
</html>
