@extends('layouts.app')

@section('title', 'Kelola KCD - Superadmin')

@section('content')
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">Kelola KCD</h1>
            <p class="text-muted small mt-1">Kelola Kawasan Pendidikan Dasar (KCD) dan mapping Kab/Kota</p>
        </div>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addKcdModal">
            <i class="bi bi-plus-circle"></i> Tambah KCD
        </button>
    </div>

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

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

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">Total KCD</h6>
                    <h3 class="mb-0">{{ count($kcds) }}</h3>
                    <small class="text-muted"><i class="bi bi-info-circle"></i> Registered</small>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card success">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">Total Kab/Kota</h6>
                    <h3 class="mb-0">27</h3>
                    <small class="text-success"><i class="bi bi-check-circle"></i> Jawa Barat</small>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card info">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">Ter-mapping</h6>
                    <h3 class="mb-0">27</h3>
                    <small class="text-info"><i class="bi bi-check2-circle"></i> Sudah mapping</small>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card warning">
                <div class="card-body">
                    <h6 class="card-title text-muted mb-2">Pengawas</h6>
                    <h3 class="mb-0">15</h3>
                    <small class="text-warning"><i class="bi bi-people"></i> Aktif</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Data Table -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">Daftar KCD</h5>
            <div class="input-group" style="max-width: 300px;">
                <span class="input-group-text bg-white border-end-0">
                    <i class="bi bi-search"></i>
                </span>
                <input type="text" class="form-control border-start-0" placeholder="Cari KCD...">
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Kode KCD</th>
                            <th>Nama KCD</th>
                            <th>Alamat</th>
                            <th>Kepala KCD</th>
                            <th>NIP</th>
                            <th>Status</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($kcds as $kcd)
                        <tr>
                            <td><strong>{{ $kcd->code }}</strong></td>
                            <td><strong>{{ $kcd->name }}</strong></td>
                            <td>
                                <small>{{ $kcd->address ?? '-' }}</small>
                            </td>
                            <td>
                                <small>
                                    @if($kcd->head_name)
                                        {{ $kcd->head_name }}
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </small>
                            </td>
                            <td>
                                <small>
                                    @if($kcd->head_nip)
                                        <code>{{ $kcd->head_nip }}</code>
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </small>
                            </td>
                            <td>
                                <span class="badge bg-{{ $kcd->is_active ? 'success' : 'danger' }}">
                                    {{ $kcd->is_active ? 'Aktif' : 'Nonaktif' }}
                                </span>
                            </td>
                            <td>
                                <button class="btn btn-sm btn-outline-primary edit-btn" data-bs-toggle="modal" data-bs-target="#editModal" data-id="{{ $kcd->id }}" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </button>
                                <button class="btn btn-sm btn-outline-danger delete-btn" data-bs-toggle="modal" data-bs-target="#deleteModal" data-id="{{ $kcd->id }}" title="Hapus">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="bi bi-inbox"></i> Tidak ada data KCD
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Add KCD Modal -->
<div class="modal fade" id="addKcdModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header border-bottom">
                <h5 class="modal-title">Tambah KCD Baru</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="{{ route('superadmin.kcd.store') }}">
                @csrf
                <div class="modal-body">
                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    @if (session('success'))
                        <div class="alert alert-success">{{ session('success') }}</div>
                    @endif

                    @if (session('error'))
                        <div class="alert alert-danger">{{ session('error') }}</div>
                    @endif
                    <!-- Data KCD Section -->
                    <div class="mb-4">
                        <h6 class="border-bottom pb-2 mb-3"><strong>Data KCD</strong></h6>
                        <div class="mb-3">
                            <label class="form-label">Kode KCD <span class="text-danger">*</span></label>
                            <input type="text" name="code" class="form-control" placeholder="Contoh: KCD 01" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Nama KCD <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" placeholder="Contoh: KCD Bandung Raya" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Alamat KCD</label>
                            <input type="text" name="address" class="form-control" placeholder="Contoh: Jl. Gatot Subroto No. 1">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">No. Telp KCD</label>
                            <input type="text" name="phone" class="form-control" placeholder="Contoh: (022) 1234567">
                        </div>
                    </div>

                    <!-- Data Kepala KCD Section -->
                    <div class="mb-4">
                        <h6 class="border-bottom pb-2 mb-3"><strong>Data Kepala KCD</strong></h6>
                        <div class="mb-3">
                            <label class="form-label">Nama Kepala KCD</label>
                            <input type="text" name="head_name" class="form-control" placeholder="Contoh: Budi Santoso">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">NIP Kepala KCD</label>
                            <input type="text" name="head_nip" class="form-control" placeholder="Contoh: 196512311990031015">
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-top">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary">Simpan KCD</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit Modal -->
<div class="modal fade" id="editModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header border-bottom">
                <h5 class="modal-title">Edit Data KCD</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="editForm" method="POST">
                @csrf
                @method('PUT')
                <div class="modal-body">
                    <!-- Data KCD Section -->
                    <div class="mb-4">
                        <h6 class="border-bottom pb-2 mb-3"><strong>Data KCD</strong></h6>
                        <div class="mb-3">
                            <label class="form-label">Kode KCD <span class="text-danger">*</span></label>
                            <input type="text" name="code" id="editCode" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Nama KCD <span class="text-danger">*</span></label>
                            <input type="text" name="name" id="editName" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Alamat KCD</label>
                            <input type="text" name="address" id="editAddress" class="form-control">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">No. Telp KCD</label>
                            <input type="text" name="phone" id="editPhone" class="form-control">
                        </div>
                    </div>

                    <!-- Data Kepala KCD Section -->
                    <div class="mb-4">
                        <h6 class="border-bottom pb-2 mb-3"><strong>Data Kepala KCD</strong></h6>
                        <div class="mb-3">
                            <label class="form-label">Nama Kepala KCD</label>
                            <input type="text" name="head_name" id="editHeadName" class="form-control">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">NIP Kepala KCD</label>
                            <input type="text" name="head_nip" id="editHeadNip" class="form-control" placeholder="Contoh: 196512311990031015">
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-top">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header border-bottom bg-danger text-white">
                <h5 class="modal-title"><i class="bi bi-exclamation-triangle-fill me-2"></i>Hapus KCD</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p class="mb-0">Apakah Anda yakin ingin menghapus <strong id="deleteKcdName"></strong>?</p>
                <p class="text-muted small mt-2">Tindakan ini tidak dapat dibatalkan dan semua data terkait akan dihapus.</p>
            </div>
            <div class="modal-footer border-top">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                <button type="button" class="btn btn-danger" id="confirmDelete">Hapus</button>
            </div>
        </div>
    </div>
