<?php
include('../../../includes/config.php');
include('header.php');

// -------------------- UPDATE LOGIC -------------------- //
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    foreach ($_POST['price'] as $id => $price) {
        $price = mysqli_real_escape_string($ahk_conn, $price);
        $id = (int)$id;
        mysqli_query($ahk_conn, "UPDATE pricing SET price='$price' WHERE id='$id'");
    }
    echo "<script>
        alert('✅ Pricing updated successfully!');
        window.location.href = window.location.href;
    </script>";
    exit;
}

// -------------------- FETCH DATA -------------------- //
$result = mysqli_query($ahk_conn, "SELECT id, service_name, price FROM pricing ORDER BY id ASC");
?>

<!--start page wrapper -->
<div class="page-wrapper">
    <div class="page-content">
        <!--breadcrumb-->
        <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
            <div class="breadcrumb-title pe-3">Website Pricing</div>
            <div class="ps-3">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-0 p-0">
                        <li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a></li>
                        <li class="breadcrumb-item active" aria-current="page">New APPLY</li>
                    </ol>
                </nav>
            </div>
        </div>
        <!--end breadcrumb-->

        <div class="row">
            <div class="col-xl-10 mx-auto">
                <h6 class="mb-0 text-uppercase">Website Pricing</h6>
                <hr/>
                <div class="card border-top border-0 border-4 border-primary">
                    <div class="card-body p-5">
                        <div class="card-title d-flex align-items-center">
                            <div><i class="bx bxs-cog me-1 font-22 text-primary"></i></div>
                            <h5 class="mb-0 text-primary">Update Pricing Details</h5>
                        </div>
                        <hr>

                        <form method="POST" class="row g-3">
                            <?php while($row = mysqli_fetch_assoc($result)) { ?>
                                <div class="col-md-3">
                                    <label class="form-label">
                                        <?php echo strtoupper(str_replace("_", " ", $row['service_name'])); ?>
                                    </label>
                                    <input type="text" 
                                           name="price[<?php echo $row['id']; ?>]" 
                                           value="<?php echo $row['price']; ?>" 
                                           class="form-control">
                                </div>
                            <?php } ?>

                            <div class="col-12">
                                <button type="submit" class="btn btn-primary px-5">Save</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<!--end page wrapper -->

	<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<!-- <script src="../template/ahkweb/assets/js/jquery.min.js"></script> -->
	<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--app JS-->
	<script src="../template/ahkweb/assets/js/app.js"></script>
</body>

<?php include('footer.php'); ?>
