
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!DOCTYPE html>
<html>
    
    <head>
		<?php
    include("../includes/header-links.php");
    
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['nav_units'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
    ?>
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <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">Units available</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   
                                   <?php 

                                   //if($_SESSION['role']=="admin"){
                                   if( $permissions['add_unit'] == 1){
                                    ?>
                                            <div class="btn-group">
                                         <a href="#myModal" data-toggle="modal" class="btn btn-primary"><i class="icon-plus icon-white"></i>&nbsp;Add New</a>
                                      </div>
                                    <?php
                                   }
                                   //else
                                   // if($_SESSION['role']=="Super Admin"){
                                    ?>
                                    <!-- <div class="btn-group">
                                         <a href="#myModal1" data-toggle="modal" class="btn btn-primary"><i class="icon-plus icon-white"></i>&nbsp;Add New</a>
                                      </div> -->
                                    
                                      

                                      <!--Model begins here -->
                                      <div id="myModal" class="modal hide">
											<div class="modal-header">
												<button data-dismiss="modal" class="close" type="button">&times;</button>
												<h3>Add new Unit</h3>
											</div>
											<div class="modal-body">
												<form action="" method="POST">
												<label>Unit Name</label>
												<input type="text" name="name" class="input-xlarge focused" placeholder="Unit Name" required>
												<label>Unit symbol</label>
												<input type="text" name="symbol" class="input-xlarge focused" placeholder="Unit Symbol" required>
												<div class="form-actions">
                                          <button type="submit" name="add" class="btn btn-primary">Save Unit</button>
                                          <a href="" data-dismiss='modal' class="btn btn-danger">Cancel</a>
                                        </div>	

												</form>
											</div>
										</div>
										<!--Model Ends here -->
                                        <!--Model begins here -->
                                <!--       <div id="myModal1" class="modal hide">
											<div class="modal-header">
												<button data-dismiss="modal" class="close" type="button">&times;</button>
												<h3>Add new Unit</h3>
											</div>
											<div class="modal-body">
												<form action="" method="POST">
                                               
                        
                    
												<label>Unit Name</label>
												<input type="text" name="uname" class="input-xlarge focused" placeholder="Unit Name eg Kilogram" required>
												<label>Unit symbol</label>
												<input type="text" name="usymbol" class="input-xlarge focused" placeholder="unit symbol eg kg" required>
												<div class="form-actions">
                                          <button type="submit" name="uadd" class="btn btn-primary">Save Unit</button>
                                          <a href="" data-dismiss='modal' class="btn btn-danger">Cancel</a>
                                        </div>	

												</form>
											</div>
										</div> -->
										<!--Model Ends here -->
                                     
                                   <?php
include '../includes/connection.php';
                                   // Create connection
//if($_SESSION['role']=="Super Admin"){
if( $_SESSION['branch_id'] == 0){
    $sql = mysqli_query($con,"SELECT * FROM units,branches where units.status=1 && units.branch_id=branches.branch_id && branches.business_id='$business'");
}
else{
$sql = mysqli_query($con,"SELECT * FROM units where status=1 && branch_id='$branch'");
}
//$result = mysqli_query($con, $sql);

if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


                                   ?>
                                     
                                    <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                     <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="false" data-key-events="true" data-show-toggle="false" data-resizable="false" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>Unit Name</th>
                                                <th>Unit Symbol</th>
                                                <?php 
                                                //if($_SESSION['role']=="Super Admin"){
                                                if( $_SESSION['branch_id'] == 1){
                                                  ?>
                                                  <th>Branch</th><?php }?>
                                                <th>Date added</th>
                                                <th>Added By</th>
                                                <?php
                                                 //if($_SESSION['role']=="admin"){
                                                if( $permissions['edit_unit'] == 1 || $permissions['delete_unit'] == 1){
                                                  ?>
                                                <th>Action</th>
                                                <?php } ?>

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
										$no = 0;
										while($row = mysqli_fetch_assoc($sql)) {
											$no++;
												?>
										<tr class="gradeU">
                                                <td><?php echo $no; ?></td>
                                                <td><?php echo $row['Name']; ?></td>
                                                <td><?php echo $row['Symbol']; ?></td>
                                                <?php 
                                                if($_SESSION['branch_id']=="0"){
                                                  ?><td><?php echo $row['branch_name']; ?></td><?php }?>
                                                <td><?php echo $row['AddedOn']; ?></td>
												<?php
												$uid = $row['AddedBy'];
												$sql2 = mysqli_query($con,"SELECT name from users where id='$uid'");
												$row2 = mysqli_fetch_array($sql2);
												?>
                                                <td><?php echo $row2['name']; ?></td>

                                                
                                                <td class="center">
                                                <?php $newid=$row['UnitID'];
                                                    $prodname=$row['Name'];?>
                                                
                                                	<?php
                                                   //if($_SESSION['role']=="admin"){
                                                  if( $permissions['edit_unit'] == 1){
                                                    ?>
                                                <a href="#edit<?php echo $row['UnitID']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                <?php } ?>

                                                <?php
                                                   //if($_SESSION['role']=="admin"){
                                                if( $permissions['delete_unit'] == 1){
                                                    ?>
                                                	<a onclick = "confirmationDelete(this); return false;" href="delete_unit.php?id=<?php echo $row['UnitID']; ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                  <?php } ?>
                                                	
									<div id="edit<?php echo $row['UnitID']; ?>" class="modal hide">
                      <div class="modal-header" style="text-align: center; background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Edit Units</h3>
                      </div>
                      <div class="modal-body">
                        <?php 
                        $eid=$row['UnitID'];
                        $sqlget = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM units where UnitID='$eid'"));
                        $unid=$sqlget['UnitID'];
                        $uname=$sqlget['Name'];
                        $usymbol=$sqlget['Symbol'];

                         ?>
                          <form action="" method="POST">
                        
                        <input type="hidden" name="uid" value="<?php echo $uid;?>">
                        <label>Unit Name</label>
                        <input type="text" name="uname" class="input-xlarge focused" value="<?php echo $uname;?>" required>
                        <label>Unit symbol</label>
                        <input type="text" name="usymbol" class="input-xlarge focused" value="<?php echo $usymbol;?>" required>
                        <div class="form-actions">
                                          <button type="submit" name="save" class="btn btn-primary">Save </button>
                                          <a href="" data-dismiss="modal" class="btn btn-danger">Cancel</a>
                                        </div>  
                        </form>
                      </div>
                    </div>
                                            
                                                </td>
                                                
                                            </tr>
                                            
                                            <?php


    }
} else {
    echo "<h4 style='color:red;'>0 results</h4>";
}




                                        ?>
                                        <?php


if(isset($_POST['save'])){
  include '../includes/connection.php';
$uid=$_POST['uid'];
$unit_namee=$_POST['uname'];
$symbole=$_POST['usymbol'];

//$time = date("Y-m-d: H:i:s");
$sql0 = mysqli_query($con,"select * from units where (Name='$unit_namee' || Symbol='$symbole') && UnitID!='$uid' && branch_id='$branch'");
$num0 = mysqli_num_rows($sql0);
if($num0>0){
  echo"<script>alert('Failed. Unit already exists');</script>";
  ?>


    <meta http-equiv="refresh" content="0;URL='units.php'" />  
    <?php
}
else{
$sqle = mysqli_query($con,"UPDATE units SET Name='$unit_namee', Symbol='$symbole' where UnitID='$uid'");


if ($sqle) {


    $id_user=$_SESSION['id'];
  $activitye="Edited a  Unit ".$unit_namee." with a symbol ".$symbole;
  $log_sqle=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activitye','$branch','$business')");
  ?>


    <meta http-equiv="refresh" content="0;URL='units.php'" />  
    <?php
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
                                            
                                        </tbody>
                                    </table>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <!--==========Footer js======-->
		<?php require_once("../includes/footerjs.php");?>
        <!--==========Footer js======-->
       <script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>
    </body>

<?php


if(isset($_POST['add'])){
	include '../includes/connection.php';
$unit_name=$_POST['name'];
$symbol=$_POST['symbol'];
$id_user=$_SESSION['id'];
//$time = date("Y-m-d: H:i:s");
$sql0 = mysqli_query($con,"SELECT * from units where (Name='$unit_name' || Symbol='$symbol') && branch_id='$branch'");
$num0 = mysqli_num_rows($sql0);
if($num0>0){
	echo"<script>alert('Failed. Unit already exists')</script>";
  ?>


    <meta http-equiv="refresh" content="0;URL='units.php'" />  
    <?php
}
else{
$sql = mysqli_query($con,"INSERT INTO units (Name, Symbol,branch_id,AddedBy)
VALUES ('$unit_name', '$symbol','$branch', '$id_user')");

if ($sql) {


    $id_user=$_SESSION['id'];
  $activity="Added a new Unit ".$unit_name." with a symbol ".$symbol;
  $log_sql=mysqli_query($con,"INSERT INTO logs(user_id,branch_id, activity) VALUES 
('$id_user','$activity','branch')");
	?>


    <meta http-equiv="refresh" content="0;URL='units.php'" />  
    <?php
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>

 
</html>
