<?php include('header.php'); ?>
<?php include('session.php'); ?>
    <body>
		<?php include('navbar.php'); ?>
        <div class="container-fluid">
            <div class="row-fluid">
				<?php include('Device_sidebar.php'); ?>
		
						<div class="span9" id="content">
		                    <div class="row-fluid">
							
		                        <!-- block -->
		                        <div class="block">
		                            <div class="navbar navbar-inner block-header">
		                                <div class="muted pull-left">Add Toner Stock</div>
										<div class="muted pull-right"><a id="return" data-placement="left" title="Click to Return" href="toner_stock.php"><i class="icon-arrow-left icon-large"></i> Back</a></div>
										<script type="text/javascript">
										$(document).ready(function(){
										$('#return').tooltip('show');
										$('#return').tooltip('hide');
										});
										</script>                          
		                            </div>
									
		                <div class="block-content collapse in">	
                         <div class="alert alert-success"><i class="icon-info-sign"></i> Please Fill in required details</div>						
							<form class="form-horizontal" method="post">											
								
								<div class="control-group">
		                                 <label class="control-label" for="inputEmail">Location</label>
			                                <div class="controls">
				                              <select name="loc_id" class="chzn-select"  required/>
				                                 <option></option>
			                                     <?php $loc=mysql_query("select * from stlocation")or die(mysql_error()); 
			                                     while ($row=mysql_fetch_array($loc)){ 												
												 ?>
				                                 <option value="<?php echo $row['stdev_id']; ?>&nbspName&nbsp<?php echo $row['stdev_location_name']; ?>"><?php echo $row['stdev_location_name']; ?></option>
				                                 <?php } ?>
				                               </select>
		                                     </div>
	                                    </div>
										<!--div class="control-group">
		                                 <label class="control-label" for="inputEmail">Toner Brand</label>
			                                <div class="controls">
				                              <select name="toner_brand" class="chzn-select"  required/>
				                                 <option></option>
			                                     <?php $toner_brand=mysql_query("select * from toner_brand")or die(mysql_error()); 
			                                     while ($row=mysql_fetch_array($toner_brand)){ 												
												 ?>
				                                 <option value="<?php echo $row['brand']; ?> "><?php echo $row['brand']; ?></option>
				                                 <?php } ?>
				                               </select>
		                                     </div>
	                                    </div-->
											
										<div class="control-group">
		                                 <label class="control-label" for="inputEmail">Toner Model</label>
			                                <div class="controls">
				                              <select name="toner_model" class="chzn-select"  required/>
				                                 <option></option>
			                                     <?php $toner_model=mysql_query("select * from toner_model")or die(mysql_error()); 
			                                     while ($row=mysql_fetch_array($toner_model)){ 												
												 ?>
				                                 <option value="<?php echo $row['model']; ?>"><?php echo $row['model']; ?></option>
				                                 <?php } ?>
				                               </select>
		                                     </div>
	                                    </div>
										<div class="control-group">
		                                 <label class="control-label" for="inputEmail">Toner Color</label>
			                                <div class="controls">
				                              <select name="color" class="chzn-select"  required/>
				                                 <option></option>
			                                     <?php $toner_color=mysql_query("select * from toner_color")or die(mysql_error()); 
			                                     while ($row=mysql_fetch_array($toner_color)){ 												
												 ?>
				                                 <option value="<?php echo $row['color']; ?>"><?php echo $row['color']; ?></option>
				                                 <?php } ?>
				                               </select>
		                                     </div>
	                                    </div>
											 <div class="control-group">
											<label class="control-label" for="inputPassword">Quantity</label>
											<div class="controls">
											<input type="text" class="span6" name="quantity" id="inputPassword" placeholder="Quantity" required>
											</div>
										</div>	
										<div class="control-group">
											<label class="control-label" for="inputPassword">Description</label>
											<div class="controls">
													<!--textarea name="toner_description" id="ckeditor_full"></textarea-->
												<textarea name="toner_description" ></textarea>
											</div>
										</div>
									 
			                                     <?php 
												 $id=$_SESSION['id'];
												 $admin=mysql_query("select * from admin WHERE admin_id='$id'")or die(mysql_error()); 
			                                     while ($row=mysql_fetch_array($admin)){ 
												 $username=$row['username'];	
																
												 ?>
				                                 <input type="hidden" class="span8" name="toner_pic" value="<?php echo $username;?>">
				                                 <?php } ?>
												 
												
				                               
											
										<div class="control-group">
										<div class="controls">
										<button name="save" id="save" name="singlebutton" data-placement="right" title="Click here to Save your new data." class="btn btn-primary"><i class="icon-save"></i> 
										Save</button>				
										</div>
										</div>
										<script type="text/javascript">
										$(document).ready(function(){
										$('#save').tooltip('show');
										$('#save').tooltip('hide');
										});
										</script>
							</form>
<?php
if (isset($_POST['save'])){
$loc_id=$_POST['loc_id'];
//$toner_brand = $_POST['toner_brand'];
$toner_model = $_POST['toner_model'];
$color = $_POST['color'];
$quantity = $_POST['quantity'];
$toner_description = $_POST['toner_description'];
$username = $_POST['toner_pic'];

$query = mysql_query("select * from cis_toner_instock where loc_id = '$loc_id' AND toner_model='$toner_model' AND color='$color' ")or die(mysql_error());
$count = mysql_num_rows($query);

if ($count > 0){ ?>
<script>
alert('Model printer and Color Already Exist');
window.location = "toner_stock.php";
</script>
<?php 
$query = mysql_query("UPDATE cis_toner_instock SET  quantity = IF('$_POST[quantity]'='', quantity,quantity + '$quantity')
                      where loc_id = '$loc_id' AND toner_model='$toner_model' AND color='$color' ")or die(mysql_error());
					  
mysql_query("insert into  cis_toner_instock_history (loc_id,toner_model,color,quantity,toner_description,toner_pic) values('$loc_id','$toner_model','$color','$quantity','$toner_description','$username')")or die(mysql_error());
}else{								

mysql_query("insert into  cis_toner_instock (loc_id,toner_model,color,quantity,toner_description,toner_pic) values('$loc_id','$toner_model','$color','$quantity','$toner_description','$username')")or die(mysql_error());

mysql_query("insert into  cis_toner_instock_history (loc_id,toner_model,color,quantity,toner_description,toner_pic) values('$loc_id','$toner_model','$color','$quantity','$toner_description','$username')")or die(mysql_error());
mysql_query("insert into activity_log (date,username,action) values(NOW(),'$admin_username','Add Toner Detail id $id')")or die(mysql_error());											
?>
<script>
window.location = "toner_stock.php";
$.jGrowl("Device Successfully added", { header: 'Toner add' });
</script>
<?php
}
}

?>
																										
		                            </div>
		                        </div>
		                        <!-- /block -->
		                    </div>
		                </div>
            </div>
		<?php include('footer.php'); ?>
        </div>
		<?php include('script.php'); ?>
    </body>