<?php  
error_reporting(E_ALL);  
ini_set('display_errors', 1);  
  
require_once('includes/load.php');  

// Database functions
function getOpenRequestsCount() {
    global $db;
    $result = $db->query("SELECT COUNT(*) as count FROM purchase_indirect WHERE job = 'open'");
    return $result->fetch_assoc()['count'];
}

function getClosedRequestsCount() {
    global $db;
    $result = $db->query("SELECT COUNT(*) as count FROM purchase_indirect WHERE job = 'close'");
    return $result->fetch_assoc()['count'];
}

function getOpenRequestsByDepartment() {
    global $db;
    return $db->query("SELECT department, COUNT(*) as count FROM purchase_indirect WHERE job = 'open' GROUP BY department ORDER BY count DESC");
}

function getClosedRequestsByDepartment() {
    global $db;
    return $db->query("SELECT department, COUNT(*) as count FROM purchase_indirect WHERE job = 'close' GROUP BY department ORDER BY count DESC");
}

function getRecentOpenRequests($limit = 5) {
    global $db;
    return $db->query("SELECT * FROM purchase_indirect WHERE job = 'open' ORDER BY request_date DESC LIMIT $limit");
}

function getRecentClosedRequests($limit = 5) {
    global $db;
    return $db->query("SELECT * FROM purchase_indirect WHERE job = 'close' ORDER BY request_date DESC LIMIT $limit");
}

// Get data
$open_count = getOpenRequestsCount();
$closed_count = getClosedRequestsCount();
$open_by_dept = getOpenRequestsByDepartment();
$closed_by_dept = getClosedRequestsByDepartment();
$recent_open = getRecentOpenRequests();
$recent_closed = getRecentClosedRequests();
?>  
  
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Purchase Indirect Dashboard</title>  
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Custom CSS -->
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2c3e50;
            --success-color: #2ecc71;
            --danger-color: #e74c3c;
            --light-color: #ecf0f1;
            --dark-color: #2c3e50;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
        }
        
        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
        }
        
        .card {
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
            border: none;
        }
        
        .card-header {
            border-radius: 10px 10px 0 0 !important;
            padding: 15px 20px;
        }
        
        .bg-primary {
            background-color: var(--primary-color) !important;
        }
        
        .bg-success {
            background-color: var(--success-color) !important;
        }
        
        .bg-danger {
            background-color: var(--danger-color) !important;
        }
        
        .stats-card {
            transition: all 0.3s ease;
        }
        
        .stats-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        
        .stats-icon {
            font-size: 2rem;
            opacity: 0.7;
        }
        
        .stats-value {
            font-size: 1.8rem;
            font-weight: 700;
        }
        
        .stats-label {
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 1px;
            opacity: 0.7;
        }
        
        .department-list {
            list-style-type: none;
            padding-left: 0;
        }
        
        .department-list li {
            padding: 8px 0;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
        }
        
        .department-list li:last-child {
            border-bottom: none;
        }
        
        .department-name {
            font-weight: 500;
        }
        
        .department-count {
            background-color: var(--primary-color);
            color: white;
            border-radius: 20px;
            padding: 2px 10px;
            font-size: 0.8rem;
        }
        
        .department-count.closed {
            background-color: var(--success-color);
        }
        
        .table th {
            background-color: var(--light-color);
            font-weight: 600;
        }
        
        .status-open {
            color: var(--primary-color);
            font-weight: bold;
        }
        
        .status-close {
            color: var(--success-color);
            font-weight: bold;
        }
        
        .status-pending {
            color: #f39c12;
            font-weight: bold;
        }
        
        .status-approved {
            color: var(--success-color);
            font-weight: bold;
        }
        
        .status-rejected {
            color: var(--danger-color);
            font-weight: bold;
        }
        
        .badge-open {
            background-color: var(--primary-color);
        }
        
        .badge-closed {
            background-color: var(--success-color);
        }
        
        @media (max-width: 768px) {
            .stats-card {
                margin-bottom: 15px;
            }
        }
    </style>
