<?php
// ============================================================
// PASSWORD
// ============================================================
$PASSWORD = "YOURPASSWORDHERE";

$productsFile = "products.txt";
$products = [];

// Load products
if (file_exists($productsFile)) {
    $raw = file_get_contents($productsFile);
    $blocks = preg_split("/\n\s*\n/", trim($raw));

    foreach ($blocks as $b) {
        $lines = array_map("trim", explode("\n", $b));
        if (count($lines) >= 7) { // NOW EXPECTS 7 LINES
            $products[] = [
                "name"     => $lines[0],
                "category" => $lines[1],
                "count"    => $lines[2],
                "image"    => $lines[3],
                "subtitle" => preg_replace("/^Subtitle:\s*/i", "", $lines[4]),
                "link"     => $lines[5],
                "tab"      => preg_replace("/^Tab:\s*/i", "", $lines[6]) // NEW
            ];
        }
    }
}

// Save edited product
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["edit"])) {
    if ($_POST["password"] !== $PASSWORD) {
        die("Invalid password");
    }

    $index = intval($_POST["index"]);

    $products[$index]["name"]     = trim($_POST["name"]);
    $products[$index]["category"] = trim($_POST["category"]);
    $products[$index]["count"]    = trim($_POST["count"]);
    $products[$index]["image"]    = trim($_POST["image"]);
    $products[$index]["subtitle"] = trim($_POST["subtitle"]);
    $products[$index]["link"]     = trim($_POST["link"]);
    $products[$index]["tab"]      = trim($_POST["tab"]); // NEW

    // Rewrite file
    $out = "";
    foreach ($products as $p) {
        $out .= $p["name"] . "\n";
        $out .= $p["category"] . "\n";
        $out .= $p["count"] . "\n";
        $out .= $p["image"] . "\n";
        $out .= "Subtitle: " . $p["subtitle"] . "\n";
        $out .= $p["link"] . "\n";
        $out .= "Tab: " . $p["tab"] . "\n\n"; // NEW
    }

    file_put_contents($productsFile, $out);

    echo "<div style='padding:20px;color:#7aff9c;'>Product updated.</div>";
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Edit Product</title>
<style>
body {
    background:#0a0a0a;
    color:#e6e2d3;
    font-family:"Cinzel", serif;
    padding:20px;
}
form {
    max-width:420px;
    margin:auto;
    background:#1a1a1a;
    padding:20px;
    border:2px solid #333;
    border-radius:8px;
}
input, select {
    width:100%;
    padding:10px;
    margin:6px 0;
    background:#222;
    border:2px solid #444;
    border-radius:6px;
    color:#e6e2d3;
}
button {
    width:100%;
    padding:12px;
    background:#333;
    border:2px solid #555;
    border-radius:6px;
    color:#e6e2d3;
    font-weight:bold;
    cursor:pointer;
}
button:hover {
    background:#444;
}
h1 {
    text-align:center;
    color:#7aff9c;
}
</style>
</head>

<body>
<h1>Edit Product</h1>

<form method="POST">
    <input type="password" name="password" placeholder="Password">

    <select name="index" onchange="this.form.submit()">
        <option value="">Select product…</option>
        <?php foreach ($products as $i => $p): ?>
            <option value="<?= $i ?>"><?= htmlspecialchars($p["name"]) ?></option>
        <?php endforeach; ?>
    </select>

    <?php if (isset($_POST["index"]) && $_POST["index"] !== ""):
        $p = $products[intval($_POST["index"])];
    ?>

    <input name="name" value="<?= htmlspecialchars($p["name"]) ?>">
    <input name="category" value="<?= htmlspecialchars($p["category"]) ?>">
    <input name="count" value="<?= htmlspecialchars($p["count"]) ?>">
    <input name="image" value="<?= htmlspecialchars($p["image"]) ?>">
    <input name="subtitle" value="<?= htmlspecialchars($p["subtitle"]) ?>">
    <input name="link" value="<?= htmlspecialchars($p["link"]) ?>">
    <input name="tab" value="<?= htmlspecialchars($p["tab"]) ?>" placeholder="Tab"> <!-- NEW -->

    <button name="edit" type="submit">Save Changes</button>

    <?php endif; ?>
</form>

</body>
</html>