<?php
	include 'db_con_PO.php'; // load file db_Connection.php
	
	if(isset($_POST['stock_search'])&&($_POST['input_search']!=""))
	{
	//get data from stock split page 10
	$option = $_POST[search_option];
	$search = $_POST['input_search'];		
	if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
	$start_from = ($page-1) * $results_per_page;	
	//$sql = "SELECT * FROM ".$datatable." WHERE ".$option." LIKE '".$search."' LIMIT $start_from, ".$results_per_page;
	$sql = "SELECT * FROM ".$datatable." WHERE ".$option." LIKE '".$search."'";	
	$rs_result = $conn->query($sql);
	//echo "<script>alert('".$search."');</script>";
	
	}
	else
	{
	//get data from stock split page 10
	if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
	$start_from = ($page-1) * $results_per_page;	
	$sql = "SELECT * FROM ".$datatable." LIMIT $start_from, ".$results_per_page;
	$rs_result = $conn->query($sql);	
	}
	
	
	//update timestamp 
	$timezone = "Asia/Jakarta";
	date_default_timezone_set($timezone);  
	$datetime = new DateTime(date("Y-m-d H:i:s"));
	$str_datetime = $datetime->format("Y-m-d H:i:s");
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>View Stock</title>
    <!-- plugins:css -->
    <link rel="stylesheet" href="../../assets/vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="../../assets/vendors/ti-icons/css/themify-icons.css">
    <link rel="stylesheet" href="../../assets/vendors/css/vendor.bundle.base.css">
    <link rel="stylesheet" href="../../assets/vendors/font-awesome/css/font-awesome.min.css">
    <!-- endinject -->
    <!-- Plugin css for this page -->
    <link rel="stylesheet" href="../../assets/vendors/select2/select2.min.css">
    <link rel="stylesheet" href="../../assets/vendors/select2-bootstrap-theme/select2-bootstrap.min.css">
    <!-- End plugin css for this page -->
    <!-- inject:css -->
    <!-- endinject -->
    <!-- Layout styles -->
    <link rel="stylesheet" href="../../assets/css/style.css">
    <!-- End layout styles -->
    <link rel="shortcut icon" href="../../assets/images/favicon.png" />		

	<!--css modal bootstrap -->
	<link rel="stylesheet" href="../../assets/modalCSS/bootstrap.min.css">
	
	<!--get jquery -->
	<script src="../js/jquery-3.4.1.min.js"></script>
	<!--get jquery bootstrap modal -->
	<script src="../js/bootstrap.bundle.min.js"></script>
	
	
<!--	
<script>
function e_submit(){
	//alert("horee");
	var Principal_Code = document.getElementById("Principal_Code");
	var displaycode = Principal_Code.value;
	alert(displaycode);
	}
</script>
-->

<script>
$(document).ready(function(){       
    $(".postsubmit").click( function() {		
	var PO_Number = $("#PO_Number").val();
	var Nominal_Amount = $('#i_amount').val();
	var PO_Amount = $('#Amount').val();	
	//alert(Nominal_Amount);	
	var dataPost = {
                "PO_Number": PO_Number,
                "Nominal_Amount": Nominal_Amount,
				"PO_Amount": PO_Amount	
                };
            var dataString = JSON.stringify(dataPost);
	
	if($("form")[0].checkValidity()) {	
	$.ajax({
                type: "POST",
                url: "..//forms/Payment.php",
                dataType: "json",                        
                data: {data_post: dataString},
                async:false,
                success : function(data){                     
                    if(data.result == "overlimit"){
                       alert("Payment exceeded");                                  
                       // swal("Warning!", "Group Already Exist", "warning"); 
                       //flag = 1;    						
                   }                   
                   else{
					   //alert("Product Has Been Change");
					   //alert(data.result);
                      //swal("Warning!", "trap", "warning"); 
                      //flag = 1;  
                   }					
                },
                error : function(data){ 
                    alert("Data Failed to send");
                    //alert(data.result);
                    //flag = 1;
                    //return false;
                }
            });
	}
	
	    });                   
});    
</script>

