<?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 Order Report - Awesome Mall Admin" />
    <meta name="author" content="" />
    <title>Awesome Mall | Between Dates Order 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-calendar-alt me-2"></i> Between Dates Order Report</h1>
                            <p>Generate order reports 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-file-invoice me-2"></i>
                                    Orders 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>Order No.</th>
                                            <th>Order By</th>
                                            <th>Order Amount</th>
                                            <th>Order Date</th>
                                            <th>Order Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php $query = mysqli_query($con, "SELECT orders.id,orderNumber,totalAmount,orderStatus,orderDate,user_details.name,user_details.contactno 
                                        FROM `orders` join users on users.id=orders.userId left join user_details on user_details.user_id=users.id where orderDate between '$fdate' and '$tdate'");
                                        $cnt = 1;
                                        while ($row = mysqli_fetch_array($query)) {
                                            // Determine status badge class
                                            $statusClass = '';
                                            switch (strtolower($row['orderStatus'])) {
                                                case 'pending':
                                                    $statusClass = 'pending';
                                                    break;
                                                case 'processing':
                                                    $statusClass = 'processing';
                                                    break;
                                                case 'dispatched':
                                                    $statusClass = 'dispatched';
                                                    break;
                                                case 'in transit':
                                                    $statusClass = 'in-transit';
                                                    break;
                                                case 'delivered':
                                                    $statusClass = 'delivered';
                                                    break;
                                                case 'cancelled':
                                                    $statusClass = 'cancelled';
                                                    break;
                                                default:
                                                    $statusClass = 'pending';
                                            }
                                            ?>
                                            <tr>
                                                <td><?php echo htmlentities($cnt); ?></td>
                                                <td><strong><?php echo htmlentities($row['orderNumber']); ?></strong></td>
                                                <td><?php echo htmlentities($row['name']); ?></td>
                                                <td><strong>$<?php echo htmlentities($row['totalAmount']); ?></strong></td>
                                                <td><?php echo htmlentities($row['orderDate']); ?></td>
                                                <td>
                                                    <span
                                                        class="status-badge <?php echo $statusClass; ?>"><?php echo htmlentities($row['orderStatus']); ?></span>
                                                </td>
                                                <td>
                                                    <a href="order-details.php?orderid=<?php echo $row['id'] ?>"
                                                        class="btn btn-sm btn-outline-primary" title="View Order Details">
                                                        <i class="fas fa-eye me-1"></i> View
                                                    </a>
                                                </td>
                                            </tr>
                                            <?php $cnt = $cnt + 1;
                                        } ?>
                                    </tbody>
                                </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 ?>
