<?php
/** @var yii\web\View $this */
/** @var app\models\Course $course */
/** @var app\models\CourseActivity[] $activities */
/** @var app\models\Tag[] $courseTags */
/** @var array $activityTagMap  activityId → Tag[] */
/** @var string[] $allTagNames */

use yii\helpers\Html;
use yii\helpers\Url;
use app\models\CourseActivity;
use app\models\EntityTag;

$this->title = $course->course_code . ' — ' . $course->course_name;

$totalMin = $course->getTotalDurationMinutes();
$totalHrs = $totalMin > 0 ? round($totalMin / 60, 1) : null;
$courseViewUrl = Url::to(['courses/view', 'id' => $course->id]);
?>

<div class="page-title">
    <i class="fas fa-book-open"></i>
    <code style="background:#f0f4ff; padding:2px 9px; border-radius:4px; font-size:14px;">
        <?= Html::encode($course->course_code) ?>
    </code>
    &nbsp;<?= Html::encode($course->course_name) ?>
    <div style="margin-left:auto; display:flex; gap:8px;">
        <a href="<?= Url::to(['courses/clone', 'id' => $course->id]) ?>"
           class="lms-btn lms-btn-warning lms-btn-sm">
            <i class="fas fa-copy"></i> Clone
        </a>
        <a href="<?= Url::to(['courses/update', 'id' => $course->id]) ?>"
           class="lms-btn lms-btn-secondary lms-btn-sm">
            <i class="fas fa-edit"></i> Edit
        </a>
        <a href="<?= Url::to(['courses/index']) ?>"
           class="lms-btn lms-btn-primary lms-btn-sm">
            <i class="fas fa-arrow-left"></i> Back
        </a>
    </div>
</div>

<!-- ===== Course Details + Stats ===== -->
<div class="course-detail-grid">
    <div class="lms-card" style="flex:1; min-width:0;">
        <div class="lms-card-header"><i class="fas fa-info-circle"></i> Course Details</div>
        <div class="lms-card-body">
            <table class="course-detail-table">
                <tr>
                    <th>Course Code</th>
                    <td><code style="background:#f0f4ff; padding:2px 8px; border-radius:4px;"><?= Html::encode($course->course_code) ?></code></td>
                </tr>
                <tr>
                    <th>Course Name</th>
                    <td><?= Html::encode($course->course_name) ?></td>
                </tr>
                <tr>
                    <th>Category</th>
                    <td><?= $course->category_id ? Html::encode($course->category_id) : '<span style="color:#aaa;">—</span>' ?></td>
                </tr>
                <tr>
                    <th>Duration</th>
                    <td><?= $course->duration_hours !== null ? $course->duration_hours . ' hours' : '<span style="color:#aaa;">—</span>' ?></td>
                </tr>
                <tr>
                    <th>Status</th>
                    <td>
                        <span class="lms-badge <?= $course->is_active === 'Y' ? 'lms-badge-success' : 'lms-badge-danger' ?>">
                            <?= $course->is_active === 'Y' ? 'Active' : 'Inactive' ?>
                        </span>
                    </td>
                </tr>
                <?php if ($course->description): ?>
                <tr>
                    <th style="vertical-align:top; padding-top:10px;">Description</th>
                    <td style="padding-top:10px; color:#555; font-size:13px; line-height:1.6;">
                        <?= nl2br(Html::encode($course->description)) ?>
                    </td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>

    <div class="course-stat-col">
        <div class="course-stat-box" style="border-top-color:#2196f3;">
            <div style="font-size:28px; font-weight:700; color:#2196f3;"><?= count($activities) ?></div>
            <div style="font-size:12px; color:#888; margin-top:4px; text-transform:uppercase;">Activities</div>
        </div>
        <?php if ($totalHrs): ?>
        <div class="course-stat-box" style="border-top-color:#27ae60;">
            <div style="font-size:28px; font-weight:700; color:#27ae60;"><?= $totalHrs ?></div>
            <div style="font-size:12px; color:#888; margin-top:4px; text-transform:uppercase;">Hrs Content</div>
        </div>
        <?php endif; ?>
        <div class="course-stat-box" style="border-top-color:#f39c12;">
            <div style="font-size:28px; font-weight:700; color:#f39c12;"><?= count($courseTags) ?></div>
            <div style="font-size:12px; color:#888; margin-top:4px; text-transform:uppercase;">Tags</div>
        </div>
        <?php
        $typeCounts = [];
        foreach ($activities as $a) {
            $typeCounts[$a->activity_type] = ($typeCounts[$a->activity_type] ?? 0) + 1;
        }
        foreach ($typeCounts as $type => $cnt): ?>
        <div class="course-stat-box">
            <span class="lms-badge act-type-<?= $type ?>" style="font-size:11px; margin-bottom:6px;">
                <?= Html::encode(CourseActivity::TYPES[$type] ?? $type) ?>
            </span>
            <div style="font-size:22px; font-weight:700; color:#333;"><?= $cnt ?></div>
        </div>
        <?php endforeach; ?>
    </div>
