<?php
/**
 * Dashboard View
 * 
 * Variables expected:
 * $stats - Dashboard statistics array
 * $activities - Recent activities array
 * $user - Current user data
 * $isSuperAdmin - Boolean for super admin
 * $userPermissions - Array of user permissions
 */
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-tachometer-alt"></i>
                Dashboard
            </h1>
            <p>Welcome back, <?php echo htmlspecialchars($user['full_name'] ?? 'User'); ?>! Here's your overview.</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-outline" onclick="refreshDashboard()">
                <i class="fas fa-sync-alt"></i> Refresh
            </button>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="dashboard-grid">
        <!-- Weapons Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #e74c3c;">
                <i class="fas fa-gun"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_weapons'] ?? 0); ?></div>
                <div class="card-label">Total Weapons</div>
                <div class="card-trend">
                    <span class="text-muted"><?php echo $stats['weapons_issued'] ?? 0; ?> issued</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/weapons" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Ammunition Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #f39c12;">
                <i class="fas fa-bullseye"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_ammunition'] ?? 0); ?></div>
                <div class="card-label">Ammunition Types</div>
                <div class="card-trend">
                    <span class="text-muted"><?php echo number_format($stats['ammunition_balance'] ?? 0); ?> rounds</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/ammunition" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Land Assets Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #27ae60;">
                <i class="fas fa-map-marked-alt"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_land'] ?? 0); ?></div>
                <div class="card-label">Land Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/land" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Buildings Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #3498db;">
                <i class="fas fa-building"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_buildings'] ?? 0); ?></div>
                <div class="card-label">Buildings</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/buildings" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Rented Properties Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #e67e22;">
                <i class="fas fa-house-user"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_rented'] ?? 0); ?></div>
                <div class="card-label">Rented Properties</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/rented" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Ongoing Projects Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #9b59b6;">
                <i class="fas fa-hard-hat"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_projects'] ?? 0); ?></div>
                <div class="card-label">Ongoing Projects</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/projects" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Movable Assets Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #1abc9c;">
                <i class="fas fa-tools"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_movable'] ?? 0); ?></div>
                <div class="card-label">Movable Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/movable" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- ICT Assets Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #34495e;">
                <i class="fas fa-server"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_ict'] ?? 0); ?></div>
                <div class="card-label">ICT Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/ict" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Vehicles Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #e67e22;">
                <i class="fas fa-car"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_vehicles'] ?? 0); ?></div>
                <div class="card-label">Vehicles</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/fleet/vehicles" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Aircraft Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #2980b9;">
                <i class="fas fa-fighter-jet"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_aircraft'] ?? 0); ?></div>
                <div class="card-label">Aircraft</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/fleet/aircraft" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Marine Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #16a085;">
                <i class="fas fa-ship"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_marine'] ?? 0); ?></div>
                <div class="card-label">Marine Assets</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/fleet/marine" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Motorcycles Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #d35400;">
                <i class="fas fa-motorcycle"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_motorcycles'] ?? 0); ?></div>
                <div class="card-label">Motorcycles</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/fleet/motorcycles" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>

        <!-- Pending Requisitions Card -->
        <div class="stat-card <?php echo ($stats['pending_requisitions'] ?? 0) > 0 ? 'stat-warning' : ''; ?>">
            <div class="card-icon" style="background: #f39c12;">
                <i class="fas fa-file-signature"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['pending_requisitions'] ?? 0); ?></div>
                <div class="card-label">Pending Requisitions</div>
                <div class="card-trend">
                    <span class="text-muted">Awaiting approval</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/requisition" class="btn-link">
                    <i class="fas fa-arrow-right"></i> Review
                </a>
            </div>
        </div>

        <!-- Users Card -->
        <div class="stat-card">
            <div class="card-icon" style="background: #2c3e50;">
                <i class="fas fa-users"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo number_format($stats['total_users'] ?? 0); ?></div>
                <div class="card-label">Active Users</div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/users" class="btn-link">
                    <i class="fas fa-arrow-right"></i> Manage
                </a>
            </div>
        </div>

        <!-- Alerts Card -->
        <div class="stat-card <?php echo (($stats['expiring_ammunition'] ?? 0) > 0 || ($stats['unserviceable_weapons'] ?? 0) > 0) ? 'stat-danger' : ''; ?>">
            <div class="card-icon" style="background: #e74c3c;">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
            <div class="card-content">
                <div class="card-value"><?php echo ($stats['expiring_ammunition'] ?? 0) + ($stats['unserviceable_weapons'] ?? 0); ?></div>
                <div class="card-label">System Alerts</div>
                <div class="card-trend">
                    <span><?php echo $stats['expiring_ammunition'] ?? 0; ?> exp | <?php echo $stats['unserviceable_weapons'] ?? 0; ?> unserv</span>
                </div>
            </div>
            <div class="card-action">
                <a href="<?php echo BASE_URL; ?>/weapons?filter=unserviceable" class="btn-link">
                    <i class="fas fa-arrow-right"></i> View
                </a>
            </div>
        </div>
    </div>
