@extends('layouts.admin')

@section('page_title', 'Admin Dashboard')

@section('content')
    <!-- Stats Cards -->
    <div class="row g-4 mb-4">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-4 d-flex align-items-center">
                    <div class="icon-box bg-primary-subtle text-primary rounded-3 me-3">
                        <i class="bi bi-people-fill fs-4"></i>
                    </div>
                    <div>
                        <h6 class="text-muted small mb-1 fw-bold">Total Employees</h6>
                        <h4 class="fw-bold mb-0">{{ $stats['total_employees'] }}</h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-4 d-flex align-items-center">
                    <div class="icon-box bg-success-subtle text-success rounded-3 me-3">
                        <i class="bi bi-person-check-fill fs-4"></i>
                    </div>
                    <div>
                        <h6 class="text-muted small mb-1 fw-bold">Present Today</h6>
                        <div class="d-flex align-items-baseline">
                            <h4 class="fw-bold mb-0 me-2">{{ $stats['present_today'] }}</h4>
                            @if ($stats['late_today'] > 0)
                                <span class="text-danger smaller fw-bold" style="font-size: 0.75rem;"><i
                                        class="bi bi-caret-down-fill"></i>
                                    {{ $stats['late_today'] }} late</span>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-4 d-flex align-items-center">
                    <div class="icon-box bg-warning-subtle text-warning rounded-3 me-3">
                        <i class="bi bi-calendar-minus-fill fs-4"></i>
                    </div>
                    <div>
                        <h6 class="text-muted small mb-1 fw-bold">On Leave</h6>
                        <h4 class="fw-bold mb-0">{{ $stats['on_leave'] }}</h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-4 d-flex align-items-center">
                    <div class="icon-box bg-info-subtle text-info rounded-3 me-3">
                        <i class="bi bi-kanban-fill fs-4"></i>
                    </div>
                    <div>
                        <h6 class="text-muted small mb-1 fw-bold">Active Projects</h6>
                        <h4 class="fw-bold mb-0">{{ $stats['active_projects'] }}</h4>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <!-- Attendance Trend Chart -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-white border-0 p-4 pb-0 d-flex justify-content-between">
                    <h6 class="fw-bold mb-0">Attendance Trend (Current Month)</h6>
                    <div class="dropdown">
                        <button class="btn btn-sm btn-light border dropdown-toggle" type="button"
                            data-bs-toggle="dropdown">Last 7 Days</button>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="#">This Week</a></li>
                            <li><a class="dropdown-item" href="#">Last Week</a></li>
                        </ul>
                    </div>
                </div>
                <div class="card-body p-4">
                    <canvas id="attendanceChart" height="120"></canvas>
                </div>
            </div>

            <!-- Recent Activity Logs -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-0 p-4 pb-0 d-flex justify-content-between align-items-center">
                    <h6 class="fw-bold mb-0">System Activity Logs</h6>
                    <a href="{{ url('/admin/activity-logs') }}"
                        class="btn btn-sm btn-outline-primary rounded-pill px-3">View All</a>
                </div>
                <div class="card-body p-4">
                    <div class="list-group list-group-flush">
                        @forelse($recentLogs as $log)
                            <div class="list-group-item px-0 py-3 border-bottom dashed">
                                <div class="d-flex w-100 justify-content-between">
                                    <h6 class="mb-1 small fw-bold">
                                        {{ $log->user->name }}
                                        <span class="fw-normal text-muted">— {{ $log->action }}</span>
                                    </h6>
                                    <small class="text-muted">{{ $log->created_at->diffForHumans() }}</small>
                                </div>
                                <p class="mb-0 text-muted smaller d-flex align-items-center">
                                    <i class="bi bi-geo-alt me-1"></i> {{ $log->ip_address }}
                                    <span class="mx-2">•</span>
                                    <i class="bi bi-laptop me-1"></i> {{ Str::limit($log->user_agent, 40) }}
                                </p>
                            </div>
                        @empty
                            <div class="text-center py-4 text-muted">No activity recorded yet</div>
                        @endforelse
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Column Widgets -->
        <div class="col-lg-4">
            <!-- Progress Summary -->
            <div class="card border-0 shadow-sm mb-4 bg-primary text-white">
                <div class="card-body p-4">
                    <h6 class="fw-bold mb-4">Pending Approvals</h6>
                    <div class="d-flex align-items-center justify-content-between mb-2">
                        <span class="small opacity-75">Work Reports</span>
                        <span class="fw-bold">{{ $stats['pending_reports'] }}</span>
                    </div>
                    <div class="progress mb-4" style="height: 6px; background: rgba(255,255,255,0.2);">
                        <div class="progress-bar bg-white" style="width: 65%;"></div>
                    </div>
                    <a href="{{ route('admin.reports') }}" class="btn btn-light w-100 fw-bold py-2 text-decoration-none">Go
                        to Approvals <i class="bi bi-arrow-right ms-2"></i></a>
                </div>
            </div>

            <!-- Quick Actions -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-0 p-4 pb-0">
                    <h6 class="fw-bold mb-0">Quick Management</h6>
                </div>
                <div class="card-body p-4 d-grid gap-2">
                    <a href="{{ route('admin.users.create') }}" class="btn btn-light text-start border p-3 rounded-3">
                        <i class="bi bi-person-plus text-primary me-2"></i> Add New Employee
                    </a>
                    <a href="{{ route('admin.announcements.create') }}"
                        class="btn btn-light text-start border p-3 rounded-3">
                        <i class="bi bi-megaphone text-success me-2"></i> Broadcast Notice
                    </a>
                    <a href="{{ route('admin.projects.create') }}" class="btn btn-light text-start border p-3 rounded-3">
                        <i class="bi bi-folder-plus text-warning me-2"></i> Initiate Project
                    </a>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
        <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
        <script>
            const ctx = document.getElementById('attendanceChart').getContext('2d');
            new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: {!! json_encode($attendanceChart->pluck('date')->map(fn($d) => date('D, d M', strtotime($d)))) !!},
                    datasets: [{
                            label: 'Present',
                            data: {!! json_encode($attendanceChart->pluck('count')) !!},
                            backgroundColor: '#0d6efd',
                            borderRadius: 5,
                            barThickness: 20,
                        },
                        {
                            label: 'Expected',
                            data: {!! json_encode($attendanceChart->pluck('expected')) !!},
                            backgroundColor: '#e2e8f0',
                            borderRadius: 5,
                            barThickness: 20,
                        }
                    ]
                },
                options: {
                    plugins: {
                        legend: {
                            display: true,
                            position: 'top',
                            align: 'end',
                            labels: {
                                boxWidth: 10,
                                usePointStyle: true,
                                pointStyle: 'circle'
                            }
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            grid: {
                                drawBorder: false,
                                color: '#f1f5f9'
                            },
                            ticks: {
                                precision: 0
                            }
                        },
                        x: {
                            grid: {
                                display: false
                            }
                        }
                    }
                }
            });
        </script>
    @endpush

    <style>
        .icon-box {
            width: 50px;
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .dashed {
            border-bottom-style: dashed !important;
        }

        .smaller {
            font-size: 0.7rem;
        }
    </style>
@endsection
