@extends('layouts.app')

@section('title', 'Pembimbing Sekolah - MAGANGIN')
@section('page-title', 'Pembimbing Sekolah')

@section('content')
<div class="card">
    <div class="card-header bg-white">
        <div class="row align-items-center">
            <div class="col-12 col-md-6 mb-2 mb-md-0">
                <h5 class="mb-0"><i class="fas fa-chalkboard-teacher me-2"></i>Kelola Data Pembimbing Sekolah</h5>
            </div>
            <div class="col-12 col-md-6 text-md-end">
                <button type="button" class="btn btn-primary w-100 w-md-auto" data-bs-toggle="modal" data-bs-target="#addPembimbingModal">
                    <i class="fas fa-plus me-2"></i>Tambah Pembimbing
                </button>
            </div>
        </div>
        
        <div class="row mt-3">
            <div class="col-12 col-md-8">
                <form method="GET" action="{{ route('pembimbing-sekolah') }}" class="d-flex flex-column flex-sm-row">
                    <input type="text" name="search" class="form-control me-sm-2 mb-2 mb-sm-0" 
                           placeholder="Cari nama pembimbing..." 
                           value="{{ request('search') }}">
                    <div class="d-flex">
                        <button type="submit" class="btn btn-outline-primary flex-fill"><i class="fas fa-search"></i></button>
                        @if(request('search'))
                            <a href="{{ route('pembimbing-sekolah') }}" class="btn btn-outline-secondary ms-2"><i class="fas fa-times"></i></a>
                        @endif
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="card-body">
        @if($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
        
        {{-- Desktop Table --}}
        <div class="table-responsive d-none d-md-block">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th width="5%" class="text-center">No</th>
                        <th>Nama Pembimbing</th>
                        <th width="20%">No. Telepon</th>
                        <th width="15%" class="text-center">Tanggal Dibuat</th>
                        <th width="8%" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($pembimbing as $index => $item)
                    <tr>
                        <td>{{ $pembimbing->firstItem() + $index }}</td>
                        <td><strong>{{ $item->nama_pembimbing }}</strong></td>
                        <td>
                            <div class="d-flex align-items-center">
                                <i class="fas fa-phone text-primary me-2"></i>
                                <span>{{ $item->no_telp }}</span>
                            </div>
                        </td>
                        <td class="text-center">{{ $item->created_at->format('d/m/Y') }}</td>
                        <td>
                            <div class="btn-group text-center" role="group">
                                <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#editPembimbingModal{{ $item->id }}">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <button class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#deletePembimbingModal{{ $item->id }}">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="text-center text-muted py-4">
                            <i class="fas fa-chalkboard-teacher fa-3x mb-3"></i>
                            <p>{{ request('search') ? 'Tidak ditemukan data pembimbing' : 'Belum ada data pembimbing sekolah yang terdaftar' }}</p>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Mobile Cards --}}
        <div class="d-block d-md-none">
            @forelse($pembimbing as $index => $item)
            <div class="card mb-3 border-start border-primary border-4">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <h6 class="fw-bold mb-1">{{ $item->nama_pembimbing }}</h6>
                        <small class="text-muted">#{{ $pembimbing->firstItem() + $index }}</small>
                    </div>
                    <div class="row">
                        <div class="col-6">
                            <small class="text-muted">No. Telepon:</small><br>
                            <div class="d-flex align-items-center">
                                <i class="fas fa-phone text-primary me-1"></i>
                                <small>{{ $item->no_telp }}</small>
                            </div>
                        </div>
                        <div class="col-6">
                            <small class="text-muted">Tanggal Dibuat:</small><br>
                            <small>{{ $item->created_at->format('d/m/Y') }}</small>
                        </div>
                    </div>
                    <div class="d-flex gap-2 mt-3">
                        <button class="btn btn-sm btn-warning flex-fill" data-bs-toggle="modal" data-bs-target="#editPembimbingModal{{ $item->id }}">
                            <i class="fas fa-edit me-1"></i>Edit
                        </button>
                        <button class="btn btn-sm btn-danger flex-fill" data-bs-toggle="modal" data-bs-target="#deletePembimbingModal{{ $item->id }}">
                            <i class="fas fa-trash me-1"></i>Hapus
                        </button>
                    </div>
                </div>
            </div>
            @empty
            <div class="text-center text-muted py-5">
                <i class="fas fa-chalkboard-teacher fa-3x mb-3"></i>
                <p>{{ request('search') ? 'Tidak ditemukan data pembimbing' : 'Belum ada data pembimbing sekolah yang terdaftar' }}</p>
            </div>
            @endforelse
        </div>
        
        @if($pembimbing->hasPages())
            {{ $pembimbing->appends(request()->query())->links('pagination::bootstrap-4') }}
        @endif
    </div>
</div>

