<?php
include 'db.php';
include 'auth.php';
require_right('Edit Taxes');


$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id <= 0) {
    die("Invalid tax rate ID.");
}

$tax = $conn->query("SELECT * FROM tax_rates WHERE id = $id")->fetch_assoc();
if (!$tax) {
    die("Tax rate not found.");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name']);
    $rate = (float) $_POST['rate'];

    $stmt = $conn->prepare("UPDATE tax_rates SET name=?, rate=? WHERE id=?");
    $stmt->bind_param("sdi", $name, $rate, $id);
    $stmt->execute();

    header("Location: tax_rate_list.php");
    exit;
}

include 'layout.php';
?>
<div class="page-wrapper">
    <div class="content">
        <div class="page-header"><div class="page-title"><h4>Edit Tax Rate</h4></div></div>
        <form method="POST">
            <input type="text" name="name" class="form-control mb-3" value="<?= htmlspecialchars($tax['name']) ?>" required>
            <input type="number" step="0.01" name="rate" class="form-control mb-3" value="<?= $tax['rate'] ?>" required>
            <div class="text-end">
                <button type="submit" class="btn btn-success">Update Tax Rate</button>
            </div>
        </form>
    </div>
</div>