<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

require_once '../config/database.php';

$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$role = $_SESSION['role'];

// Get user info
$sql = "SELECT * FROM users WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();

// Get statistics
if ($role == 'driver') {
    // Driver statistics
    $sql = "SELECT COUNT(*) as total_receipts, MAX(uploaded_at) as last_upload FROM receipts WHERE driver_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $user_id);
    $stmt->execute();
    $stats = $stmt->get_result()->fetch_assoc();
    $stmt->close();
    
    // Get driver's routes
    $sql = "SELECT r.id, r.route_name, r.route_code FROM routes r 
            INNER JOIN driver_routes dr ON r.id = dr.route_id 
            WHERE dr.driver_id = ?  AND dr.status = 'active'";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $user_id);
    $stmt->execute();
    $routes = $stmt->get_result();
    $stmt->close();
    
} else {
    // Admin statistics
    $sql = "SELECT COUNT(*) as total_receipts FROM receipts";
    $result = $conn->query($sql);
    $stats = $result->fetch_assoc();
    
    $sql = "SELECT COUNT(*) as total_drivers FROM users WHERE role = 'driver' AND status = 'active'";
    $result = $conn->query($sql);
    $driver_count = $result->fetch_assoc();
    
    $sql = "SELECT COUNT(*) as total_routes FROM routes";
    $result = $conn->query($sql);
    $route_count = $result->fetch_assoc();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Receipt Management System</title>
    <link href="https://cdn. jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs. cloudflare.com/ajax/libs/font-awesome/6. 4.0/css/all.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: #f5f7fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            color: white;
        }
        
        .sidebar . logo {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.3);
        }
        
        .sidebar ul {
            list-style: none;
        }
        
        .sidebar ul li {
            margin-bottom: 15px;
        }
        
        .sidebar ul li a {
            color: white;
            text-decoration: none;
            display: flex;
            align-items: center;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background 0.3s;
        }
        
        .sidebar ul li a:hover,
        .sidebar ul li a. active {
            background: rgba(255,255,255,0.2);
        }
        
        .sidebar ul li a i {
            margin-right: 10px;
            width: 20px;
        }
        
        .main-content {
            padding: 30px;
        }
        
        .top-bar {
            background: white;
            padding: 20px;
            border-radius: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        
        .top-bar h2 {
            color: #333;
            margin: 0;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .stat-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            text-align: center;
        }
        
        .stat-card i {
            font-size: 32px;
            color: #667eea;
            margin-bottom: 10px;
        }
        
        .stat-card h3 {
            color: #667eea;
            font-size: 28px;
            margin-bottom: 5px;
        }
        
        .stat-card p {
            color: #666;
            font-size: 14px;
            margin: 0;
        }
        
        .btn-logout {
            background: #ff6b6b;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: background 0.3s;
        }
        
        .btn-logout:hover {
            background: #ff5252;
            text-decoration: none;
            color: white;
        }
        
        .content-section {
            background: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            margin-bottom: 20px;
        }
        
        .content-section h3 {
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }
        
        .btn-primary-custom {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: transform 0.2s;
        }
        
        .btn-primary-custom:hover {
            transform: translateY(-2px);
            color: white;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="d-flex">
        <!-- Sidebar -->
        <div class="sidebar" style="width: 250px;">
            <div class="logo">
                📋 Receipt Manager
            </div>
            <ul>
                <li><a href="dashboard.php" class="active"><i class="fas fa-home"></i> Dashboard</a></li>
                <? php if ($role == 'driver'): ?>
                    <li><a href="receipts.php"><i class="fas fa-folder"></i> My Receipts</a></li>
                    <li><a href="upload.php"><i class="fas fa-cloud-upload-alt"></i> Upload Receipts</a></li>
                <?php else: ?>
                    <li><a href="admin_receipts.php"><i class="fas fa-folder"></i> All Receipts</a></li>
                    <li><a href="admin_users.php"><i class="fas fa-users"></i> Manage Users</a></li>
                    <li><a href="admin_routes.php"><i class="fas fa-map"></i> Manage Routes</a></li>
                <?php endif; ?>
            </ul>
        </div>
        
        <!-- Main Content -->
        <div style="flex: 1;">
            <div class="top-bar">
                <h2><i class="fas fa-chart-line"></i> Dashboard</h2>
                <div class="user-info">
                    <span><? php echo htmlspecialchars($username); ?> (<?php echo ucfirst($role); ?>)</span>
                    <a href="logout.php" class="btn-logout"><i class="fas fa-sign-out-alt"></i> Logout</a>
                </div>
            </div>
            
            <div class="main-content">
                <? php if ($role == 'driver'): ?>
                    <!-- Driver Dashboard -->
                    <div class="row">
                        <div class="col-md-4">
                            <div class="stat-card">
                                <i class="fas fa-file"></i>
                                <h3><? php echo $stats['total_receipts']; ? ></h3>
                                <p>Total Receipts</p>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="stat-card">
                                <i class="fas fa-map"></i>
                                <h3><?php echo $routes->num_rows; ?></h3>
                                <p>Assigned Routes</p>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="stat-card">
                                <i class="fas fa-clock"></i>
                                <h3><?php echo $stats['last_upload'] ?  date('M d', strtotime($stats['last_upload'])) : 'N/A'; ?></h3>
                                <p>Last Upload</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="content-section">
                        <h3><i class="fas fa-plus-circle"></i> Quick Actions</h3>
                        <a href="upload.php" class="btn-primary-custom">
                            <i class="fas fa-cloud-upload-alt"></i> Upload New Receipts
                        </a>
                        <a href="receipts.php" class="btn-primary-custom" style="background: #47b881; margin-left: 10px;">
                            <i class="fas fa-folder"></i> View My Receipts
                        </a>
                    </div>
                    
                    <div class="content-section">
                        <h3><i class="fas fa-info-circle"></i> Your Information</h3>
                        <table class="table">
                            <tr>
                                <td><strong>Username:</strong></td>
                                <td><?php echo htmlspecialchars($user['username']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Email:</strong></td>
                                <td><?php echo htmlspecialchars($user['email']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Role:</strong></td>
                                <td><?php echo ucfirst($user['role']); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Member Since:</strong></td>
                                <td><?php echo date('F d, Y', strtotime($user['created_at'])); ?></td>
                            </tr>
                        </table>
                    </div>
                    
                <? php else: ?>
                    <!-- Admin Dashboard -->
                    <div class="row">
                        <div class="col-md-3">
                            <div class="stat-card">
                                <i class="fas fa-file-alt"></i>
                                <h3><?php echo $stats['total_receipts']; ?></h3>
                                <p>Total Receipts</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card">
                                <i class="fas fa-users"></i>
                                <h3><?php echo $driver_count['total_drivers']; ?></h3>
                                <p>Active Drivers</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card">
                                <i class="fas fa-map-marker-alt"></i>
                                <h3><?php echo $route_count['total_routes']; ?></h3>
                                <p>Total Routes</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stat-card">
                                <i class="fas fa-check-circle"></i>
                                <h3 style="color: #47b881;">✓</h3>
                                <p>System Status: Active</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="content-section">
                        <h3><i class="fas fa-tasks"></i> Admin Functions</h3>
                        <div class="row">
                            <div class="col-md-6" style="margin-bottom: 15px;">
                                <a href="admin_receipts.php" class="btn-primary-custom w-100" style="justify-content: center;">
                                    <i class="fas fa-folder-open"></i> View All Receipts
                                </a>
                            </div>
                            <div class="col-md-6" style="margin-bottom: 15px;">
                                <a href="admin_users.php" class="btn-primary-custom w-100" style="justify-content: center; background: #47b881;">
                                    <i class="fas fa-users"></i> Manage Users
                                </a>
                            </div>
                        </div>
                    </div>
                    
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>