<!-- Add Pembimbing Modal -->
<div class="modal fade" id="addPembimbingModal" tabindex="-1" aria-labelledby="addPembimbingModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('pembimbing-sekolah.store') }}" method="POST">
                @csrf
                <div class="modal-header bg-success text-white">
                    <h5 class="modal-title" id="addPembimbingModalLabel">
                        <i class="fas fa-plus me-2"></i>Tambah Pembimbing Sekolah Baru
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="nama_pembimbing" class="form-label">Nama Pembimbing <span class="text-danger">*</span></label>
                        <input type="text" 
                               class="form-control @error('nama_pembimbing') is-invalid @enderror" 
                               id="nama_pembimbing" 
                               name="nama_pembimbing" 
                               placeholder="Masukkan nama lengkap pembimbing"
                               value="{{ old('nama_pembimbing') }}"
                               required>
                        @error('nama_pembimbing')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <div class="form-text">
                            <i class="fas fa-info-circle me-1"></i>
                            Masukkan nama lengkap guru pembimbing dari sekolah
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="no_telp" class="form-label">No. Telepon <span class="text-danger">*</span></label>
                        <input type="tel" 
                               class="form-control @error('no_telp') is-invalid @enderror" 
                               id="no_telp" 
                               name="no_telp" 
                               placeholder="Contoh: 08123456789"
                               value="{{ old('no_telp') }}"
                               required>
                        @error('no_telp')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <div class="form-text">
                            <i class="fas fa-info-circle me-1"></i>
                            Masukkan nomor telepon yang bisa dihubungi untuk koordinasi
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-1"></i>Batal
                    </button>
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-save me-1"></i>Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

@foreach($pembimbing as $item)
<!-- Edit Pembimbing Modal -->
<div class="modal fade" id="editPembimbingModal{{ $item->id }}" tabindex="-1" aria-labelledby="editPembimbingModalLabel{{ $item->id }}" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('pembimbing-sekolah.update', $item->id) }}" method="POST">
                @csrf
                @method('PUT')
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title" id="editPembimbingModalLabel{{ $item->id }}">
                        <i class="fas fa-edit me-2"></i>Edit Pembimbing Sekolah
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="edit_nama_pembimbing{{ $item->id }}" class="form-label">Nama Pembimbing <span class="text-danger">*</span></label>
                        <input type="text" 
                               class="form-control" 
                               id="edit_nama_pembimbing{{ $item->id }}" 
                               name="nama_pembimbing" 
                               value="{{ $item->nama_pembimbing }}"
                               required>
                        <div class="form-text">
                            <i class="fas fa-info-circle me-1"></i>
                            Pastikan nama pembimbing sesuai dengan data resmi sekolah
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="edit_no_telp{{ $item->id }}" class="form-label">No. Telepon <span class="text-danger">*</span></label>
                        <input type="tel" 
                               class="form-control" 
                               id="edit_no_telp{{ $item->id }}" 
                               name="no_telp" 
                               value="{{ $item->no_telp }}"
                               required>
                        <div class="form-text">
                            <i class="fas fa-info-circle me-1"></i>
                            Nomor telepon aktif untuk komunikasi sekolah
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-1"></i>Batal
                    </button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i>Perbarui
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Pembimbing Modal -->
<div class="modal fade" id="deletePembimbingModal{{ $item->id }}" tabindex="-1" aria-labelledby="deletePembimbingModalLabel{{ $item->id }}" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('pembimbing-sekolah.destroy', $item->id) }}" method="POST">
                @csrf
                @method('DELETE')
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title" id="deletePembimbingModalLabel{{ $item->id }}">
                        <i class="fas fa-exclamation-triangle me-2"></i>Konfirmasi Hapus
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="text-center">
                        <div class="mb-3">
                            <i class="fas fa-exclamation-triangle text-danger" style="font-size: 3rem;"></i>
                        </div>
                        <h6>Apakah Anda yakin ingin menghapus pembimbing sekolah ini?</h6>
                        <div class="alert alert-warning mt-3">
                            <strong>{{ $item->nama_pembimbing }}</strong><br>
                            <small>{{ $item->no_telp }}</small>
                        </div>
                        <p class="text-muted small">
                            <i class="fas fa-info-circle me-1"></i>
                            Tindakan ini tidak dapat dibatalkan. Pastikan tidak ada data siswa yang terkait dengan pembimbing ini.
                        </p>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-1"></i>Batal
                    </button>
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash me-1"></i>Hapus
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endforeach

@endsection

@section('scripts')
<script>
    // Auto close modal after form submission
    document.addEventListener('DOMContentLoaded', function() {
        // If there are validation errors, show the add modal
        @if($errors->any() && old('_method') != 'PUT')
            var addModal = new bootstrap.Modal(document.getElementById('addPembimbingModal'));
            addModal.show();
        @endif
        
        // Format phone number input
        document.querySelectorAll('input[type="tel"]').forEach(function(input) {
            input.addEventListener('input', function(e) {
                // Remove all non-digits
                let value = e.target.value.replace(/\D/g, '');
                
                // Limit to 15 digits
                if (value.length > 15) {
                    value = value.slice(0, 15);
                }
                
                e.target.value = value;
            });
        });
    });
</script>
@endsection