<div class="container-fluid">
	<div class="row column_title">
		<div class="col-md-12">
			<div class="page_title">
				<h2><?= ($mode === 'edit') ? 'Edit Chapter' : 'Add Chapter' ?></h2>
			</div>
		</div>
	</div>

	<div class="card shadow mb-4">
		<div class="card-body">
			<form method="post" enctype="multipart/form-data" action="<?= ($mode === 'edit') ? base_url('episodes/update/' . $courseId . '/' . $episodeId) : base_url('episodes/store/' . $courseId) ?>">
				<div class="row">
					<div class="col-md-6">
						<div class="form-group">
							<label>Title</label>
							<input type="text" name="title" class="form-control" required
								value="<?= ($episode && isset($episode['title'])) ? html_escape((string)$episode['title']) : '' ?>">
						</div>
					</div>

					<div class="col-md-6">
						<div class="form-group">
							<label>Duration</label>
							<input type="number" step="0.01" name="duration" class="form-control" required
								value="<?= ($episode && isset($episode['duration'])) ? (float)$episode['duration'] : 0 ?>">
						</div>
					</div>

					<div class="col-md-12">
						<div class="form-group">
							<label>About Description</label>
							<textarea name="aboutDescription" rows="3" class="form-control" required><?= ($episode && isset($episode['aboutDescription'])) ? html_escape((string)$episode['aboutDescription']) : '' ?></textarea>
						</div>
					</div>

					<div class="col-md-12">
						<div class="form-group">
							<label>Goals</label>
							<textarea name="goals" rows="2" class="form-control" required><?= ($episode && isset($episode['goals'])) ? html_escape((string)$episode['goals']) : '' ?></textarea>
						</div>
					</div>

					<div class="col-md-4">
						<div class="form-group">
							<label>Length</label>
							<input type="text" name="length" class="form-control"
								value="<?= ($episode && isset($episode['length'])) ? html_escape((string)$episode['length']) : '' ?>"
								placeholder="6">
						</div>
					</div>

					<div class="col-md-4">
						<div class="form-group">
							<label>Sets</label>
							<input type="text" name="sets" class="form-control"
								value="<?= ($episode && isset($episode['sets'])) ? html_escape((string)$episode['sets']) : '' ?>"
								placeholder="1">
						</div>
					</div>

					<div class="col-md-4">
						<div class="form-group">
							<label>Time</label>
							<input type="text" name="time" class="form-control"
								value="<?= ($episode && isset($episode['time'])) ? html_escape((string)$episode['time']) : '' ?>"
								placeholder="25">
						</div>
					</div>

					<div class="col-md-12">
						<div class="form-group">
							<label>Video (Upload)</label>
							<input type="hidden" name="videoUri" value="<?= ($episode && isset($episode['videoUri'])) ? html_escape((string)$episode['videoUri']) : '' ?>">
							<input type="file" name="video_file" id="video_file" class="form-control" accept="video/*">
							<?php if ($episode && !empty($episode['videoUri'])) { ?>
								<div class="mt-2">
									<a class="btn btn-sm btn-outline-info" target="_blank" href="<?= html_escape((string)$episode['videoUri']) ?>">Open current video</a>
								</div>
							<?php } ?>
						</div>
					</div>
				</div>

				<div class="mt-3">
					<button type="submit" class="btn btn-primary"><?= ($mode === 'edit') ? 'Update Chapter' : 'Create Chapter' ?></button>
					<a class="btn btn-secondary" href="<?= base_url('episodes/index/' . $courseId) ?>">Back</a>
				</div>
			</form>
		</div>
	</div>
</div>

