<?php 
require("../connection.php");
include("../user.php");
$user_id=$_SESSION['user'];
$username=$_SESSION['user_name'];
$branch_id=$_SESSION['branch_id'];
$ho_access=$_SESSION['ha'];	
$branch=$_SESSION['division_name'];
$location=$_SESSION['location'];

$location_det=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM `location_master` where location='$location'")); 
 $location_id=$location_det['id'];

?>
<html>
<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="stylesheet" href="../css/normalize.min.css">
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/themify-icons.css">
    <link rel="stylesheet" href="../css/pe-icon-7-stroke.min.css">
    <link rel="stylesheet" href="../css/flag-icon.min.css">
    <link rel="stylesheet" href="../assets/css/cs-skin-elastic.css">
    <link rel="stylesheet" href="../assets/css/style.css">
    <link href="../css/chartist.min.css" rel="stylesheet">
    <link href="../css/jqvmap.min.css" rel="stylesheet">

    <link href="../css/weather-icons.css" rel="stylesheet" />
    <link href="../css/fullcalendar.min.css" rel="stylesheet" />

<script src="../js/jquery-1.12.4.min.js"></script>
<script src="../js/jquery-3.3.1.js"></script>
<script src="../js/jquery.dataTables.min.js"></script>


    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/jquery.matchHeight.min.js"></script>
    <script src="../assets/js/main.js"></script>
    <script src="../js/jquery-3.3.1.min.js"></script>
   <style>
    #weatherWidget .currentDesc 
	{
        color: #ffffff!important;
    }
        .traffic-chart {
            min-height: 335px;
        }
        #flotPie1  {
            height: 150px;
        }
        #flotPie1 td {
            padding:3px;
        }
        #flotPie1 table {
            top: 20px!important;
            right: -10px!important;
        }
        .chart-container {
            display: table;
            min-width: 270px ;
            text-align: left;
            padding-top: 10px;
            padding-bottom: 10px;
        }
        #flotLine5  {
             height: 105px;
        }

        #flotBarChart {
            height: 150px;
        }
        #cellPaiChart{
            height: 160px;
        }
th{
	text-transform:uppercase;
}
td{
	text-transform:uppercase;
}
    </style>

<link rel="stylesheet" href="../datatable/css/jquery.dataTables.min.css">
<script src="../datatable/js/jquery.dataTables.min.js"></script>
<!-- button --><link rel="stylesheet" type="text/css" href="../button/css/buttons.css" /><!-- button -->

<script>
$(document).ready(function() {
		$('#example').DataTable();
		} );
</script>
</head>
<body>
 <?php require('../sidemenu.php');?>
  <!-- /#left-panel -->
    <!-- Right Panel -->
    <div id="right-panel" class="right-panel">
        <!-- Header-->
        <?php require('../header.php');?>
		<div class="content">
            <!-- Animated -->
            <div class="animated fadeIn">
		<div style="background-color:white;margin-bottom:10px;">
	<strong style="color:blue"><center>Stock Transfer List</center></strong>
	</div>
	<button class="button button--moema button--text-thick button--text-upper button--size-s" style="padding:1.5px;" onclick="stock()"><i class="fa fa-retweet">TRANSFER</i></button>	