</head>  
<body>  
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">  
        <div class="container">  
            <a class="navbar-brand" href="#">
                <i class="fas fa-tachometer-alt me-2"></i>Purchase Indirect Dashboard
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="purchase_indirect.php">
                            <i class="fas fa-shopping-cart me-1"></i> Purchase Requests
                        </a>
                    </li>
                </ul>
            </div>
        </div>  
    </nav>  
  
    <div class="container">  
        <!-- Stats Section -->
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="card stats-card bg-white">
                    <div class="card-body text-center">
                        <i class="fas fa-inbox stats-icon text-primary mb-3"></i>
                        <h3 class="stats-value text-dark"><?= $open_count ?></h3>
                        <p class="stats-label text-muted">Open Requests</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card stats-card bg-white">
                    <div class="card-body text-center">
                        <i class="fas fa-check-circle stats-icon text-success mb-3"></i>
                        <h3 class="stats-value text-dark"><?= $closed_count ?></h3>
                        <p class="stats-label text-muted">Closed Requests</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card stats-card bg-white">
                    <div class="card-body text-center">
                        <i class="fas fa-tasks stats-icon text-info mb-3"></i>
                        <h3 class="stats-value text-dark"><?= $open_count + $closed_count ?></h3>
                        <p class="stats-label text-muted">Total Requests</p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Department Breakdown -->
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header bg-primary text-white">
                        <h5 class="card-title mb-0"><i class="fas fa-building me-2"></i>Open Requests by Department</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($open_by_dept->num_rows > 0): ?>
                            <ul class="department-list mb-0">
                                <?php while ($dept = $open_by_dept->fetch_assoc()): ?>
                                    <li>
                                        <span class="department-name"><?= $dept['department'] ?></span>
                                        <span class="department-count"><?= $dept['count'] ?></span>
                                    </li>
                                <?php endwhile; ?>
                            </ul>
                        <?php else: ?>
                            <p class="text-muted mb-0">No open requests found</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header bg-success text-white">
                        <h5 class="card-title mb-0"><i class="fas fa-building me-2"></i>Closed Requests by Department</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($closed_by_dept->num_rows > 0): ?>
                            <ul class="department-list mb-0">
                                <?php while ($dept = $closed_by_dept->fetch_assoc()): ?>
                                    <li>
                                        <span class="department-name"><?= $dept['department'] ?></span>
                                        <span class="department-count closed"><?= $dept['count'] ?></span>
                                    </li>
                                <?php endwhile; ?>
                            </ul>
                        <?php else: ?>
                            <p class="text-muted mb-0">No closed requests found</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Recent Requests -->
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="card-title mb-0"><i class="fas fa-clock me-2"></i>Recent Open Requests</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($recent_open->num_rows > 0): ?>
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead>
                                        <tr>
                                            <th>Request #</th>
                                            <th>Product</th>
                                            <th>Department</th>
                                            <th>Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row = $recent_open->fetch_assoc()): ?>
                                            <tr>
                                                <td>
                                                    <a href="purchase_indirect.php" class="text-decoration-none">
                                                        <?= $row['request_number'] ?>
                                                    </a>
                                                </td>
                                                <td><?= htmlspecialchars($row['product_name']) ?></td>
                                                <td><?= $row['department'] ?></td>
                                                <td>
                                                    <span class="status-<?= strtolower($row['status']) ?>">
                                                        <?= ucfirst($row['status']) ?>
                                                    </span>
                                                </td>
                                            </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                            <div class="text-end">
                                <a href="purchase_indirect.php?job_filter=open" class="btn btn-sm btn-outline-primary">
                                    View All Open Requests
                                </a>
                            </div>
                        <?php else: ?>
                            <p class="text-muted mb-0">No recent open requests found</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5 class="card-title mb-0"><i class="fas fa-check-circle me-2"></i>Recent Closed Requests</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($recent_closed->num_rows > 0): ?>
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead>
                                        <tr>
                                            <th>Request #</th>
                                            <th>Product</th>
                                            <th>Department</th>
                                            <th>Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row = $recent_closed->fetch_assoc()): ?>
                                            <tr>
                                                <td>
                                                    <a href="purchase_indirect.php" class="text-decoration-none">
                                                        <?= $row['request_number'] ?>
                                                    </a>
                                                </td>
                                                <td><?= htmlspecialchars($row['product_name']) ?></td>
                                                <td><?= $row['department'] ?></td>
                                                <td>
                                                    <span class="status-<?= strtolower($row['status']) ?>">
                                                        <?= ucfirst($row['status']) ?>
                                                    </span>
                                                </td>
                                            </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                            <div class="text-end">
                                <a href="purchase_indirect.php?job_filter=close" class="btn btn-sm btn-outline-success">
                                    View All Closed Requests
                                </a>
                            </div>
                        <?php else: ?>
                            <p class="text-muted mb-0">No recent closed requests found</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
  
    <!-- JavaScript Libraries -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>  
        $(document).ready(function() {
            // Add animation to cards
            $('.stats-card').hover(
                function() {
                    $(this).addClass('shadow');
                },
                function() {
                    $(this).removeClass('shadow');
                }
            );
            
            // Add animation to table rows
            $('table tbody tr').hover(
                function() {
                    $(this).addClass('bg-light');
                },
                function() {
                    $(this).removeClass('bg-light');
                }
            );
        });
    </script>  
</body>  
</html>