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

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

// Fetch services from service_pricing table
$services = [];
$sql = "SELECT pricing_id as service_id, service_name, service_type as category, base_price as price, is_active 
        FROM service_pricing 
        ORDER BY service_type, service_name";
$res = $conn->query($sql);
if ($res) {
    while ($row = $res->fetch_assoc()) {
        $services[] = $row;
    }
}
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Service Pricing')); ?>
    <?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">Service Pricing</h4>
                                <div class="page-title-right">
                                    <button class="btn btn-primary" data-bs-toggle="modal"
                                        data-bs-target="#addServiceModal"><i class="ri-add-line"></i> Add
                                        Service</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="pricingTable"
                                        class="table table-bordered dt-responsive nowrap table-striped align-middle"
                                        style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>Service Name</th>
                                                <th>Category</th>
                                                <th>Price (KES)</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($services as $svc): ?>
                                                <tr>
                                                    <td><?php echo htmlspecialchars($svc['service_name']); ?></td>
                                                    <td><?php echo htmlspecialchars($svc['category']); ?></td>
                                                    <td><?php echo number_format($svc['price'], 2); ?></td>
                                                    <td>
                                                        <span
                                                            class="badge <?php echo $svc['is_active'] ? 'bg-success' : 'bg-danger'; ?>">
                                                            <?php echo $svc['is_active'] ? 'Active' : 'Inactive'; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <button class="btn btn-sm btn-info"
                                                            onclick="editService(<?php echo $svc['service_id']; ?>)">Edit</button>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Add Service Modal -->
                    <div class="modal fade" id="addServiceModal" tabindex="-1" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title">Add New Service</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <form>
                                        <div class="mb-3">
                                            <label class="form-label">Service Name</label>
                                            <input type="text" class="form-control" required>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Category</label>
                                            <select class="form-select">
                                                <option>Consultation</option>
                                                <option>Procedure</option>
                                                <option>Surgery</option>
                                                <option>Lab Test</option>
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Price (KES)</label>
                                            <input type="number" class="form-control" required>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
                                    <button type="button" class="btn btn-primary">Save Service</button>
                                </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 () {
            $('#pricingTable').DataTable();
        });
        function editService(id) {
            alert('Edit Service ID: ' + id);
        }
    </script>
</body>

</html>