<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$store_id = intval($_GET['id']);
$store = $conn->query("SELECT * FROM stores WHERE id=$store_id")->fetch_assoc();

if(!$store){
    echo "<script>alert('Store not found');window.location='products.php';</script>";
    exit;
}

if(isset($_POST['update_store'])){
    $name = $conn->real_escape_string($_POST['store_name']);
    $location = $conn->real_escape_string($_POST['location']);
    $manager = $conn->real_escape_string($_POST['manager']);

    $conn->query("UPDATE stores SET store_name='$name', location='$location', manager='$manager' WHERE id=$store_id");

    echo "<script>alert('Store updated successfully');window.location='products.php';</script>";
    exit;
}
?>

<div class="container mt-4">
      <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
    <h3>Edit Store: <?= htmlspecialchars($store['store_name']) ?></h3>
    <form method="POST" class="row g-3">
        <div class="col-md-4">
            <input type="text" name="store_name" class="form-control" placeholder="Store Name" value="<?= htmlspecialchars($store['store_name']) ?>" required>
        </div>
        <div class="col-md-4">
            <input type="text" name="location" class="form-control" placeholder="Location" value="<?= htmlspecialchars($store['location']) ?>">
        </div>
        <div class="col-md-4">
            <input type="text" name="manager" class="form-control" placeholder="Manager" value="<?= htmlspecialchars($store['manager']) ?>">
        </div>
        <div class="col-md-2 mt-3">
            <button type="submit" name="update_store" class="btn btn-success w-100">Update Store</button>
        </div>
    </form>
</div>

<?php include 'includes/footer.php'; ?>