<table id="example" class="table table-striped table-bordered" >
        <thead>
            <tr>
                <th>S.No</th>
                <th>Category</th>
                <th>Date</th>
                <th>Fa Serial Number</th>
                <th>No Of Qty</th>
                <th>Location</th>
				<?php 
				if($ho_access=="yes")
				{?>
                <th>From Division</th><?php } else { } ?>
                <th>To Division</th>
                <th>Status</th>
                <th>Action</th>
                <th>Return</th>

            </tr>
        </thead>
		
        <tbody>
		<?php 
		if($ho_access=="yes")
		{
			$row=mysqli_query($con,"SELECT st.transaction_id,st.date,cm.category_type as category,cm.category_description,lm.location as from_locations,lt.location as to_location,st.flag,st.status, st.no_of_item as quantity,case WHEN st.STATUS=0 THEN 'In Transit' else 'Delivered' end as sta,st.*,sd.fa_serial_no as fa FROM stock_transfer st join stock_item_detail sd on st.transaction_id=sd.transaction_id join location_master lm on st.from_location=lm.id join location_master lt on st.to_location=lt.id inner join category_master cm on cm.id = sd.category GROUP BY sd.transaction_id");
		}
		else
		{
			$row=mysqli_query($con,"SELECT st.transaction_id,st.date,sd.category,sd.category_description,lm.location as from_locations,st.flag,st.status,st.no_of_item as quantity,case WHEN st.STATUS=0 THEN 'In Transit' else 'Delivered' end as sta,st.to_division,sd.fa_serial_no as fa FROM stock_transfer st join stock_item_detail sd on st.transaction_id=sd.transaction_id join location_master lm on st.from_location=lm.id where st.branch_id='$branch_id' and st.from_division='$branch' and st.from_location='$location_id' GROUP BY sd.transaction_id"); 
			
		}
		$s=1;
				while($type=mysqli_fetch_array($row))
				{
					?>
            <tr>
			   <td><?php echo $s++ ;?></td>
               <td><?php echo $type['category'].'-'.$type['category_description'];?></td>
               <td><?php echo date("d-m-Y",strtotime($type['date']));?></td>
               <td><?php echo $type['fa']; ?></td>
			   <td><?php echo $type['quantity'];?></td>
               <td><?php echo $type['from_locations'];?></td>
               <?php 
				if($ho_access=="yes")
				{?><td><?php echo $type['from_division'];?></td><?php } else { } ?>
               <td><?php echo $type['to_division'];?></td>
               <?php 
			 $sts=$type['sta'];
			   if ($sts=="In Transit")
				{
				?>
                <td style="color:red;"><?php echo $type['sta'];?></td>
				<?php
				}
				else {
				?>
				<td style="color:green;"><?php echo $type['sta'];?></td>
				<?php
				}
				?>
			     <?php 
			   if ($sts=="In Transit")
				{
				?>
                <td style="color:red;"><i class="fa fa-exchange"></i></td>
				<?php
				}
				else {
				?>
				<td style="color:green;"><i class="fa fa-check"></i></td>
				<?php
				}
				?>
			   <td>
  <button class="btn btn-primary" data-toggle="modal" data-target="#returnModal" onclick="populateReturnModal('<?php echo $type['transaction_id']; ?>', '<?php echo $type['category']; ?>', '<?php echo $type['quantity']; ?>', '<?php echo $type['from_locations']; ?>')">Return</button>
</td>

              
				
            </tr>
				<?php } ?>
		</tbody>
    </table>
    <!-- Return Modal -->
<div class="modal fade" id="returnModal" tabindex="-1" role="dialog" aria-labelledby="returnModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="returnModalLabel">Return Stock</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form id="returnForm">
          <div class="form-group">
            <label for="returnCategory">Category</label>
            <input type="text" class="form-control" id="returnCategory" name="category" readonly>
          </div>
          <div class="form-group">
            <label for="returnQuantity">Quantity</label>
            <input type="number" class="form-control" id="returnQuantity" name="quantity" required>
          </div>
          <div class="form-group">
            <label for="returnLocation">Location</label>
            <input type="text" class="form-control" id="returnLocation" name="location" readonly>
          </div>
          <input type="hidden" id="transactionId" name="transaction_id">
          <button type="submit" class="btn btn-primary">Save</button>
        </form>
      </div>
    </div>
  </div>
</div>

	</div>
	</div>
	<div class="clearfix"></div>
	</div>
	</body>

<script>
    // Function to populate the modal with data
function populateReturnModal(transactionId, category, quantity, location) {
    $('#returnCategory').val(category);
    $('#returnQuantity').val(quantity);
    $('#returnLocation').val(location);
    $('#transactionId').val(transactionId);
}

// Handle form submission
$('#returnForm').submit(function(event) {
    event.preventDefault();
    
    // Gather form data
    var formData = $(this).serialize();
    
    // Send data via AJAX
    $.ajax({
        url: 'process_return.php', // Replace with the URL of your PHP script to handle the form submission
        type: 'POST',
        data: formData,
        success: function(response) {
            alert('Return saved successfully');
            $('#returnModal').modal('hide');
            location.reload(); // Refresh the page to show updated data
        },
        error: function(xhr, status, error) {
            alert('An error occurred: ' + error);
        }
    });
});

</script>


	</html>
	
 <?php require("../mainjavascript.js");?>


