@extends('layouts.admin')

@section('title', 'Set Soalselidik')
@section('header', 'Set Soalselidik')

@section('breadcrumb')
<li class="breadcrumb-item active">Soalselidik</li>
@endsection

@section('header-actions')
<a href="{{ route('admin.questionnaires.create') }}" class="btn btn-sm btn-primary">
    <i class="bi bi-plus-lg me-1"></i> Buat Set Baru
</a>
@endsection

@section('content')

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white py-3">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-5">
                <input type="text" name="q" value="{{ request('q') }}"
                       class="form-control form-control-sm" placeholder="Cari tajuk soalselidik...">
            </div>
            <div class="col-md-3">
                <select name="status" class="form-select form-select-sm">
                    <option value="">Semua Status</option>
                    <option value="draft"     {{ request('status') === 'draft'     ? 'selected' : '' }}>Draf</option>
                    <option value="published" {{ request('status') === 'published' ? 'selected' : '' }}>Diterbitkan</option>
                    <option value="archived"  {{ request('status') === 'archived'  ? 'selected' : '' }}>Diarkib</option>
                </select>
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-search"></i> Cari
                </button>
                @if(request()->hasAny(['q', 'status']))
                <a href="{{ route('admin.questionnaires.index') }}" class="btn btn-sm btn-link text-muted">Set Semula</a>
                @endif
            </div>
        </form>
    </div>

    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <thead class="table-light">
                <tr>
                    <th>Tajuk</th>
                    <th class="text-center">Soalan</th>
                    <th>Status</th>
                    <th>Dicipta Oleh</th>
                    <th>Tarikh Cipta</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            @forelse($sets as $set)
                <tr>
                    <td>
                        <a href="{{ route('admin.questionnaires.show', $set) }}" class="fw-medium text-decoration-none">
                            {{ $set->title }}
                        </a>
                        @if($set->description)
                        <div class="text-muted small text-truncate" style="max-width:300px;">{{ $set->description }}</div>
                        @endif
                    </td>
                    <td class="text-center">
                        <span class="badge bg-secondary">{{ $set->questions_count }}</span>
                    </td>
                    <td>
                        @if($set->status === 'published')
                            <span class="badge bg-success">Diterbitkan</span>
                        @elseif($set->status === 'archived')
                            <span class="badge bg-secondary">Diarkib</span>
                        @else
                            <span class="badge bg-warning text-dark">Draf</span>
                        @endif
                    </td>
                    <td class="small text-muted">{{ $set->creator?->name ?? '—' }}</td>
                    <td class="small text-muted">{{ $set->created_at->format('d/m/Y') }}</td>
                    <td class="text-end">
                        <a href="{{ route('admin.questionnaires.show', $set) }}"
                           class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-eye"></i>
                        </a>
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" class="text-center py-4 text-muted">
                        <i class="bi bi-clipboard2-x d-block fs-2 mb-2 opacity-25"></i>
                        Tiada set soalselidik dijumpai.
                    </td>
                </tr>
            @endforelse
            </tbody>
        </table>
    </div>

    @if($sets->hasPages())
    <div class="card-footer bg-white">
        {{ $sets->links() }}
    </div>
    @endif
</div>

@endsection
