<?php
session_start();
require_once '../config.php';

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

function e($v){ return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8'); }

$message = '';
$error = '';

// load current discount
$currentDiscount = 0.00;
$res = $conn->query("SELECT discount_percent FROM discount_settings ORDER BY id DESC LIMIT 1");
if ($res && $row = $res->fetch_assoc()) {
    $currentDiscount = (float)$row['discount_percent'];
}

// save
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $discount = (float)($_POST['discount_percent'] ?? 0);

    if ($discount < 0 || $discount > 100) {
        $error = 'Discount must be between 0 and 100.';
    } else {
        $userId = (int)$_SESSION['user_id'];

        // keep only one latest setting
        $check = $conn->query("SELECT id FROM discount_settings ORDER BY id DESC LIMIT 1");
        if ($check && $check->num_rows > 0) {
            $row = $check->fetch_assoc();
            $id = (int)$row['id'];

            $stmt = $conn->prepare("UPDATE discount_settings SET discount_percent=?, updated_by=? WHERE id=?");
            $stmt->bind_param("dii", $discount, $userId, $id);
            $stmt->execute();
            $stmt->close();
        } else {
            $stmt = $conn->prepare("INSERT INTO discount_settings (discount_percent, updated_by) VALUES (?, ?)");
            $stmt->bind_param("di", $discount, $userId);
            $stmt->execute();
            $stmt->close();
        }

        $currentDiscount = $discount;
        $message = 'Discount saved successfully.';
    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Discount</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
body{
    margin:0;
    font-family:Arial,Helvetica,sans-serif;
    background:#eef2f7;
}
.container{
    margin-left:260px;
    padding:75px 24px 24px;
}
.card{
    background:#fff;
    border-radius:16px;
    padding:22px;
    box-shadow:0 10px 25px rgba(0,0,0,.08);
    max-width:600px;
    width:100%;
}
h2{
    margin:0 0 18px;
    font-size:24px;
    color:#111827;
    display:flex;
    align-items:center;
    gap:10px;
}

.label{
    display:block;
    margin-bottom:8px;
    font-weight:600;
    color:#374151;
    font-size:14px;
}
.input{
    width:100%;
    height:48px;
    border:1px solid #dbe3ee;
    border-radius:12px;
    padding:0 14px;
    font-size:15px;
    outline:none;
    box-sizing:border-box;
}

.btn{
    height:46px;
    padding:0 18px;
    border:0;
    border-radius:12px;
    background:#374155;
    color:#fff;
    font-weight:700;
    cursor:pointer;
}
.notice{
    background:#ecfdf5;
    color:#065f46;
    padding:12px 14px;
    border-radius:10px;
    margin-bottom:14px;
}
.error{
    background:#fef2f2;
    color:#991b1b;
    padding:12px 14px;
    border-radius:10px;
    margin-bottom:14px;
}
.small{
    color:#6b7280;
    font-size:13px;
    margin-top:8px;
    line-height:1.5;
}
@media (max-width:768px){
    .container{
        margin-left:0;
        padding:16px;
    }
    .card{
        max-width:100%;
        width:100%;
        padding:18px;
    }
}
</style>
</head>
<body>

<?php include 'adminsidebar.php'; ?>
<?php include '../header.php'; ?>

<div class="container">
    <div class="card">
        <h2><i class="fa-solid fa-tags"></i>Discount Limit</h2>

        <?php if ($message): ?>
            <div class="notice"><?php echo e($message); ?></div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="error"><?php echo e($error); ?></div>
        <?php endif; ?>

        <form method="post">
            <label class="label">Discount Percent</label>
            <input
                type="number"
                class="input"
                name="discount_percent"
                min="0"
                max="100"
                step="0.01"
                value="<?php echo e($currentDiscount); ?>"
                placeholder="Enter discount percent"
            >
            <div class="small">Example: 5 = 5% discount on every bill.</div>

            <div style="margin-top:16px;">
                <button type="submit" class="btn">Save Discount</button>
            </div>
        </form>
    </div>
</div>

</body>
</html>