@extends('layouts.app')

@section('content')
<div class="container-fluid py-4">
    <div class="mb-4">
        <a href="{{ route('superadmin.user-school-mapping.index') }}" class="btn btn-outline-secondary btn-sm mb-3">
            <i class="bi bi-arrow-left me-1"></i>Kembali
        </a>
        <h4 class="mb-1 fw-bold">
            <i class="bi bi-diagram-2-fill text-primary me-2"></i>
            Mapping User ke Sekolah
        </h4>
        <p class="text-muted mb-0">Petakan user ke sekolah dengan mengisi form di bawah</p>
    </div>

    @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

    <div class="row">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-bottom py-3">
                    <h6 class="mb-0 fw-bold">Form Mapping</h6>
                </div>
                <div class="card-body">
                    <form action="{{ route('superadmin.user-school-mapping.store') }}" method="POST">
                        @csrf

                        <div class="mb-4">
                            <label class="form-label fw-bold required">
                                <i class="bi bi-person-fill text-muted me-1"></i>Pilih User
                            </label>
                            <select name="user_id" id="user_id" class="form-select @error('user_id') is-invalid @enderror" required>
                                <option value="">-- Pilih User --</option>
                                @foreach($users as $user)
                                <option value="{{ $user->id }}" {{ old('user_id', request('user_id')) == $user->id ? 'selected' : '' }}>
                                    {{ $user->name }} ({{ $user->email }}) - {{ $user->role->name }}
                                </option>
                                @endforeach
                            </select>
                            @error('user_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <small class="text-muted">Pilih user yang akan dipetakan ke sekolah</small>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold required">
                                <i class="bi bi-building text-muted me-1"></i>Pilih Sekolah
                            </label>
                            <select name="school_id" id="school_id" class="form-select @error('school_id') is-invalid @enderror" required>
                                <option value="">-- Pilih Sekolah --</option>
                                @foreach($schools as $school)
                                <option value="{{ $school->id }}" {{ old('school_id') == $school->id ? 'selected' : '' }}>
                                    {{ $school->name }} - NPSN: {{ $school->npsn }}
                                </option>
                                @endforeach
                            </select>
                            @error('school_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">
                                    <i class="bi bi-card-text text-muted me-1"></i>NIP
                                </label>
                                <input type="text" name="nip" class="form-control @error('nip') is-invalid @enderror" value="{{ old('nip') }}" placeholder="Nomor Induk Pegawai">
                                @error('nip')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">
                                    <i class="bi bi-telephone text-muted me-1"></i>No. Telepon
                                </label>
                                <input type="text" name="phone" class="form-control @error('phone') is-invalid @enderror" value="{{ old('phone') }}" placeholder="0812-3456-7890">
                                @error('phone')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-6">
                                <label class="form-label fw-bold required">
                                    <i class="bi bi-calendar text-muted me-1"></i>Tanggal Mulai
                                </label>
                                <input type="date" name="start_date" class="form-control @error('start_date') is-invalid @enderror" value="{{ old('start_date', date('Y-m-d')) }}" required>
                                @error('start_date')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">
                                    <i class="bi bi-briefcase text-muted me-1"></i>Jabatan
                                </label>
                                <input type="text" name="position" class="form-control @error('position') is-invalid @enderror" value="{{ old('position') }}" placeholder="Guru Matematika, Staff TU, dll">
                                @error('position')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <div class="d-flex justify-content-end gap-2 mt-4 pt-3 border-top">
                            <a href="{{ route('superadmin.user-school-mapping.index') }}" class="btn btn-secondary">
                                <i class="bi bi-x-circle me-1"></i>Batal
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-save me-1"></i>Simpan Mapping
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card border-0 shadow-sm bg-primary bg-opacity-10">
                <div class="card-body">
                    <h6 class="fw-bold mb-3">
                        <i class="bi bi-info-circle me-2"></i>Panduan
                    </h6>
                    <ul class="small mb-0">
                        <li class="mb-2">Pilih user yang akan dipetakan ke sekolah</li>
                        <li class="mb-2">Pilih sekolah tujuan penugasan</li>
                        <li class="mb-2">Isi NIP jika tersedia</li>
                        <li class="mb-2">Tanggal mulai menandai kapan penugasan dimulai</li>
                        <li class="mb-2">Jabatan bersifat opsional, bisa diisi sesuai posisi di sekolah</li>
                        <li>Setelah mapping dibuat, user dapat login dengan email atau NPSN sekolah</li>
                    </ul>
                </div>
            </div>

            <div class="card border-0 shadow-sm mt-3">
                <div class="card-body">
                    <h6 class="fw-bold mb-3">
                        <i class="bi bi-shield-check me-2"></i>Catatan Penting
                    </h6>
                    <p class="small text-muted mb-0">
                        Mapping user ke sekolah akan membuat relasi employee dan assignment. 
                        Jika user sudah memiliki mapping sebelumnya, mapping lama akan dinonaktifkan 
                        dan mapping baru akan menjadi aktif.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.required::after {
    content: " *";
    color: red;
}
</style>
@endsection
