<?php
// Check if user is admin
if (empty($_SESSION['is_admin']) || $_SESSION['is_admin'] != 1) {
header('Location: index.php?page=home');
exit;
}
// Include necessary files
require_once 'film/film_functions.php';
require_once 'film/film_data.php';
// Process form submissions and actions
require_once 'film/film_actions.php';
// HTML header and main content
?>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />
<link rel="stylesheet" href="assets/css/films.css">
<body>
<div class="films-container">
<!-- Header -->
<div class="films-header">
<div class="films-title-section">
<h1 class="films-title">Film Management</h1>
<p class="films-subtitle">Add, edit, and manage films in the cinema database</p>
</div>
<div class="films-actions">
<a href="index.php?page=dashboard" class="films-btn films-btn-secondary">
<span class="material-symbols-outlined">dashboard</span> Dashboard
</a>
<?php if (!$editMode): ?>
<button class="films-btn films-btn-primary" id="showFormBtn">
<span class="material-symbols-outlined">add</span> Add New Film
</button>
<?php endif; ?>
</div>
</div>
<!-- Success/Error Message (if any) -->
<?php if ($msg): ?>
<div class="films-alert films-alert-<?php echo $msgType; ?>">
<div class="films-alert-icon">
<span class="material-symbols-outlined">
<?php echo $msgType === 'success' ? 'check_circle' : 'error'; ?>
</span>
</div>
<div class="films-alert-content">
<?php echo htmlspecialchars($msg); ?>
</div>
</div>
<?php endif; ?>
<!-- Form Section (initially hidden if not in edit mode) -->
<div class="films-card" id="formCard" <?php echo (!$editMode && empty($msg)) ? 'style="display: none;"' : ''; ?>>
<div class="films-card-header">
<h2 class="films-card-title"><?php echo $editMode ? 'Edit Film' : 'Add New Film'; ?></h2>
<?php if (!$editMode): ?>
<button class="films-btn-icon" id="closeFormBtn">
<span class="material-symbols-outlined">close</span>
</button>
<?php endif; ?>
</div>
<div class="films-card-body">
<form method="post" action="" class="films-form" enctype="multipart/form-data">
<input type="hidden" name="action" value="<?php echo $editMode ? 'update' : 'create'; ?>">
<?php if ($editMode): ?>
<input type="hidden" name="id" value="<?php echo $filmToEdit['pk_Film']; ?>">
<?php endif; ?>
<div class="films-form-row">
<div class="films-form-group films-form-group-large">
<label class="films-form-label" for="name">Film Title</label>
<input type="text" id="name" name="name" class="films-form-control"
value="<?php echo $editMode ? htmlspecialchars($filmToEdit['name']) : ''; ?>" required>
</div>
<div class="films-form-group">
<label class="films-form-label" for="length">Duration (minutes)</label>
<input type="number" id="length" name="length" class="films-form-control"
value="<?php echo $editMode ? intval($filmToEdit['length']) : ''; ?>" min="1" required>
</div>
</div>
<div class="films-form-row">
<div class="films-form-group">
<label class="films-form-label" for="releaseDate">Release Date</label>
<input type="date" id="releaseDate" name="releaseDate" class="films-form-control"
value="<?php echo $editMode ? $filmToEdit['releaseDate'] : ''; ?>" required>
</div>
<div class="films-form-group">
<label class="films-form-label" for="kdmStart">KDM Start Date</label>
<input type="datetime-local" id="kdmStart" name="kdmStart" class="films-form-control"
value="<?php echo $editMode && !empty($filmToEdit['kdmStart']) ? date('Y-m-d\TH:i', strtotime($filmToEdit['kdmStart'])) : ''; ?>">
<div class="films-form-help">Start date for film screening period</div>
</div>
<div class="films-form-group">
<label class="films-form-label" for="kdmEnd">KDM End Date</label>
<input type="datetime-local" id="kdmEnd" name="kdmEnd" class="films-form-control"
value="<?php echo $editMode && !empty($filmToEdit['kdmEnd']) ? date('Y-m-d\TH:i', strtotime($filmToEdit['kdmEnd'])) : ''; ?>">
<div class="films-form-help">End date for film screening period</div>
</div>
</div>
<?php if (!$editMode): ?>
<div class="films-form-group">
<label class="films-form-label" for="image">Film Poster</label>
<div class="films-file-input-wrapper">
<input type="file" id="image" name="image" class="films-form-control films-file-input" accept="image/*">
<div class="films-file-input-display">
<span class="material-symbols-outlined">upload_file</span>
<span class="films-file-name">No file selected</span>
</div>
</div>
<div id="imagePreviewContainer" class="films-image-preview-container" style="display: none;">
<img id="imagePreview" class="films-image-preview" src="/placeholder.svg" alt="Preview">
</div>
<div class="films-form-help">Upload a poster image for the film</div>
</div>
<?php endif; ?>
<div class="films-form-actions">
<?php if ($editMode): ?>
<a href="index.php?page=films" class="films-btn films-btn-secondary">Cancel</a>
<?php endif; ?>
<button type="submit" class="films-btn films-btn-primary">
<?php echo $editMode ? 'Update Film' : 'Add Film'; ?>
</button>
</div>
</form>
<?php if ($editMode): ?>
<!-- KDM Upload Section -->
<div class="films-kdm-upload">
<h4>Upload KDM File</h4>
<p>Upload a KDM zip file to automatically extract and set the screening period dates for this film. The system will parse the XML content to find valid screening dates.</p>
<form method="post" action="" class="films-kdm-form" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload_kdm">
<input type="hidden" name="film_id" value="<?php echo $filmToEdit['pk_Film']; ?>">
<div class="films-file-input-wrapper kdm-file-wrapper">
<input type="file" name="kdm_file" id="kdm_file" class="films-form-control films-file-input" accept=".zip" required>
<div class="films-file-input-display">
<span class="material-symbols-outlined">upload_file</span>
<span class="films-file-name">No file selected</span>
</div>
</div>
<button type="submit" class="films-btn films-btn-primary">
<span class="material-symbols-outlined">upload_file</span> Upload KDM
</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
<!-- Films Table -->
<div class="films-card">
<div class="films-card-header">
<h2 class="films-card-title">All Films</h2>
<span class="films-card-count"><?php echo count($films); ?> films</span>
</div>
<div class="films-table-container">
<?php if (count($films) > 0): ?>
<table class="films-table">
<thead>
<tr>
<th>Poster</th>
<th>Title</th>
<th>Duration</th>
<th>Release Date</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($films as $film): ?>
<tr>
<td class="films-poster-cell">
<?php
$imgSrc = !empty($film['imagePath']) ? htmlspecialchars($film['imagePath']) : $default_image;
?>
<img src="<?php echo $imgSrc; ?>"
alt="<?php echo htmlspecialchars($film['name']); ?>"
class="films-thumbnail"
onerror="this.src='<?php echo $default_image; ?>'">
</td>
<td>
<div class="films-title-cell">
<span class="films-title-text"><?php echo htmlspecialchars($film['name']); ?></span>
<?php if (!empty($film['kdmStart']) && !empty($film['kdmEnd'])): ?>
<span class="films-synopsis-preview">
KDM: <?php echo date('Y-m-d H:i', strtotime($film['kdmStart'])); ?> to <?php echo date('Y-m-d H:i', strtotime($film['kdmEnd'])); ?>
</span>
<?php endif; ?>
</div>
</td>
<td><?php echo formatDuration($film['length']); ?></td>
<td><?php echo date('M j, Y', strtotime($film['releaseDate'])); ?></td>
<td>
<?php if (isCurrentlyShowing($film['kdmStart'], $film['kdmEnd'])): ?>
<span class="films-badge films-badge-active">
<span class="material-symbols-outlined">movie</span> Now Showing
</span>
<?php else: ?>
<span class="films-badge films-badge-inactive">
<span class="material-symbols-outlined">movie_edit</span> Not Showing
</span>
<?php endif; ?>
</td>
<td class="films-actions-cell">
<a href="index.php?page=films&edit=<?php echo $film['pk_Film']; ?>" class="films-btn-icon films-btn-edit" title="Edit">
<span class="material-symbols-outlined">edit</span>
</a>
<button type="button" class="films-btn-icon films-btn-delete"
data-film-id="<?php echo $film['pk_Film']; ?>"
data-film-name="<?php echo htmlspecialchars($film['name'], ENT_QUOTES); ?>"
title="Delete">
<span class="material-symbols-outlined">delete</span>
</button>
<a href="index.php?page=moviedetail&id=<?php echo $film['pk_Film']; ?>" class="films-btn-icon films-btn-view" title="View Details" target="_blank">
<span class="material-symbols-outlined">visibility</span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<div class="films-empty-state">
<div class="films-empty-icon">
<span class="material-symbols-outlined">movie_filter</span>
</div>
<p class="films-empty-text">No films have been added yet.</p>
<button class="films-btn films-btn-primary" id="emptyStateBtn">
<span class="material-symbols-outlined">add</span> Add First Film
</button>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Include modals -->
<?php require_once 'film/film_modals.php'; ?>
<!-- Include scripts -->
<?php require_once 'film/film_scripts.php'; ?>
</body>
</html>