</div>

<style>
/* Dashboard Grid Layout */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 15px;
    margin-top: 15px;
}

/* Stat Card Styles */
.stat-card {
    background: white;
    border-radius: 10px;
    padding: 12px;
    display: flex;
    align-items: center;
    gap: 10px;
    box-shadow: 0 1px 6px rgba(0,0,0,0.04);
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 3px 12px rgba(0,0,0,0.08);
}

.stat-card.stat-warning {
    background: linear-gradient(135deg, #fff8e1 0%, #fff3cd 100%);
    border-left: 3px solid #f39c12;
}

.stat-card.stat-danger {
    background: linear-gradient(135deg, #fee2e2 0%, #f8d7da 100%);
    border-left: 3px solid #e74c3c;
}

.card-icon {
    width: 45px;
    height: 45px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    color: white;
    flex-shrink: 0;
}

.card-content {
    flex: 1;
}

.card-value {
    font-size: 1.3rem;
    font-weight: 700;
    color: #2c3e50;
    line-height: 1.2;
    margin-bottom: 3px;
}

.card-label {
    font-size: 0.65rem;
    color: #7f8c8d;
    font-weight: 500;
    margin-bottom: 3px;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.card-trend {
    font-size: 0.6rem;
    color: #95a5a6;
}

.card-trend .text-muted {
    color: #95a5a6;
}

.card-action {
    position: absolute;
    bottom: 10px;
    right: 10px;
}

.btn-link {
    color: #7f8c8d;
    font-size: 0.65rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 3px;
    transition: all 0.2s;
}

.btn-link:hover {
    color: #27ae60;
}

/* Page Header */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e2e8f0;
}

.header-content h1 {
    margin: 0 0 3px 0;
    font-size: 1.3rem;
    color: #2c3e50;
}

.header-content p {
    margin: 0;
    color: #7f8c8d;
    font-size: 0.75rem;
}

.header-actions {
    display: flex;
    gap: 8px;
}

.btn-outline {
    background: transparent;
    border: 1px solid #cbd5e0;
    color: #4a5568;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 0.7rem;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.btn-outline:hover {
    background: #f7fafc;
    border-color: #27ae60;
    color: #27ae60;
}

/* Responsive */
@media (max-width: 768px) {
    .dashboard-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .page-header {
        flex-direction: column;
        gap: 10px;
    }
    
    .header-actions {
        width: 100%;
    }
    
    .btn-outline {
        width: 100%;
        justify-content: center;
    }
    
    .stat-card {
        padding: 10px;
    }
    
    .card-icon {
        width: 40px;
        height: 40px;
        font-size: 1.1rem;
    }
    
    .card-value {
        font-size: 1.2rem;
    }
    
    .card-label {
        font-size: 0.6rem;
    }
}

@media print {
    .header-actions,
    .btn-outline,
    .card-action {
        display: none !important;
    }
    
    .stat-card {
        break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ddd;
    }
}
</style>

<script>
function refreshDashboard() {
    const btn = event.target.closest('button');
    const originalHtml = btn.innerHTML;
    
    btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
    btn.disabled = true;
    
    setTimeout(() => {
        location.reload();
    }, 800);
}
</script>