@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 TPS</h4>
                                <p class="card-description mb-0">
                                    Daftar Tempat Pengelolaan Sampah (TPS)
                                </p>
                            </div>
                            <a href="{{ route('admin.tps.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 TPS</th>
                                        <th>Kategori</th>
                                        <th>Foto</th>
                                        <th>Status</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    @forelse ($tps as $item)
                                        <tr>
                                            <!-- NAMA -->
                                            <td>{{ $item->nama_tps }}</td>

                                            <!-- KATEGORI -->
                                            <td>
                                                {{ $item->kategori->nama_kategori ?? '-' }}
                                            </td>

                                            <!-- FOTO -->
                                            <td>
                                                @if ($item->foto_tps)
                                                    <img src="{{ asset('storage/' . $item->foto_tps) }}" alt="Foto TPS"
                                                        style="
                                                        width:200px;
                                                        height:auto;
                                                        border-radius:2px;
                                                        ">
                                                @else
                                                    <span class="text-muted">-</span>
                                                @endif
                                            </td>


                                            <!-- STATUS -->
                                            <td>
                                                @if ($item->status_tps == 'Aktif')
                                                    <label class="badge badge-success">Aktif</label>
                                                @elseif ($item->status_tps == 'Tidak Aktif')
                                                    <label class="badge badge-secondary">Tidak Aktif</label>
                                                @else
                                                    <label class="badge badge-warning">Pembangunan</label>
                                                @endif
                                            </td>

                                            <!-- AKSI -->
                                            <td class="text-center">
                                                <a href="{{ route('admin.tps.edit', $item->id_tps) }}"
                                                    class="btn btn-warning btn-sm me-1">
                                                    <i class="bi bi-pencil-square"></i>
                                                </a>

                                                <form action="{{ route('admin.tps.destroy', $item->id_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>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="text-center">
                                                Data TPS 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 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
