<?php

include("auth.php");

/* ================= ADD STOCK ================= */

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

    $server_id = (int)$_POST['server_id'];
    $username  = trim($_POST['username']);
    $password  = trim($_POST['password']);
    $info      = trim($_POST['extra_info']);

    if ($server_id && $username && $password) {

        $stmt = $pdo->prepare("
            INSERT INTO accounts_stock (server_id, username, password, extra_info)
            VALUES (?, ?, ?, ?)
        ");
        $stmt->execute([$server_id, $username, $password, $info]);

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

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

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

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

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

    echo '<div class="alert alert-success">🗑 Stock Deleted</div>';
}

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

$servers = $pdo->query("
    SELECT id, name FROM account_servers
    ORDER BY name ASC
")->fetchAll(PDO::FETCH_ASSOC);

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

$stmt = $pdo->prepare("
    SELECT 
        a.*,
        s.name AS server_name
    FROM accounts_stock a
    JOIN account_servers s ON s.id = a.server_id
    ORDER BY a.id DESC
");
$stmt->execute();

?>

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

<head>
<title>Accounts Stock</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");
    $("#accounts_stock").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">Accounts Stock</li>
</ol>
</div>

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

<div class="form-box">

<h5 class="mb-3">➕ Add Account Stock</h5>

<form method="post">

<div class="row">

<div class="col-md-3">
<select class="form-control" name="server_id" required>
<option value="">Select Server</option>
<?php foreach ($servers as $s) { ?>
<option value="<?= $s['id'] ?>"><?= $s['name'] ?></option>
<?php } ?>
</select>
</div>

<div class="col-md-3">
<input class="form-control" name="username" placeholder="Username" required>
</div>

<div class="col-md-3">
<input class="form-control" name="password" placeholder="Password" required>
</div>

<div class="col-md-2">
<input class="form-control" name="extra_info" placeholder="Extra Info">
</div>

<div class="col-md-1">
<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">📦 Accounts Stock</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>Server</th>
<th>Username</th>
<th>Password</th>
<th>Status</th>
<th>Used By</th>
<th>Used At</th>
<th>Actions</th>
</tr>
</thead>

<tbody>

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

<tr>

<td><?= $row['id'] ?></td>
<td><?= $row['server_name'] ?></td>
<td><?= $row['username'] ?></td>
<td><?= $row['password'] ?></td>

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

<td><?= $row['used_by'] ?: '-' ?></td>
<td><?= $row['used_at'] ?: '-' ?></td>

<td>

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