@extends('layouts.dashboard')

@section('title', 'Message Logs')
@section('page-title', 'Message Logs (SMS & WhatsApp)')

@section('content')
<div class="table-container">
    <!-- Filters -->
    <form action="{{ route('dashboard.sms-logs') }}" method="GET" class="row g-3 mb-4">
        <div class="col-md-2">
            <select name="channel" class="form-select">
                <option value="">All Channels</option>
                <option value="sms" {{ request('channel') == 'sms' ? 'selected' : '' }}>SMS</option>
                <option value="whatsapp" {{ request('channel') == 'whatsapp' ? 'selected' : '' }}>WhatsApp</option>
            </select>
        </div>

        <div class="col-md-2">
            <select name="status" class="form-select">
                <option value="">All Status</option>
                <option value="sent" {{ request('status') == 'sent' ? 'selected' : '' }}>Sent</option>
                <option value="failed" {{ request('status') == 'failed' ? 'selected' : '' }}>Failed</option>
                <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
            </select>
        </div>

        <div class="col-md-2">
            <input type="date" name="from_date" class="form-control" placeholder="From" value="{{ request('from_date') }}">
        </div>

        <div class="col-md-2">
            <input type="date" name="to_date" class="form-control" placeholder="To" value="{{ request('to_date') }}">
        </div>

        <div class="col-md-3">
            <button type="submit" class="btn btn-primary me-2">
                <i class="bi bi-filter"></i> Filter
            </button>
            <a href="{{ route('dashboard.sms-logs') }}" class="btn btn-outline-secondary">
                <i class="bi bi-x"></i> Clear
            </a>
        </div>
    </form>

    <!-- Results Info -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <span class="text-muted">Showing {{ $smsLogs->count() }} of {{ $smsLogs->total() }} message logs</span>
    </div>

    <!-- Message Logs Table -->
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>User</th>
                    <th>Channel</th>
                    <th>Phone / Contact</th>
                    <th>Message</th>
                    <th>Status</th>
                    <th>Sent At</th>
                </tr>
            </thead>
            <tbody>
                @forelse($smsLogs as $sms)
                <tr>
                    <td>{{ $sms->id }}</td>
                    <td>
                        @if($sms->user)
                        <a href="{{ route('dashboard.user-details', $sms->user_id) }}">
                            {{ $sms->user->name }}
                        </a>
                        @else
                        <span class="text-muted">N/A</span>
                        @endif
                    </td>
                    <td>
                        @if(($sms->channel ?? 'sms') == 'whatsapp')
                        <span class="badge bg-success"><i class="bi bi-whatsapp"></i> WhatsApp</span>
                        @else
                        <span class="badge bg-primary"><i class="bi bi-chat-text"></i> SMS</span>
                        @endif
                    </td>
                    <td>
                        <div>{{ $sms->phone_number }}</div>
                        @if($sms->contact_name)
                        <small class="text-muted">{{ $sms->contact_name }}</small>
                        @endif
                    </td>
                    <td>
                        <span title="{{ $sms->message }}">
                            {{ Str::limit($sms->message, 50) }}
                        </span>
                    </td>
                    <td>
                        @php
                        $statusClass = match(strtolower($sms->status)) {
                        'sent', 'delivered' => 'bg-success',
                        'pending' => 'bg-warning',
                        'failed' => 'bg-danger',
                        default => 'bg-secondary',
                        };
                        @endphp
                        <span class="badge {{ $statusClass }}">
                            {{ ucfirst($sms->status) }}
                        </span>
                    </td>
                    <td>{{ $sms->sent_at->format('M d, Y H:i:s') }}</td>
                </tr>
                @empty
                <tr>
                    <td colspan="7" class="text-center text-muted py-4">
                        No message logs found
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    <div class="d-flex justify-content-center mt-4">
        {{ $smsLogs->withQueryString()->links() }}
    </div>
</div>
@endsection