<?php 

include 'load.php';

if (isset($_SESSION['user_id'])) {
	$user_qury_02 = mysqli_query($conn, "SELECT * FROM tbl_user_branch WHERE user_id='" . $_SESSION['user_id'] . "'");
	$user_res = mysqli_fetch_array($user_qury_02);
	$user_branch = $user_res['branch_id'];
}

if (isset($_POST['save'])) {
	
	$booknumber = $_POST['booknumber'];
	$customer = $_POST['customer'];
	$room = $_POST['room'];
	$checkindate = $_POST['checkindate'];
	$checkoutdate = $_POST['checkoutdate'];
	$capacity = $_POST['capacity'];
	$note = $_POST['note'];
	$status = $_POST['status'];
	$advance = $_POST['advance'];
	

	if (!isset($errMSG)) {
		$stmt = $DB_con->prepare('INSERT INTO mobileroombooking(booknumber,customer,room,checkindate,checkoutdate,capacity,note,status,advance) VALUES(:booknumber,:customer,:room,:checkindate,:checkoutdate,:capacity,:note,:status,:advance)');
		$stmt->bindParam(':booknumber', $booknumber);
		$stmt->bindParam(':customer', $customer);
		$stmt->bindParam(':room', $room);
		$stmt->bindParam(':checkindate', $checkindate);
		$stmt->bindParam(':checkoutdate', $checkoutdate);
		$stmt->bindParam(':capacity', $capacity);
		$stmt->bindParam(':note', $note);
		$stmt->bindParam(':status', $status);
		$stmt->bindParam(':advance', $advance);
		
		if ($stmt->execute()) {
				$successMSG = "Successfully! Add Your Booking....";
				header("refresh:2;bookinglist.php"); // redirects image view page after 5 seconds.
		} else {
			$errMSG = "Error while inserting product data....";
		}
	}
}; 

?>
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
	<meta name="description" content="POS - 
Eat More Resturant  
 | Pos & Invetory System | Dashboard">
	<meta name="keywords" content="
Eat More Resturant  
 | Pos & Invetory System | Dashboard">
	<meta name="author" content="
Eat More Resturant  
 | Pos & Invetory System | Dashboard">
	<meta name="robots" content="noindex, nofollow">
	<title>Add Booking | 
Eat More Resturant  
 | Pos & Invetory System | Dashboard</title>
	<?php require_once 'headercss.php';	?>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.1.0-rc.0/js/select2.min.js"></script>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
</head>

