@extends('client.layouts.dashboard')

@section('title', 'Call Logs')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="mb-1">Call Logs</h4>
        <p class="text-muted mb-0">View all call activity from your organization's users</p>
    </div>
    <a href="{{ route('client.call-logs.export', request()->query()) }}" class="btn btn-success">
        <i class="bi bi-file-earmark-excel me-1"></i> Export Excel
    </a>
</div>

<!-- Stats Row -->
<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="stat-card text-center">
            <div class="stat-value text-primary">{{ $stats['total'] }}</div>
            <div class="stat-label">Total Calls</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card text-center">
            <div class="stat-value text-success">{{ $stats['incoming'] }}</div>
            <div class="stat-label">Incoming</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card text-center">
            <div class="stat-value text-primary">{{ $stats['outgoing'] }}</div>
            <div class="stat-label">Outgoing</div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card text-center">
            <div class="stat-value text-danger">{{ $stats['missed'] }}</div>
            <div class="stat-label">Missed</div>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="table-card mb-4">
    <div class="card-body p-3">
        <form method="GET" action="{{ route('client.call-logs') }}" class="row g-3">
            <div class="col-md-3">
                <input type="text" class="form-control" name="search" placeholder="Search phone number..." value="{{ request('search') }}">
            </div>
            <div class="col-md-2">
                <select class="form-select" name="user_id">
                    <option value="">All Users</option>
                    @foreach($users as $user)
                    <option value="{{ $user->id }}" {{ request('user_id') == $user->id ? 'selected' : '' }}>{{ $user->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" name="type">
                    <option value="">All Types</option>
                    <option value="incoming" {{ request('type') === 'incoming' ? 'selected' : '' }}>Incoming</option>
                    <option value="outgoing" {{ request('type') === 'outgoing' ? 'selected' : '' }}>Outgoing</option>
                    <option value="missed" {{ request('type') === 'missed' ? 'selected' : '' }}>Missed</option>
                </select>
            </div>
            <div class="col-md-2">
                <select class="form-select" name="status_key">
                    <option value="">All Statuses</option>
                    @foreach(($statusOptions ?? []) as $key => $label)
                    <option value="{{ $key }}" {{ request('status_key') === $key ? 'selected' : '' }}>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <input type="date" class="form-control" name="date_from" value="{{ request('date_from') }}" placeholder="From Date">
            </div>
            <div class="col-md-2">
                <input type="date" class="form-control" name="date_to" value="{{ request('date_to') }}" placeholder="To Date">
            </div>
            <div class="col-md-2 d-flex align-items-center">
                <div class="form-check mt-1">
                    <input class="form-check-input" type="checkbox" name="has_note" value="1" id="client_has_note" {{ request('has_note') ? 'checked' : '' }}>
                    <label class="form-check-label small" for="client_has_note">Only with notes</label>
                </div>
            </div>
            <div class="col-md-1">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="bi bi-funnel"></i>
                </button>
            </div>
        </form>
    </div>
</div>

<!-- Call Logs Table -->
<div class="table-card">
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>User</th>
                    <th>Phone Number</th>
                    <th>Type</th>
                    <th>Status</th>
                    <th>Note</th>
                    <th>Duration</th>
                    <th>Auto-Reply</th>
                    <th>Time</th>
                </tr>
            </thead>
            <tbody>
                @forelse($callLogs as $log)
                <tr>
                    <td>
                        <div class="d-flex align-items-center gap-2">
                            <div class="rounded-circle bg-success text-white d-flex align-items-center justify-content-center" style="width: 32px; height: 32px; font-size: 0.75rem;">
                                {{ strtoupper(substr($log->user->name ?? 'U', 0, 1)) }}
                            </div>
                            <span>{{ $log->user->name ?? 'Unknown' }}</span>
                        </div>
                    </td>
                    <td>
                        <code>{{ $log->phone_number }}</code>
                        @if($log->contact_name)
                        <br><small class="text-muted">{{ $log->contact_name }}</small>
                        @endif
                    </td>
                    <td>
                        @if($log->call_type === 'incoming')
                        <span class="badge bg-info"><i class="bi bi-telephone-inbound me-1"></i>Incoming</span>
                        @elseif($log->call_type === 'outgoing')
                        <span class="badge bg-primary"><i class="bi bi-telephone-outbound me-1"></i>Outgoing</span>
                        @else
                        <span class="badge bg-danger"><i class="bi bi-telephone-x me-1"></i>Missed</span>
                        @endif
                    </td>
                    <td>
                        @if($log->status_label)
                        <span class="badge bg-{{ $log->status_badge_class }}">{{ $log->status_label }}</span>
                        @else
                        <span class="text-muted">—</span>
                        @endif
                    </td>
                    <td style="max-width: 220px; white-space: normal;">
                        {{ $log->status_note ?: '—' }}
                    </td>
                    <td>
                        @if($log->call_duration)
                        <span class="badge bg-light text-dark">
                            <i class="bi bi-clock me-1"></i>{{ gmdate('H:i:s', $log->call_duration) }}
                        </span>
                        @else
                        <span class="text-muted">-</span>
                        @endif
                    </td>
                    <td>
                        @if($log->sms_sent)
                        <span class="badge bg-success"><i class="bi bi-check me-1"></i>Sent</span>
                        @else
                        <span class="badge bg-secondary">No</span>
                        @endif
                    </td>
                    <td>
                        <small class="text-muted">
                            {{ optional($log->call_time)->format('M d, Y') ?? $log->created_at->format('M d, Y') }}<br>
                            {{ optional($log->call_time)->format('h:i A') ?? $log->created_at->format('h:i A') }}
                        </small>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="8" class="text-center py-5">
                        <div class="text-muted mb-3">
                            <i class="bi bi-telephone" style="font-size: 3rem;"></i>
                        </div>
                        <p class="mb-0">No call logs found</p>
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @if($callLogs->hasPages())
    <div class="card-footer bg-transparent">
        {{ $callLogs->withQueryString()->links() }}
    </div>
    @endif
</div>
@endsection