<?php
/**
 * Fleet Dashboard View
 */

// These variables should come from the controller
$stats = isset($stats) ? $stats : [
    'total_vehicles' => 0,
    'total_aircraft' => 0,
    'total_marine' => 0,
    'total_motorcycles' => 0,
    'active_vehicles' => 0,
    'vehicles_in_repair' => 0,
    'expiring_insurance' => 0,
    'total_fleet_value' => 0
];

$recent_vehicles = isset($recent_vehicles) ? $recent_vehicles : [];
$user = isset($user) ? $user : ['full_name' => 'User', 'roles' => ['Staff']];
$isSuperAdmin = isset($isSuperAdmin) ? $isSuperAdmin : false;

// Set page variables
$title = isset($title) ? $title : 'Fleet Dashboard';
$active = isset($active) ? $active : 'fleet';
$init_charts = isset($init_charts) ? $init_charts : true;

// Include layouts
require_once __DIR__ . '/../layouts/header.php';
require_once __DIR__ . '/../layouts/sidebar.php';
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-truck"></i>
                Fleet Management Dashboard
            </h1>
            <p>Overview of all fleet assets including vehicles, aircraft, marine, and motorcycles</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-outline" onclick="location.reload()">
                <i class="fas fa-sync-alt"></i> Refresh
            </button>
        </div>
    </div>

    <!-- Summary Statistics -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon vehicles">
                <i class="fas fa-car"></i>
            </div>
            <div class="stat-details">
                <h4>Vehicles</h4>
                <p class="stat-number"><?php echo number_format($stats['total_vehicles']); ?></p>
                <div class="stat-sub">
                    <span class="active"><?php echo $stats['active_vehicles']; ?> Active</span>
                    <span class="repair"><?php echo $stats['vehicles_in_repair']; ?> In Repair</span>
                </div>
            </div>
            <div class="stat-action">
                <a href="<?php echo BASE_URL; ?>/fleet/vehicles" class="btn-link">View <i class="fas fa-arrow-right"></i></a>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon aircraft">
                <i class="fas fa-helicopter"></i>
            </div>
            <div class="stat-details">
                <h4>Aircraft</h4>
                <p class="stat-number"><?php echo number_format($stats['total_aircraft']); ?></p>
            </div>
            <div class="stat-action">
                <a href="<?php echo BASE_URL; ?>/fleet/aircraft" class="btn-link">View <i class="fas fa-arrow-right"></i></a>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon marine">
                <i class="fas fa-ship"></i>
            </div>
            <div class="stat-details">
                <h4>Marine</h4>
                <p class="stat-number"><?php echo number_format($stats['total_marine']); ?></p>
            </div>
            <div class="stat-action">
                <a href="<?php echo BASE_URL; ?>/fleet/marine" class="btn-link">View <i class="fas fa-arrow-right"></i></a>
            </div>
        </div>

        <div class="stat-card">
            <div class="stat-icon motorcycles">
                <i class="fas fa-motorcycle"></i>
            </div>
            <div class="stat-details">
                <h4>Motorcycles</h4>
                <p class="stat-number"><?php echo number_format($stats['total_motorcycles']); ?></p>
            </div>
            <div class="stat-action">
                <a href="<?php echo BASE_URL; ?>/fleet/motorcycles" class="btn-link">View <i class="fas fa-arrow-right"></i></a>
            </div>
        </div>
    </div>

    <!-- Fleet Value & Alerts -->
    <div class="stats-grid">
        <div class="stat-card total-value">
            <div class="stat-icon value">
                <i class="fas fa-coins"></i>
            </div>
            <div class="stat-details">
                <h4>Total Fleet Value</h4>
                <p class="stat-number">₦<?php echo number_format($stats['total_fleet_value'], 2); ?></p>
            </div>
        </div>

        <div class="stat-card alert-card">
            <div class="stat-icon alert">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
            <div class="stat-details">
                <h4>Insurance Expiring Soon</h4>
                <p class="stat-number"><?php echo $stats['expiring_insurance']; ?></p>
                <div class="stat-sub">Vehicles needing renewal within 30 days</div>
            </div>
            <div class="stat-action">
                <a href="<?php echo BASE_URL; ?>/fleet/vehicles?filter=expiring_insurance" class="btn-link">Review <i class="fas fa-arrow-right"></i></a>
            </div>
        </div>
    </div>

    <!-- Recent Vehicles -->
    <div class="content-card">
        <div class="section-title">
            <h2><i class="fas fa-history"></i> Recently Added Vehicles</h2>
            <a href="<?php echo BASE_URL; ?>/fleet/vehicles" class="view-all">View All</a>
        </div>

        <div class="table-responsive">
            <?php if (empty($recent_vehicles)): ?>
                <div class="empty-state">
                    <i class="fas fa-car"></i>
                    <p>No vehicles found</p>
                </div>
            <?php else: ?>
            <table class="asset-table">
                <thead>
                    <tr>
                        <th>Asset Code</th>
                        <th>Make/Model</th>
                        <th>Year</th>
                        <th>Registration</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent_vehicles as $vehicle): ?>
                    <tr>
                        <td><span class="asset-code"><?php echo Security::escape($vehicle['asset_code'] ?? ''); ?></span></td>
                        <td><?php echo Security::escape($vehicle['make_manufacturer'] ?? ''); ?></td>
                        <td><?php echo Security::escape($vehicle['model_year'] ?? ''); ?></td>
                        <td><?php echo Security::escape($vehicle['registration_number'] ?? ''); ?></td>
                        <td>
                            <?php 
                            $status = $vehicle['operational_status'] ?? '';
                            $statusClass = '';
                            if ($status == 'Active') $statusClass = 'status-active';
                            elseif ($status == 'In Repair') $statusClass = 'status-warning';
                            elseif ($status == 'Grounded') $statusClass = 'status-rejected';
                            else $statusClass = 'status-pending';
                            ?>
                            <span class="status-badge <?php echo $statusClass; ?>">
                                <?php echo Security::escape($status); ?>
                            </span>
                        </td>
                        <td>
                            <a href="<?php echo BASE_URL; ?>/fleet/vehicles/show/<?php echo $vehicle['id'] ?? ''; ?>" class="btn-icon">
                                <i class="fas fa-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="quick-actions">
        <h3><i class="fas fa-bolt"></i> Quick Actions</h3>
        <div class="action-grid">
            <?php if (Auth::can('fleet.create')): ?>
            <a href="<?php echo BASE_URL; ?>/fleet/vehicles/create" class="action-card">
                <i class="fas fa-car"></i>
                <span>Add Vehicle</span>
            </a>
            <a href="<?php echo BASE_URL; ?>/fleet/aircraft/create" class="action-card">
                <i class="fas fa-helicopter"></i>
                <span>Add Aircraft</span>
            </a>
            <a href="<?php echo BASE_URL; ?>/fleet/marine/create" class="action-card">
                <i class="fas fa-ship"></i>
                <span>Add Marine</span>
            </a>
            <a href="<?php echo BASE_URL; ?>/fleet/motorcycles/create" class="action-card">
                <i class="fas fa-motorcycle"></i>
                <span>Add Motorcycle</span>
            </a>
            <?php endif; ?>
            
            <a href="<?php echo BASE_URL; ?>/reports/fleet" class="action-card">
                <i class="fas fa-chart-bar"></i>
                <span>Fleet Report</span>
            </a>
        </div>
    </div>
