<?php
include("../connection.php");
include("../user.php");
$cur_date = date('Y-m-d H:i:s');
$user = $_SESSION['user'];
$branch_id = $_SESSION['branch_id'];
$ho_access = $_SESSION['ha'];
$branch = $_SESSION['division_name'];
$location = $_SESSION['location'];
// $branch_detail = mysqli_fetch_array(mysqli_query($con, "SELECT location FROM `location_master` where id='$branch_id'"));
// $location_name = $branch_detail['location'];

// echo "Current Date: " . $cur_date . "<br>";
// echo "User: " . $user . "<br>";
// echo "Branch ID: " . $branch_id . "<br>";
// echo "HO Access: " . $ho_access . "<br>";
// echo "Branch: " . $branch . "<br>";
// echo "Location (Session): " . $location . "<br>";
// echo "Location (From DB): " . $location_name . "<br>";
?>
<!doctype html>
<html class="no-js" lang="">

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title><?php echo TITLE; ?></title>
	<meta name="description" content="Ela Admin - HTML5 Admin Template">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="apple-touch-icon" href="../images/bluebase.png">
	<link rel="shortcut icon" href="../images/bluebase.png">
	<!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script> -->

	<style>
		input {
			text-transform: uppercase;
		}

		textarea {
			text-transform: uppercase;
		}

		select {
			text-transform: uppercase;
		}
	</style>
</head>

<body>
	<div class="col-lg-8">
		<div class="card">
			<div class="card-header">
				<strong>Repair Stock</strong>
				<a href="repair.php"><button class="btn btn-primary" style="width:50px;float:right;" onclick=""><i
							class="fa fa-backward"></i></button></a>
			</div>
			<div class="card-body card-block">
				<form method="post" enctype="multipart/form-data" class="form-horizontal" action="repair_submit.php">
					<?php
					if ($ho_access == "yes") {
						?>
						<div class="row form-group" id="asset_set">
							<div class="col col-md-3"><label for="text-input" class=" form-control-label">Asset
									ID</label></div>
							<div class="col-12 col-md-9">
								<select id="asset_code" name="asset_code" onchange="asse_code(this.value)"
									class="form-control select-search" autocomplete="off">
									<option value="" selected disabled>Select Asset Code</option>
									<?php $asset_id = mysqli_query($con, "SELECT * FROM `asset_master` where status=0");
									while ($asset = mysqli_fetch_array($asset_id)) { ?>
										<option value="<?php echo $asset['id']; ?>">
											<?php echo $asset['description']; ?>
										</option>
									<?php } ?>
								</select>
							</div>
						</div>
					<?php } else { ?>
						<div class="row form-group">
							<div class="col col-md-3"><label for="text-input" class=" form-control-label">Asset
									ID</label></div>
							<div class="col-12 col-md-9">
								<select id="asset_code" name="asset_code" onchange="asse_code(this.value)"
									class="form-control select-search" autocomplete="off">
									<option value="" selected disabled>Select Asset Code</option>
									<?php
									$asset_id = mysqli_query($con, "SELECT * FROM `asset_master` where location='$location' and division='$branch' and status=0");
									while ($asset = mysqli_fetch_array($asset_id)) { ?>
										<option value="<?php echo $asset['id']; ?>">
											<?php echo $asset['category'] . '-' . $asset['category_description']; ?>
										</option>
									<?php } ?>
								</select>
							</div>
						</div>
					<?php } ?>
					<!--<div class="row form-group">-->
					<!--	<div class="col col-md-3"><label for="text-input"-->
					<!--			class="form-control-label">Division</label></div>-->
					<!--	<div class="col-12 col-md-9">-->
					<!--		<input type="text" name="division" id="division" class="form-control" placeholder="Division"-->
					<!--			readonly>-->
					<!--	</div>-->
					<!--</div>-->
					<!--<div class="row form-group">-->
					<!--	<div class="col col-md-3"><label for="text-input"-->
					<!--			class="form-control-label">Department</label></div>-->
					<!--	<div class="col-12 col-md-9">-->
					<!--		<input type="text" name="department" id="department" class="form-control"-->
					<!--			placeholder="Department" readonly>-->
					<!--	</div>-->
					<!--</div>-->
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input"
								class="form-control-label">Location</label></div>
						<div class="col-12 col-md-9">
							<input type="text" name="location" id="location" class="form-control" placeholder="Location"
								readonly>
						</div>
					</div>
					<!--<div class="row form-group">-->
					<!--	<div class="col col-md-3"><label for="text-input" class=" form-control-label">No of-->
					<!--			items</label></div>-->
					<!--	<div class="col-12 col-md-9">-->
					<!--		<input type="number" name="no_of_counts" placeholder="Enter Counts" class="form-control"-->
					<!--		>-->
					<!--	</div>-->
					<!--</div>-->
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input"
								class=" form-control-label">Priority</label></div>
						<div class="col-12 col-md-9">
							<select id="asset_code" name="priority" class="form-control">
								<option selected disabled>select priority</option>
								<option value="High">High</option>
								<option value="Medium">Medium</option>
								<option value="Low">Low</option>
							</select>
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">Vendor
								Name</label></div>
						<div class="col-12 col-md-9">
							<select id="vendor_det" name="vendor_det" class="form-control select-search" autocomplete="off">
								<option value="" selected disabled>Select Vendor</option>
								<?php
								$vendors = mysqli_query($con, "SELECT * FROM `vendor_master` where status=0");
								while ($vendors_details = mysqli_fetch_array($vendors)) { ?>
									<option value="<?php echo $vendors_details['id']; ?>">
										<?php echo $vendors_details['vendor_name'] . '-' . $vendors_details['vendor_address']; ?>
									</option>
								<?php } ?>
							</select>
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class="form-control-label">Send
								on</label></div>
						<div class="col-12 col-md-9">
							<input type="date" name="submit_dates" class="form-control">
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class="form-control-label">Expect Return
								Date</label></div>
						<div class="col-12 col-md-9">
							<input type="date" name="expect_date" class="form-control">
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">Repair
								Remarks</label></div>
						<div class="col-12 col-md-9">
							<textarea name="remarks" placeholder="Repair Remarks" class="form-control"></textarea>
						</div>
					</div>

					<div style="">
						<button type="submit" name="submit" style="float:right;" class="btn btn-success"><i
								class="fa fa-inbox"></i> Submit</button>
					</div>
				</form>
			</div>
		</div>
		<div class="clearfix"></div>

		<footer class="site-footer">

		</footer>

	</div><!-- /#right-panel-->
</body>
<script>
	// Initialize Select2
	$(document).ready(function () {
		$('.select-search').select2({
			placeholder: "-- SELECT --",  // Optional placeholder text
			allowClear: true                // Optional: allows clearing the selection
		});
	});
	function asse_code(asset_id) {
		if (asset_id !== '') {
			$.ajax({
				url: "get_asset_details.php",
				type: "POST",
				data: { id: asset_id },
				success: function (response) {
					let data = JSON.parse(response);
					$('#location').val(data.location);
					// alert(response);
				},
				error: function () {
					alert('Something went wrong. Could not fetch asset details.');
				}
			});
		}
	}
</script>

</html>