<script type="text/javascript">
//for row edit selected
$(function(){ //jQuery shortcut for .ready (ensures DOM ready)
 $('.edit').click(function(){  
  var serviceID = this.id;
  //alert(serviceID); 
  var PO_Amount = $('#po_Amount_'+serviceID).text();
  var Payment_Type = $('#paymentType_'+serviceID).text();
  var status = $('#status_'+serviceID).text();
  var PO_Number = $('#poNumber_'+serviceID).text();  
  var button = document.getElementById('b_update');  
  	
  $('#po_amount').text(PO_Amount);
  $('#PO_Number').val(PO_Number);
  $('#Amount').val(PO_Amount);  
  
  if(Payment_Type == "Cash"){
	  $('#i_amount').val(PO_Amount);
		document.getElementById("i_amount").disabled = true;	  
  };
  
  if(status == "Closed"){
		button.disabled = true;
	}
	  else{
		button.disabled = false;	  
	  };
  
  
 });
});
</script>
	
<script type="text/javascript">
$(document).ready(function(){
	$('#stock_search').click(function(){
		var test = "test";
		var dataPost = {                
				"test": test
                };
        var dataString = JSON.stringify(dataPost);
		$.ajax({
                type: "POST",
                url: "..//forms/stock_search.php",
                dataType: "json",                        
                data: {data_post: dataString},
                async:false,
                success : function(data){                     
                    if(data.result == "duplicate"){
                       alert("Product Already Exist");                                  
                       // swal("Warning!", "Group Already Exist", "warning"); 
                       //flag = 1;                  
                   }                   
                   else{
					   alert("Product Has Been Change");
                      //swal("Warning!", "trap", "warning"); 
                      //flag = 1;  
                   }
                },
                error : function(data){ 
                    alert("Data Failed to send");
                    //alert(data.result);
                    //flag = 1;
                    //return false;
                }
            });
	});
});
</script>  
	
  </head>
  <body>
    <div class="container-scroller">
      <!-- partial:../../partials/_navbar.html -->
      <nav class="navbar default-layout-navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
        <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-start">
          <a class="navbar-brand brand-logo" href="../../index.php"><img src="../../assets/images/logo_IMS.svg" alt="logo" /></a>
          <a class="navbar-brand brand-logo-mini" href="../../index.php"><img src="../../assets/images/logo-mini.svg" alt="logo" /></a>
        </div>
        <div class="navbar-menu-wrapper d-flex align-items-stretch">
          <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize">
            <span class="mdi mdi-menu"></span>
          </button>
		  
          <ul class="navbar-nav navbar-nav-right">
            <li class="nav-item nav-profile dropdown">
              <a class="nav-link dropdown-toggle" id="profileDropdown" href="#" data-bs-toggle="dropdown" aria-expanded="false">
                <div class="nav-profile-img">
                  <img src="../../assets/images/faces/blank.png" alt="image">
                  <span class="availability-status online"></span>
                </div>
                <div class="nav-profile-text">
                  <p class="mb-1 text-black">User Name</p>
                </div>
              </a>
              <div class="dropdown-menu navbar-dropdown" aria-labelledby="profileDropdown">
                <a class="dropdown-item" href="#">
                  <i class="mdi mdi-cached me-2 text-success"></i> Activity Log </a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="#">
                  <i class="mdi mdi-logout me-2 text-primary"></i> Signout </a>
              </div>
            </li>			
            
            <li class="nav-item nav-settings d-none d-lg-block">
              <a class="nav-link" href="#">
                <i class="mdi mdi-format-line-spacing"></i>
              </a>
            </li>
			
          </ul>
          <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas">
            <span class="mdi mdi-menu"></span>
          </button>
        </div>
      </nav>
      <!-- partial -->
      <div class="container-fluid page-body-wrapper">
        <!-- partial:../../partials/_sidebar.html -->
        <nav class="sidebar sidebar-offcanvas" id="sidebar">
          <ul class="nav">
            <li class="nav-item nav-profile">
              <a href="#" class="nav-link">
                <div class="nav-profile-image">
                  <img src="../../assets/images/faces/blank.png" alt="profile" />
                  <span class="login-status online"></span>
                  <!--change to offline or busy as needed-->
                </div>
                <div class="nav-profile-text d-flex flex-column">
                  <span class="font-weight-bold mb-2">User Name</span>
                  <span class="text-secondary text-small">Project Manager</span>
                </div>
                <i class="mdi mdi-bookmark-check text-success nav-profile-badge"></i>
              </a>
            </li>
			
            <li class="nav-item">
              <a class="nav-link" data-bs-toggle="collapse" href="#manage" aria-expanded="false" aria-controls="manage">
                <span class="menu-title">Manage</span>
                <i class="mdi mdi-format-list-bulleted menu-icon"></i>
              </a>
              <div class="collapse" id="manage">
                <ul class="nav flex-column sub-menu">
                  <li class="nav-item">
                    <a class="nav-link" href="../../pages/forms/Principal.php">Principal</a>
					<a class="nav-link" href="../../pages/forms/Customer.php">Customer</a>
					<a class="nav-link" href="../../pages/forms/Product.php">Product</a>
                  </li>
                </ul>
              </div>
            </li>
			
			<li class="nav-item">
              <a class="nav-link" data-bs-toggle="collapse" href="#maintance" aria-expanded="false" aria-controls="maintance">
                <span class="menu-title">Maintance</span>
                <i class="mdi mdi-table-large menu-icon"></i>
              </a>
              <div class="collapse" id="maintance">
                <ul class="nav flex-column sub-menu">
                  <li class="nav-item">
                    <a class="nav-link" href="Principal_Maintance.php">Principal</a>
					<a class="nav-link" href="Customer_Maintance.php">Customer</a>
					<a class="nav-link" href="Product_Maintance.php">Product</a>
                  </li>
                </ul>
              </div>
            </li>
			
          </ul>
        </nav>					
        <!-- partial -->			
        <div class="main-panel">			
		<form action="#" method="post">
          <div class="content-wrapper">		  
            <div class="page-header">
              <h3 class="page-title"> </h3>			  
            </div>			
			<div class="form-group">
				<label for="exampleFormControlSelect3">Search By</label>
                      <div class="input-group">								
						<select class="form-select-sm" id="exampleFormControlSelect3" name="search_option">
							<option value="PO_Number"> PO Number</option>
							<option Value="Principal_Name" <?php if($_POST['search_option']=="Principal_Name") echo 'selected';?> >Principal Name</option>
							<option Value="PO_Date" <?php if($_POST['search_option']=="PO_Date") echo 'selected';?> >PO Date</option>	
						</select> &nbsp;
                        <input type="text" name="input_search" class="form-control" placeholder="" aria-label="Recipient's username" aria-describedby="basic-addon2">
                        <div class="input-group-append">
                          <button id="search" name="stock_search" class="btn btn-sm btn-gradient-primary" type="submit">Search</button>
                        </div>
                      </div>
                    </div>												 
			
            <div class="row">					
			<div class="container">
			<div class="table-wrapper"> 						 					
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>PO Number</th>
						<th>Principal Code</th>
						<th>Principal Name</th>
						<th>PO Date</th>						
						<th>Payment Type</th>
						<th>Amount</th>							
						<th>Status</th>
						<th>Cash-Out</th>
						<th>Detail</th>				
                    </tr>
                </thead>
                <tbody> 
                    <?php 								
                            while($row = $rs_result->fetch_assoc()) {						
                            echo '<tr style="font-size: 13px;">
                                    <td id="poNumber_'.$row['ID'].'"  width="15%">'.$row['PO_Number'].'</td>
									<td id="principalCode_'.$row['ID'].'"  width="30%">'.$row['Principal_Code'].'</td>
                                    <td id="principalName_'.$row['ID'].'"  width="10%">'.$row['Principal_Name'].'</td>
									<td id="poDate_'.$row['ID'].'" >'.$row['PO_Date'].'</td>									
									<td id="paymentType_'.$row['ID'].'"  width="10%">'.$row['Payment_Type'].'</td>
									<td style="text-align:right" id="po_Amount_'.$row['ID'].'"  width="10%">'.$row['PO_Amount'].'</td>									
									<td id="status_'.$row['ID'].'"  width="10%">'.$row['Status'].'</td>									
									<td style="text-align:center">
                                        <a href="#editModal" name="editR" class="edit" data-toggle="modal" id="'.$row['ID'].'"><i class="fa fa-money" data-toggle="tooltip" title="Edit"></i></a>                                        
                                    </td>
									<td style="text-align:center">
                                        <a href="po_detail.php?id='.$row['PO_Number'].'"><i class="fa fa-list" data-toggle="tooltip" title="Edit"></i></a>                                        
                                    </td>
								</tr>';			
                        }
                    ?>
                </tbody>
            </table>
                <div class="clearfix">
                <div class="hint-text">Showing <b><?php echo $page ?></b> out of <b>
                 <?php 
                      $sql_to = "SELECT COUNT(ID) AS total FROM " .$datatable;
                        $result = $conn->query($sql_to);
                        $row_to = $result->fetch_assoc();
                        $pages_to = ceil($row_to["total"] / $results_per_page); // calculate total pages with results
                        echo"$pages_to";                    
                 ?></b> entries</div>
                <ul class="pagination">
                <li class="page-item">    
                   <?php                     
                        $sql = "SELECT COUNT(ID) AS total FROM " .$datatable;
                        $result = $conn->query($sql);
                        $row = $result->fetch_assoc();
                        $total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results
  
                    for ($i=1; $i<=$total_pages; $i++) {  // print links for all pages
                        echo "<a href='view_po.php?page=".$i."'";
                         if ($i==$page)  echo " class='curPage'";
                            echo ">".$i."</a> "; 
                     }; 
                    ?>
                </li>    
                </ul>
            </div>
				
        </div>
    </div>			                		  		  		
          <!-- content-wrapper ends -->
          <!-- partial:../../partials/_footer.html -->
          <footer class="footer">
		  <!--
            <div class="d-sm-flex justify-content-center justify-content-sm-between">
              <span class="text-muted text-center text-sm-left d-block d-sm-inline-block">Copyright © 2023 <a href="https://www.bootstrapdash.com/" target="_blank">BootstrapDash</a>. All rights reserved.</span>
              <span class="float-none float-sm-right d-block mt-1 mt-sm-0 text-center">Hand-crafted & made with <i class="mdi mdi-heart text-danger"></i></span>
            </div
		  -->
          </footer>
          <!-- partial -->
        </div>
        <!-- main-panel ends -->
      </div>
      <!-- page-body-wrapper ends -->
	  
	<!-- Edit Modal HTML -->
	<div id="editModal" class="modal fade">
		<div class="modal-dialog">
			<div class="modal-content">
				<form method="POST" enctype="multipart/form-data">
					<div class="modal-header">						
						<h4 class="modal-title">Cash-Out</h4>
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					</div>
					<div class="modal-body">
                        <div class="form-group">
							<label for="exampleInputName1">Nominal Amount</label>
						<div class="input-group">                       						
                        <input type="number" onkeydown="javascript:return['Backspace','Delete','ArrowLeft','ArrowRight'].includes(event.code) ? true : !isNaN(Number(event.key)) && event.code!=='Space'" min="1" class="form-control" id="i_amount" aria-label="Amount (to the nearest dollar)" required />                       
						</div>
                    </div>
						<div style="text-align:right; color:red;">	
							<label><b><h1 id="po_amount"></label>
						</div>										
					</div>
					
					<table class="table table-striped table-hover">
						<thead>
							<tr>
								<th>PO Number</th>
								<th>Date</th>
								<th>Amount</th>					
							</tr>
						</thead>
					<tbody> 
							<?php 
							//get data from payment	
								//$PO_Number = $_POST['PO_Number'];								
								$sql = "SELECT * FROM payment WHERE PO_Number = '".$PO_Number."'";
								//$sql = "SELECT * FROM payment";
								$rs_result = $conn->query($sql);
								//echo "<script>console.log('".$PO_Number."');</script>";
								//echo "<script>console.log('".$test."');</script>";
							
                            while($row = $rs_result->fetch_assoc()) {						
                            echo '<tr style="font-size: 13px;">
                                    <td>'.$row['PO_Number'].'</td>
									<td>'.$row['Date'].'</td>
                                    <td>'.$row['Amount'].'</td>									
								</tr>';			
							}
							?>
					</tbody>
					</table>
					
					<div class="modal-footer">
						<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
						<input type="submit" id="b_update" class="btn btn-info postsubmit" value="Update" name="submit_val">
                                                <input type="hidden" id="PO_Number" name="PO_Number" />
												<input type="hidden" id="Amount" name="Amount" />
					</div>
				</form>
			</div>
		</div>						
	</div>
	<!-- end edit modal -->
	  
	</form>  
    </div>	
    <!-- container-scroller -->
    <!-- plugins:js -->
    <script src="../../assets/vendors/js/vendor.bundle.base.js"></script>
    <!-- endinject -->
    <!-- Plugin js for this page -->
    <script src="../../assets/vendors/select2/select2.min.js"></script>
    <script src="../../assets/vendors/typeahead.js/typeahead.bundle.min.js"></script>
    <!-- End plugin js for this page -->
    <!-- inject:js -->
    <script src="../../assets/js/off-canvas.js"></script>
    <script src="../../assets/js/misc.js"></script>
    <script src="../../assets/js/settings.js"></script>
    <script src="../../assets/js/todolist.js"></script>
    <script src="../../assets/js/jquery.cookie.js"></script>
    <!-- endinject -->
    <!-- Custom js for this page -->
    <script src="../../assets/js/file-upload.js"></script>
    <script src="../../assets/js/typeahead.js"></script>
    <script src="../../assets/js/select2.js"></script>
    <!-- End custom js for this page -->	
  </body>
</html>