<?php

header('Content-Type: text/plain');

include 'config.php';

$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";
    exit();
}

$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();
$conn->close();

?>