<?php
// View/HTML rendering for plan responses
?>

<div class="plan-responses-container">
    <div class="page-header">
        <h1>Plan Responses</h1>
        <p>View active plans, plages, and participant responses</p>
    </div>
    
    <?php if (empty($activePlans)): ?>
        <?php renderEmptyState(); ?>
    <?php else: ?>
        <?php foreach ($activePlans as $plan): ?>
            <?php renderPlanCard($plan); ?>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<!-- Toast Notification -->
<div id="toast" class="toast">
    <span id="toast-icon" class="material-symbols-outlined"></span>
    <span id="toast-message"></span>
</div>

<link rel="stylesheet" href="assets/css/planresponse.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />

<script src="assets/js/planresponse.js"></script>

<?php
/**
 * Render empty state when no plans are available
 */
function renderEmptyState() {
    
?>
    <div class="empty-state">
        <div class="empty-icon">
            <span class="material-symbols-outlined">event_note</span>
        </div>
        <h3>No Active Plans</h3>
        <p>There are currently no active plans with responses to display.</p>
        <a href="index.php?page=plans" class="btn">
            <span class="material-symbols-outlined btn-icon">add</span>
            Manage Plans
        </a>
    </div>
    <?php
}

/**
 * Render a plan card with all its plages
 */
function renderPlanCard($plan) {
    
?>
    <div class="plan-card">
        <div class="plan-header" onclick="togglePlan(<?php echo $plan['pk_Plan']; ?>)">
            <div class="plan-info">
                <h2 class="plan-title">
                    <span class="material-symbols-outlined">event_note</span>
                    Plan #<?php echo $plan['pk_Plan']; ?>
                    <span class="expand-icon material-symbols-outlined" id="expand-icon-<?php echo $plan['pk_Plan']; ?>">expand_more</span>
                </h2>
                <div class="plan-details">
                    <span class="plan-date">
                        <span class="material-symbols-outlined">calendar_today</span>
                        <?php echo date('M j, Y'strtotime($plan['fromDate'])); ?> - 
                        <?php echo date('M j, Y'strtotime($plan['endDate'])); ?>
                    </span>
                    <span class="plan-duration">
                        <span class="material-symbols-outlined">schedule</span>
                        <?php echo $plan['weeks']; ?> week<?php echo $plan['weeks'] > 's' ''?>
                    </span>
                </div>
            </div>
            <div class="plan-stats">
                <div class="stat">
                    <span class="stat-number"><?php echo count($plan['plages']); ?></span>
                    <span class="stat-label">Plages</span>
                </div>
                <div class="stat">
                    <span class="stat-number">
                        <?php 
                        $totalRegistrations 
0;
                        foreach (
$plan['plages'] as $plage) {
                            
$totalRegistrations += count($plage['registrations']);
                        }
                        echo 
$totalRegistrations;
                        
?>
                    </span>
                    <span class="stat-label">Responses</span>
                </div>
            </div>
        </div>
        
        <div class="plages-container" id="plages-container-<?php echo $plan['pk_Plan']; ?>" style="display: none;">
            <?php if (empty($plan['plages'])): ?>
                <div class="no-plages">
                    <span class="material-symbols-outlined">event_busy</span>
                    <p>No plages found for this plan period.</p>
                </div>
            <?php else: ?>
                <?php foreach ($plan['plages'] as $plage): ?>
                    <?php renderPlageCard($plage); ?>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
    <?php
}

/**
 * Render a plage card with its registrations
 */
