@extends('admin.template')

@section('title', 'Data Aduan TPS')

@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 -->
                        <div class="mb-3 d-flex justify-content-between align-items-center">
                            <div>
                                <h4 class="mb-0 card-title">Data Aduan TPS</h4>
                                <p class="mb-0 card-description">
                                    Daftar aduan masyarakat terhadap TPS
                                </p>
                            </div>
                            <div class="mb-3 d-flex align-items-center">

                                <!-- SEARCH -->
                                <form action="{{ route('admin.aduan.index') }}" method="GET"
                                    style="width:300px; margin-right:20px;">
                                    <input type="text" name="search" value="{{ request('search') }}"
                                        class="form-control" placeholder="Cari Aduan...">
                                </form>

                            </div>
                        </div>

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

                                                <form action="{{ route('admin.aduan.destroy', $item->id_aduan) }}"
                                                    method="POST" class="d-inline form-hapus">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-danger btn-sm"
                                                        title="Hapus Aduan">
                                                        <i class="bi bi-trash"></i>
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="6" class="text-center text-muted">
                                                Data aduan belum tersedia
                                            </td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                        <!-- PAGINATION -->
                        <div class="mt-3">
                            {{ $aduan->appends(request()->query())->links() }}
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>

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

                Swal.fire({
                    title: 'Hapus Aduan?',
                    text: 'Data yang dihapus tidak dapat dikembalikan!',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#d33',
                    cancelButtonColor: '#6c757d',
                    confirmButtonText: 'Ya, Hapus',
                    cancelButtonText: 'Batal'
                }).then((result) => {
                    if (result.isConfirmed) {
                        form.submit();
                    }
                });
            });
        });
    </script>

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