@extends('admin.layouts.layout')
@section('title')اطلاعیه‌ها@endsection

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1">اطلاعیه‌ها</h4>
            <p class="text-muted mb-0">اطلاعیه‌هایی که به کاربران در پنل نمایش داده می‌شوند</p>
        </div>
        <a href="{{ route('admin.announcements.create') }}" class="btn btn-primary">
            <i class="ti ti-plus me-1"></i> اطلاعیه جدید
        </a>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible mb-4">
            {{ session('success') }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card">
        <div class="table-responsive">
            <table class="table border-top align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>#</th>
                        <th>عنوان اطلاعیه</th>
                        <th>نویسنده</th>
                        <th>تاریخ</th>
                        <th style="width:130px">عملیات</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($announcements as $announcement)
                        <tr>
                            <td><span class="text-muted small">{{ $loop->iteration }}</span></td>
                            <td>
                                <span class="fw-medium d-block">{{ $announcement->title }}</span>
                                <small class="text-muted" dir="ltr">{{ $announcement->slug }}</small>
                            </td>
                            <td>{{ $announcement->author?->first_name }} {{ $announcement->author?->last_name }}</td>
                            <td><span class="text-muted small">{{ $announcement->shamsiDate() }}</span></td>
                            <td>
                                <div class="d-flex gap-1">
                                    <a href="{{ route('panel.announcements.show', $announcement->slug) }}" target="_blank"
                                       class="btn btn-sm btn-outline-secondary">
                                        <i class="ti ti-eye ti-xs"></i>
                                    </a>
                                    <a href="{{ route('admin.announcements.edit', $announcement->slug) }}"
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="ti ti-edit ti-xs"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.announcements.destroy', $announcement->slug) }}"
                                          class="delete-form">
                                        @csrf @method('DELETE')
                                        <button type="button" class="btn btn-sm btn-outline-danger btn-delete"
                                                data-name="{{ $announcement->title }}">
                                            <i class="ti ti-trash ti-xs"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center py-5 text-muted">
                                <i class="ti ti-bell-off fs-3 d-block mb-2"></i>
                                هیچ اطلاعیه‌ای ثبت نشده است.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($announcements->isNotEmpty())
            <div class="card-footer text-muted small d-flex align-items-center justify-content-between">
                <span>{{ $announcements->total() }} اطلاعیه</span>
                {{ $announcements->links() }}
            </div>
        @endif
    </div>

</div>
@endsection

@section('customjs')
<script>
document.querySelectorAll('.btn-delete').forEach(function (btn) {
    btn.addEventListener('click', function () {
        var name = this.dataset.name;
        var form = this.closest('.delete-form');
        Swal.fire({
            title: 'حذف اطلاعیه',
            html: 'حذف <strong>' + name + '</strong> قابل بازگشت نیست. ادامه؟',
            icon: 'warning', showCancelButton: true,
            confirmButtonText: 'بله، حذف شود', cancelButtonText: 'انصراف',
            confirmButtonColor: '#d33', reverseButtons: true,
        }).then(function (r) { if (r.isConfirmed) form.submit(); });
    });
});
</script>
@endsection
