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

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

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

// Commission by Doctor
$sql = "SELECT 
            u.full_name,
            COUNT(*) as service_count,
            SUM(sc.commission_amount) as total_commission,
            SUM(sc.service_amount) as total_revenue_generated
        FROM staff_commissions sc
        JOIN users u ON sc.staff_id = u.user_id
        WHERE sc.date_earned BETWEEN ? AND ?
        GROUP BY sc.staff_id
        ORDER BY total_commission DESC";
// Assuming `staff_commissions` table exists as per previous context (Group 1 pages).
// If not, I'll use a simpler query or check schema. Assuming it exists.

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $start, $end);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Commission Reports')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</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">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Commission Reports</h4>
                                <div class="page-title-right">
                                    <form method="GET" class="d-flex gap-2">
                                        <input type="date" name="start" class="form-control form-control-sm"
                                            value="<?php echo $start; ?>">
                                        <input type="date" name="end" class="form-control form-control-sm"
                                            value="<?php echo $end; ?>">
                                        <button type="submit" class="btn btn-primary btn-sm">Update</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="commTable" class="table table-bordered dt-responsive nowrap">
                                        <thead>
                                            <tr>
                                                <th>Staff Member</th>
                                                <th>Services Performed</th>
                                                <th>Revenue Generated</th>
                                                <th>Commission Earned</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($row = $result->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($row['full_name']); ?></td>
                                                    <td><?php echo number_format($row['service_count']); ?></td>
                                                    <td><?php echo number_format($row['total_revenue_generated'], 2); ?>
                                                    </td>
                                                    <td><?php echo number_format($row['total_commission'], 2); ?></td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                        <tfoot>
                                            <tr class="table-light fw-bold">
                                                <td>TOTAL</td>
                                                <td>-</td>
                                                <td>-</td>
                                                <td>-</td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#commTable').DataTable({ dom: 'Bfrtip', buttons: ['excel', 'pdf'] });</script>
</body>

</html>