<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('con.php');
include('header.php');

/* ================= LOGIN CHECK ================= */

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

$username = $_SESSION['phone'];

/* ================= USER CHECK ================= */

$userData = mysqli_fetch_assoc(
    mysqli_query(
        $ahk_conn,
        "SELECT * FROM users WHERE phone='$username'"
    )
);

if (!$userData || $userData['type'] != 'admin') {
    die("
        <div style='
            padding:40px;
            font-family:Arial;
            text-align:center;
            color:red;
            font-size:22px;
        '>
            Access Denied
        </div>
    ");
}

$successMsg = "";
$errorMsg   = "";

/* ================= UPDATE SERVICE ================= */

if (isset($_POST['update_service'])) {

    $id      = intval($_POST['id']);
    $service = mysqli_real_escape_string(
        $ahk_conn,
        trim($_POST['service'])
    );

    $rate = floatval($_POST['rate']);

    $status = intval($_POST['status']);

    if ($service == "") {

        $errorMsg = "Service name cannot be empty.";

    } else {

        $update = mysqli_query(
            $ahk_conn,
            "UPDATE price SET
                service='$service',
                rate='$rate',
                status='$status'
             WHERE id='$id'"
        );

        if ($update) {

            $successMsg = "Service updated successfully.";

        } else {

            $errorMsg = mysqli_error($ahk_conn);
        }
    }
}

/* ================= FETCH SERVICES ================= */

$services = mysqli_query(
    $ahk_conn,
    "SELECT * FROM price ORDER BY id DESC"
);
?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Service Management</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>

body{
    background:#f4f7fb;
    font-family:'Segoe UI',sans-serif;
}

/* ================= PAGE TITLE ================= */

.page-title{
    font-size:32px;
    font-weight:800;
    color:#4f46e5;
}

.page-sub{
    color:#6b7280;
    margin-top:5px;
}

/* ================= CARD ================= */

.card-box{
    background:#fff;
    border-radius:22px;
    padding:25px;
    box-shadow:0 10px 35px rgba(0,0,0,0.08);
    border:none;
}

/* ================= TABLE ================= */

.table{
    border-radius:15px;
    overflow:hidden;
}

.table thead{
    background:linear-gradient(135deg,#4f46e5,#7c3aed);
    color:#fff;
}

.table th{
    font-size:14px;
    padding:15px;
    white-space:nowrap;
}

.table td{
    vertical-align:middle;
    padding:12px;
}

/* ================= INPUTS ================= */

.form-control,
.form-select{
    border-radius:12px;
    height:45px;
    border:1px solid #dbe2ea;
    box-shadow:none;
}

.form-control:focus,
.form-select:focus{
    border-color:#4f46e5;
    box-shadow:0 0 0 0.15rem rgba(79,70,229,.15);
}

/* ================= BUTTON ================= */

.update-btn{
    background:linear-gradient(135deg,#4f46e5,#7c3aed);
    border:none;
    color:#fff;
    border-radius:12px;
    padding:10px 18px;
    font-weight:600;
    transition:.3s;
}

.update-btn:hover{
    transform:translateY(-2px);
    box-shadow:0 10px 20px rgba(79,70,229,.25);
}

/* ================= STATUS BADGE ================= */

.badge-active{
    background:#dcfce7;
    color:#166534;
    padding:6px 12px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

.badge-inactive{
    background:#fee2e2;
    color:#991b1b;
    padding:6px 12px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

/* ================= MOBILE ================= */

@media(max-width:768px){

    .page-title{
        font-size:24px;
    }

    .table th,
    .table td{
        font-size:12px;
    }

    .update-btn{
        width:100%;
    }

}

</style>

</head>

<body>

<div class="container-fluid py-4">

<!-- ================= HEADING ================= -->

<div class="mb-4">

<div class="page-title">
Service Management
</div>

<div class="page-sub">
Manage all services, rates and status
</div>

</div>

<!-- ================= CARD ================= -->

<div class="card-box">

<div class="table-responsive">

<table class="table table-bordered align-middle text-center">

<thead>

<tr>
    <th>ID</th>
    <th>Service</th>
    <th>Rate</th>
    <th>Status</th>
    <th>Current</th>
    <th>Action</th>
</tr>

</thead>

<tbody>

<?php if(mysqli_num_rows($services) > 0){ ?>

<?php while($row = mysqli_fetch_assoc($services)){ ?>

<tr>

<form method="POST">

<td>

#<?= $row['id'] ?>

<input
type="hidden"
name="id"
value="<?= $row['id'] ?>"
>

</td>

<td>

<input
type="text"
name="service"
class="form-control"
value="<?= htmlspecialchars($row['service']) ?>"
required
>

</td>

<td>

<input
type="number"
step="0.01"
name="rate"
class="form-control"
value="<?= $row['rate'] ?>"
required
>

</td>

<td>

<select
name="status"
class="form-select"
required
>

<option value="1"
<?= ($row['status'] == 1 ? 'selected' : '') ?>>
1 - Active
</option>

<option value="0"
<?= ($row['status'] == 0 ? 'selected' : '') ?>>
0 - Inactive
</option>

</select>

</td>

<td>

<?php if($row['status'] == 1){ ?>

<span class="badge-active">
ACTIVE
</span>

<?php } else { ?>

<span class="badge-inactive">
INACTIVE
</span>

<?php } ?>

</td>

<td>

<button
type="submit"
name="update_service"
class="update-btn"
>
Update
</button>

</td>

</form>

</tr>

<?php } ?>

<?php } else { ?>

<tr>

<td colspan="6">

No Services Found

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

<!-- ================= SUCCESS ALERT ================= -->

<?php if($successMsg != ""){ ?>

<script>

Swal.fire({
    icon:'success',
    title:'Success',
    text:'<?= $successMsg ?>'
});

</script>

<?php } ?>

<!-- ================= ERROR ALERT ================= -->

<?php if($errorMsg != ""){ ?>

<script>

Swal.fire({
    icon:'error',
    title:'Error',
    text:'<?= $errorMsg ?>'
});

</script>

<?php } ?>

</body>
</html>