@extends('admin.template')

@section('title', 'Data Informasi')

@section('content')

    <style>
        .isi-truncate-div {
            max-width: 350px;
            white-space: normal !important;
            word-wrap: break-word !important;
            overflow-wrap: break-word !important;
        }

        .table td {
            vertical-align: top;
        }
    </style>

    <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="mb-3 d-flex justify-content-between">
                            <div>
                                <h4 class="mb-0 card-title">Data Informasi</h4>
                                <p class="mb-0 card-description">
                                    Daftar semua berita dan pengumuman
                                </p>
                            </div>
                            <div class="mb-3 d-flex align-items-center">

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

                                <!-- BUTTON TAMBAH -->
                                <a href="{{ route('admin.informasi.create') }}" class="btn btn-primary">
                                    <i class="bi bi-plus-lg"></i> Tambah
                                </a>

                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Kategori</th>
                                        <th>Judul</th>
                                        <th>Gambar</th>
                                        <th>Tanggal</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    @foreach ($informasi as $item)
                                        <tr>

                                            <td>
                                                @if ($item->kategori_informasi == 'berita')
                                                    <label class="badge badge-success">Berita</label>
                                                @elseif ($item->kategori_informasi == 'pengumuman')
                                                    <label class="badge badge-warning">Pengumuman</label>
                                                @endif
                                            </td>

                                            <td>{{ $item->judul_informasi }}</td>

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



                                            <td>
                                                {{ \Carbon\Carbon::parse($item->tanggal_informasi)->format('d-m-Y') }}
                                            </td>

                                            <td class="text-center">

                                                <a href="{{ route('admin.informasi.edit', $item->id_informasi) }}"
                                                    class="btn btn-warning btn-sm me-1" title="Edit">
                                                    <i class="bi bi-pencil-square"></i>
                                                </a>

                                                <form action="{{ route('admin.informasi.destroy', $item->id_informasi) }}"
                                                    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>
                        <!-- PAGINATION -->
                        <div class="mt-3">
                            {{ $informasi->appends(request()->query())->links() }}
                        </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 Informasi?',
                    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
