<!DOCTYPE html>
<html>
    
    <head>
		<?php
		include("../includes/header-links.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>
        <script type="text/javascript">
    function FormatCurrency(ctrl) {
                  //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
                  if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                      return;
                  }

                  var val = ctrl.value;

                  val = val.replace(/,/g, "")
                  ctrl.value = "";
                  val += '';
                  x = val.split('.');
                  x1 = x[0];
                  x2 = x.length > 1 ? '.' + x[1] : '';

                  var rgx = /(\d+)(\d{3})/;

                  while (rgx.test(x1)) {
                      x1 = x1.replace(rgx, '$1' + ',' + '$2');
                  }

                  ctrl.value = x1 + x2;
              }

              function CheckNumeric() {
                  return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
              }
</script>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Product Categories </div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   
                                   <?php
                                   // if($_SESSION['role']=="Super Admin"){
                                   if( $permissions['add_product_category'] == 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
                                   }
                                   ?>
                                      

                                      <!--Model begins here -->
                                      <div id="myModal" class="modal hide">
											<div class="modal-header" style="background: #08c; color: white; text-align: center;">
												<button data-dismiss="modal" class="close" type="button">&times;</button>
												<h3>Add Product Category</h3>
											</div>
											<div class="modal-body">
												<form action="" method="POST">
                                                <div class="col-lg-12">
                          <label style=" font-size: 20px; ">Branch</label>
                        
                        <select name="branch" class="input-xlarge focusedform-control" required style="width: 95%; height: 30px;font-size: 20px;">
                          <option disabled selected="" value="">Select Branch</option>
                          <!-- <option value="all">All</option> -->
                         <?php 
                         $cat=mysqli_query($con,"SELECT * from branches where status=1 && business_id='$business'");
                         while ( $row1=mysqli_fetch_array($cat)) {
                           ?>
                           <option value="<?php echo $row1['branch_id'];?>"><?php echo $row1['branch_name'];?></option>
                           <?php
                         }
                          ?>
                         
                          
                        </select>
                      </div>
												<label style=" font-size: 20px; ">Category</label>
												<input type="text" name="name" class="input-xlarge focusedform-control" placeholder="Category Name" required style="width: 
                        95%; height: 30px; font-size: 20px;">
												<label style=" font-size: 20px; "> Description</label>
												<textarea name="description" style="width: 95%" class="form-control"></textarea>
                        
												<div class="span12" >
                          <div class="span8">
                                          <button type="submit" name="add" class="btn btn-primary">Save</button>
                                        </div>
                                        <div  class="span4">
                                          <a href="" class="btn btn-danger" data-dismiss="modal">Cancel</a>
                                        </div>	
                                      </div>
												</form>
											</div>
										</div>
										<!--Model Ends here -->
                                  
                                   <?php
include '../includes/connection.php';
                                   // Create connection
