<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

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

// Stats
$total_tests = $conn->query("SELECT COUNT(*) as c FROM lab_order_tests")->fetch_assoc()['c'];
$pending_tests = $conn->query("SELECT COUNT(*) as c FROM lab_order_tests WHERE status='PENDING'")->fetch_assoc()['c'];
$completed_tests = $conn->query("SELECT COUNT(*) as c FROM lab_order_tests WHERE status='COMPLETED'")->fetch_assoc()['c'];
$revenue = $conn->query("SELECT SUM(test_cost) as s FROM lab_order_tests WHERE status='COMPLETED'")->fetch_assoc()['s'];

// Daily Breakdown
$daily_sql = "SELECT DATE(created_at) as date, COUNT(*) as count FROM lab_order_tests 
              WHERE created_at > DATE_SUB(NOW(), INTERVAL 30 DAY) 
              GROUP BY DATE(created_at) ORDER BY date ASC";
$daily_res = $conn->query($daily_sql);
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Lab Reports')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <h4 class="mb-sm-0">Lab Performance Reports</h4>
                        </div>
                    </div>

                    <div class="row mt-4">
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold font-size-12">Total Tests</h6>
                                    <h4 class="mb-0"><?php echo number_format($total_tests); ?></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold font-size-12">Pending</h6>
                                    <h4 class="mb-0"><?php echo number_format($pending_tests); ?></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold font-size-12">Completed</h6>
                                    <h4 class="mb-0"><?php echo number_format($completed_tests); ?></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold font-size-12">Revenue Generated
                                    </h6>
                                    <h4 class="mb-0">KES <?php echo number_format($revenue, 2); ?></h4>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Daily Test Volume (Last 30 Days)</h4>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>Tests Performed</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($d = $daily_res->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo $d['date']; ?></td>
                                                    <td><?php echo $d['count']; ?></td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>