<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Enable error reporting for development
error_reporting(E_ALL);
ini_set('display_errors', 0); // Don't display errors on page
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/php_errors.log');

require_once('../includes/admin-security.php');
include_once('includes/config.php');

// Access Check
if (!has_permission('view_reports')) {
    echo "<script>alert('Access Denied. Redirecting to Dashboard...'); window.location.href='dashboard.php';</script>";
    exit();
}
?>


?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="description" content="Between Dates Sales Report - Awesome Mall Admin" />
    <meta name="author" content="" />
    <title>Awesome Mall | Between Dates Sales Report</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="css/styles.css" rel="stylesheet" />
    <link href="css/admin-professional.css" rel="stylesheet" />
    <script src="js/all.min.js" crossorigin="anonymous"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('includes/header.php'); ?>
    <div id="layoutSidenav">
        <?php include_once('includes/sidebar.php'); ?>
        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <!-- Page Header -->
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-chart-line me-2"></i> Between Dates Sales Report</h1>
                            <p>View comprehensive sales data for a specific date range</p>
                        </div>
                    </div>

                    <!-- Date Range Filter -->
                    <div class="table-card mb-4">
                        <div class="card-header">
                            <h5 class="mb-0"><i class="fas fa-filter me-2"></i>Select Date Range</h5>
                        </div>
                        <div class="card-body">
                            <form method="post">
                                <div class="row g-3 align-items-end">
                                    <div class="col-md-4">
                                        <label for="fromdate" class="form-label fw-semibold">From Date</label>
                                        <input type="date" name="fromdate" id="fromdate" class="form-control"
                                            value="<?php echo isset($_POST['fromdate']) ? $_POST['fromdate'] : ''; ?>"
                                            required>
                                    </div>
                                    <div class="col-md-4">
                                        <label for="todate" class="form-label fw-semibold">To Date</label>
                                        <input type="date" name="todate" id="todate" class="form-control"
                                            value="<?php echo isset($_POST['todate']) ? $_POST['todate'] : ''; ?>"
                                            required>
                                    </div>
                                    <div class="col-md-4">
                                        <button type="submit" name="submit" class="btn btn-primary w-100">
                                            <i class="fas fa-search me-2"></i>Generate Report
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <?php if (isset($_POST['submit'])) {
                        $fdate = $_POST['fromdate'];
                        $tdate = $_POST['todate'];
                        ?>

                        <!-- Results Table -->
                        <div class="table-card">
                            <div class="card-header">
                                <h5 class="mb-0">
                                    <i class="fas fa-chart-bar me-2"></i>
                                    Sales Report: <?php echo date('M d, Y', strtotime($fdate)); ?> to
                                    <?php echo date('M d, Y', strtotime($tdate)); ?>
                                </h5>
                            </div>
                            <div class="card-body">
                                <table class="table table-hover" id="datatablesSimple">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Month-Year</th>
                                            <th class="text-end">Total Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $query = mysqli_query($con, "SELECT sum(totalAmount) as totalamount, month(orderDate) as ordermonth, year(orderDate) as orderyear
                                        FROM `orders` where orderDate between '$fdate' and '$tdate' group by ordermonth, orderyear");
                                        $cnt = 1;
                                        $gamount = 0;
                                        while ($row = mysqli_fetch_array($query)) {
                                            $tamount = $row['totalamount'];
                                            $gamount += $tamount;

                                            // Format month name
                                            $monthName = date('F', mktime(0, 0, 0, $row['ordermonth'], 1));
                                            ?>
                                            <tr>
                                                <td><?php echo htmlentities($cnt); ?></td>
                                                <td><strong><?php echo htmlentities($monthName . ' ' . $row['orderyear']); ?></strong>
                                                </td>
                                                <td class="text-end"><strong>$<?php echo number_format($tamount, 2); ?></strong>
                                                </td>
                                            </tr>
                                            <?php
                                            $cnt = $cnt + 1;
                                        }
                                        ?>
                                    </tbody>
                                    <tfoot>
                                        <tr class="table-active">
                                            <th colspan="2" class="text-end">Grand Total:</th>
                                            <th class="text-end text-primary">$<?php echo number_format($gamount, 2); ?>
                                            </th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    <?php } ?>

            </main>
            <?php include_once('includes/footer.php'); ?>
        </div>
    </div>
    <script src="js/bootstrap.bundle.min.js"></script>
    <script src="js/scripts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" crossorigin="anonymous"></script>
    <script src="js/datatables-simple-demo.js"></script>
</body>

</html>
<?php ?>
