<?php
if (empty($_SESSION['is_admin']) || $_SESSION['is_admin'] != 1) {
    
header('Location: index.php?page=home');
    exit;
}

require_once 
'calendar/calendar_actions.php';
require_once 
'calendar/calendar_data.php';
require_once 
'calendar/calendar_stats.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Timesheet Management - CinĂ© Le Paris</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <link rel="stylesheet" href="assets/css/calendar.css">
  <style>
    .ts-btn-danger {
      background-color: var(--danger);
      color: white;
    }

    .ts-btn-danger:hover {
      background-color: #c82333;
    }

    .ts-modal-footer {
      display: flex;
      justify-content: space-between;
    }
  </style>
</head>
<body>
  <div class="timesheet-app">
    <!-- Dashboard Header -->
    <div class="ts-dashboard-header">
      <div class="ts-dashboard-title">
        <h1>Timesheet Management</h1>
        <p>Schedule and manage cinema activities, shifts, and events</p>
      </div>
      <div class="ts-dashboard-actions">
        <button class="ts-btn ts-btn-primary" id="addEntryBtn">
          <i class="fas fa-plus"></i> New Entry
        </button>
      </div>
    </div>

    <!-- Stats Cards -->
    <div class="ts-stats-container">
      <div class="ts-stat-card">
        <div class="ts-stat-header">
          <div class="ts-stat-icon hours">
            <i class="fas fa-clock"></i>
          </div>
          <h3 class="ts-stat-title">Total Hours</h3>
        </div>
        <p class="ts-stat-value"><?php echo $totalHours?></p>
        <div class="ts-stat-footer">
          <span>This month</span>
        </div>
      </div>
      
      <div class="ts-stat-card">
        <div class="ts-stat-header">
          <div class="ts-stat-icon entries">
            <i class="fas fa-calendar-check"></i>
          </div>
          <h3 class="ts-stat-title">Schedule Entries</h3>
        </div>
        <p class="ts-stat-value"><?php echo $entriesCount?></p>
        <div class="ts-stat-footer">
          <span>This month</span>
        </div>
      </div>
      
      <div class="ts-stat-card">
        <div class="ts-stat-header">
          <div class="ts-stat-icon activities">
            <i class="fas fa-tasks"></i>
          </div>
          <h3 class="ts-stat-title">Activity Types</h3>
        </div>
        <p class="ts-stat-value"><?php echo count($extras); ?></p>
        <div class="ts-stat-footer">
          <span>Available options</span>
        </div>
      </div>
    </div>

    <!-- Success Message (if any) -->
    <?php if ($msg): ?>
      <div class="ts-alert ts-alert-<?php echo $msgType?>">
        <div class="ts-alert-icon">
          <i class="fas fa-<?php echo $msgType === 'success' 'check-circle' 'exclamation-circle'?>"></i>
        </div>
        <div class="ts-alert-content">
          <h4 class="ts-alert-title"><?php echo $msgType === 'success' 'Success!' 'Error!'?></h4>
          <p class="ts-alert-message"><?php echo htmlspecialchars($msg); ?></p>
        </div>
      </div>
    <?php endif; ?>

    <!-- Main Content Container -->
    <div class="ts-content-container">
      <!-- Calendar Card -->
      <div class="ts-calendar-card">
        <div class="ts-calendar-header">
          <h3 class="ts-calendar-title">Schedule Calendar</h3>
          <div class="ts-calendar-actions">
            <button class="ts-btn ts-btn-secondary ts-btn-sm" id="refreshCalendarBtn">
              <i class="fas fa-sync-alt"></i> Refresh
            </button>
          </div>
        </div>
        <div id="calendar"></div>
      </div>

      <!-- Form Card -->
      <div class="ts-form-card hidden" id="entryFormCard">
        <div class="ts-form-header">
          <h3 class="ts-form-title">Add New Entry</h3>
          <p class="ts-form-subtitle">Fill in the details to create a new schedule entry</p>
        </div>
        <form method="post" action="" class="ts-form" id="entryForm">
          <div class="ts-form-group">
            <label class="ts-form-label" for="date">Date</label>
            <input type="date" id="date" name="date" class="ts-form-control" required>
          </div>
          
          <div class="ts-form-group">
            <label class="ts-form-label" for="start_time">Start Time</label>
            <input type="time" id="start_time" name="start_time" class="ts-form-control" required>
          </div>
          
          <div class="ts-form-group">
            <label class="ts-form-label" for="end_time">End Time</label>
            <input type="time" id="end_time" name="end_time" class="ts-form-control" required>
          </div>
          
          <div class="ts-form-group">
            <label class="ts-form-label" for="extra_id">Activity Type</label>
            <select id="extra_id" name="extra_id" class="ts-form-control">
              <option value="">-- Select Activity Type --</option>
              <?php foreach ($extras as $extra): ?>
                <option value="<?php echo $extra['pk_Extra']; ?>" data-defaulttime="<?php echo $extra['defaultTime']; ?>">
                  <?php echo htmlspecialchars($extra['name']); ?> (+<?php echo $extra['defaultTime']; ?> min)
                </option>
              <?php endforeach; ?>
            </select>
            <p class="ts-form-help">Select an activity type to automatically set extra time</p>
          </div>
          
          <div class="ts-form-group">
            <label class="ts-form-label" for="extra_time">Extra Time (minutes)</label>
            <input type="number" id="extra_time" name="extra_time" class="ts-form-control" min="0" value="0">
            <p class="ts-form-help">Additional time needed beyond the scheduled hours</p>
          </div>
          
          <div class="ts-form-group">
            <label class="ts-form-label" for="total_duration">Total Duration</label>
            <input type="text" id="total_duration" name="total_duration" class="ts-form-control" readonly>
            <p class="ts-form-help">Calculated automatically based on start/end times and extra time</p>
          </div>
          
          <div class="ts-form-footer">
            <button type="button" class="ts-btn ts-btn-secondary" id="cancelEntryBtn">Cancel</button>
            <button type="submit" class="ts-btn ts-btn-primary">Save Entry</button>
          </div>
        </form>
      </div>
    </div>

    <?php include 'calendar/calendar_modals.php'?>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.js"></script>
  <?php include 'calendar/calendar_scripts.php'?>
</body>
</html>