<!DOCTYPE HTML>  
<html>
<head>
<style>
body {
	text-align: center;
    border: 1px solid;
    padding: 10px;
    margin: 10px;
}
.error {color: #FF0000;}
.success {color: green;}
	.subnit{
		padding: 5px 10px;
    background: #0c355e;
    color: white;
    border: none;
	}
	h2{
		color: brown
	}
	span {
		display: block;
		margin: 10px;
	}
</style>
</head>
<body>  

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

$mysqli = new mysqli("109.106.255.67","root","Innoknights@19","hygeia_uat");
date_default_timezone_set('Asia/Kolkata');

if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$orderid = $_POST["orderid"];
  if (empty($orderid)){
    $orderidErr = "Order id is required";
  } else {
    $status_id = $_POST["status"];
	$current_time = date("Y-m-d H:i:s");

	if($status_id != 3) {
		$query = "update HYGEIA_ORDER SET order_stage_".$status_id."=1, order_stage_".$status_id."_ts='".$current_time."' where Order_Id=".$orderid;
		if ($mysqli->query($query) === TRUE) {
		  $successmsg = "Order updated successfully";
		} else {
		  $errorsmsg = "Error updating record: " . $mysqli->error;
		}
	} else {
		
		$result = $mysqli->query("SELECT Service_Id FROM HYGEIA_ORDER where Order_Id=".$orderid);
		$order = $result->fetch_row();
		$serviceid = $order[0];
		
		if ($serviceid > 0 ){
			if($serviceid == 1){
				$order_cost = 100;
				$item_sql = "INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','1',".$orderid.",45,45); 
				INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','2',".$orderid.",55,55);";
			} else if ($serviceid == 2) {
				$order_cost = 50; 
				$item_sql = "INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('2','6',".$orderid.",10,20); 
				INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('3','7',".$orderid.",10,30);";
			} else if ($serviceid == 3) {
				$order_cost = 85;
				$item_sql = "INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','11',".$orderid.",45,45); 
				INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('2','12',".$orderid.",20,40);";
			} else if ($serviceid == 4) {
				$order_cost = 92; 
				$item_sql = "INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','16',".$orderid.",22,22); 
				INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('2','17',".$orderid.",35,70);";
			} else if ($serviceid == 5) {
				$order_cost = 79; 
				$item_sql = "INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('2','21',".$orderid.",22,44); 
				INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','22',".$orderid.",35,35);";
			} else {
				$order_cost = 57;
				$item_sql = "INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','26',".$orderid.",22,22); 
				INSERT INTO HYGEIA_ORDER_ITEM (Item_quantity, Item_map_id, Order_id, Unit_Amount, Total_Amount) VALUES ('1','27',".$orderid.",35,35);";
			}
		}
		
		$query = "update HYGEIA_ORDER SET Cost=".$order_cost.", order_stage_3=1, order_stage_3_ts='".$current_time."' where Order_Id=".$orderid;

		if ($mysqli->query($query) === TRUE) {			
			if ($mysqli->multi_query($item_sql) === TRUE) {
				$successmsg = "Order updated successfully";
			} else {
			  $errorsmsg  = "Error: " . $item_sql . "<br>" . $mysqli->error;
			}
		} else {
		   $errorsmsg = "Error updating record: " . $mysqli->error;
		}
	}
  }      
}
?>

<h2>Update Hygeia order</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Order ID: <input type="text" name="orderid" value="">
  <br><br>
  <label for="status">Status:</label>
	<select name="status" id="status" >
		<option value="1">Assigned</option>
		<option value="2">Waiting for Pickup</option>
		<option value="3">In Progress</option>
		<option value="4">Waiting for Delivery</option>
		<option value="5">Closed</option>
		<option value="6">Cancelled</option>
	</select>
  <br><br>
  <input class="subnit" type="submit" name="submit" value="Submit">  
  <span class="success"><?php echo $successmsg;?></span>
  <span class="error"><?php echo $errorsmsg;?></span>
    <span class="error"><?php echo $orderidErr;?></span>

</form>
</body>
</html>