<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Inventory.php';


$database = new Database();
$conn = $database->getConnection();


$auth = new Auth($conn);


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


if (!$auth->isSuperAdmin($_SESSION['user_id'])) {
    header('Location: /unauthorized.php');
    exit();
}


$inventory = new Inventory($conn);

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $branchId = $_POST['branch_id'];
    $startDate = $_POST['start_date'];
    $endDate = $_POST['end_date'];

    if ($inventory->generateSalesReport($branchId, $startDate, $endDate)) {
        $message = 'Sales report generated successfully.';
    } else {
        $message = 'Failed to generate sales report.';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Generate Sales Report</title>
    <link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../assets/css/style.css">
    <link href="/IMS/assets/css/responsive.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h1 class="mb-4">Generate Sales Report</h1>
        <?php if ($message): ?>
            <div class="alert alert-info"><?php echo htmlspecialchars($message); ?></div>
        <?php endif; ?>
        <form method="POST" action="">
            <div class="form-group">
                <label for="branch_id">Branch ID</label>
                <input type="text" name="branch_id" id="branch_id" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="start_date">Start Date</label>
                <input type="date" name="start_date" id="start_date" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="end_date">End Date</label>
                <input type="date" name="end_date" id="end_date" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-primary mt-3">Generate Report</button>
        </form>
    </div>
    <script src="../../assets/js/bootstrap.min.js"></script>
</body>
</html>