<?php
    include("../connection.php");
    include("../user.php");
    $cur_date = date('Y-m-d H:i:s');
    $sys_date = date('d-m-Y');
    $user = $_SESSION['user'];
    $branch_id = $_SESSION['branch_id'];
    $ho_access = $_SESSION['ha'];
    $location = $_SESSION['location'];
    $id = $_REQUEST['id'];
    $editoutward = mysqli_fetch_array(mysqli_query($con, "select  * from outward_master where id='$id'"));
?>
<!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 src="../js/sweet-alert.js"></script>
	<style>
		input {
			text-transform: uppercase;
		}

		textarea {
			text-transform: uppercase;
		}
	</style>
</head>
<body>
	<div class="col-lg-8">
		<div class="card">
			<div class="card-header">
				<strong>Edit Outward</strong>
				<a href="outward_master.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" id="editOutwardForm" class="form-horizontal">

    			    <input type="hidden" name="id" class="form-control" value=" <?php echo $editoutward['id'] ?>">
                   <div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">DATE</label></div>
						<div class="col-12 col-md-9"><input type="date" id="so_date" name="so_date" value="<?php echo $editoutward['so_date']; ?>" class="form-control" autocomplete="off"></div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">MR No.</label></div>
						<div class="col-12 col-md-9"><input type="number" id="mr_no" name="mr_no" value="<?php echo $editoutward['mr_no']; ?>" class="form-control" placeholder="Enter Mr.no" autocomplete="off"></div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">TICKET No.</label></div>
						<div class="col-12 col-md-9"><input type="text" id="ticket_no" name="ticket_no" value="<?php echo $editoutward['ticket_no']; ?>" class="form-control" placeholder="Enter Mr.no" autocomplete="off"></div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="material_name" class="form-control-label">MATERIAL NAME</label></div>
						<div class="col-12 col-md-9">
						    <select class="form-control select-search" name="material_name" id="material_name" required>
									<option value="">CHOOSE MATERIAL NAME</option>
									<?php
									$category_list = mysqli_query($con, "SELECT id,category_type FROM category_master WHERE status='0'");
									while ($category = mysqli_fetch_array($category_list)) {
									?>
										<option value="<?php echo $category['id']; ?>" <?php if($editoutward['material_name'] == $category['id']) { echo "selected"; } ?>><?php echo strtoupper($category['category_type']); ?></option>
									<?php
									}
									?>
							</select>
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">QUANTITY</label></div>
						<div class="col-12 col-md-9">
							<input type="number" id="qty" name="qty" value="<?php echo $editoutward['qty']; ?>" class="form-control" placeholder="Enter Quantity" autocomplete="off" >
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">QUM</label></div>
						<div class="col-12 col-md-9">
						    <select class="form-control select-search" name="qum" id="qum">
								<option value="">CHOOSE QUM</option>
								<?php
								$qums = mysqli_query($con, "SELECT id,name FROM measurement_master WHERE status='0'");
								while ($qum = mysqli_fetch_array($qums)) {
									$selected = ($qum['id'] == $editoutward['qum']) ? "selected" : "";
								?>
									<option value="<?php echo $qum['id']; ?>" <?php echo $selected; ?>><?php echo strtoupper($qum['name']); ?></option>
								<?php
								}
								?>
							</select>
						</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">
							<select class="form-control select-search" name="department" id="department" >
								<?php
								$department = mysqli_query($con, "SELECT * FROM type_master where status='0'");
								?>
								<option value="">CHOOSE DEPARTMENT</option>
								<?php
								while ($department_det = mysqli_fetch_array($department)) { 
									$selected = ($department_det['id'] == $editoutward['department']) ? "selected" : "";
									?>
									<option value="<?php echo $department_det['id']; ?>" <?php echo $selected; ?>><?php echo strtoupper($department_det['type']); ?></option>
									<?php	}	?>
							</select>
						</div>
					</div>
					<div class="row form-group">
            <div class="col col-md-3"><label for="text-input" class=" form-control-label">BRANCH</label></div>
            <div class="col-12 col-md-9">
              <select class="form-control select-search" name="branch" id="branch">
                <?php
                $branch = mysqli_query($con, "SELECT * FROM location_master where status='0'");
                ?>
                <option value="">CHOOSE BRANCH</option>
                <?php
                while ($branch_det = mysqli_fetch_array($branch)) {
                  $selected = ($branch_det['id'] == $editoutward['branch']) ? "selected" : "";
                  ?>
                  <option value="<?php echo $branch_det['id']; ?>" <?php echo $selected; ?>>
                    <?php echo strtoupper($branch_det['location']); ?>
                  </option>
                <?php } ?>
              </select>
            </div>
          </div>
          <div class="row form-group">
            <div class="col col-md-3"><label for="text-input" class=" form-control-label">STORE</label></div>
            <div class="col-12 col-md-9">
              <select class="form-control select-search" name="store" id="store">
                <?php
                $store = mysqli_query($con, "SELECT * FROM branch_master where status='0'");
                ?>
                <option value="">CHOOSE BRANCH</option>
                <?php
                while ($store_det = mysqli_fetch_array($store)) {
                  $selected = ($store_det['id'] == $editoutward['division']) ? "selected" : "";
                  ?>
                  <option value="<?php echo $store_det['id']; ?>" <?php echo $selected; ?>>
                    <?php echo strtoupper($store_det['branch_name']); ?>
                  </option>
                <?php } ?>
              </select>
            </div>
          </div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">MATERIAL REQUESTED BY TECHNICIAN NAME</label></div>
						<div class="col-12 col-md-9">
							<input type="text" id="technician" name="technician" value="<?php echo $editoutward['technician']; ?>" class="form-control" placeholder="Enter Technician" autocomplete="off" >
						</div>
    				</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">APPROVED BY SHIFT ENGINEER</label></div>
						<div class="col-12 col-md-9">
							<input type="text" id="approved_by" name="approved_by" value="<?php echo $editoutward['approved_by']; ?>" class="form-control" placeholder="Enter approved_by" autocomplete="off" >
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">PURPOSE</label></div>
						<div class="col-12 col-md-9">
							<input type="text" id="purpose" name="purpose" value="<?php echo $editoutward['purpose']; ?>" class="form-control" placeholder="Enter Purpose" autocomplete="off" >
						</div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">VALUE</label></div>
						<div class="col-12 col-md-9"><input type="number" id="value" name="value" value="<?php echo $editoutward['value']; ?>" oninput="qtys(this.value)" class="form-control" placeholder="Enter Value" autocomplete="off"></div>
					</div>
					<div class="row form-group">
						<div class="col col-md-3"><label for="text-input" class=" form-control-label">AMOUNT</label></div>
						<div class="col-12 col-md-9"><input type="number" id="amount" name="amount" value="<?php echo $editoutward['total_amount']; ?>" class="form-control" autocomplete="off" Placeholder="AMOUNT" readonly></div>
					</div>
					<div>
						<button type="submit" name="submit" title="Submit" class="btn btn-success btn btn-lg" style="float:right"><i class="fa fa-dot-circle-o"></i> Submit</button>
					</div>
			    </form>
			</div>
		</div>
		<div class="clearfix"></div>
		<footer class="site-footer">
		</footer>
	</div><!-- /#right-panel -->
