<x-app-layout>

    @include('pengaduan.create')
    @include('pengaduan.edit')
    @include('pengaduan.delete')

    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('DATA PENGADUAN') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-4xlxl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 flex justify-between items-center">

                    @hasanyrole('Siswa|GuruBK|WaliKelas')
                        <div class="alert alert-danger w-full" role="alert">
                            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                                {{ __('Informasi Pengaduan') }}
                            </h2>
                            <hr>

                            <p>
                                Untuk melihat hasil riwayat pengaduan, bisa dilihat di halaman <a
                                    class="underline hover:text-red-500" href="{{ route('siswa.index') }}">Detail Siswa</a>.
                                Pemberian tinjauan BK dilakukan
                                didalam jam kerja Bimbingan Konseling yang dpat dilihat di halaman <a
                                    class="underline hover:text-red-500" href="{{ route('jadwalbk.index') }}">Jadwal BK</a>
                            </p>
                            <br>

                            <div class="mb-3">
                                <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal"
                                    data-bs-target="#tambahModal">BUAT PENGADUAN</button>
                            </div>

                        </div>
                    @endhasanyrole

                </div>

                <x-table :tableId="'myTable_' . uniqid()">
                    <x-slot name="header">
                        <tr class="bg-gray-400 text-center">
                            <th>NO.</th>
                            <th>NAMA PENGADU</th>
                            <th>TANGGAL</th>
                            <th>METODE BIMBINGAN</th>
                            <th>JUDUL PENGADUAN</th>
                            <th>JENIS PENGADUAN BK</th>
                            <th>ISI KELUHAN</th>
                            <th>DOKUMENTASI</th>
                            <th>STATUS</th>
                            @hasrole('Siswa')
                                @if ($pengaduans->where('status', 0)->isNotEmpty())
                                    <th>AKSI</th>
                                @endif
                            @endhasrole

                            @hasanyrole('WaliKelas|GuruBK')
                                <th>AKSI</th>
                            @endhasrole
                        </tr>
                    </x-slot>

                    @php $num = 1; @endphp
                    @foreach ($pengaduans as $data)
                        <tr class="text-center">
                            <td>{{ $num++ }}</td>
                            <td>{{ $data->user->name }}</td>
                            <td>{{ $data->tanggal }}</td>
                            <td>{{ $data->metode }}</td>
                            <td>{{ $data->judul_pengaduan }}</td>
                            <td>{{ $data->jenis_pengaduan }}</td>
                            <td>{{ $data->isi_keluhan }}</td>
                            <td>
                                @if ($data->dokumentasi)
                                    <a class="text-blue-600 underline"
                                        href="{{ asset('storage/dokumentasi/' . $data->dokumentasi) }}"
                                        target="_blank">
                                        Lihat dokumentasi
                                    </a>
                                @else
                                    -
                                @endif
                            </td>
                            <td>
                                @if ($data->status == 0)
                                    <span class="badge rounded-pill text-bg-warning">Proses</span>
                                @elseif ($data->status == 1)
                                    <span class="badge rounded-pill text-bg-success">Sudah Dibalas</span>
                                @endif
                            </td>
                            @hasanyrole('Siswa|WaliKelas')
                                @if ($data->status == 0)
                                    <td>
                                        <button tag="a" type="button" class="btn btn-outline-warning"
                                            data-bs-toggle="modal"
                                            data-bs-target="#exampleModal_{{ $data->id_pengaduan_bk }}"><i
                                                class="fa-solid fa-pen-to-square"></i></button>
                                        <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal"
                                            data-bs-target="#hapusModal_{{ $data->id_pengaduan_bk }}"><i
                                                class="fa-solid fa-trash-can"></i></button>
                                    </td>
                                @endif
                            @endhasrole

                            @hasrole('GuruBK')
                                <td>
                                    @if ($data->status == 0)
                                        <form action="{{ route('pengaduan.verify', $data->id_pengaduan_bk) }}"
                                            method="POST">
                                            @csrf
                                            @method('PATCH')
                                            <button class="btn btn-outline-primary">
                                                Selesai
                                            </button>
                                        </form>
                                    @else
                                        <form action="{{ route('pengaduan.unverify', $data->id_pengaduan_bk) }}"
                                            method="POST">
                                            @csrf
                                            @method('PATCH')
                                            <button class="btn btn-outline-danger">
                                                Batalkan
                                            </button>
                                        </form>
                                    @endif
                                </td>
                            @endhasrole
                        </tr>
                    @endforeach
                </x-table>

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

    <script>
        function submitFilterForm() {
            document.getElementById("filterForm").submit();
        }
    </script>
</x-app-layout>
