<?php

header('Content-Type: text/plain');
include 'config.php';

/* =========================
   ADMIN AUTH CHECK
   ========================= */

$admin_username = $_POST['admin_username'] ?? '';
$admin_password = $_POST['admin_password'] ?? '';

$admin_stmt = $conn->prepare(
    "SELECT 1 FROM admin WHERE username = ? AND password = ?"
);

$admin_stmt->bind_param("ss", $admin_username, $admin_password);
$admin_stmt->execute();
$admin_result = $admin_stmt->get_result();

if ($admin_result->num_rows === 0) {
    echo "Access Denied";
    $admin_stmt->close();
    $conn->close();
    exit();
}

$admin_stmt->close();

/* =========================
   TYPE HANDLER
   ========================= */

$type = $_POST['type'] ?? '';

/* =========================
   ADD TOPUP
   ========================= */

if ($type == "add") {

    $title = trim($_POST['title'] ?? '');
    $real_price = trim($_POST['real_price'] ?? '');
    $discount_price = trim($_POST['discount_price'] ?? '');
    $image_link = trim($_POST['image_link'] ?? '');
    $stock_message = trim($_POST['stock_message'] ?? '');

    if (
        empty($title) ||
        empty($real_price) ||
        empty($discount_price) ||
        empty($image_link) ||
        empty($stock_message)
    ) {
        echo "All Fields Required";
        exit();
    }

    $stmt = $conn->prepare(
        "INSERT INTO topup_list
        (title, real_price, discount_price, image_link, stock_message)
        VALUES (?, ?, ?, ?, ?)"
    );

    $stmt->bind_param(
        "sssss",
        $title,
        $real_price,
        $discount_price,
        $image_link,
        $stock_message
    );

    if ($stmt->execute()) {
        echo "Topup Added Successfully";
    } else {
        echo "Failed To Add Topup";
    }

    $stmt->close();
}

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

elseif ($type == "delete") {

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

    if (empty($id)) {
        echo "ID Required";
        exit();
    }

    $stmt = $conn->prepare(
        "DELETE FROM topup_list WHERE id = ?"
    );

    $stmt->bind_param("i", $id);

    if ($stmt->execute()) {
        echo "Topup Deleted Successfully";
    } else {
        echo "Failed To Delete Topup";
    }

    $stmt->close();
}

/* =========================
   UPDATE TOPUP
   ========================= */

elseif ($type == "update") {

    $id = $_POST['id'] ?? '';
    $title = trim($_POST['title'] ?? '');
    $real_price = trim($_POST['real_price'] ?? '');
    $discount_price = trim($_POST['discount_price'] ?? '');
    $image_link = trim($_POST['image_link'] ?? '');
    $stock_message = trim($_POST['stock_message'] ?? '');

    if (
        empty($id) ||
        empty($title) ||
        empty($real_price) ||
        empty($discount_price) ||
        empty($image_link) ||
        empty($stock_message)
    ) {
        echo "All Fields Required";
        exit();
    }

    $stmt = $conn->prepare(
        "UPDATE topup_list
         SET title = ?, real_price = ?, discount_price = ?, image_link = ?, stock_message = ?
         WHERE id = ?"
    );

    $stmt->bind_param(
        "sssssi",
        $title,
        $real_price,
        $discount_price,
        $image_link,
        $stock_message,
        $id
    );

    if ($stmt->execute()) {
        echo "Topup Updated Successfully";
    } else {
        echo "Failed To Update Topup";
    }

    $stmt->close();
}

else {
    echo "Invalid Type";
}

$conn->close();

?>