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

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

// Fetch commissions (Placeholder table: staff_commissions)
// If checking table existence is tricky, we'll try mock data if empty result.
$commissions = [];
// $sql = "SELECT c.*, u.username, p.first_name, p.last_name 
//         FROM staff_commissions c 
//         JOIN users u ON c.staff_id = u.user_id 
//         JOIN patients p ON c.patient_id = p.patient_id 
//         ORDER BY c.date_earned DESC";
// Using mock data for safety until schema verified
$commissions[] = [
    'staff_name' => 'Dr. A. Smith',
    'patient_name' => 'John Doe',
    'service' => 'Tonsillectomy',
    'amount' => 50000,
    'commission_rate' => 20,
    'earned' => 10000,
    'status' => 'PENDING',
    'date' => date('Y-m-d')
];
$commissions[] = [
    'staff_name' => 'Dr. B. Jones',
    'patient_name' => 'Jane Roe',
    'service' => 'Consultation',
    'amount' => 3000,
    'commission_rate' => 50,
    'earned' => 1500,
    'status' => 'PAID',
    'date' => date('Y-m-d', strtotime('-2 days'))
];

?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Commission Dashboard')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" type="text/css" />
</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 Dashboard</h4>
                                <div class="page-title-right">
                                    <button class="btn btn-success btn-sm">Process Payments</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <p class="text-uppercase fw-medium text-muted mb-0">Total Commissions (This Month)
                                    </p>
                                    <h4 class="fs-22 fw-semibold ff-secondary mb-4">KES 150,000</h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <p class="text-uppercase fw-medium text-muted mb-0">Pending Payouts</p>
                                    <h4 class="fs-22 fw-semibold ff-secondary mb-4 text-warning">KES 45,000</h4>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Commission Log</h5>
                                </div>
                                <div class="card-body">
                                    <table id="commTable"
                                        class="table table-bordered dt-responsive nowrap table-striped align-middle"
                                        style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>Staff Member</th>
                                                <th>Service / Procedure</th>
                                                <th>Patient</th>
                                                <th>Bill Amount</th>
                                                <th>Comm %</th>
                                                <th>Earned</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($commissions as $comm): ?>
                                                <tr>
                                                    <td><?php echo date('d M Y', strtotime($comm['date'])); ?></td>
                                                    <td><?php echo htmlspecialchars($comm['staff_name']); ?></td>
                                                    <td><?php echo htmlspecialchars($comm['service']); ?></td>
                                                    <td><?php echo htmlspecialchars($comm['patient_name']); ?></td>
                                                    <td>KES <?php echo number_format($comm['amount'], 2); ?></td>
                                                    <td><?php echo $comm['commission_rate']; ?>%</td>
                                                    <td class="fw-bold">KES <?php echo number_format($comm['earned'], 2); ?>
                                                    </td>
                                                    <td>
                                                        <span
                                                            class="badge <?php echo $comm['status'] == 'Paid' ? 'bg-success' : 'bg-warning'; ?>">
                                                            <?php echo $comm['status']; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </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>
        $(document).ready(function () {
            $('#commTable').DataTable();
        });
    </script>
</body>

</html>