@extends('admin.template')

@section('content')
    <div class="content-wrapper">
        <div class="row">
            <div class="col-lg-12 grid-margin stretch-card">
                <div class="card">
                    <div class="card-body">

                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <div>
                                <h4 class="card-title mb-0">Data Sampah</h4>
                                <p class="card-description mb-0">
                                    Daftar Sampah Per Tahun di Kabupaten Nganjuk
                                </p>
                            </div>

                            <a href="{{ route('admin.sampah.create') }}" class="btn btn-primary">
                                <i class="bi bi-plus-lg"></i> Tambah
                            </a>


                        </div>

                        <!-- Tabel -->
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Tahun</th>
                                        <th>Total Sampah</th>
                                        <th>Total Kelola</th>
                                        <th>Total Daur Ulang</th>
                                        <th>Sisa Sampah</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    @forelse ($sampah as $item)
                                        <tr>
                                            <td>{{ $item->tahun }}</td>
                                            <td>{{ $item->total_sampah }}</td>
                                            <td>{{ $item->total_kelola }}</td>
                                            <td>{{ $item->total_daur_ulang }}</td>
                                            <td>{{ $item->sisa_sampah }}</td>
                                            <td class="text-center">
                                                <a href="{{ route('admin.sampah.edit', $item->id_sampah) }}"
                                                    class="btn btn-warning btn-sm me-1">
                                                    <i class="bi bi-pencil-square"></i>
                                                </a>

                                                <form action="{{ route('admin.sampah.destroy', $item->id_sampah) }}" method="POST"
                                                    class="form-hapus" style="display:inline;">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-danger btn-sm">
                                                        <i class="bi bi-trash"></i>
                                                    </button>
                                                </form>

                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="text-center">
                                                Data sampah belum tersedia
                                            </td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        document.querySelectorAll('.form-hapus').forEach(form => {
            form.addEventListener('submit', function(e) {
                e.preventDefault();

                Swal.fire({
                    title: 'Hapus Data Sampah?',
                    text: 'Data yang sudah dihapus tidak dapat dikembalikan!',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#d33',
                    cancelButtonColor: '#6c757d',
                    confirmButtonText: 'Ya, Hapus',
                    cancelButtonText: 'Batal',
                    didOpen: () => {
                        document.querySelector('.swal2-popup').style.fontFamily =
                            'Nunito, sans-serif';
                    }
                }).then((result) => {
                    if (result.isConfirmed) {
                        form.submit();
                    }
                });
            });
        });
    </script>


    @if (session('success'))
        <script>
            Swal.fire({
                icon: 'success',
                title: 'Berhasil',
                text: '{{ session('success') }}',
                timer: 2000,
                showConfirmButton: false
            });
        </script>
    @endif
@endsection