</div>

<!-- ===== Course Tags Panel ===== -->
<div class="lms-card" style="margin-bottom:20px;">
    <div class="lms-card-header">
        <i class="fas fa-tags"></i> Course Tags
        <?php if (!empty($courseTags)): ?>
            <span class="lms-badge lms-badge-info" style="margin-left:8px;"><?= count($courseTags) ?></span>
        <?php endif; ?>
    </div>
    <?= $this->renderPartial('/tags/_tag_panel', [
        'entityType'  => EntityTag::TYPE_COURSE,
        'entityId'    => $course->id,
        'tags'        => $courseTags,
        'allTagNames' => $allTagNames,
        'returnUrl'   => $courseViewUrl,
    ]) ?>
</div>

<!-- ===== Activities ===== -->
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-list-ol"></i> Activities
        <span class="lms-badge lms-badge-info" style="margin-left:8px;"><?= count($activities) ?></span>
        <a href="<?= Url::to(['courses/add-activity', 'courseId' => $course->id]) ?>"
           class="lms-btn lms-btn-success lms-btn-sm" style="margin-left:auto;">
            <i class="fas fa-plus"></i> Add Activity
        </a>
    </div>

    <?php if (empty($activities)): ?>
        <div style="padding:30px; text-align:center; color:#aaa;">
            <i class="fas fa-clipboard-list" style="font-size:36px; display:block; margin-bottom:10px;"></i>
            No activities yet.
            <a href="<?= Url::to(['courses/add-activity', 'courseId' => $course->id]) ?>">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:120px;">Code</th>
                    <th>Activity Name</th>
                    <th style="width:105px;">Type</th>
                    <th style="width:95px; text-align:center;">Duration</th>
                    <th>Tags</th>
                    <th style="width:70px;">Status</th>
                    <th style="width:100px; text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($activities as $i => $act):
                $actTags = $activityTagMap[$act->id] ?? [];
            ?>
                <tr>
                    <td style="text-align:center; color:#aaa; font-size:13px;"><?= $act->sort_order ?: ($i + 1) ?></td>
                    <td>
                        <?php if ($act->activity_code): ?>
                        <code style="background:#f9f9f9; padding:2px 6px; border-radius:4px; font-size:11px; color:#888;">
                            <?= Html::encode($act->activity_code) ?>
                        </code>
                        <?php else: ?><span style="color:#ddd;">—</span><?php endif; ?>
                    </td>
                    <td style="font-weight:500;"><?= Html::encode($act->activity_name) ?></td>
                    <td>
                        <span class="lms-badge act-type-<?= Html::encode($act->activity_type) ?>">
                            <?= Html::encode($act->getTypeLabel()) ?>
                        </span>
                    </td>
                    <td style="text-align:center; font-size:13px; color:#555;">
                        <?= $act->duration_minutes ? $act->duration_minutes . ' min' : '<span style="color:#ccc;">—</span>' ?>
                    </td>
                    <td>
                        <?php if (empty($actTags)): ?>
                            <span style="color:#ddd; font-size:12px;">—</span>
                        <?php else: ?>
                            <div style="display:flex; flex-wrap:wrap; gap:4px;">
                                <?php foreach ($actTags as $t): ?>
                                <span class="tag-pill tag-pill-sm" style="background:<?= Html::encode($t->color) ?>;">
                                    <?= Html::encode($t->tag_name) ?>
                                </span>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <span class="lms-badge <?= $act->is_active === 'Y' ? 'lms-badge-success' : 'lms-badge-danger' ?>">
                            <?= $act->is_active === 'Y' ? 'Active' : 'Inactive' ?>
                        </span>
                    </td>
                    <td style="text-align:center; white-space:nowrap;">
                        <a href="<?= Url::to(['courses/edit-activity', 'id' => $act->id]) ?>"
                           class="lms-btn lms-btn-primary lms-btn-sm" title="Edit / Tag">
                            <i class="fas fa-edit"></i>
                        </a>
                        <?= Html::beginForm(['courses/delete-activity', 'id' => $act->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 activity \'<?= Html::encode($act->activity_name) ?>\'?')">
                            <i class="fas fa-trash"></i>
                        </button>
                        <?= Html::endForm() ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
