@extends('layouts.app')

@section('content')
<div class="container-fluid">
    <div class="row mb-3">
        <div class="col-md-6">
            <h4><i class="bi bi-megaphone-fill me-2"></i>Manajemen Informasi Terkini</h4>
        </div>
        <div class="col-md-6 text-end">
            <a href="{{ route('admin-dinas.announcements.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i> Tambah Pengumuman
            </a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card border-0 shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="bg-light">
                        <tr>
                            <th width="5%">#</th>
                            <th width="25%">Judul</th>
                            <th width="30%">Konten</th>
                            <th width="10%">Tipe</th>
                            <th width="10%">Popup</th>
                            <th width="10%">Status</th>
                            <th width="5%">Urutan</th>
                            <th width="5%">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($announcements as $announcement)
                            <tr>
                                <td>{{ $loop->iteration + ($announcements->currentPage() - 1) * $announcements->perPage() }}</td>
                                <td><strong>{{ $announcement->title }}</strong></td>
                                <td>{{ Str::limit($announcement->content, 100) }}</td>
                                <td>
                                    <span class="badge bg-{{ $announcement->type == 'info' ? 'info' : ($announcement->type == 'warning' ? 'warning' : ($announcement->type == 'success' ? 'success' : 'danger')) }}">
                                        {{ ucfirst($announcement->type) }}
                                    </span>
                                </td>
                                <td>
                                    @if($announcement->is_popup)
                                        <span class="badge bg-success"><i class="bi bi-check-circle"></i> Ya</span>
                                    @else
                                        <span class="badge bg-secondary"><i class="bi bi-x-circle"></i> Tidak</span>
                                    @endif
                                </td>
                                <td>
                                    <div class="form-check form-switch">
                                        <input class="form-check-input toggle-status" type="checkbox" 
                                               data-id="{{ $announcement->id }}" 
                                               {{ $announcement->is_active ? 'checked' : '' }}>
                                    </div>
                                </td>
                                <td>{{ $announcement->order }}</td>
                                <td>
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{{ route('admin-dinas.announcements.edit', $announcement) }}" 
                                           class="btn btn-outline-primary" title="Edit">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <form action="{{ route('admin-dinas.announcements.destroy', $announcement) }}" 
                                              method="POST" class="d-inline"
                                              onsubmit="return confirm('Yakin ingin menghapus pengumuman ini?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-outline-danger" title="Hapus">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center py-4">
                                    <i class="bi bi-inbox fs-1 text-muted"></i>
                                    <p class="text-muted mt-2">Belum ada pengumuman</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($announcements->hasPages())
                <div class="mt-3">
                    {{ $announcements->links() }}
                </div>
            @endif
        </div>
    </div>
</div>

@push('scripts')
<script>
$(document).ready(function() {
    $('.toggle-status').on('change', function() {
        const announcementId = $(this).data('id');
        const isChecked = $(this).is(':checked');
        
        $.ajax({
            url: `/admin-dinas/announcements/${announcementId}/toggle-status`,
            type: 'POST',
            data: {
                _token: '{{ csrf_token() }}'
            },
            success: function(response) {
                if (response.success) {
                    toastr.success(response.message);
                }
            },
            error: function() {
                toastr.error('Gagal mengubah status pengumuman');
            }
        });
    });
});
</script>
@endpush
@endsection