</div>

<style>
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 25px;
}

.stat-card {
    background: white;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 15px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    position: relative;
}

.stat-card.total-value {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.stat-card.total-value .stat-number {
    color: white;
}

.stat-card.alert-card {
    border-left: 4px solid #f39c12;
}

.stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
}

.stat-icon.vehicles {
    background: #e3f2fd;
    color: #1976d2;
}

.stat-icon.aircraft {
    background: #e8f5e9;
    color: #388e3c;
}

.stat-icon.marine {
    background: #e0f2f1;
    color: #00796b;
}

.stat-icon.motorcycles {
    background: #fff3e0;
    color: #f57c00;
}

.stat-icon.value {
    background: rgba(255,255,255,0.2);
    color: white;
}

.stat-icon.alert {
    background: #fff3e0;
    color: #f57c00;
}

.stat-details {
    flex: 1;
}

.stat-details h4 {
    margin: 0 0 5px 0;
    font-size: 0.9rem;
    color: #7f8c8d;
}

.stat-card.total-value .stat-details h4 {
    color: rgba(255,255,255,0.9);
}

.stat-number {
    font-size: 1.8rem;
    font-weight: bold;
    margin: 0 0 5px 0;
    color: #2c3e50;
    line-height: 1.2;
}

.stat-sub {
    font-size: 0.85rem;
    color: #7f8c8d;
}

.stat-sub .active {
    color: #27ae60;
    margin-right: 10px;
}

.stat-sub .repair {
    color: #f39c12;
}

.stat-action {
    position: absolute;
    bottom: 15px;
    right: 20px;
}

.stat-action .btn-link {
    color: #7f8c8d;
    font-size: 0.85rem;
    text-decoration: none;
}

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

.content-card {
    background: white;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 25px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.section-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e2e8f0;
}

.section-title h2 {
    margin: 0;
    font-size: 1.2rem;
    color: #2c3e50;
    display: flex;
    align-items: center;
    gap: 8px;
}

.view-all {
    color: #27ae60;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 600;
}

.view-all:hover {
    text-decoration: underline;
}

.table-responsive {
    overflow-x: auto;
}

.asset-table {
    width: 100%;
    border-collapse: collapse;
}

.asset-table th {
    background: #f8f9fa;
    padding: 12px 10px;
    text-align: left;
    font-weight: 600;
    color: #7f8c8d;
    border-bottom: 2px solid #e2e8f0;
}

.asset-table td {
    padding: 10px;
    border-bottom: 1px solid #e2e8f0;
}

.asset-code {
    font-family: monospace;
    font-weight: 600;
    color: #2c3e50;
    background: #f8f9fa;
    padding: 3px 6px;
    border-radius: 4px;
}

.status-badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
}

.status-active {
    background: #d4edda;
    color: #155724;
}

.status-warning {
    background: #fff3cd;
    color: #856404;
}

.status-rejected {
    background: #f8d7da;
    color: #721c24;
}

.status-pending {
    background: #e2e3e5;
    color: #383d41;
}

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    background: #f8f9fa;
    color: #2c3e50;
    text-decoration: none;
    transition: all 0.3s;
}

.btn-icon:hover {
    background: #27ae60;
    color: white;
}

.empty-state {
    text-align: center;
    padding: 40px;
    color: #7f8c8d;
}

.empty-state i {
    font-size: 3rem;
    margin-bottom: 15px;
    opacity: 0.5;
}

.quick-actions {
    margin-top: 30px;
}

.quick-actions h3 {
    margin-bottom: 20px;
    color: #2c3e50;
    font-size: 1.2rem;
}

.action-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 15px;
}

.action-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    text-decoration: none;
    color: #2c3e50;
    transition: all 0.3s;
}

.action-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 20px rgba(0,0,0,0.1);
    background: #27ae60;
    color: white;
}

.action-card i {
    font-size: 2rem;
    margin-bottom: 10px;
    color: #27ae60;
}

.action-card:hover i {
    color: white;
}

.action-card span {
    font-size: 0.9rem;
    font-weight: 600;
}

@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .action-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}
</style>

