<?php
/** @var yii\web\View $this */
/** @var app\models\Course[]  $courses */
/** @var app\models\Tenant[]  $tenants */
/** @var string $statusFilter */
/** @var int    $tenantFilter */
/** @var int    $activeCount */
/** @var int    $inactiveCount */

use yii\helpers\Html;
use yii\helpers\Url;

$this->title = 'Courses';
?>

<div class="page-title">
    <i class="fas fa-book-open"></i> Courses
    <a href="<?= Url::to(['courses/create']) ?>" class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
        <i class="fas fa-plus"></i> New Course
    </a>
</div>

<!-- ===== Stats + tenant filter row ===== -->
<div style="display:flex; gap:12px; margin-bottom:16px; flex-wrap:wrap; align-items:center;">
    <a href="<?= Url::to(['courses/index', 'tenant' => $tenantFilter ?: null]) ?>"
       class="type-stat-pill <?= $statusFilter === '' ? 'type-stat-pill-active' : '' ?>">
        <span class="type-stat-num"><?= count($courses) ?></span><span>All</span>
    </a>
    <a href="<?= Url::to(['courses/index', 'status' => 'Y', 'tenant' => $tenantFilter ?: null]) ?>"
       class="type-stat-pill <?= $statusFilter === 'Y' ? 'type-stat-pill-active' : '' ?>" style="border-color:#27ae60;">
        <span class="type-stat-num" style="color:#27ae60;"><?= $activeCount ?></span>
        <span style="color:#27ae60;">Active</span>
    </a>
    <a href="<?= Url::to(['courses/index', 'status' => 'N', 'tenant' => $tenantFilter ?: null]) ?>"
       class="type-stat-pill <?= $statusFilter === 'N' ? 'type-stat-pill-active' : '' ?>" style="border-color:#e74c3c;">
        <span class="type-stat-num" style="color:#e74c3c;"><?= $inactiveCount ?></span>
        <span style="color:#e74c3c;">Inactive</span>
    </a>

    <!-- Tenant filter -->
    <form method="get" action="<?= Url::to(['courses/index']) ?>"
          style="margin-left:auto; display:flex; gap:8px; align-items:center;">
        <?php if ($statusFilter): ?>
            <input type="hidden" name="status" value="<?= Html::encode($statusFilter) ?>">
        <?php endif; ?>
        <select name="tenant" class="form-control"
                style="width:200px; height:34px; font-size:13px; padding:4px 10px;"
                onchange="this.form.submit()">
            <option value="">All Tenants</option>
            <?php foreach ($tenants as $t): ?>
                <option value="<?= $t->id ?>" <?= $tenantFilter === $t->id ? 'selected' : '' ?>>
                    <?= Html::encode($t->tenant_name) ?>
                </option>
            <?php endforeach; ?>
        </select>
        <?php if ($tenantFilter): ?>
            <a href="<?= Url::to(['courses/index', 'status' => $statusFilter ?: null]) ?>"
               class="lms-btn lms-btn-secondary lms-btn-sm" title="Clear tenant filter">
                <i class="fas fa-times"></i>
            </a>
        <?php endif; ?>
    </form>
</div>

<!-- ===== Table ===== -->
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-book-open"></i>
        <?php
        $label = $statusFilter === 'Y' ? 'Active' : ($statusFilter === 'N' ? 'Inactive' : 'All');
        if ($tenantFilter) {
            foreach ($tenants as $t) {
                if ($t->id === $tenantFilter) {
                    echo Html::encode($t->tenant_name) . ' — ';
                }
            }
        }
        echo $label . ' Courses';
        ?>
        <span class="lms-badge lms-badge-info" style="margin-left:8px;"><?= count($courses) ?></span>
    </div>

    <?php if (empty($courses)): ?>
        <div style="padding:40px; text-align:center; color:#aaa;">
            <i class="fas fa-book-open" style="font-size:40px; display:block; margin-bottom:12px;"></i>
            No courses found. <a href="<?= Url::to(['courses/create']) ?>">Add the first one.</a>
        </div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="lms-table">
            <thead>
                <tr>
                    <th style="width:50px; text-align:center;">#</th>
                    <th style="width:60px; text-align:center;">Banner</th>
                    <th>Course Name</th>
                    <th style="width:160px;">Tenant</th>
                    <th style="width:130px;">Leads By</th>
                    <th style="width:105px; text-align:center;">Start</th>
                    <th style="width:105px; text-align:center;">End</th>
                    <th style="width:90px; text-align:center;">Status</th>
                    <th style="width:100px; text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($courses as $c): ?>
            <tr class="<?= $c->isActive === 'N' ? 'row-inactive' : '' ?>">
                <td style="text-align:center; color:#aaa; font-size:13px;"><?= $c->id ?></td>
                <td style="text-align:center;">
                    <?php $url = $c->getFileUrl('course_banners'); ?>
                    <?php if ($url): ?>
                        <img src="<?= Html::encode($url) ?>" alt="" class="course-banner-thumb">
                    <?php else: ?>
                        <span class="course-banner-placeholder"><i class="fas fa-image"></i></span>
                    <?php endif; ?>
                </td>
                <td style="font-weight:600;"><?= Html::encode($c->course_name) ?></td>
                <td style="font-size:13px;">
                    <?php if ($c->tenant): ?>
                        <a href="<?= Url::to(['tenant/update', 'id' => $c->tenant_id]) ?>"
                           style="color:#3498db; text-decoration:none;">
                            <?= Html::encode($c->tenant->tenant_name) ?>
                        </a>
                    <?php else: ?>
                        <span style="color:#ccc;">—</span>
                    <?php endif; ?>
                </td>
                <td style="font-size:13px; color:#666;">
                    <?= Html::encode($c->leads_by) ?: '<span style="color:#ccc;">—</span>' ?>
                </td>
                <td style="text-align:center; font-size:13px; color:#888;">
                    <?= ($c->start_date && $c->start_date !== '0000-00-00')
                        ? date('d M Y', strtotime($c->start_date)) : '—' ?>
                </td>
                <td style="text-align:center; font-size:13px; color:#888;">
                    <?= ($c->end_date && $c->end_date !== '0000-00-00')
                        ? date('d M Y', strtotime($c->end_date)) : '—' ?>
                </td>
                <td style="text-align:center;">
                    <?= Html::beginForm(['courses/toggle', 'id' => $c->id], 'post', ['style' => 'display:inline;']) ?>
                    <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                    <button type="submit"
                            class="lms-badge <?= $c->isActive === 'Y' ? 'lms-badge-success' : 'lms-badge-danger' ?>"
                            style="border:none; cursor:pointer; padding:4px 10px;" title="Click to toggle">
                        <?= $c->isActive === 'Y' ? 'Active' : 'Inactive' ?>
                    </button>
                    <?= Html::endForm() ?>
                </td>
                <td style="text-align:center; white-space:nowrap;">
                    <a href="<?= Url::to(['courses/update', 'id' => $c->id]) ?>"
                       class="lms-btn lms-btn-primary lms-btn-sm" title="Edit">
                        <i class="fas fa-edit"></i>
                    </a>
                    <?= Html::beginForm(['courses/delete', 'id' => $c->id], 'post', ['style' => 'display:inline;']) ?>
                    <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                    <button type="submit" class="lms-btn lms-btn-danger lms-btn-sm" title="Delete"
                            onclick="return confirm('Delete course \'<?= Html::encode($c->course_name) ?>\'?')">
                        <i class="fas fa-trash"></i>
                    </button>
                    <?= Html::endForm() ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