$sql = mysqli_query($con,"SELECT * FROM product_categories where status=1 && business_id='$business'");
//$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> Category</th>
                                                <th>Description</th>
                                              
                                                <th>Added By</th>
                                                <th>Branch</th>
                                                <th>AddedOn</th>
                                                <?php
                                                 if( $permissions['edit_product_category'] == 1){
                                                  ?>
                                                <th>Edit</th>
                                                <?php } ?>
                                               <?php
                                                 if( $permissions['delete_product_category'] == 1){
                                                  ?>
                                                <th>Delete</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['category_name']; ?></td>
                                                <td><?php echo $row['description']; ?></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>
                                                <?php
                                                $bra = $row['branch_id'];
                                                $sql3 = mysqli_query($con,"select * from branches where branch_id='$bra'");
                                                $row3 = mysqli_fetch_array($sql3);
                                                ?>
                                                <td><?php echo $row3['branch_name']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>


                                                <?php
                                                 //if($_SESSION['role']=="admin"){
                                                 if( $permissions['edit_product_category'] == 1){ 
                                                  ?>

                                                <td class="center">
                                                
                                                <a href="#edit<?php echo $row['category_id']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                	
                                                	
                                                	
									<div id="edit<?php echo $row['category_id']; ?>" 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 Product Category</h3>
											</div>
											<div class="modal-body">
                        <?php  
                        $sid=$row['category_id'];
                        $values=mysqli_fetch_array(mysqli_query($con,"SELECT * from product_categories where category_id='$sid'"));
                        $eid=$values['category_id'];
                        $eid=$values['category_id'];
                        $ename=$values['category_name'];
                        $econtact=$values['description'];
                        ?>
												<form action="" method="POST">
                          <input type="hidden" name="editid" value="<?php echo $eid;?>">
                        <label style=" font-size: 20px; ">Category Name</label>
                        <input type="text" name="ename" value="<?php echo $ename;?>" class="input-xlarge focusedform-control"  required style="width: 
                        95%; height: 30px; font-size: 20px;">
                        <label style=" font-size: 20px; "> Description</label>
                        <textarea name="edescription" style="width: 95%" > <?php echo $econtact;?></textarea>
                        
                       
                        <div class="span12" >
                          <div class="span8">
                                          <button type="submit" name="save" class="btn btn-primary">Save </button>
                                        </div>
                                         
                                      </div>
                        </form>

											</div>
											<div class="modal-footer">
												
												<a data-dismiss="modal" class="btn btn-warning" href="#">cancel</a>
											</div>
										</div>
                                            
                                                </td>
                                                <?php } ?>
                                                <?php
                                                 //if($_SESSION['role']=="admin"){
                                                 if( $permissions['delete_product_category'] == 1){ 
                                                  ?>
       
                                                <td><a onclick = "confirmationDelete(this); return false;" href="delete_pcategory.php?categoryid=<?php echo $row['category_id'];?>" ><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a></td>

                                                <?php } ?>
                                            </tr>
                                            
                                            <?php


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

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 type = "text/javascript">
    function confirmationDelete(anchor){
        var conf = confirm("Are you sure you want to delete this record?");
        if(conf){
            window.location = anchor.attr("href");
        }
    } 
    function confirmationSuspend(anchor){
        var conf1 = confirm("Are you sure you want to Suspend this supplier?");
        if(conf1){
            window.location = anchor.attr("href");
        }
    }  
    function confirmationReturn(anchor){
        var conf2 = confirm("Are you sure you want to Return this Supplier?");
        if(conf2){
            window.location = anchor.attr("href");
        }
    }      
</script>
<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';
$branchc=mysqli_real_escape_string($con,ucwords($_POST['branch']));
$name=mysqli_real_escape_string($con,ucwords($_POST['name']));
$description=mysqli_real_escape_string($con,$_POST['description']);

$id_user=$_SESSION['id'];
//$time = date("Y-m-d: H:i:s");
$sql0 = mysqli_query($con,"SELECT * from product_categories where category_name='$name' && branch_id='$branchc' ") or die(mysqli_error($con));
$num0 = mysqli_num_rows($sql0);
if($num0>0){
	echo"<script>alert('Failed!. Cagtegory $name already exists'),history.go(-1)</script>";
}
else{
$sql = mysqli_query($con,"INSERT INTO product_categories (category_name, description,branch_id,business_id,AddedBy)
VALUES ('$name', '$description','$branchc','$business','$id_user')");

if ($sql) {


    $id_user=$_SESSION['id'];
  $activity="Added a new product Category ".$name;
  $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");
	
  echo "<script>
  alert('Cagtegory $name  has been recorded successfully');
  window.location='product_category_admin.php'
  </script>";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
 
<?php


if(isset($_POST['save'])){
  include '../includes/connection.php';
  $editid=$_POST['editid'];
$ename=mysqli_real_escape_string($con,ucwords($_POST['ename']));
$edescription=mysqli_real_escape_string($con,$_POST['edescription']);

$id_user=$_SESSION['id'];
//$time = date("Y-m-d: H:i:s");
$esql0 = mysqli_query($con,"SELECT * from product_categories where category_name='$ename' && category_id!='$editid' && branch_id='$branch'");
$enum0 = mysqli_num_rows($esql0);
if($enum0>0){
  echo"<script>alert('Failed!. Cagtegory $ename  already exists')</script>";
}
else{
$esql = mysqli_query($con,"UPDATE  product_categories set category_name='$ename', description='$edescription' where category_id='$editid'");
if ($esql) {


    $id_user=$_SESSION['id'];
  $eactivity="Edited Category ".$ename;
  $elog_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id) VALUES 
('$id_user','$eactivity','$branch')");
  
  echo "<script>
  alert('Category $ename  has been updated successfully');
  window.location='product_category.php'
  </script>";
} else {
    echo "Error: " . $esql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>
 
</html>

