<?php
include 'config.php';
checkCaptainAuth();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $order_id = $conn->real_escape_string($_POST['order_id']);
    $status = $conn->real_escape_string($_POST['status']);
    $captain_id = $_SESSION['captain_id'];
    
    // Verify that the order belongs to this captain
    $verify_order = $conn->query("SELECT id FROM orders WHERE id='$order_id' AND captain_id='$captain_id'");
    
    if ($verify_order->num_rows == 1) {
        $sql = "UPDATE orders SET status='$status' WHERE id='$order_id'";
        
        if ($conn->query($sql) === TRUE) {
            $_SESSION['message'] = "تم تحديث حالة الطلب بنجاح";
            $_SESSION['message_type'] = "success";
        } else {
            $_SESSION['message'] = "خطأ في تحديث حالة الطلب: " . $conn->error;
            $_SESSION['message_type'] = "danger";
        }
    } else {
        $_SESSION['message'] = "لا يمكنك تحديث هذا الطلب";
        $_SESSION['message_type'] = "danger";
    }
    
    header('Location: orders.php');
    exit();
}
?>