<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Progress Tracker</title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Instrument+Sans:ital,wght@0,400..700;1,400..700&display=swap" rel="stylesheet">
    <link rel="icon" type="image/png" href="{{ asset('images/faviocntaskpannel.png') }}">
    
    <script>
        if (window.location.search) {
            const navEntries = performance.getEntriesByType("navigation");
            if ((navEntries.length > 0 && navEntries[0].type === "reload") || (performance.navigation && performance.navigation.type === 1)) {
                window.location.replace(window.location.pathname);
            }
        }
    </script>
    
    <style>
        :root {
            --primary-color: #3b46f1;
            --primary-hover: #2a35d8;
            --bg-main: #f8fafc;
            --bg-card: #ffffff;
            
            --text-main: #0f172a;
            --text-muted: #64748b;
            --text-label: #334155;
            --border-color: #e2e8f0;
            --border-hover: #cbd5e1;
            --font-family: 'Instrument Sans', sans-serif;

            --color-completed: #10b981;
            --color-completed-light: #ecfdf5;
            --color-progress: #3b82f6;
            --color-progress-light: #eff6ff;
            --color-pending: #f59e0b;
            --color-pending-light: #fffbeb;
            --color-cancelled: #ef4444;
            --color-cancelled-light: #fef2f2;
            
            --sidebar-width: 260px;
            --border-radius: 12px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--bg-main);
            color: var(--text-main);
            min-height: 100vh;
        }

        .dashboard-layout {
            display: flex;
            position: relative;
            min-height: 100vh;
        }

        .main-content {
            margin-left: var(--sidebar-width);
            flex-grow: 1;
            padding: 1.5rem;
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
        }

        /* OVERVIEW CARDS */
        .overview-header {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-weight: 700;
            font-size: 1rem;
            color: var(--text-main);
            margin-bottom: 1rem;
        }

        .overview-cards-container {
            display: flex;
            gap: 1rem;
            flex-wrap: wrap;
        }

        .overview-card {
            flex: 1;
            min-width: 200px;
            background: var(--bg-card);
            border: 1px solid var(--border-color);
            border-radius: 10px;
            padding: 1.25rem;
            display: flex;
            flex-direction: column;
        }
        .card-bg-green {
            background-color: #f0fdf4;
            border-color: #bbf7d0;
        }
        .card-bg-purple {
            background-color: #faf5ff;
            border-color: #e9d5ff;
        }
        
        .card-label {
            font-size: 0.8rem;
            font-weight: 700;
            color: var(--text-muted);
            margin-bottom: 0.75rem;
        }
        .card-value {
            font-size: 2.5rem;
            font-weight: 800;
            color: var(--text-main);
            line-height: 1;
            margin-bottom: 0.5rem;
        }
        .card-subtext {
            font-size: 0.8rem;
            color: var(--text-muted);
            font-weight: 500;
        }

        /* CHART & SUMMARY ROW */
        .chart-summary-row {
            display: flex;
            gap: 1.5rem;
            flex-wrap: wrap;
        }

        .chart-panel, .summary-panel {
            background: var(--bg-card);
            border: 1px solid var(--border-color);
            border-radius: 12px;
            padding: 1.5rem;
        }

        .chart-panel {
            flex: 2;
            min-width: 400px;
        }

        .summary-panel {
            flex: 1;
            min-width: 250px;
        }

        .panel-header {
            font-size: 0.95rem;
            font-weight: 700;
            margin-bottom: 1.5rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        /* BAR CHART */
        .bar-chart {
            display: flex;
            align-items: flex-end;
            justify-content: space-around;
            height: 200px;
            border-bottom: 1px solid var(--border-color);
            position: relative;
        }
        .bar-col {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 40px;
            height: 100%;
            justify-content: flex-end;
        }
        .bar {
            width: 100%;
            background-color: var(--primary-color);
            border-radius: 4px 4px 0 0;
            transition: height 0.3s;
        }
        .bar-val {
            font-size: 0.75rem;
            font-weight: 700;
            margin-bottom: 0.25rem;
        }
        .bar-day {
            font-size: 0.75rem;
            font-weight: 600;
            color: var(--text-muted);
            margin-top: 0.5rem;
            text-align: center;
            position: absolute;
            bottom: -35px;
            line-height: 1.2;
            width: 60px;
        }
        
        .chart-y-axis {
            position: absolute;
            left: -20px;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            font-size: 0.7rem;
            color: var(--text-muted);
            font-weight: 600;
        }

        /* SUMMARY LIST */
        .summary-list {
            display: flex;
            flex-direction: column;
            gap: 1.25rem;
        }
        .summary-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px dashed var(--border-color);
            padding-bottom: 0.5rem;
        }
        .summary-label {
            font-size: 0.85rem;
            color: var(--text-label);
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-weight: 500;
        }
        .summary-value {
            font-size: 0.95rem;
            font-weight: 700;
        }

        /* TABLE */
        .table-panel {
            background: var(--bg-card);
            border: 1px solid var(--border-color);
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th {
            font-size: 0.7rem;
            text-transform: uppercase;
            color: var(--text-muted);
            font-weight: 700;
            padding: 1rem 0.5rem;
            border-bottom: 1px solid var(--border-color);
            text-align: left;
        }
        td {
            padding: 1rem 0.5rem;
            border-bottom: 1px solid var(--border-color);
            font-size: 0.85rem;
            font-weight: 500;
            color: var(--text-main);
        }
        .prog-bar-bg {
            background: #e2e8f0;
            height: 6px;
            width: 100px;
            border-radius: 3px;
            margin-top: 0.25rem;
        }
        .prog-bar-fill {
            background: var(--primary-color);
            height: 100%;
            border-radius: 3px;
        }
        .btn-week-filter {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.5rem 1rem;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            background: var(--bg-card);
            font-weight: 600;
            font-size: 0.85rem;
            cursor: pointer;
        }
        
    </style>
</head>
<body>

    <div class="dashboard-layout">
        @include('admin.sidebar.sidebar')

        <main class="main-content">
            @php ob_start(); @endphp
            <form method="GET" action="{{ url()->current() }}" style="display:inline-block; margin: 0;">
                <div style="position:relative; display:inline-block;">
                    <svg fill="none" stroke="currentColor" width="16" height="16" viewBox="0 0 24 24" style="position:absolute; left:12px; top:50%; transform:translateY(-50%); pointer-events:none; color: var(--text-main);"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
                    <select name="week" onchange="this.form.submit()" class="btn-week-filter" style="appearance:none; padding-left:2.25rem; padding-right:2rem; outline:none; background: #fff; cursor: pointer;">
                        <option value="current" {{ (!isset($weekFilter) || $weekFilter === 'current') ? 'selected' : '' }}>This Week</option>
                        <option value="last" {{ (isset($weekFilter) && $weekFilter === 'last') ? 'selected' : '' }}>Last Week</option>
                    </select>
                    <svg fill="none" stroke="currentColor" width="14" height="14" viewBox="0 0 24 24" style="position:absolute; right:10px; top:50%; transform:translateY(-50%); pointer-events:none; color: var(--text-main);"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                </div>
            </form>
            @php $rightContent = ob_get_clean(); @endphp

            @include('admin.sidebar.topbar', [
                'title' => 'Progress Tracker',
                'rightContent' => $rightContent
            ])

            @php
                $twStartStr = $startOfThisWeek->format('d M');
                $twEndStr = $endOfThisWeek->format('d M');
                $lwStartStr = $startOfLastWeek->format('d M');
                $lwEndStr = $endOfLastWeek->format('d M');
            @endphp

            <!-- Overview Cards -->
            <div style="background: #ffffff; border: 1px solid var(--border-color); border-radius: 12px; padding: 1.5rem;">
                <div class="overview-header">
                    <svg fill="none" stroke="var(--primary-color)" width="20" height="20" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
                    Weekly Progress Overview <span style="color: var(--text-muted); font-weight: 500; font-size: 0.85rem;">{{ $twStartStr }} - {{ $twEndStr }} ({{ (isset($weekFilter) && $weekFilter === 'last') ? 'Last Week' : 'This Week' }})</span>
                </div>
                
                <div class="overview-cards-container" style="align-items: center;">
                    <div class="overview-card" style="padding: 1rem; flex: 1; min-width: 130px;">
                        <div class="card-label">{{ (isset($weekFilter) && $weekFilter === 'last') ? 'Previous Week' : 'Last Week' }} ({{ $lwStartStr }} - {{ $lwEndStr }})</div>
                        <div class="card-value" style="font-size: 1.8rem;">{{ $completedLastWeek }}</div>
                        <div class="card-subtext">Tasks Completed</div>
                    </div>
                    
                    <div class="overview-card card-bg-green" style="padding: 1rem; flex: 1; min-width: 130px;">
                        <div class="card-label" style="color: var(--color-completed);">{{ (isset($weekFilter) && $weekFilter === 'last') ? 'Last Week' : 'This Week' }} ({{ $twStartStr }} - {{ $twEndStr }})</div>
                        <div class="card-value" style="font-size: 1.8rem; color: var(--color-completed);">{{ $completedThisWeek }}</div>
                        <div class="card-subtext" style="color: var(--color-completed);">Tasks Completed</div>
                    </div>
                         
                    <div class="overview-card" style="padding: 1rem; flex: 1; min-width: 130px; background-color: #fffbeb; border-color: #fde68a;">
                        <div class="card-label" style="color: #d97706; display: flex; align-items: center; gap: 0.35rem;">
                            <svg fill="none" stroke="currentColor" width="14" height="14" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                            Avg. Per Day
                        </div>
                        <div class="card-value" style="font-size: 1.8rem; color: #d97706;">{{ $summary['avg_per_day'] }}</div>
                        <div class="card-subtext" style="color: #d97706;">Tasks</div>
                    </div>

                    <div class="overview-card" style="padding: 1rem; flex: 1; min-width: 130px; background-color: #f0fdf4; border-color: #bbf7d0;">
                        <div class="card-label" style="color: #15803d; display: flex; align-items: center; gap: 0.35rem;">
                            <svg fill="none" stroke="currentColor" width="14" height="14" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6"></path></svg>
                            Highest
                        </div>
                        <div class="card-value" style="font-size: 1.8rem; color: #15803d;">{{ $summary['highest'] }}</div>
                        <div class="card-subtext" style="color: #15803d; font-size: 0.7rem;">{{ $summary['highest_days'] }}</div>
                    </div>

                    <div class="overview-card" style="padding: 1rem; flex: 1; min-width: 130px; background-color: #fef2f2; border-color: #fecaca;">
                        <div class="card-label" style="color: #b91c1c; display: flex; align-items: center; gap: 0.35rem;">
                            <svg fill="none" stroke="currentColor" width="14" height="14" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 17h8m0 0v-8m0 8l-8-8-4 4-6-6"></path></svg>
                            Lowest
                        </div>
                        <div class="card-value" style="font-size: 1.8rem; color: #b91c1c;">{{ $summary['lowest'] }}</div>
                        <div class="card-subtext" style="color: #b91c1c; font-size: 0.7rem;">{{ $summary['lowest_days'] }}</div>
                    </div>
                </div>
            </div>

            <!-- Chart & Summary -->
            <div class="chart-summary-row">
                <!-- Bar Chart -->
                <div class="chart-panel">
                    <div class="panel-header">
                        Daily Task Progress ({{ (isset($weekFilter) && $weekFilter === 'last') ? 'Last Week' : 'This Week' }})
                        <span style="font-size: 0.75rem; color: var(--text-muted); display:flex; align-items:center; gap:0.5rem;">
                            <span style="display:flex; align-items:center; gap:0.25rem;">
                                <span style="display:inline-block; width:10px; height:10px; background:var(--primary-color); border-radius:2px;"></span>
                                Tasks Added
                            </span>
                            <span style="display:flex; align-items:center; gap:0.25rem;">
                                <span style="display:inline-block; width:10px; height:10px; background:var(--color-completed); border-radius:2px;"></span>
                                Tasks Completed
                            </span>
                        </span>
                    </div>
                    
                    <div style="padding-left: 20px; padding-bottom: 30px; margin-top: 2rem;">
                        <div class="bar-chart">
                            @php
                                $maxVal = max(1, max($dailyAddedData ?? []), max($dailyData ?? []));
                                $chartMax = ceil($maxVal / 5) * 5; // round up to nearest 5
                                if($chartMax < 10) $chartMax = 10;
                            @endphp
                            
                            <!-- Y-axis lines -->
                            <div style="position:absolute; width:100%; height:100%; top:0; left:0; display:flex; flex-direction:column; justify-content:space-between; pointer-events:none; border-top:1px dashed #f1f5f9; border-bottom:1px solid var(--border-color);">
                                <div style="border-bottom:1px dashed #f1f5f9; flex:1;"></div>
                                <div style="border-bottom:1px dashed #f1f5f9; flex:1;"></div>
                                <div style="border-bottom:1px dashed #f1f5f9; flex:1;"></div>
                            </div>
                            
                            <div class="chart-y-axis">
                                <span>{{ $chartMax }}</span>
                                <span>{{ round($chartMax * 0.75) }}</span>
                                <span>{{ round($chartMax * 0.5) }}</span>
                                <span>{{ round($chartMax * 0.25) }}</span>
                                <span>0</span>
                            </div>
                            
                            @foreach($chartLabels as $key => $labelData)
                                @php 
                                    $valAdded = $dailyAddedData[$key] ?? 0;
                                    $valCompleted = $dailyData[$key] ?? 0;
                                    $hAdded = ($chartMax > 0) ? ($valAdded / $chartMax) * 100 : 0;
                                    $hCompleted = ($chartMax > 0) ? ($valCompleted / $chartMax) * 100 : 0;
                                    $dateLabel = $labelData['dateLabel'];
                                    $d = $labelData['dayName'];
                                @endphp
                                <div class="bar-col" style="z-index: 5; position: relative; width: 50px; flex-direction: row; justify-content: center; align-items: flex-end; gap: 4px;">
                                    <div style="display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; width: 20px;">
                                        <span class="bar-val" style="color: {{ $valAdded > 0 ? 'var(--text-main)' : 'transparent' }}">{{ $valAdded }}</span>
                                        <div class="bar" style="height: {{ $hAdded }}%; width: 100%;"></div>
                                    </div>
                                    <div style="display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; width: 20px;">
                                        <span class="bar-val" style="color: {{ $valCompleted > 0 ? 'var(--color-completed)' : 'transparent' }}">{{ $valCompleted }}</span>
                                        <div class="bar" style="height: {{ $hCompleted }}%; width: 100%; background-color: var(--color-completed);"></div>
                                    </div>
                                    <span class="bar-day"><span style="font-size:0.65rem;font-weight:500;">{{ $dateLabel }}</span><br>{{ $d }}</span>
                                </div>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>

            <div class="table-panel">
                <div class="panel-header" style="margin-bottom: 1rem;">Project Progress Overview</div>
                <table>
                    <thead>
                        <tr>
                            <th>S. NO.</th>
                            <th>PROJECT NAME</th>
                            <th>DEVELOPER</th>
                            <th style="text-align:center;">TOTAL TASKS</th>
                            <th style="text-align:center;">ACHIEVED TASKS</th>
                            <th style="text-align:center;">PENDING TASKS</th>
                            <th style="text-align:center;">THIS WEEK <span style="font-weight:500;">({{ $twStartStr }} - {{ $twEndStr }})</span></th>
                            <th style="text-align:center;">LAST WEEK <span style="font-weight:500;">({{ $lwStartStr }} - {{ $lwEndStr }})</span></th>
                            <!--<th style="text-align:center;">WEEKLY CHANGE</th>-->
                            <th>OVERALL PROGRESS</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($progressTrackerProjects ?? [] as $index => $project)
                            @php
                                $developerName = 'N/A';
                                if ($project->tasks && $project->tasks->isNotEmpty()) {
                                    $users = $project->tasks->map(function($task) {
                                        return $task->user ? $task->user->name : null;
                                    })->filter()->unique()->values()->all();
                                    
                                    if (!empty($users)) {
                                        $developerName = implode(',<br>', $users);
                                    }
                                }

                                $total = $project->total_tasks ?? 0;
                                $achieved = $project->achieved_tasks ?? 0;
                                $pending = $project->pending_tasks ?? 0;
                                
                                $twCount = $project->this_week_completed ?? 0;
                                $lwCount = $project->last_week_completed ?? 0;
                                $weeklyChange = $twCount - $lwCount;
                                
                                $overallProgress = $total > 0 ? round(($achieved / $total) * 100) : 0;
                            @endphp
                            <tr>
                                <td style="font-weight: 700;">{{ $index + 1 }}</td>
                                <td style="font-weight: 600;">{{ $project->name }}</td>
                                <td style="font-size: 0.75rem; line-height: 1.4;">{!! $developerName !!}</td>
                                <td style="text-align:center; color: #3b82f6; font-weight:700;">{{ $total }}</td>
                                <td style="text-align:center; color: #10b981; font-weight:700;">{{ $achieved }}</td>
                                <td style="text-align:center; color: #f59e0b; font-weight:700;">{{ $pending }}</td>
                                <td style="text-align:center; color: #3b82f6; font-weight:700; background:#f8fafc; border-radius: 6px;">{{ $twCount }}</td>
                                <td style="text-align:center; font-weight:600; color:var(--text-muted);">{{ $lwCount }}</td>
                                <!--<td style="text-align:center;">-->
                                <!--    @if($weeklyChange > 0)-->
                                <!--        <span style="color:#10b981; font-weight:700;">+{{ $weeklyChange }} <svg style="display:inline;width:12px;height:12px;vertical-align:middle;" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"></path></svg></span>-->
                                <!--    @elseif($weeklyChange < 0)-->
                                <!--        <span style="color:#ef4444; font-weight:700;">{{ $weeklyChange }} <svg style="display:inline;width:12px;height:12px;vertical-align:middle;" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 14l-7 7m0 0l-7-7m7 7V3"></path></svg></span>-->
                                <!--    @else-->
                                <!--        <span style="color:var(--text-muted); font-weight:600;">0</span>-->
                                <!--    @endif-->
                                <!--</td>-->
                                <td>
                                    <div style="font-size:0.75rem; font-weight:700; margin-bottom:2px;">{{ $overallProgress }}%</div>
                                    <div class="prog-bar-bg">
                                        <div class="prog-bar-fill" style="width: {{ $overallProgress }}%;"></div>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="10" style="text-align: center; padding: 2rem; color: var(--text-muted);">No projects found</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

        </main>
    </div>
</body>
</html>
