<?php 


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


if(isset($_POST['submit'])){
    $item_id =$_POST['id'];
    $orderid =$_POST['orderid'];
    $reason=$_POST['reason'];
    $date=$_POST['date'];
    $by=$_POST['by'];
    $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 id='$item_id'";
		if(mysqli_query($conn,$sql)){
      echo"<script>alert('You Have Successfully Canceled This order'); window.location='../orders'</script>";
		}
		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='../orders'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
    }

	}
 ?>