@extends('layouts.dashboard')

@section('title', 'Call Summary Report')

@section('content')
<div class="content-wrapper">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-1">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard.reports') }}">Reports</a></li>
                    <li class="breadcrumb-item active">Call Summary</li>
                </ol>
            </nav>
            <h4 class="mb-0">Call Summary Report</h4>
        </div>
        @if($canExport)
        <a href="{{ route('dashboard.reports.call-summary.export', request()->query()) }}" class="btn btn-success">
            <i class="bi bi-file-earmark-excel me-1"></i> Export to Excel
        </a>
        @endif
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('dashboard.reports.call-summary') }}" class="row g-3">
                @if(isset($clients) && $clients->count() > 0)
                <div class="col-md-3">
                    <label class="form-label">Organization</label>
                    <select name="client_id" class="form-select">
                        <option value="">All Organizations</option>
                        @foreach($clients as $client)
                        <option value="{{ $client->id }}" {{ ($clientId ?? '') == $client->id ? 'selected' : '' }}>
                            {{ $client->name }}
                        </option>
                        @endforeach
                    </select>
                </div>
                @endif
                <div class="col-md-2">
                    <label class="form-label">Date From</label>
                    <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Date To</label>
                    <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Call Type</label>
                    <select name="call_type" class="form-select">
                        <option value="">All Types</option>
                        <option value="incoming" {{ $callType == 'incoming' ? 'selected' : '' }}>Incoming</option>
                        <option value="outgoing" {{ $callType == 'outgoing' ? 'selected' : '' }}>Outgoing</option>
                        <option value="missed" {{ $callType == 'missed' ? 'selected' : '' }}>Missed</option>
                        <option value="rejected" {{ $callType == 'rejected' ? 'selected' : '' }}>Rejected</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Status</label>
                    <select name="status_key" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(($statusOptions ?? []) as $key => $label)
                        <option value="{{ $key }}" {{ ($statusKey ?? '') == $key ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">User</label>
                    <select name="user_id" class="form-select">
                        <option value="">All Users</option>
                        @foreach($users as $user)
                        <option value="{{ $user->id }}" {{ $userId == $user->id ? 'selected' : '' }}>
                            {{ $user->name }}
                        </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <div class="form-check mb-2">
                        <input class="form-check-input" type="checkbox" name="has_note" value="1" id="has_note" {{ !empty($hasNote) ? 'checked' : '' }}>
                        <label class="form-check-label" for="has_note">Only with notes</label>
                    </div>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-funnel me-1"></i> Filter
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Summary Stats -->
    <div class="row g-3 mb-4">
        <div class="col-md-2">
            <div class="card border-0 shadow-sm">
                <div class="card-body text-center">
                    <h3 class="text-primary mb-0">{{ number_format($summary['total']) }}</h3>
                    <small class="text-muted">Total Calls</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-success bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-success mb-0">{{ number_format($summary['incoming']) }}</h3>
                    <small class="text-muted">Incoming</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-info bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-info mb-0">{{ number_format($summary['outgoing']) }}</h3>
                    <small class="text-muted">Outgoing</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-danger bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-danger mb-0">{{ number_format($summary['missed']) }}</h3>
                    <small class="text-muted">Missed</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-warning bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-warning mb-0">{{ gmdate('H:i:s', $summary['total_duration'] ?? 0) }}</h3>
                    <small class="text-muted">Total Duration</small>
                </div>
            </div>
        </div>
        <div class="col-md-2">
            <div class="card border-0 shadow-sm bg-secondary bg-opacity-10">
                <div class="card-body text-center">
                    <h3 class="text-secondary mb-0">{{ gmdate('i:s', $summary['avg_duration'] ?? 0) }}</h3>
                    <small class="text-muted">Avg Duration</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Chart -->
    <div class="row g-4 mb-4">
        <div class="col-md-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-transparent">
                    <h6 class="mb-0">Call Trend</h6>
                </div>
                <div class="card-body">
                    <canvas id="callTrendChart" height="120"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-transparent">
                    <h6 class="mb-0">Call Type Distribution</h6>
                </div>
                <div class="card-body">
                    <canvas id="callTypeChart" height="200"></canvas>
                </div>
            </div>
        </div>
    </div>

    <!-- Report Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-transparent">
            <h6 class="mb-0">Call Details</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>User</th>
                            <th>Phone Number</th>
                            <th>Type</th>
                            <th>Status</th>
                            <th>Note</th>
                            <th>Duration</th>
                            <th>SMS Sent</th>
                            <th>Time</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($report as $log)
                        <tr>
                            <td>{{ $log->id }}</td>
                            <td>{{ $log->user->name ?? 'N/A' }}</td>
                            <td><span class="masked-data">{{ $log->phone_number }}</span></td>
                            <td>
                                @switch(strtoupper($log->call_type))
                                @case('INCOMING')
                                <span class="badge badge-incoming">Incoming</span>
                                @break
                                @case('OUTGOING')
                                <span class="badge badge-outgoing">Outgoing</span>
                                @break
                                @case('MISSED')
                                <span class="badge badge-missed">Missed</span>
                                @break
                                @case('REJECTED')
                                <span class="badge bg-secondary">Rejected</span>
                                @break
                                @default
                                <span class="badge bg-secondary">{{ $log->call_type }}</span>
                                @endswitch
                            </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: 240px; white-space: normal;">{{ $log->status_note ?: '—' }}</td>
                            <td>{{ gmdate('i:s', $log->call_duration ?? 0) }}</td>
                            <td>
                                @if($log->sms_sent)
                                <span class="badge bg-success">Yes</span>
                                @else
                                <span class="badge bg-secondary">No</span>
                                @endif
                            </td>
                            <td>{{ $log->call_time }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" class="text-center text-muted py-4">No calls found for the selected criteria.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-4">
                {{ $report->appends(request()->query())->links() }}
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Call Trend Chart
        const callsByDay = @json($callsByDay);
        const labels = Object.keys(callsByDay);
        const data = Object.values(callsByDay);

        new Chart(document.getElementById('callTrendChart'), {
            type: 'line',
            data: {
                labels: labels,
                datasets: [{
                    label: 'Call Count',
                    data: data,
                    borderColor: '#667eea',
                    backgroundColor: 'rgba(102, 126, 234, 0.1)',
                    fill: true,
                    tension: 0.4
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        display: false
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });

        // Call Type Distribution Chart
        const callsByType = @json($callsByType);
        new Chart(document.getElementById('callTypeChart'), {
            type: 'doughnut',
            data: {
                labels: Object.keys(callsByType).map(s => s.charAt(0).toUpperCase() + s.slice(1)),
                datasets: [{
                    data: Object.values(callsByType),
                    backgroundColor: ['#28a745', '#007bff', '#dc3545', '#6c757d']
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'bottom'
                    }
                }
            }
        });
    });
</script>
@endpush
@endsection