<?php

include("auth.php");

/* ================= ADD SERVER ================= */

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

    $name  = trim($_POST['name']);
    $price = (float)$_POST['price'];
    $msg   = trim($_POST['success_message']);

    if ($name && $price >= 0) {

        $stmt = $pdo->prepare("
            INSERT INTO account_servers (name, price, success_message)
            VALUES (?, ?, ?)
        ");
        $stmt->execute([$name, $price, $msg]);

        echo '<div class="alert alert-success">✅ Server Added</div>';
    }
}

/* ================= DELETE ================= */

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

    $id = (int)$_POST['id'];

    $stmt = $pdo->prepare("DELETE FROM account_servers WHERE id=?");
    $stmt->execute([$id]);

    echo '<div class="alert alert-success">🗑 Delete Success</div>';
}

/* ================= TOGGLE ================= */

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

    $id = (int)$_POST['id'];

    $stmt = $pdo->prepare("
        UPDATE account_servers
        SET is_active = IF(is_active=1,0,1)
        WHERE id=?
    ");
    $stmt->execute([$id]);
}

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

$stmt = $pdo->prepare("SELECT * FROM account_servers ORDER BY id DESC");
$stmt->execute();

?>

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

<head>
<title>Account Servers</title>
<?php include("include/head.php"); ?>
<link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">

<style>

.form-box{
    background:#fff;
    padding:20px;
    border-radius:10px;
    box-shadow:0 4px 10px rgba(0,0,0,.08);
    margin-bottom:25px;
}

</style>

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

</head>

<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">

<!-- PAGE TITLE -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">Account Servers</li>
</ol>
</div>

<!-- ================= ADD FORM ================= -->

<div class="form-box">

<h5 class="mb-3">➕ Add Reselling Server</h5>

<form method="post">

<div class="row">

<div class="col-md-4">
<input class="form-control" name="name" placeholder="Server Name" required>
</div>

<div class="col-md-2">
<input class="form-control" name="price" placeholder="Price" required>
</div>

<div class="col-md-4">
<input class="form-control" name="success_message" placeholder="Success Message">
</div>

<div class="col-md-2">
<button class="btn btn-primary btn-block" name="add">Add</button>
</div>

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

<!-- ================= TABLE ================= -->

<div class="card mb-4">

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

<div class="table-responsive p-3">

<table class="table align-items-center table-flush" id="dataTable">

<thead class="thead-light">
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Status</th>
<th>Message</th>
<th>Actions</th>
</tr>
</thead>

<tbody>

<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>

<tr>

<td><?= $row['id'] ?></td>
<td><?= $row['name'] ?></td>
<td>₹<?= $row['price'] ?></td>

<td>
<?php if ($row['is_active']) { ?>
<span class="badge badge-success">ACTIVE</span>
<?php } else { ?>
<span class="badge badge-danger">DISABLED</span>
<?php } ?>
</td>

<td><?= $row['success_message'] ?></td>

<td>

<form method="post" style="display:inline;">
<input type="hidden" name="id" value="<?= $row['id'] ?>">
<button class="btn btn-sm btn-warning" name="toggle">Toggle</button>
</form>

<form method="post" style="display:inline;">
<input type="hidden" name="id" value="<?= $row['id'] ?>">
<button class="btn btn-sm btn-danger" name="delete">Delete</button>
</form>

</td>

</tr>

<?php } ?>

</tbody>
</table>

</div>
</div>

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

<?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>
