<?php
$pageTitle = 'تقرير المبيعات حسب الفترة';
include __DIR__ . '/../../includes/header.php';

// التحقق من الصلاحيات
checkLogin();
if (!hasPermission('view_sales_reports')) {
    header('Location: ' . SITE_URL . '/pages/errors/access_denied.php');
    exit();
}

$db = getDB();

$start_date = $_GET['start_date'] ?? date('Y-m-01');
$end_date = $_GET['end_date'] ?? date('Y-m-t');

$sql = "SELECT si.invoice_date, SUM(si.total_amount) as daily_total_sales
        FROM sales_invoices si
        WHERE si.invoice_date BETWEEN ? AND ?
        GROUP BY si.invoice_date
        ORDER BY si.invoice_date ASC";

$daily_sales = $db->select($sql, [$start_date, $end_date]);

$total_sales_amount = $db->selectOne("SELECT SUM(total_amount) as total FROM sales_invoices WHERE invoice_date BETWEEN ? AND ?", [$start_date, $end_date])['total'] ?? 0;
$total_invoices = $db->selectOne("SELECT COUNT(*) as count FROM sales_invoices WHERE invoice_date BETWEEN ? AND ?", [$start_date, $end_date])['count'] ?? 0;

?>

<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title"><?php echo $pageTitle; ?></h3>
                </div>
                <div class="card-body">
                    <form action="" method="GET" class="row g-3 align-items-center mb-4">
                        <div class="col-md-4">
                            <label for="start_date" class="form-label">تاريخ البدء:</label>
                            <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo htmlspecialchars($start_date); ?>">
                        </div>
                        <div class="col-md-4">
                            <label for="end_date" class="form-label">تاريخ الانتهاء:</label>
                            <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo htmlspecialchars($end_date); ?>">
                        </div>
                        <div class="col-md-4">
                            <button type="submit" class="btn btn-primary mt-4">
                                <i class="fas fa-filter me-2"></i>تصفية التقرير
                            </button>
                        </div>
                    </form>

                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="card bg-light mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">إجمالي المبيعات</h5>
                                    <p class="card-text fs-4">SAR <?php echo number_format($total_sales_amount, 2); ?></p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card bg-light mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">عدد الفواتير</h5>
                                    <p class="card-text fs-4"><?php echo $total_invoices; ?></p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php if (empty($daily_sales)): ?>
                        <div class="alert alert-info text-center" role="alert">
                            لا توجد بيانات مبيعات للفترة المحددة.
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-hover table-striped">
                                <thead>
                                    <tr>
                                        <th>التاريخ</th>
                                        <th>إجمالي المبيعات اليومية</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($daily_sales as $sale): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($sale['invoice_date']); ?></td>
                                            <td>SAR <?php echo htmlspecialchars(number_format($sale['daily_total_sales'], 2)); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>

                    <div class="mt-4">
                        <a href="<?php echo SITE_URL; ?>/pages/reports/index.php" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>العودة للتقارير
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../../includes/footer.php'; ?>