function renderPlageCard($plage) {
    
?>
    <div class="plage-card" id="plage-<?php echo $plage['pk_Plage']; ?>">
        <div class="plage-header">
            <div class="plage-info">
                <h3 class="plage-title">
                    <?php if ($plage['film_name']): ?>
                        <span class="material-symbols-outlined">movie</span>
                        <?php echo htmlspecialchars($plage['film_name']); ?>
                    <?php elseif ($plage['extra_name']): ?>
                        <span class="material-symbols-outlined">category</span>
                        <?php echo htmlspecialchars($plage['extra_name']); ?>
                    <?php else: ?>
                        <span class="material-symbols-outlined">event</span>
                        General Event
                    <?php endif; ?>
                </h3>
                <div class="plage-details">
                    <span class="plage-date">
                        <span class="material-symbols-outlined">calendar_today</span>
                        <?php echo date('l, M j, Y'strtotime($plage['date'])); ?>
                    </span>
                    <span class="plage-time">
                        <span class="material-symbols-outlined">schedule</span>
                        <?php echo date('H:i'strtotime($plage['fromTime'])); ?> - 
                        <?php echo date('H:i'strtotime($plage['toTime'])); ?>
                        <?php if ($plage['extraTime']): ?>
                            (+<?php echo $plage['extraTime']; ?>min)
                        <?php endif; ?>
                    </span>
                    <?php if ($plage['supervisor_first']): ?>
                        <span class="plage-supervisor">
                            <span class="material-symbols-outlined">person</span>
                            Supervisor: <?php echo htmlspecialchars($plage['supervisor_first'] . ' ' $plage['supervisor_last']); ?>
                        </span>
                    <?php else: ?>
                        <span class="plage-supervisor no-supervisor">
                            <span class="material-symbols-outlined">person_off</span>
                            No supervisor assigned
                        </span>
                    <?php endif; ?>
                </div>
            </div>
            <div class="plage-stats">
                <span class="registration-count">
                    <?php echo count($plage['registrations']); ?> 
                    response<?php echo count($plage['registrations']) !== 's' ''?>
                </span>
            </div>
        </div>
        
        <div class="registrations-container">
            <?php if (empty($plage['registrations'])): ?>
                <div class="no-registrations">
                    <span class="material-symbols-outlined">person_off</span>
                    <p>No responses for this plage yet.</p>
                </div>
            <?php else: ?>
                <div class="registrations-grid">
                    <?php foreach ($plage['registrations'] as $registration): ?>
                        <?php renderRegistrationCard($registration$plage); ?>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <?php
}

/**
 * Render a registration card
 */
function renderRegistrationCard($registration$plage) {
    
?>
    <div class="registration-card">
        <div class="participant-info">
            <div class="participant-avatar">
                <?php echo generateParticipantInitials($registration['firstName'], $registration['lastName']); ?>
            </div>
            <div class="participant-details">
                <h4 class="participant-name">
                    <?php echo htmlspecialchars($registration['firstName'] . ' ' $registration['lastName']); ?>
                </h4>
                <p class="participant-email">
                    <?php echo htmlspecialchars($registration['email']); ?>
                </p>
                <div class="work-frequency">
                    <span class="material-symbols-outlined">work</span>
                    <?php echo $registration['work_count']; ?> assignment<?php echo $registration['work_count'] !== 's' ''?>
                </div>
            </div>
            <div class="participant-actions">
                <?php if ($plage['supervisor_id'] == $registration['pk_Person'] || $registration['answer_label'] === 'No'): ?>
                    <?php if ($registration['answer_label'] === 'No'): ?>
                        <span class="cannot-assign">
                            <span class="material-symbols-outlined">block</span>
                            Cannot assign
                        </span>
                    <?php else: ?>
                        <span class="supervisor-badge">
                            <span class="material-symbols-outlined">star</span>
                            Supervisor
                        </span>
                    <?php endif; ?>
                <?php else: ?>
                    <button class="btn-assign" 
                            onclick="assignSupervisor(<?php echo $plage['pk_Plage']; ?><?php echo $registration['pk_Person']; ?>)"
                            title="Assign as supervisor">
                        <span class="material-symbols-outlined">person_add</span>
                        Assign
                    </button>
                <?php endif; ?>
            </div>
        </div>
        <div class="response-info">
            <div class="response-answer">
                <?php if ($registration['answer_label']): ?>
                    <span class="answer-label">
                        <?php echo htmlspecialchars($registration['answer_label']); ?>
                    </span>
                <?php else: ?>
                    <span class="answer-label no-answer">No response</span>
                <?php endif; ?>
            </div>
            <div class="response-time">
                <span class="material-symbols-outlined">schedule</span>
                <?php echo date('M j, H:i'strtotime($registration['createdAt'])); ?>
            </div>
        </div>
    </div>
    <?php
}

?>