<?php
	session_start();
	date_default_timezone_set("America/Caracas");
	$servername = "localhost";
$username = "malcacorp";
$password = "daniel,@2018*";
$db="malcacorp_overlord";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$db);
	if(!isset($_SESSION['username']))
	{
		header("location: index.php");
	}
$today = getdate(); 
$_SESSION['page']='10';

if(isset($_POST['name']))
{
	$sql="SELECT * FROM Items where Name='".$_POST['name']."' ";
	$result=mysqli_query($conn, $sql);
	if(mysqli_num_rows($result)==0){
	$query="INSERT INTO Items(ID, Name, Qty, Unit) VALUES ('0', '".$_POST['name']."', '".$_POST['qty']."', '".$_POST['unit']."' )";
		if (mysqli_query($conn, $query)) {
			$sql= "true";
			$sms="you have inserted a new item to the inventory, ".$_POST['qty']." ".$_POST['unit']." of ".$_POST['name'];
		}	
	}else
	{
		$row = mysqli_fetch_assoc($result);
		$number=$row['Qty']+$_POST['qty'];
		
		$query="UPDATE Items SET Qty='".$number."' WHERE Name='".$_POST['name']."' ";
			if (mysqli_query($conn, $query)) {
			$sql= "true";
			$sms="you have added ".$_POST['qty']." ".$_POST['unit']." of ".$_POST['name']." to the Inventory";
		}	
	}
}
if(isset($_POST['namet'])){
	/*consulta a copiar*/
	$sql="SELECT * FROM Items where Name='".$_POST['namet']."' "; //consulta: selecciona todo de la tabla Items donde el Name= 'Arroz'; 
	$result=mysqli_query($conn, $sql); //hace la consulta y guarda el resultado
	$row = mysqli_fetch_assoc($result);//del resultado se trae las filas, 1 a la vez 
	
		
	$number=$row['Qty'] - $_POST['qtyt'];
	if($number< 0){
	
		$sql="true";
		$sms="sorry we do not have that much ".$_POST['namet'].", just take the ".$row['Qty']." ".$row['Unit']." of ".$row['Name']." existing in the inventory, and OVERLORD will automatically create a task for Christina to find the rest"; 
	}else{
	
			$quer="INSERT INTO Mafinests(ID, Item , Qty, Explanation, User) VALUES ('0' ,'".$_POST['namet']."' , '".$_POST['qtyt']."' , '".$_POST['explanation']."' , '".$_SESSION['username']."' )";
			mysqli_query($conn, $quer);
			$query="UPDATE Items SET Qty='".$number."' WHERE Name='".$_POST['namet']."' ";
			IF($number==0){
			
				if (mysqli_query($conn, $query)) {
					
					$sql= "true";
					
					$sms="Perfect, I updated  the inventory, also added a task for Cristina, so she can Re-stock the item";
					$variable="Cristina we are out of ".$_POST['namet']." please re-stock";
					$variable2="OVERLORD YELLS YOU: OBBEY ";
					$sqll="INSERT INTO Tasks (ID, User, addedBy, StartDate, Deadline, Title, Description) VALUES ('0', 'christina', 'OVERLORD', curdate() , curdate() , '".$variable."', '".$variable2."')";
					mysqli_query($conn, $sqll);
				}
			}else{
					if (mysqli_query($conn, $query)) {
					
					$sql= "true";
						
					$sms="Perfect, I updated  the inventory, thanks for letting me know";
		
				
				}
			
			}
	
	
	}
		
		
}
function humanTiming ($time)
{

    $time = time() - $time; // to get the time since that moment
    $time = ($time<1)? 1 : $time;
    $tokens = array (
        31536000 => 'a&ntilde;o',
        2592000 => 'mes',
        604800 => 'semana',
        86400 => 'd&iacute;a',
        3600 => 'hora',
        60 => 'minuto',
        1 => 'segundo'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }

}
?>

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <link rel="apple-touch-icon" sizes="76x76" href="assets/iapple-icon.png>
    <link rel="icon" type="image/png" href="assets/img/favicon.png" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>OVERLORD</title>
    <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
    <meta name="viewport" content="width=device-width" />
    <!-- Bootstrap core CSS     -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" />
    <!--  Material Dashboard CSS    -->
    <link href="assets/css/material-dashboard.css?v=1.2.0" rel="stylesheet" />
    
    <link href="assets/css/demo.css" rel="stylesheet" />
    <!--     Fonts and icons     -->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700,300|Material+Icons" rel='stylesheet'>
    <script src="assets/js/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="assets/js/bootstrap.min.js" type="text/javascript"></script>
<style>
.green:hover
{
	background: #545454 !important;
	box-shadow:none;
}
#popup
{
	z-index:999999;
	position:fixed;
}
#bg
{
	background:black;
	opacity:0.6;
	position:fixed;
	z-index:999998;
	width:100%;
	height:1000px;
	
}
</style>
</head>

