@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 mb-3">
                            <div>
                                <h4 class="card-title mb-0">Data Kategori TPS</h4>
                                <p class="card-description mb-0">
                                    Daftar Kategori Tempat Pengelolaan Sampah (TPS)
                                </p>
                            </div>
                            <a href="{{ route('admin.kategori.create') }}" class="btn btn-primary">
                                <i class="bi bi-plus-lg"></i> Tambah
                            </a>
                        </div>

                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Nama Kategori</th>
                                        <th>Foto</th>
                                        <th>Deskripsi</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($kategori as $item)
                                        <tr>
                                            <td>{{ $item->nama_kategori }}</td>
                                            <td>
                                                @if ($item->foto_kategori)
                                                    <img src="{{ asset('storage/' . $item->foto_kategori) }}" alt="Foto Kategori"
                                                        style="
                                                        width:200px;
                                                        height:auto;
                                                        border-radius:2px;
                                                        ">
                                                @else
                                                    <span class="text-muted">-</span>
                                                @endif
                                            </td>
                                            <td>{{ $item->deskripsi ?? '-' }}</td>
                                            <td>
                                                <a href="{{ route('admin.kategori.edit', $item->id_kategori_tps) }}"
                                                    class="btn btn-warning btn-sm me-1" title="Edit">
                                                    <i class="bi bi-pencil-square"></i>
                                                </a>
                                                <form action="{{ route('admin.tps.destroy', $item->id_kategori_tps) }}" 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>
                                    @endforeach
                                </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 Kategori TPS?',
                    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
