<?php
require_once("../php/db_connect.php");

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["user_id"])) {
    header("Location: index.php");
    exit();
}

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $order_id = (int) $_POST["order_id"];
    $status = trim($_POST["status"]);
    $payment_status = trim($_POST["payment_status"]);
    $shop_id = (int) $_SESSION["user_id"];

    $stmt = $conn->prepare("
        UPDATE orders
        SET status = ?, payment_status = ?
        WHERE id = ? AND shop_id = ?
    ");

    if (!$stmt) {
        die("Prepare failed: " . $conn->error);
    }

    $stmt->bind_param("ssii", $status, $payment_status, $order_id, $shop_id);

    if (!$stmt->execute()) {
        die("Execute failed: " . $stmt->error);
    }

    // ✅ Trigger update success modal on admin.php
    header("Location: admin.php?page=orders&update=success");
    exit();
}

// fallback redirect if not POST
header("Location: admin.php?page=orders");
exit();
?>