</div>

<script>
    // Handle delete button clicks
    let selectedKcdId = null;
    let selectedKcdName = null;
    
    document.querySelectorAll('.delete-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            // Extract numeric ID from button data attribute
            selectedKcdId = this.dataset.id;
            // Get the row containing the button
            const row = this.closest('tr');
            // Extract KCD name from second column
            selectedKcdName = row.querySelector('td:nth-child(2)').textContent.trim();
            
            // Update modal with KCD name
            document.getElementById('deleteKcdName').textContent = selectedKcdName;
        });
    });
    
    // Handle confirm delete
    document.getElementById('confirmDelete').addEventListener('click', function() {
        if (selectedKcdId) {
            // Create a form and submit it to delete the KCD
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = `/superadmin/kcd/${selectedKcdId}`;
            form.innerHTML = `
                @csrf
                @method('DELETE')
            `;
            document.body.appendChild(form);
            form.submit();
        }
    });
    
    // Handle edit button clicks
    document.querySelectorAll('.edit-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            // Extract numeric ID from button data attribute
            const kcdId = this.dataset.id;
            // Get the row containing the button
            const row = this.closest('tr');
            
            // Extract data from the row
            const kcdCode = row.querySelector('td:nth-child(1)').textContent.trim();
            const kcdName = row.querySelector('td:nth-child(2)').textContent.trim();
            const kcdAddress = row.querySelector('td:nth-child(3)').textContent.trim();
            const kcdHeadName = row.querySelector('td:nth-child(4)').textContent.trim();
            const kcdHeadNip = row.querySelector('td:nth-child(5)').textContent.trim();
            
            // Populate form fields
            document.getElementById('editCode').value = kcdCode;
            document.getElementById('editName').value = kcdName;
            document.getElementById('editAddress').value = kcdAddress === '-' ? '' : kcdAddress;
            document.getElementById('editPhone').value = ''; // Will fetch from DB if needed
            document.getElementById('editHeadName').value = kcdHeadName === '-' ? '' : kcdHeadName;
            document.getElementById('editHeadNip').value = kcdHeadNip === '-' ? '' : kcdHeadNip;
            
            // Set form action with numeric ID
            document.getElementById('editForm').action = `/superadmin/kcd/${kcdId}`;
        });
    });
</script>

@endsection
