<?php

include("auth.php"); // yahan $pdo hona chahiye

$id = $_GET['id'] ?? '';

// 🔹 FETCH SERVICES
$stmt = $pdo->prepare(
    "SELECT * FROM services WHERE api_id = ? ORDER BY id DESC"
);
$stmt->execute([$id]);
$services = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 🔹 DELETE SERVICE
if (isset($_POST['delete'])) {
    $delete_id = $_POST['id'];

    $del = $pdo->prepare("DELETE FROM services WHERE id = ?");
    $del->execute([$delete_id]);

    echo '<div class="alert alert-success">Delete success</div>';
    echo "<meta http-equiv='refresh' content='0'>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>View Service - @radiumsahil</title>
    <?php include("include/head.php"); ?>
    <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
</head>

<script>
$(document).ready(function() {
    $('#dashboard').removeClass("active");
    $("#show_service").addClass("active");
});
</script>

<body id="page-top">
<div id="wrapper">

<?php include("include/slidebar.php"); ?>

<div id="content-wrapper" class="d-flex flex-column">
<div id="content">

<?php include("include/topbar.php"); ?>

<div class="container-fluid" id="container-wrapper">

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="#">Home</a></li>
        <li class="breadcrumb-item active">View Service</li>
    </ol>
</div>

<div class="row">
<div class="col-lg-12">
<div class="card mb-4">

<div class="card-header py-3">
    <h6 class="m-0 font-weight-bold text-primary">View Service</h6>
</div>

<div class="table-responsive p-3">
<table class="table align-items-center table-flush" id="dataTable">
<thead class="thead-light">
<tr>
    <th>Service Name</th>
    <th>Service Id</th>
    <th>Service Price</th>
    <th>Status</th>
    <th>Delete</th>
    <th>Action</th>
</tr>
</thead>
<tbody>

<?php foreach ($services as $data): 
    if (true) {
        $statusClass = "badge badge-success";
        $statusText  = "Active";
    } else {
        $statusClass = "badge badge-danger";
        $statusText  = "Inactive";
    }
?>
<tr>
    <td><?= htmlspecialchars($data['service_name']) ?></td>
    <td><?= htmlspecialchars($data['service_code']) ?></td>
    <td><?= htmlspecialchars($data['price']) ?></td>
    <td><span class="<?= $statusClass ?>"><?= $statusText ?></span></td>

    <td>
        <form method="post">
            <input type="hidden" name="id" value="<?= $data['id'] ?>">
            <button class="btn btn-sm btn-danger" type="submit" name="delete">
                Delete
            </button>
        </form>
    </td>

    <td>
        <a href="edit_service?id=<?= $data['id'] ?>" class="btn btn-sm btn-primary">
            Edit
        </a>
    </td>
</tr>
<?php endforeach; ?>

</tbody>
</table>
</div>
</div>
</div>
</div>

</div>
</div>

<a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
</a>

<?php include("include/script.php"); ?>
<script src="vendor/datatables/jquery.dataTables.min.js"></script>
<script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>

<script>
$(document).ready(function () {
    $('#dataTable').DataTable();
});
</script>

</body>
</html>