<body>
	<div id="global-loader">
		<div class="whirly-loader"> </div>
	</div>

	<div class="main-wrapper">
		<?php require_once 'header.php'; ?>
		<?php require_once 'navbar.php'; ?>

		<div class="page-wrapper">
			<div class="content">
				<div class="page-header_update">
					<div class="page-title">
						<h2>Add Room Booking</h2>
						<p>Create new booking</p>
					</div>
				</div>

				<div class="card">
					<div class="card-body">
						<form method="POST" enctype="multipart/form-data">
							<?php if (isset($errMSG)) { ?>
								<div class="alert alert-danger border-0 bg-danger alert-dismissible fade show py-2">
									<div class="d-flex align-items-center">
										<div class="font-35 text-white"><i class="bx bxs-message-square-x"></i>
										</div>
										<div class="ms-3">
											<h6 class="mb-0 text-white">Error Alerts</h6>
											<div class="text-white">
												<?php echo $errMSG; ?>!
											</div>
										</div>
									</div>
									<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
								</div>
							<?php } else if (isset($successMSG)) { ?>
								<div class="alert alert-success border-0 bg-success alert-dismissible fade show py-2">
									<div class="d-flex align-items-center">
										<div class="font-35 text-white"><i class="bx bxs-check-circle"></i>
										</div>
										<div class="ms-3">
											<h6 class="mb-0 text-white">Success Alerts</h6>
											<div class="text-white">
												<?php echo $successMSG; ?>!
											</div>
										</div>
									</div>
									<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
								</div>
							<?php } ?>

							<div class="row">
								<div class="col-lg-3 col-sm-3 col-12">
									<div class="form-group">
										<label>Booking Number</label>
										<?php
										$bookid = '';
										$booknumber = '';
										$stmt = $DB_con->prepare('SELECT IFNULL(MAX(rbid)+1,1) AS rbid FROM `mobileroombooking`');
										$stmt->execute();

										while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
											extract($row);
											$bookid = $row['rbid'];
											$booknumber = "0000$bookid";
										}
										?>
										<input type="text" name="booknumber" class="form-control" placeholder="Enter Booking Number" value="<?php echo $booknumber; ?>" readonly>
									</div>
								</div>
								<div class="col-lg-4 col-sm-4 col-12">
									<div class="form-group">
										<label>Customer</label>
										<select name="customer" class="form-control select2" id="inputcustomer" required>
											<option selected value="0">Select Customer</option>
											<?php

											$stmt = $DB_con->prepare('SELECT * FROM mobilecustomers ORDER BY coid');

											$stmt->execute();

											if ($stmt->rowCount() > 0) {

												while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

												extract($row);

											?>
													<option value="<?php echo $row['coid']; ?>"><?php echo $row['cname']; ?> | <?php echo $row['cphone']; ?></option>
											<?php
												}
											}
											?>
										</select>
									</div>
								</div>
								<div class="col-lg-5 col-sm-5 col-12">
									<div class="form-group">
										<label>Rooms</label>
										<select name="room" class="form-control select2" id="inputrooms" onchange="changeroom(this.value);">
											<option selected value="0">Select Rooms</option>
											<?php

											$stmt = $DB_con->prepare('SELECT * FROM mobileproducts where producttype="Room" ORDER BY prid');

											$stmt->execute();

											if ($stmt->rowCount() > 0) {

											while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

											extract($row);

											?>
											<option value="<?php echo $row['prid']; ?>"><?php echo $row['itemcode']; ?> | <?php echo $row['proname']; ?></option>
											<?php
											}
											}
											?>
										</select>
									</div>
								</div>
								<div class="col-lg-4 col-sm-4 col-12">
									<div class="form-group">
										<label>Check In Date</label>
										<input type="text" name="checkindate" id="checkindate" class="form-control" placeholder="Select Check In Date" required>
									</div>
								</div>
								<div class="col-lg-4 col-sm-4 col-12">
									<div class="form-group">
										<label>Check Out Date</label>
										<input type="text" name="checkoutdate" id="checkoutdate" class="form-control" placeholder="Select Check Out Date" required>
									</div>
								</div>
								<div class="col-lg-4 col-sm-4 col-12">
									<div class="form-group">
										<label>Capacity</label>
										<input type="text" name="capacity" class="form-control" placeholder="Enter Capacity" value="0" required>
									</div>
								</div>
								<div class="col-lg-4 col-sm-4 col-12">
									<div class="form-group">
										<label>Advance Amount</label>
										<input type="text" name="advance" class="form-control" placeholder="Enter Advance" value="0" required>
									</div>
								</div>
								<div class="col-lg-8 col-sm-8 col-12">
									<div class="form-group">
										<label>Note</label>
										<input type="text" name="note" class="form-control" placeholder="Enter Note">
									</div>
								</div>
								<div class="col-lg-4 col-sm-4 col-12">
									<div class="form-group">
										<label>Status</label>
										<select class="form-control select2" required name="status" required>
											<option value="1">Active</option>
											<option value="0">Deactive</option>
										</select>
									</div>
								</div>
								<div class="col-lg-12">
									<a href="bookinglist.php" class="btn btn-cancel">Cancel</a>
									<button type="submit" name="save" class="btn btn-submit me-2">Save</button>
								</div>
							</div>
						</form>
					</div>
				</div>

			</div>
		</div>
	</div>
	<script>
						flatpickr("#checkindate", {
							dateFormat: "Y-m-d",
							minDate: "today"  
						});
						flatpickr("#checkoutdate", {
    						dateFormat: "Y-m-d",
							minDate: "today" 
						});
						
		function calculateSellingPrice() {
			var costPrice = parseFloat(document.getElementById("price").value);
			var profitmargin = parseFloat(document.getElementById("profitmargin").value);

			if (costPrice != "" && profitmargin != "") {
				var sellingPrice = costPrice + profitmargin;
				document.getElementById("sellprice").value = sellingPrice.toFixed(2);
			}
		}

		function calculateProfitPrice() {
			var costPrice = parseFloat(document.getElementById("price").value);
			var sellprice = parseFloat(document.getElementById("sellprice").value);

			if (costPrice != "" && sellprice != "") {
				var profitmargin = sellprice - costPrice;
			} else {
				var profitmargin = 0;

			}
			document.getElementById("profitmargin").value = profitmargin.toFixed(2);
		}

		function disableDiscount() {
			var checkbox = document.getElementById("checkboxDiscount");
			var isChecked = checkbox.checked;

			document.getElementById("discount").disabled = isChecked;
			document.getElementById("discountpercentage").disabled = isChecked;
		}

		function convertToPercentage() {
			var percentageValue = parseFloat(document.getElementById("discount").value);

			if (numericValue != "") {
				var numericValue = percentageValue / 100;
				document.getElementById("discountpersentage").value = numericValue.toFixed(2);
			}
		}



		$(document).ready(function() {
			$('#inputcategory').change(function() {
				var inputcategory = $(this).val();
				$.ajax({
					type: "POST",
					data: {
						inputcategory: inputcategory
					},
					url: "customer-change.php",
					success: function(data) {
						$("#inputsubcategory").html(data);
					}
				});
			});
		});

		function changeroom(room){
				$.ajax({
					type: "POST",
					dataType: "json",
					data: {
						room: room
					},
					url: "change-room.php",
					success: function(data) {
						
						

						flatpickr("#checkindate", {
							disable: data, 
							dateFormat: "Y-m-d",
							minDate: "today" 
						});
						flatpickr("#checkoutdate", {
							disable: data, 
							dateFormat: "Y-m-d",
							minDate: "today" 
						});
					}
				});
		}
		

	</script>
	<?php
	require_once 'footerjs.php';
	?>
</body>

</html>