@extends('layouts.app')
@section('title', 'Communication')
@section('page_title', 'Communication Logs')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li><li class="breadcrumb-item active">Communication</li>
@endsection
@section('page_actions')
    @can('communication.send')<a href="{{ route('communication.compose') }}" class="btn btn-primary"><i class="bi bi-send me-1"></i>New Message</a>@endcan
@endsection

@section('content')
    <div class="card mb-3"><div class="card-body py-2">
        <form method="GET" class="d-flex flex-wrap gap-2 align-items-center">
            <span class="small text-muted">Channel:</span>
            <a href="{{ route('communication.index') }}" class="btn btn-sm {{ !$channel ? 'btn-primary' : 'btn-light border' }}">All</a>
            @foreach(App\Models\CommunicationLog::CHANNELS as $val => $label)
                <a href="{{ route('communication.index', ['channel'=>$val]) }}" class="btn btn-sm {{ $channel===$val ? 'btn-primary' : 'btn-light border' }}">{{ $label }}</a>
            @endforeach
        </form>
    </div></div>

    <div class="card"><div class="card-body">
        <table class="table table-hover align-middle">
            <thead><tr><th>When</th><th>Channel</th><th>To</th><th>Recipient</th><th>Message</th><th class="text-center">Status</th><th>By</th></tr></thead>
            <tbody>
                @forelse($logs as $log)
                    <tr>
                        <td class="small">{{ $log->sent_at?->format('d M, H:i') ?? $log->created_at->format('d M, H:i') }}</td>
                        <td><span class="badge bg-soft-{{ $log->channel_color }}">{{ App\Models\CommunicationLog::CHANNELS[$log->channel] }}</span></td>
                        <td class="small">{{ $log->to }}</td>
                        <td class="small">{{ $log->recipient_name ?: '—' }}</td>
                        <td class="small text-muted">@if($log->subject)<strong>{{ $log->subject }}</strong> · @endif{{ Str::limit($log->body, 50) }}</td>
                        <td class="text-center"><span class="badge bg-soft-{{ $log->status_color }} text-capitalize">{{ $log->status }}</span></td>
                        <td class="small">{{ $log->sender?->name ?? '—' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No messages sent yet.</td></tr>
                @endforelse
            </tbody>
        </table>
        {{ $logs->links() }}
    </div></div>
@endsection
