@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">

                        <!-- Header card -->
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <div>
                                <h4 class="card-title mb-0">Data Aduan</h4>
                                <p class="card-description mb-0">
                                    Daftar aduan masyarakat
                                </p>
                            </div>
                        </div>

                        <!-- Tabel -->
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Nama Pelapor</th>
                                        <th>Alamat</th>
                                        <th>Tanggal Aduan</th>
                                        <th>Status</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse ($aduan as $item)
                                        <tr>
                                            <td>{{ $item->nama_pelapor }}</td>
                                            <td>{{ $item->alamat_pelapor }}</td>
                                            <td>
                                                {{ \Carbon\Carbon::parse($item->tanggal_aduan)->format('d M Y') }}
                                            </td>
                                            <td>
                                                @if ($item->tanggapan_admin)
                                                    <label class="badge badge-success">Ditanggapi</label>
                                                @else
                                                    <label class="badge badge-danger">Belum</label>
                                                @endif
                                            </td>
                                            <td class="text-center">
                                                <a href="{{ route('admin.aduan.show', $item->id_aduan) }}"
                                                    class="btn btn-warning btn-sm me-1" title="Tanggapi">
                                                    <i class="bi bi-pencil-square"></i>
                                                </a>

                                                <form action="{{ route('admin.aduan.destroy', $item->id_aduan) }}"
                                                    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 aduan 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 Aduan?',
                    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
