<?php 


include('../conn.php');


if(isset($_POST['submit'])){
    $item_id =$_POST['id'];
    $orderid =$_POST['orderid'];
    $reason=$_POST['reason'];
    $date=$_POST['date'];
    $by=$_POST['by'];
     $l=$_POST['location'];
    $status=$_POST['status'];

    if ($reason=="nan") {
        echo"<script>alert('Reason Cannot be empty'); window.location='../orders'</script>";
    }
    else {
        
		$sql = "UPDATE `orders` SET `status`='$status' WHERE orderid='$orderid'";
		if(mysqli_query($conn,$sql)){
     
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}

    $sql2 = "UPDATE `orders_details` SET `status`='$status' WHERE orderid='$orderid'";
		if(mysqli_query($conn,$sql2)){
     
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}

        $sql1 = "INSERT INTO `order_status`( `orderid`,`item_id`, `status`, `by`, `date`, `note`) VALUES ('$orderid','$item_id','$status','$by','$date','$reason')";
		if(mysqli_query($conn,$sql1)){
     echo"<script>alert('You Have Successfully Canceled This order'); window.location='../$l'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
    }

	}
 ?>