<div class="container-fluid">
	<div class="row column_title">
		<div class="col-md-12">
			<div class="page_title">
				<h2>Courses</h2>
			</div>
		</div>
	</div>

	<div class="mb-3 text-right">
		<a class="btn btn-primary" href="<?= base_url('courses/create') ?>">Add Course</a>
	</div>

	<div class="card shadow mb-4">
		<div class="card-body">
			<div class="table-responsive">
				<table class="table table-bordered" width="100%" cellspacing="0">
					<thead class="thead-dark">
						<tr>
							<th>Title</th>
							<th>Category</th>
							<th>Duration</th>
							<th>Price</th>
							<th>Thumbnail</th>
							<th>User</th>
							<th>Actions</th>
						</tr>
					</thead>
					<tbody>
					<?php if (!empty($courses)) { ?>
						<?php foreach ($courses as $id => $course) { ?>
							<tr>
								<td><?= isset($course['title']) ? html_escape($course['title']) : '' ?></td>
								<td><?= isset($course['category']) ? html_escape($course['category']) : '' ?></td>
								<td><?= isset($course['duration']) ? (int)$course['duration'] : 0 ?></td>
								<td><?= isset($course['price']) ? html_escape((string)$course['price']) : '' ?></td>
								<td>
									<?php if (!empty($course['thumbnail'])) { ?>
										<img class="img-thumbnail" src="<?= html_escape($course['thumbnail']) ?>" style="height:80px;width:80px;">
									<?php } ?>
								</td>
								<td><?= isset($course['userId']) ? html_escape($course['userId']) : '' ?></td>
								<td style="width: 300px;">
									<a class="btn btn-success btn-sm" href="<?= base_url('courses/show/' . $id) ?>">View</a>
									<a class="btn btn-info btn-sm" href="<?= base_url('courses/edit/' . $id) ?>">Edit</a>
									<a class="btn btn-secondary btn-sm" href="<?= base_url('episodes/index/' . $id) ?>">Chapters</a>
									<a class="btn btn-danger btn-sm" href="<?= base_url('courses/delete/' . $id) ?>" onclick="return confirm('Delete this course?')">Delete</a>
								</td>
							</tr>
						<?php } ?>
					<?php } else { ?>
						<tr>
							<td colspan="7" class="text-center">No courses found.</td>
						</tr>
					<?php } ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>

