<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Database configuration
$host = 'localhost';
$dbname = 'equipment';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

// Admin credentials
define('ADMIN_EMAIL', 'big@admin.com');
define('ADMIN_PASSWORD', 'plentymoney');

// Check if user is logged in
function isAdminLoggedIn() {
    return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
}

// Redirect if not logged in
function requireAdminLogin() {
    if (!isAdminLoggedIn()) {
        header('Location: index.php');
        exit;
    }
}

// Get total counts for dashboard
function getTotalProducts($pdo) {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM equipment");
    return $stmt->fetch()['total'];
}

function getTotalOrders($pdo) {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM orders");
    return $stmt->fetch()['total'];
}

function getTotalCustomers($pdo) {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM customers");
    return $stmt->fetch()['total'];
}

function getTotalRevenue($pdo) {
    $stmt = $pdo->query("SELECT COALESCE(SUM(total_amount), 0) as total FROM orders WHERE payment_status = 'paid'");
    return $stmt->fetch()['total'];
}

function getPendingOrders($pdo) {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM orders WHERE payment_status = 'pending'");
    return $stmt->fetch()['total'];
}
?>