
<?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>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php
    include("../includes/add_licence.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_settings_branch'] != 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>
    <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">Business Licences</div>
                        </div>
                        <div class="block-content collapse in">
                            <div class="span12">
                               
                               
                                    <!--Model Ends here -->
                              
                               <?php
include '../includes/connection.php';
                               // Create connection
if ($business!=0) {
 $sql = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licences.business_id) as business FROM licences where business_id='$business'");
}else{
$sql = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licences.business_id) as business FROM licences ");
}

//$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> Business</th>
                                            <th> Number Of branches</th>
                                            <th> Annual Payment</th>
                                            <th>startdate</th>
                                            <th>Endate</th>
                                            
                                            <th>Remaining Days</th>
                                            <th>Action</th>

                                      
                                            

                                        </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['business']; ?></td>
                                            <td><?php echo $row['branches']; ?></td>
                                            <td><?php echo number_format($row['annual_bill']); ?></td>
                                            <td><?php echo $row['startdate']; ?></td>
                                            <td><?php echo $row['enddate']; ?></td>
                                            <td><?php
                                            $exp=strtotime($row['enddate']);
                                              $thistoday=strtotime(date('Y-m-d'));
                                             echo $range=($exp-$thistoday)/(86400)." days";
                                          
                                              ?></td>
                                              <td> <a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                              </td>
                                                <div id="edit<?php echo $row['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 Licence Details of <?php echo $row['business'];?></h3>
                                        </div>
                                        <div class="modal-body">
                    
                                            <form action="" method="POST">
                      <input type="hidden" name="editid" value="<?php echo $row['id'];?>">
                    <label style=" font-size: 20px; ">Number of Branches</label>
                    <input type="number" name="branches" value="<?php echo $row['branches'];?>" class="input-xlarge focusedform-control"  required style="width: 
                    95%; height: 30px; font-size: 20px;">
                    <label style=" font-size: 20px; "> Annual Bill</label>
                    <input type="number" name="annual_bill" value="<?php echo $row['annual_bill'];?>" required="required" style="width: 
                    95%; height: 30px; font-size: 20px;" minlength="9" maxlength="10">
                    <label style=" font-size: 20px; "> End Date</label>
                    <input type="date" value="<?php echo $row['enddate'];?>" name="enddate" class="form-control"  style="width: 
                    95%; height: 30px; font-size: 20px;">
                    
                    
                    <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>
                                           
                                           

                                            
                                        </tr>
                                        
                                        <?php


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




?>


                                    
                                        
                                    </tbody>
                                </table>
                                
                            </div>
                        </div>
                    </div>
                    <!-- /block -->
                </div>
            </div>
        </div>
        <?php

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


$id_user=$_SESSION['id'];
//$time = date("Y-m-d: H:i:s");

$esql = mysqli_query($con,"UPDATE  licences set branches='$branches', annual_bill='$annual_bill',enddate='$enddate' where id='$editid'");
if ($esql) {


echo "<script>
alert('Licence has been updated successfully');
window.location='business_licence.php'
</script>";
} else {
echo "Error: " . $esql . "<br>" . mysqli_error($con);
}

mysqli_close($con);
}
        ?>
        <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 Branch?");
    if(conf){
        window.location = anchor.attr("href");
    }
} 
function confirmationSuspend(anchor){
    var conf1 = confirm("Are you sure you want to Deactivate this Branch?");
    if(conf1){
        window.location = anchor.attr("href");
    }
}  
function confirmationReturn(anchor){
    var conf2 = confirm("Are you sure you want to Activate this Branch?");
    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>


</html>