<body>

    <div class="wrapper">
          <?include "menu_izq.php";?>
        <div class="main-panel">
			<div hidden id="bg"></div>
            <nav class="navbar navbar-transparent navbar-absolute">
                <div class="container-fluid">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="#">  Bienvenido <b><?echo $_SESSION['username'];?></b></a>
                    </div>
                    <div class="collapse navbar-collapse">
                        <ul class="nav navbar-nav navbar-right">
							<li>
							<a>
							 <b>Hora del servidor: </b><label id="timer"></label>
							 </a>
							</li>
                            <li>
                                <a href="" class="dropdown-toggle" data-toggle="dropdown">
                                    <i class="material-icons">dashboard</i>
                                    <p class="hidden-lg hidden-md">Dashboard</p>
                                </a>
                            </li>
                            	<?include 'dropdown_notificaciones.php';?>
								
                        <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <i class="material-icons">person</i>
                                    <p class="hidden-lg hidden-md">Profile</p>
                                </a>
                                <ul class="dropdown-menu">
                                    <li><a href="usuario.php"><i class="material-icons">person</i><p>Perfil</p></a></li>
                                    <li>
                                         <a href="logout.php">
                            <i class="material-icons">unarchive</i>
                            <p>Salir</p>
                        </a>
                                    </li>
                                  
                                </ul>
                            </li>
                        </ul>
                  
                    </div>
                </div>
            </nav>
             
			<div class="content">
	<?if($sql=="true"){?>
				<div class="row">
				<div class="col-md-8" style="margin: auto auto;">
				<div class="alert alert-success">
					
  <strong>Success,</strong> <?echo $sms;?>  <a href="inventario.php" clasS="pull-right">go back</a>
</div> 
				</div>
				</div>
				<?}?>
		
                <div class="container-fluid" style="position:relative;">

		
		
		
		
		
                    
					<div  class="row" id="popupo">
									
    <div   >
	        <div class="col-md-12">
		
				<DIV class="col-md-6" style="">
				      <div class="card-content table-responsive">
				
					  <table class="table table-hover" style="text-align:left;">
                                        <thead class="text-primary">
                                            <th>ID</th>
                                             <th>Objeto</th>
                                            <th >Cantidad</th>
											<th>&Uacute;ltima vez----</th>
                                            
                                        </thead>
                                        <tbody>
								<?
								$query="select * from Items";
								$result = mysqli_query($conn, $query);
                                             while (	$row = mysqli_fetch_assoc($result) ){?>
										
											
												<tr >
													<td><?echo $row['ID'];?></td>
													<td><?echo $row['Name'];?></td>
													<td style="<?if($row['Qty'] < '4'){echo "color:red";}?>"  > <?echo $row['Qty'];?> <?echo $row['Unit'];?></td>
													<td><? $time = strtotime($row['Last']); echo  "hace ".humanTiming($time).".";?></td>
												  
												</tr>
											
										<?}	?>
                                     
                                        </tbody>
                                    </table>
				</div>	
				</div>
		        <div class="col-md-6">
		            <!-- Wizard container -->
		            <div class="wizard-container">
		                <div class="card wizard-card" style="margin-top:0px;" data-color="red" id="wizard">
						<div class="container" style="width:100%;position:relative;">
						<div class="pull-right">
	                                   
	                                </div>
		                    <form action="inventory.php" method="post">
		                <!--        You can switch " data-color="blue" "  with one of the next bright colors: "green", "orange", "red", "purple"             -->

		                    	<div class="wizard-header">
		                        	<h3 class="wizard-title">
		                        		A&ntilde;adir objeto
		                        	</h3>
								
		                    	</div>
								

		                        <div class="tab-content">
		                     
		                               <div class="col-md-6">
                                                <div class="form-group label-floating">
                                                    <label class="control-label">Objeto</label>
                                                    <input  value="" type="text" name="name" required class="form-control">
                                                </div>
                                            </div>
										    <div class="col-md-3">
                                                <div class="form-group label-floating">
                                                    <label class="control-label">Cantidad</label>
                                                    <input  value="" min="1"  value="1" type="number" name="qty" required class="form-control">
                                                </div>
                                            </div>
											    <div class="col-md-3">
                                                <div class="form-group label-floating">
                                                    <label class="control-label">Unidad</label>
                                                    <select class="form-control" name="unit">
														<option value="kilograms" >Kilos</option>
														<option value="Liters">Litros</option>
														<option value="Units"> Unidades</option>
														<option value="Dozens"> Docenas </option>
														
											
													</select>
                                                </div>
                                            </div>
		                        
		                        </div>
	                        	<div class="wizard-footer">
	                            	<div class="pull-right">
	                                   
	                                    <input type='submit' class='btn btn-finish btn-fill btn-success btn-wd' name='finish' value='a&ntilde;adir objeto al inventario' />
	                                </div>
	                                
	                                <div class="clearfix"></div>
	                        	</div>
		                    </form>
		                </div>
						</div>
		            </div> <!-- wizard container -->
		        </div>
				
			<div class="col-md-12">
					<h3>Retiros</h3>
					<div class="divider"></div>
				<div class="col-md-6"> 
				
					<div class="card-content table-responsive">
				
					  <table class="table table-hover" style="text-align:left;">
                                        <thead class="text-primary">
                                            <th>ID</th>
                                             <th>Objeto</th>
                                            <th>Cantidad</th>
											<th>Registro</th>
											<th>Explicaci&oacute;n</th>
                                            
                                        </thead>
                                        <tbody>
								<?
								$query="select * from Mafinests";
								$result = mysqli_query($conn, $query);
                                             while (	$row = mysqli_fetch_assoc($result) ){?>
										
											
												<tr>
													<td><?echo $row['ID'];?></td>
													<td><?echo $row['Item'];?></td>
													<td  > <?echo $row['Qty'];?> <?echo $row['Unit'];?></td>
													<td><? $time = strtotime($row['_Timestamp']); echo  "hace ".humanTiming($time).".";?></td>
													<td><?echo $row['User'];?></td>
													<td><?echo $row['Explanation'];?></td>
												</tr>
											
										<?}	?>
                                     
                                        </tbody>
                                    </table>
				</div>	
				
				
				
				</div>
				
				 <div class="col-md-6" id="sexo">
		            <!-- Wizard container -->
		            <div class="wizard-container">
		                <div class="card wizard-card" data-color="red" style="margin-top:0px;" id="wizard">
						<div class="container" style="width:100%;position:relative;">
						<div class="pull-right">
	                                   
	                                </div>
		                    <form action="inventory.php" method="post">
		                <!--        You can switch " data-color="blue" "  with one of the next bright colors: "green", "orange", "red", "purple"             -->

		                    	<div class="wizard-header">
		                        	<h3 class="wizard-title">
		                        		Usar objeto
		                        	</h3>
								
		                    	</div>
								

		                        <div class="tab-content">
		                     
		                               <div class="col-md-6">
                                                <div class="form-group label-floating">
                                                    <label class="control-label">Objeto </label>
                                                  <select class="form-control" name="namet">
													<option value=""> </option>
													<?
													 $query="select Name from Items";
													 $result = mysqli_query($conn, $query);
													
													 while($row = mysqli_fetch_assoc($result)) {
													?>
													<option  value="<?echo $row['Name'];?>"> <?echo $row['Name'];?> </option>
													<?}?>
													</select>
                                                </div>
                                            </div>
										    <div class="col-md-3">
                                                <div class="form-group label-floating">
                                                    <label class="control-label">Cantidad</label>
                                                    <input  value="" min="1"  value="1" type="number" name="qtyt" required class="form-control">
                                                </div>
                                            </div>
											    
											 <div class="col-md-12">
                                                <div class="form-group label-floating">
                                                    <label class="control-label">Explicaci&oacute;n</label>
                                                    <Textarea rows="4" cols="50" class="form-control" name="explanation" value=""></textarea>
                                                </div>
                                            </div>
		                        
		                        </div>
	                        	<div class="wizard-footer">
	                            	<div class="pull-right">
	                                   
	                                    <input type='submit' class='btn btn-finish btn-fill btn-success btn-wd' name='finish' value='tomar objeto del inventario' />
	                                </div>
	                                
	                                <div class="clearfix"></div>
	                        	</div>
		                    </form>
		                </div>
						</div>
		            </div> <!-- wizard container -->
		        </div>
			</div>	
	    	</div> <!-- row -->
		</div> <!--  big container -->
					</div>
				</div>
			</div>
				







             
            <footer class="footer">
                <div class="container-fluid">
               
                    <p class="copyright pull-right">
                        &copy;
                        <script>
                            document.write(new Date().getFullYear())
                        </script>
                        <a href="">Overlord</a>, la plataforma para profesionales
                    </p>
                </div>
            </footer>
        </div>
    </div>
</body>
<!--   Core JS Files   -->

<script src="assets/js/material.min.js" type="text/javascript"></script>
<!--  Charts Plugin -->
<script src="assets/js/chartist.min.js"></script>
<!--  Dynamic Elements plugin -->
<script src="assets/js/arrive.min.js"></script>
<!--  PerfectScrollbar Library -->
<script src="assets/js/perfect-scrollbar.jquery.min.js"></script>
<!--  Notifications Plugin    -->
<script src="assets/js/bootstrap-notify.js"></script>
<!--  Google Maps Plugin    -->

<!-- Material Dashboard javascript methods -->
<script src="assets/js/material-dashboard.js?v=1.2.0"></script>
<script src="assets/js/demo.js"></script>
<script type="text/javascript">

  
		
	  var d = new Date();
    setInterval(function() {
        d.setSeconds(d.getSeconds() + 1);
        $('#timer').text(formatAMPM(d));
    }, 1000);
    function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}

function anadir()
{
	

}

</script>

</html>
