@extends('layouts.app')

@section('title', 'Kelola Bank Soal IKK - Admin')

@section('content')
<div class="container">
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h3 class="mb-1"><i class="bi bi-journal-text me-2"></i>Kelola Bank Soal IKK</h3>
                    <p class="text-muted mb-0">Manajemen Indikator Kinerja Kunci (IKK)</p>
                </div>
                <a href="{{ route('admin.ikk.bank-soal.create') }}" class="btn btn-gov-primary">
                    <i class="bi bi-plus-circle me-1"></i> Tambah Bank Soal
                </a>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-triangle me-2"></i>{{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- Filter -->
    <div class="card card-gov shadow-sm mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-5">
                    <label class="form-label">Filter Urusan</label>
                    <select name="urusan" class="form-select">
                        <option value="">Semua Urusan</option>
                        @foreach($urusanList as $u)
                            <option value="{{ $u }}" {{ $urusan == $u ? 'selected' : '' }}>{{ $u }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-5">
                    <label class="form-label">Filter Status</label>
                    <select name="status" class="form-select">
                        <option value="">Semua Status</option>
                        <option value="Aktif" {{ $status == 'Aktif' ? 'selected' : '' }}>Aktif</option>
                        <option value="Tidak Aktif" {{ $status == 'Tidak Aktif' ? 'selected' : '' }}>Tidak Aktif</option>
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-gov-primary w-100">
                        <i class="bi bi-search"></i> Filter
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card card-gov shadow-sm">
        <div class="card-header">
            <h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>Daftar Bank Soal</h5>
        </div>
        <div class="card-body">
            @if($bankSoal->count() > 0)
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-light">
                            <tr>
                                <th width="10%">Kode IKK</th>
                                <th width="15%">Urusan</th>
                                <th width="15%">Bidang</th>
                                <th width="25%">Output</th>
                                <th width="15%">OPD</th>
                                <th width="10%">Status</th>
                                <th width="10%">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($bankSoal as $soal)
                                <tr>
                                    <td><strong class="text-primary">{{ $soal->kode_ikk }}</strong></td>
                                    <td>{{ $soal->urusan }}</td>
                                    <td>{{ $soal->bidang }}</td>
                                    <td>{{ Str::limit($soal->ikk_output, 50) }}</td>
                                    <td>{{ $soal->opd_penanggung_jawab }}</td>
                                    <td>
                                        @if($soal->status == 'Aktif')
                                            <span class="badge bg-success">Aktif</span>
                                        @else
                                            <span class="badge bg-secondary">Tidak Aktif</span>
                                        @endif
                                    </td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ route('admin.ikk.bank-soal.edit', $soal->id) }}"
                                               class="btn btn-sm btn-warning"
                                               title="Edit">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <button type="button"
                                                    class="btn btn-sm btn-danger"
                                                    onclick="confirmDelete({{ $soal->id }})"
                                                    title="Hapus">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                        <form id="delete-form-{{ $soal->id }}"
                                              action="{{ route('admin.ikk.bank-soal.destroy', $soal->id) }}"
                                              method="POST"
                                              style="display: none;">
                                            @csrf
                                            @method('DELETE')
                                        </form>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="d-flex justify-content-center mt-4">
                    {{ $bankSoal->appends(request()->query())->links() }}
                </div>
            @else
                <div class="text-center py-5">
                    <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                    <p class="text-muted mt-3">Belum ada data bank soal</p>
                    <a href="{{ route('admin.ikk.bank-soal.create') }}" class="btn btn-gov-primary mt-2">
                        <i class="bi bi-plus-circle me-1"></i> Tambah Bank Soal Pertama
                    </a>
                </div>
            @endif
        </div>
    </div>
</div>

@push('scripts')
<script>
function confirmDelete(id) {
    if (confirm('Apakah Anda yakin ingin menghapus bank soal ini?')) {
        document.getElementById('delete-form-' + id).submit();
    }
}
</script>
@endpush
@endsection
