<?php
	include 'db_con_Maintance_Principal.php'; // load file db_Connection.php
	//include 'session_validate.php'; // cek session validate	
	
	$userID = $_SESSION['username'];
	$Level = $_SESSION['level'];
	
	//get data from principal split page 10
	if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
	$start_from = ($page-1) * $results_per_page;
	$sql = "SELECT * FROM ".$datatable." WHERE STATUS = 0 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>Maintance Principal</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() {	
	//alert($('#principal_code').val());
	//alert($('#principal_name').val());
	var Principal_Code = $('#principal_code').val();
	var Principal_Name = $('#principal_name').val();
	var Address = $('#address').val();
	var Email = $('#email').val();
	var Phone_Number = $('#phone').val();
	var City = $('#city').val();
	//alert(Principal_Code_Val);
	//alert(Principal_Name_Val);		
	var id_edit = $("#id_edit").val();	
	var dataPost = {
                "Principal_Code": Principal_Code,
                "Principal_Name": Principal_Name, 
                "Address": Address,
                "Email": Email,
                "Phone_Number": Phone_Number,
                "City": City,
				"id_edit": id_edit
                };
            var dataString = JSON.stringify(dataPost);
	
	if($("form")[0].checkValidity()) {	
	$.ajax({
                type: "POST",
                url: "..//forms/principal_validate_edit.php",
                dataType: "json",                        
                data: {data_post: dataString},
                async:false,
                success : function(data){                     
                    if(data.result == "duplicate"){
                       alert("Principal Already Exist");                                  
                       // swal("Warning!", "Group Already Exist", "warning"); 
                       //flag = 1;                  
                   }                   
                   else{
					   alert("Principal 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>

<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 principalCode = $('#principalCode_'+serviceID).text();
  var principalName = $('#principalName_'+serviceID).text();
  var address = $('#address_'+serviceID).text();
  var email = $('#email_'+serviceID).text();
  var phone = $('#phone_'+serviceID).text();
  var city = $('#city_'+serviceID).text();
    
  $('#principal_code').val(principalCode);
  $('#principal_name').val(principalName);
  $('#address').val(address);
  $('#email').val(email);
  $('#phone').val(phone);
  $('#city').val(city);
  $('#id_edit').val(serviceID);
 });
});
</script>
	
<script type="text/javascript">
$(function(){ //jQuery shortcut for .ready (ensures DOM ready)
 $('.delete').click(function(){    
  var serviceID = this.id;    
  $('#Id_del').val(serviceID);
 
 });
});
</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"><?php echo $userID; ?></p>
                </div>
              </a>
              <div class="dropdown-menu navbar-dropdown" aria-labelledby="profileDropdown">
                <a class="dropdown-item" href="../../pages/forms/change_password.php">
                  <i class="mdi mdi-cached me-2 text-success"></i>Change Password</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="../../pages/forms/logout.php">
                  <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"><?php echo $userID; ?></span>
                  <span class="text-secondary text-small"><?php echo $Level; ?></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/Users.php">Users</a>
                    <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="../../pages/forms/Users_Maintance.php">Users</a>
                    <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>
			
			<li class="nav-item">
              <a class="nav-link" data-bs-toggle="collapse" href="#transaction" aria-expanded="false" aria-controls="transaction">
                <span class="menu-title">Transaction</span>
                <i class="mdi mdi-format-list-bulleted menu-icon"></i>
              </a>
              <div class="collapse" id="transaction">
                <ul class="nav flex-column sub-menu">
                  <li class="nav-item">
                    <a class="nav-link" href="Purchase_Order.php">Purchase Order</a>
					<a class="nav-link" href="Sales_Invoice.php">Sales Invoive</a>
                  </li>
                </ul>
              </div>
            </li>
			
			<li class="nav-item">
              <a class="nav-link" data-bs-toggle="collapse" href="#view" aria-expanded="false" aria-controls="view">
                <span class="menu-title">View</span>
                <i class="mdi mdi-table-large menu-icon"></i>
              </a>
              <div class="collapse" id="view">
                <ul class="nav flex-column sub-menu">
                  <li class="nav-item">
                    <a class="nav-link" href="Stock.php">Stock</a>
					<a class="nav-link" href="view_po.php">PO</a>
					<a class="nav-link" href="view_si.php">SI</a>	
                  </li>
                </ul>
              </div>
            </li>
			
          </ul>
        </nav>
        <!-- partial -->
        <div class="main-panel">
          <div class="content-wrapper">
            <div class="page-header">
              <h3 class="page-title"> </h3>			  
            </div>
            <div class="row">			
			
			<div class="container">
			<div class="table-wrapper">
            <div class="table-title">
                <div class="row">
                    <div class="col-sm-6">
						<h2>Maintance <b>Principal</b></h2>
					</div>					
                </div>
            </div> 
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>Principal Code</th>
						<th>Principal Name</th>
						<th>Address</th>
						<th>Email</th>
						<th>Phone Number</th>
						<th>City</th>
						<th>Actions</th>
                    </tr>
                </thead>
                <tbody> 
                    <?php 
                            while($row = $rs_result->fetch_assoc()) {						
                            echo '<tr style="font-size: 13px;">
                                    <td id="principalCode_'.$row['ID'].'"  width="15%">'.$row['Principal_Code'].'</td>
									<td id="principalName_'.$row['ID'].'"  width="30%">'.$row['Principal_Name'].'</td>
                                    <td id="address_'.$row['ID'].'" >'.$row['Address'].'</td>
                                    <td id="email_'.$row['ID'].'"  width="10%">'.$row['Email'].'</td>
									<td id="phone_'.$row['ID'].'"  width="10%">'.$row['Phone'].'</td>
									<td id="city_'.$row['ID'].'"  width="10%">'.$row['City'].'</td>
                                    <td>
                                        <a href="#editModal" name="editR" class="edit" data-toggle="modal" id="'.$row['ID'].'"><i class="fa fa-edit" data-toggle="tooltip" title="Edit"></i></a>
                                        <a href="#deleteModal" class="delete" data-toggle="modal" id="'.$row['ID'].'"><i class="fa fa-trash-o" data-toggle="tooltip" title="Delete"></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. " WHERE STATUS = 0";
                        $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. " WHERE STATUS = 0";
                        $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='Principal_Maintance.php?page=".$i."'";
                         if ($i==$page)  echo " class='curPage'";
                            echo ">".$i."</a> "; 
                     }; 
                    ?>
                </li>    
                </ul>
            </div>
			
	<!-- 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">Edit Principal</h4>
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					</div>
					<div class="modal-body">
                        <div class="form-group">
							<label>Principal Code</label>
							<input id="principal_code" type="text" class="form-control principal_code" name="principal_code" required>
						</div>		
						<div class="form-group">
							<label>Principal Name</label>
							<input id="principal_name" type="text" class="form-control group_name" name="group_name" required>
						</div>						
						<div class="form-group">
							<label>Address</label>
							<textarea id="address" class="form-control" name="group_desc" required></textarea>
						</div>
						<div class="form-group">
							<label>Email Address</label>
							<input id="email" type="text" class="form-control group_name" name="group_name" required>
						</div>
						<div class="form-group">
							<label>Phone Number</label>
							<input id="phone" type="text" class="form-control group_name" name="group_name" required>
						</div>
						<div class="form-group">
							<label>City</label>
							<input id="city" type="text" class="form-control group_name" name="group_name" required>
						</div>											
					</div>
					<div class="modal-footer">
						<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
						<input type="submit" class="btn btn-info postsubmit" value="Save" name="submit_val">
                                                <input type="hidden" id="id_edit" name="id_edit" />
					</div>
				</form>
			</div>
		</div>						
	</div>
	<!-- Delete Modal HTML -->
	<div id="deleteModal" class="modal fade delchose" data-modal="del1">
		<div class="modal-dialog">
			<div class="modal-content">
				<form method="POST">
					<div class="modal-header">						
						<h4 class="modal-title">Delete Principal</h4>
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					</div>
					<div class="modal-body">					
						<p>Are you sure you want to delete these Records?</p>
						<p class="text-warning"><small>This action cannot be undone.</small></p>
					</div>
					<div class="modal-footer">
						<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
						<input type="submit" class="btn btn-danger" value="Delete" name="btn_delete">
                                                <input type="hidden" id="Id_del" name="id_del" />
					</div>
				</form>
			</div>
		</div>
	</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 -->
    </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 -->
	
	
<?php
if(isset($_POST['btn_delete'])) {   
// Attempt update query execution
$sql  = ("UPDATE principal SET STATUS = 1, Date_Updated = '$str_datetime' WHERE ID = '".$_POST["id_del"]."'");
$query = mysqli_query($conn, $sql);
echo "<script>  window.location.href='../forms/Principal_Maintance.php';</script>";
}
?>
		
  </body>
</html>
<?php	
	include 'session_validate.php'; // cek session validate	
?>
