@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <div>
                <h5 class="mb-0">Chat</h5>
                <small class="text-muted">Messages & chat logs</small>
            </div>
        </div>
        <hr class="my-0">

        <div class="card-body">

            @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif
            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $e)
                            <li>{{ $e }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            {{-- Filters --}}
            <form class="row g-2 mb-3" method="GET" action="{{ route('chats.index') }}">
                <div class="col-md-4">
                    <select name="course_id" class="form-control">
                        <option value="">All Courses</option>
                        @foreach($courses as $c)
                            <option value="{{ $c->id }}" {{ request('course_id') == $c->id ? 'selected' : '' }}>
                                {{ $c->title }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <button class="btn btn-secondary">Filter</button>
                    <a href="{{ route('chats.index') }}" class="btn btn-light">Reset</a>
                </div>
            </form>

            {{-- Chats table --}}
            <div class="table-responsive">
                <table class="table" id="chats-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Course</th>
                            <th>Sender</th>
                            <th>Receiver</th>
                            <th>Message</th>
                            <th>Flag</th>
                            <th>Date</th>
                            <th style="width:200px;">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($chats as $chat)
                            <tr>
                                <td>{{ $chat->id }}</td>
                                <td>{{ $chat->course?->title ?? '-' }}</td>
                                <td>{{ $chat->sender?->name ?? '-' }}</td>
                                <td>{{ $chat->receiver?->name ?? '-' }}</td>
                                <td>{{ \Illuminate\Support\Str::limit($chat->message, 60) }}</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>
                                <td class="d-flex gap-1 flex-wrap">
                                    <a href="{{ route('chats.show', $chat) }}" class="btn btn-sm btn-info">
                                        Open
                                    </a>

                                    @can('flag chats')
                                        <form method="POST" action="{{ route('chats.flag', $chat) }}">
                                            @csrf
                                            @method('PATCH')
                                            <button class="btn btn-sm btn-warning">
                                                {{ $chat->is_flagged ? 'Unflag' : 'Flag' }}
                                            </button>
                                        </form>
                                    @endcan

                                    @can('delete chats')
                                        <form method="POST" action="{{ route('chats.destroy', $chat) }}"
                                              onsubmit="return confirm('Delete this message?');">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-sm btn-danger">Delete</button>
                                        </form>
                                    @endcan

                                    
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center">No chats found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <hr class="my-4">

            {{-- Send message --}}
            @can('chat send')
            <h6 class="mb-3">Send Message</h6>

            <form method="POST" action="{{ route('chats.store') }}">
                @csrf

                <div class="row">
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Receiver</label>
                        <select name="receiver_id" class="form-control" required>
                            <option value="">Select Receiver</option>
                            @foreach($receivers as $u)
                                <option value="{{ $u->id }}" {{ old('receiver_id') == $u->id ? 'selected' : '' }}>
                                    {{ $u->name }} (ID: {{ $u->id }})
                                </option>
                            @endforeach
                        </select>
                        @error('receiver_id') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>

                    <div class="col-md-4 mb-3">
                        <label class="form-label">Course (optional)</label>
                        <select name="course_id" class="form-control">
                            <option value="">No Course</option>
                            @foreach($courses as $c)
                                <option value="{{ $c->id }}" {{ old('course_id') == $c->id ? 'selected' : '' }}>
                                    {{ $c->title }}
                                </option>
                            @endforeach
                        </select>
                        @error('course_id') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>

                    <div class="col-md-12 mb-3">
                        <label class="form-label">Message</label>
                        <textarea name="message" class="form-control" rows="3" required>{{ old('message') }}</textarea>
                        @error('message') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>
                </div>

                <button class="btn btn-primary">Send</button>
            </form>
            @endcan

        </div>
    </div>
</div>
@endsection

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