</body>
</html>
<script>
	function gsttype(val) {
		if (val == 'gst') {
			$('#gst').show();
			$('#igst').hide();
			$('#sgst').hide();
			$('#ugst').hide();
		} else if (val == 'igst') {
			$('#igst').show();
			$('#sgst').hide();
			$('#ugst').hide();
			$('#gst').hide();
		} else if (val == 'sgst') {
			$('#sgst').show();
			$('#ugst').hide();
			$('#igst').hide();
			$('#gst').hide();
		} else if (val == 'ugst') {
			$('#ugst').show();
			$('#gst').hide();
			$('#sgst').hide();
			$('#igst').hide();
		}
	}
	function deleteRow(btn) {
		var row = btn.parentNode.parentNode;
		row.parentNode.removeChild(row);
	}
	function qtys(value) {
		var quantity = $("#qty").val();
		var mul_price = (quantity) * (value);
		$('#amount').val(mul_price);
	}
</script>
<script>
	function calculateTotalAmount() {
		const value = parseFloat(document.getElementById('value').value) || 0;
		const amount = parseFloat(document.getElementById('amount').value) || 0;
		const gstPercentage = parseFloat(document.getElementById('gst_percentage').value) || 0;
		const totalAmount = amount + (amount * gstPercentage / 100);
		document.getElementById('total_amount').value = totalAmount.toFixed(2);
	}
</script>
<script>
	$(document).ready(function() {
		$('#asset-select').change(function() {
			// Get the selected value
			const selectedValue = $(this).val();
			// Show or hide the S.NO input based on the selected value
			if (selectedValue === 'asset') {
				$('#serial-number-row').show(); // Show the S.NO row if 'Asset' is selected
			} else {
				$('#serial-number-row').hide(); // Hide the S.NO row for any other selection
			}
		});
	});
</script>
<script>
	function fa_serial_get() {
		debugger;
		var qty = document.getElementById('qty').value;
		var serialRow = document.getElementById('serial-number-row');
		var serialContainer = document.getElementById('serial-number-container');

		// Clear previous serial number fields
		serialContainer.innerHTML = '';

		if (qty > 0) {
			serialRow.style.display = 'block'; // Show the serial number section

			// Generate input fields based on the quantity entered
			for (var i = 1; i <= qty; i++) {
				var input = document.createElement('input');
				input.type = 'text';
				input.name = 'fa_serial_no'; // Using array notation to store multiple values
				input.id = 'fa_serial_no-' + i;
				input.className = 'form-control'; // Add margin for spacing between inputs
				input.placeholder = 'Enter FA Serial Number ' + i;
				input.autocomplete = 'off';

				serialContainer.appendChild(input);
			}
		} else {
			serialRow.style.display = 'none'; // Hide the serial number section if quantity is 0 or invalid
		}
	}
</script>
<script>
    // Initialize Select2
    $(document).ready(function() {
        $('.select-search').select2({
            placeholder: "-- SELECT --",  // Optional placeholder text
            allowClear: true                // Optional: allows clearing the selection
        });
    });
$(document).on('submit', '#editOutwardForm', function(e) {
    e.preventDefault();

    $.ajax({
        url: 'outward_update.php',
        type: 'POST',
        data: new FormData(this),
        contentType: false,
        processData: false,
        success: function(response) {
            if (response.success) {
                swal({
                    title: "Success!",
                    text: response.message,
                    type: "success"
                }, function () {
                    $('#editOutwardContainer').slideUp().html('');
                    reloadOutwardTable(); // optional table refresh
                });
            } else {
                swal("Failed", response.message, "error");
            }
        },
        error: function(xhr, status, error) {
            swal("Error", "Something went wrong in AJAX: " + error, "error");
        }
    });
});


</script>