@extends('layouts.master')

@section('content')
<div class="container">
    <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem;">
        <h3 style="font-weight: 800; margin: 0;">الرسائل الواردة</h3>
        <?php $unread = \App\Contact::where('is_read', 0)->count(); ?>
        @if($unread > 0)
            <span class="badge badge-danger" style="font-size: 0.85em; padding: 5px 12px;">{{ $unread }} رسالة جديدة</span>
        @endif
    </div>
    <hr>

    @if($messages->count() > 0)
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>#</th>
                    <th>المرسل</th>
                    <th>الموضوع</th>
                    <th>التاريخ</th>
                    <th>الحالة</th>
                    <th style="text-align: center;">إجراءات</th>
                </tr>
            </thead>
            <tbody>
                @foreach($messages as $msg)
                <tr style="{{ !$msg->is_read ? 'font-weight: 700; background: #fffbe6;' : '' }}">
                    <td>{{ $msg->id }}</td>
                    <td>
                        <div>{{ $msg->name }}</div>
                        <small style="color: #888;">{{ $msg->email }}</small>
                    </td>
                    <td>{{ $msg->subject }}</td>
                    <td style="direction: ltr; text-align: right;">{{ $msg->created_at }}</td>
                    <td>
                        @if($msg->is_read)
                            <span style="color: #888;">مقروءة</span>
                        @else
                            <span class="badge badge-danger">جديدة</span>
                        @endif
                    </td>
                    <td style="text-align: center; white-space: nowrap;">
                        <a href="/admin/contacts/{{ $msg->id }}" class="btn btn-sm btn-outline-primary">عرض</a>
                        <form class="delete" method="POST" action="/admin/contacts/{{ $msg->id }}/delete" style="display: inline;">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-sm btn-danger">حذف</button>
                        </form>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    @else
    <div class="alert alert-info text-center" style="padding: 2rem;">لا توجد رسائل حالياً</div>
    @endif
</div>
@endsection
