@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Course Chat Logs - {{ $course->title }}</h5>
            <hr/>
        </div>

        <div class="card-body">
            <div class="table-responsive">
                <table class="table" id="course-chats-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Sender</th>
                            <th>Receiver</th>
                            <th>Message</th>
                            <th>Flag</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($chats as $chat)
                            <tr>
                                <td>{{ $chat->id }}</td>
                                <td>{{ $chat->sender?->name }} ({{ $chat->sender_id }})</td>
                                <td>{{ $chat->receiver?->name }} ({{ $chat->receiver_id }})</td>
                                <td>{{ $chat->message }}</td>
                                <td>
                                    @if($chat->is_flagged)
                                        <span class="badge bg-danger">Flagged</span>
                                    @else
                                        <span class="badge bg-secondary">No</span>
                                    @endif
                                </td>
                                <td>{{ $chat->created_at?->format('Y-m-d H:i') }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center">No chats found for this course.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <a href="{{ route('chats.index') }}" class="btn btn-secondary mt-3">Back</a>
        </div>
    </div>
</div>
@endsection

@push('script')
<script>
$(document).ready(function () {
    $('#course-chats-table').DataTable({
        responsive: false,
        scrollX: true,
        pageLength: 10,
        order: [[0, 'desc']]
    });
});
</script>
@endpush