@extends('master')
@push('styles')
    <!-- Mapbox CSS -->
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        .dashboard-page {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .page-head {
            display: flex;
            align-items: flex-start;
            justify-content: space-between;
            gap: 16px;
            flex-wrap: wrap;
        }

        .page-title {
            font-family: 'Sora', sans-serif;
            font-size: 24px;
            font-weight: 600;
            color: var(--gray-900);
            margin-bottom: 6px;
        }

        .page-subtitle {
            font-size: 13px;
            color: var(--gray-600);
        }

        .page-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .btn-primary,
        .btn-outline {
            height: 40px;
            padding: 0 16px;
            border-radius: 10px;
            font-size: 13px;
            font-weight: 600;
            border: none;
            cursor: pointer;
        }

        .btn-primary {
            background: var(--red-main);
            color: white;
        }

        .btn-outline {
            background: white;
            color: var(--gray-900);
            border: 1px solid var(--gray-200);
        }

        .stat-grid {
            display: grid;
            grid-template-columns: repeat(4, minmax(0, 1fr));
            gap: 16px;
        }

        .stat-card,
        .section-card {
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: 16px;
            box-shadow: 0 8px 24px rgba(28, 26, 24, 0.04);
        }

        .stat-card {
            padding: 18px;
        }

        .stat-card-label {
            font-size: 12px;
            color: var(--gray-600);
            margin-bottom: 8px;
        }

        .stat-card-value {
            font-family: 'Sora', sans-serif;
            font-size: 32px;
            font-weight: 600;
            color: var(--gray-900);
            margin-bottom: 6px;
        }

        .stat-card-note {
            font-size: 12px;
            color: var(--gray-400);
        }

        .section-card {
            padding: 18px;
        }

        .section-head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
            margin-bottom: 16px;
        }

        .section-head h2 {
            font-family: 'Sora', sans-serif;
            font-size: 15px;
            font-weight: 600;
            color: var(--gray-900);
        }

        .section-badge {
            font-size: 11px;
            padding: 6px 10px;
            border-radius: 999px;
            background: var(--red-pale);
            color: var(--red-main);
            font-weight: 600;
        }

        .filter-grid {
            display: flex;
            flex-wrap: wrap;
            gap: 14px;
            margin-bottom: 12px;
        }

        .filter-field {
            display: flex;
            flex-direction: column;
            gap: 6px;
            flex: 1 1 180px;
        }

        /* Tom-select fix */
        .ts-control .item {
            display: inline-flex !important;
        }

        .filter-field label {
            font-size: 12px;
            font-weight: 500;
            color: var(--gray-600);
        }

        .filter-field select,
        .filter-field input {
            height: 40px;
            border-radius: 10px;
            border: 1px solid var(--gray-200);
            padding: 0 12px;
            background: white;
            font-size: 13px;
            color: var(--gray-900);
            outline: none;
        }

        .filter-actions {
            justify-content: flex-end;
            align-self: end;
        }

        .dashboard-grid {
            display: grid;
            grid-template-columns: 1fr;
            gap: 16px;
        }

        .map-card {
            min-height: 500px;
        }

        .map-placeholder {
            border-radius: 14px;
            border: 1px solid var(--gray-200);
            background: var(--gray-50);
            min-height: 420px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }

        .map-empty {
            text-align: center;
            color: var(--gray-600);
        }

        .map-icon {
            font-size: 40px;
            margin-bottom: 10px;
        }

        .map-empty span {
            display: block;
            margin-top: 6px;
            font-size: 12px;
            color: var(--gray-400);
        }

        .map-legend {
            padding: 12px 16px;
            display: flex;
            gap: 16px;
            flex-wrap: wrap;
            border-top: 1px solid var(--gray-200);
            background: var(--gray-50);
        }

        .map-legend-item {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 11px;
            font-weight: 500;
            color: var(--gray-600);
            font-family: 'DM Sans', sans-serif;
        }

        .map-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            flex-shrink: 0;
        }

        .dot-dealer   { background: #185FA5; }
        .dot-retailer { background: #3B6D11; }
        .dot-farmer   { background: #854F0B; }
        .dot-checkin  { background: #A32D2D; }

        .activity-list {
            display: flex;
            flex-direction: column;
            gap: 14px;
        }

        .activity-item {
            display: flex;
            gap: 12px;
            align-items: flex-start;
        }

        .activity-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: var(--red-main);
            margin-top: 5px;
            flex-shrink: 0;
        }

        .activity-title {
            font-size: 13px;
            font-weight: 600;
            color: var(--gray-900);
        }

        .activity-meta {
            margin-top: 4px;
            font-size: 12px;
            color: var(--gray-400);
        }

        /* Mapbox custom styling */
        .mapboxgl-popup-content {
            padding: 12px;
            border-radius: 8px;
            font-family: 'DM Sans', sans-serif;
        }
        .mapboxgl-popup-content h4 {
            margin: 0 0 8px 0;
            font-family: 'Sora', sans-serif;
            font-size: 14px;
            font-weight: 600;
            color: var(--gray-900);
        }
        .mapboxgl-popup-content p {
            margin: 4px 0;
            font-size: 12px;
            color: var(--gray-600);
        }
        .mapboxgl-popup-close-button {
            width: 24px;
            height: 24px;
            font-size: 20px;
        }

        #visitMap {
            width: 100%;
            height: 400px;
            border-radius: 12px;
            overflow: hidden;
        }

        .map-info-badge {
            font-size: 11px;
            font-weight: 600;
            padding: 3px 9px;
            background: var(--red-pale);
            color: var(--red-800);
            border-radius: 20px;
            border: 1px solid var(--red-light);
        }

        /* Loading Overlay Styles */
        #loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.8);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 9999;
            flex-direction: column;
            gap: 12px;
            display: none; /* Hidden by default */
        }

        .spinner {
            width: 40px;
            height: 40px;
            border: 4px solid var(--gray-200);
            border-top-color: var(--red-main);
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        #loading-overlay p {
            font-weight: 500;
            color: var(--gray-600);
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* Custom Marker Styles */
        .custom-marker {
            display: flex;
            flex-direction: column;
            align-items: center;
            cursor: pointer;
        }
        .marker-label {
            background: white;
            color: var(--gray-900);
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 11px;
            font-weight: 600;
            font-family: 'Sora', sans-serif;
            box-shadow: 0 2px 6px rgba(0,0,0,0.15);
            white-space: nowrap;
            margin-bottom: 4px;
            border: 1px solid var(--gray-200);
        }
        .marker-pin {
            width: 16px;
            height: 16px;
            border-radius: 50% 50% 50% 0;
            transform: rotate(-45deg);
            box-shadow: -1px 1px 3px rgba(0,0,0,0.3);
            border: 2px solid white;
        }
        .marker-pin.is-visit {
            border-radius: 4px;
            transform: none;
        }
    </style>
@endpush
@section('content')
    <div id="loading-overlay">
        <div class="spinner"></div>
        <p>Memuat data...</p>
    </div>

    <div class="dashboard-page">
        <div class="page-head">
            <div>
                <h1 class="page-title">Dashboard</h1>
                <p class="page-subtitle">Pantau visit FA, Agronomis, dan aktivitas lapangan secara real-time.</p>
            </div>

            <div class="page-actions">
                <button class="btn-outline" onclick="exportDashboard()">Export</button>
            </div>
        </div>

        <div class="stat-grid">
            <div class="stat-card">
                <div class="stat-card-label">Check-in / Absensi</div>
                <div class="stat-card-value" id="stat-checkin">—</div>
                <div class="stat-card-note">Jumlah check-in</div>
            </div>

            <div class="stat-card">
                <div class="stat-card-label">Visit Dealer</div>
                <div class="stat-card-value" id="stat-dealer">—</div>
                <div class="stat-card-note">Total kunjungan</div>
            </div>

            <div class="stat-card">
                <div class="stat-card-label">Visit Retailer</div>
                <div class="stat-card-value" id="stat-retailer">—</div>
                <div class="stat-card-note">Total kunjungan</div>
            </div>

            <div class="stat-card">
                <div class="stat-card-label">Visit Farmer</div>
                <div class="stat-card-value" id="stat-farmer">—</div>
                <div class="stat-card-note">Total kunjungan</div>
            </div>
        </div>

        <div class="section-card filter-card">
            <div class="section-head">
                <h2>Filter Visit</h2>
            </div>

            <div class="filter-grid">
                @canany(['isSuperAdmin', 'isAdmin'])
                    <div class="filter-field">
                        <label>Marketing Manager</label>
                        <select id="dashFilterMk" multiple>
                        </select>
                    </div>
                    <div class="filter-field" >
                        <label>Agronomis</label>
                        <select id="dashFilterAgronomis" multiple>
                        </select>
                    </div>
                    <div class="filter-field">
                        <label>FA</label>
                        <select id="dashFilterFA" multiple>
                        </select>
                    </div>
                @endcanany
                @can('isMK')
                    <div class="filter-field" >
                        <label>Agronomis</label>
                        <select id="dashFilterAgronomis" multiple>
                        </select>
                    </div>
                    <div class="filter-field">
                        <label>FA</label>
                        <select id="dashFilterFA" multiple>
                        </select>
                    </div>
                @endcan
                @can('isAgronomist')
                    <div class="filter-field">
                        <label>FA</label>
                        <select id="dashFilterFA" multiple>
                        </select>
                    </div>
                @endcan
                <div class="filter-field">
                    <label>Pilih Tanggal</label>
                    <input id="date-range" type="text" name="daterange" />
                </div>
            </div>

            <div style="display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; flex-basis: 100%;">
                <button class="btn-primary" onclick="applyDashboardFilter()">Terapkan Filter</button>
                <button class="btn-outline" onclick="resetDashboardFilter()">Reset</button>
            </div>
        </div>

        <div class="dashboard-grid">
            <div class="section-card map-card">
                <div class="section-head">
                    <h2>Peta Visit</h2>
                    <span class="section-badge">
                        <span id="map-badge-count">— titik</span>
                    </span>
                </div>

                <div id="visitMap" class="map-placeholder">
                    <div class="map-empty">
                        <div class="map-icon">📍</div>
                        <p>Memuat peta...</p>
                        <span>Jangan tutup halaman ini</span>
                    </div>
                </div>

                <div class="map-legend">
                    <div class="map-legend-item"><div class="map-dot dot-checkin"></div> Check-in / Absensi</div>
                    <div class="map-legend-item"><div class="map-dot dot-dealer"></div> Visit Dealer</div>
                    <div class="map-legend-item"><div class="map-dot dot-retailer"></div> Visit Retailer</div>
                    <div class="map-legend-item"><div class="map-dot dot-farmer"></div> Visit Farmer</div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <!-- Mapbox GL JS -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
    <script>
        window.Laravel = {
            routes: {
                query: '{{ route("dashboard.query") }}',
                export: '{{ route("dashboard.export") }}',
                listMk: '{{ route("team.list-marketing-managers") }}',
                listAgronomis: '{{ route("team.list-agronomists") }}',
                listFa: '{{ route("team.list-fa") }}',
                mapboxToken: 'pk.eyJ1IjoiY2VjZTIzNCIsImEiOiJjbXAyMDd6NGgwMDJzMnNxOGh6OTRlZzBpIn0.fvPSOhpAiWnGGBavXnWkZA' // Replace with your token
            },
            userRole: '{{ Auth::user()->role->type_role->value }}'
        };
    </script>
    @vite(['resources/js/dashboard.js'])
@endpush
