@extends('layouts.app')

@section('content')
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-1 fw-bold">
                <i class="bi bi-diagram-2-fill text-primary me-2"></i>
                Mapping User & Sekolah
            </h4>
            <p class="text-muted mb-0">Kelola penugasan user ke sekolah</p>
        </div>
        <a href="{{ route('superadmin.user-school-mapping.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-2"></i>
            Mapping Baru
        </a>
    </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 Card -->
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('superadmin.user-school-mapping.index') }}">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label fw-bold">
                            <i class="bi bi-search me-1"></i>Cari User
                        </label>
                        <input type="text" name="search" class="form-control" placeholder="Nama atau Email" value="{{ request('search') }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label fw-bold">
                            <i class="bi bi-shield me-1"></i>Role
                        </label>
                        <select name="role_id" class="form-select">
                            <option value="">Semua Role</option>
                            @foreach($roles as $role)
                            <option value="{{ $role->id }}" {{ request('role_id') == $role->id ? 'selected' : '' }}>
                                {{ $role->name }}
                            </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-bold">
                            <i class="bi bi-building me-1"></i>Sekolah
                        </label>
                        <select name="school_id" class="form-select">
                            <option value="">Semua Sekolah</option>
                            @foreach($schools as $school)
                            <option value="{{ $school->id }}" {{ request('school_id') == $school->id ? 'selected' : '' }}>
                                {{ $school->name }}
                            </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-funnel me-1"></i>Filter
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Data Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th width="5%">No</th>
                            <th width="20%">Nama User</th>
                            <th width="15%">Email/NPSN</th>
                            <th width="12%">Role</th>
                            <th width="10%">NIP</th>
                            <th width="20%">Sekolah</th>
                            <th width="10%">Status</th>
                            <th width="8%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($users as $index => $user)
                        <tr>
                            <td>{{ $users->firstItem() + $index }}</td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="rounded-circle bg-primary bg-opacity-10 d-flex align-items-center justify-content-center me-2" style="width: 40px; height: 40px;">
                                        <i class="bi bi-person-fill text-primary"></i>
                                    </div>
                                    <div>
                                        <div class="fw-bold">{{ $user->name }}</div>
                                        <small class="text-muted">ID: {{ $user->id }}</small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div>{{ $user->email }}</div>
                                @if($user->npsn)
                                <small class="text-muted">NPSN: {{ $user->npsn }}</small>
                                @endif
                            </td>
                            <td>
                                <span class="badge bg-info">{{ $user->role->name }}</span>
                            </td>
                            <td>{{ $user->employee->nip ?? '-' }}</td>
                            <td>
                                @if($user->employee && $user->employee->activeAssignment)
                                    <div class="fw-bold">{{ $user->employee->activeAssignment->school->name }}</div>
                                    <small class="text-muted">{{ $user->employee->activeAssignment->notes ?? 'Tidak ada jabatan' }}</small>
                                @else
                                    <span class="text-muted fst-italic">Belum dipetakan</span>
                                @endif
                            </td>
                            <td>
                                @if($user->employee && $user->employee->activeAssignment)
                                    <span class="badge bg-success">
                                        <i class="bi bi-check-circle me-1"></i>Aktif
                                    </span>
                                @else
                                    <span class="badge bg-secondary">
                                        <i class="bi bi-dash-circle me-1"></i>Tidak Aktif
                                    </span>
                                @endif
                            </td>
                            <td class="text-center">
                                <div class="btn-group btn-group-sm" role="group">
                                    @if($user->employee && $user->employee->activeAssignment)
                                        <a href="{{ route('superadmin.user-school-mapping.edit', $user->id) }}" class="btn btn-outline-warning" title="Edit Mapping">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <form action="{{ route('superadmin.user-school-mapping.destroy', $user->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Hapus mapping user ini?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-outline-danger" title="Hapus Mapping">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    @else
                                        <a href="{{ route('superadmin.user-school-mapping.create') }}?user_id={{ $user->id }}" class="btn btn-outline-primary" title="Buat Mapping">
                                            <i class="bi bi-plus-circle"></i>
                                        </a>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox display-4 d-block mb-3 opacity-50"></i>
                                <p class="mb-0">Tidak ada data user</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($users->hasPages())
            <div class="d-flex justify-content-center mt-4">
                {{ $users->links() }}
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
