<!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">Branches</div>
                        </div>
                        <div class="block-content collapse in">
                            <div class="span12">
                               
                               <?php if($_SESSION['role']=="Super Admin"){
                                ?>
                                        <div class="btn-group" >
                                     <a href="#myModal" data-toggle="modal" class="btn btn-primary"><i class="icon-plus icon-white"></i>&nbsp;Add a new branch</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 A New Branch</h3>
                                        </div>
                                        <div class="modal-body">
                                            <form action="" method="POST">
                                            <label style=" font-size: 20px; ">Branch Name</label>
                                            <input type="text" name="branch" class="input-xlarge focusedform-control" placeholder="Supplier Name" required style="width: 95%; height: 30px; font-size: 20px;">
                                            <label style=" font-size: 20px; "> Contact</label>
                                            <input type="tel" pattern="[0][0-9]{9}" name="contact" class="form-control" placeholder="0774367210" required="required" style="width:  95%; height: 30px; font-size: 20px;"  >
                                            <label style=" font-size: 20px; "> Email</label>
                                      <input type="email" name="email" class="form-control" placeholder="info@info.com" style="width: 95%; height: 30px; font-size: 20px;">
                      
                                   <label style=" font-size: 20px; "> Location</label>
                                  <input type="text" name="address" class="form-control" placeholder="Address"  style="width: 95%; height: 30px; font-size: 20px;">
                    
                       
                    
                                            <div class="span12" >
                                        <div class="span8">
                                      <button type="submit" name="addbranch" 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 branches where 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> Name</th>
                                            <th>Contact</th>
                                            <th>Address</th>
                                            <th>Email</th>
                                           
                                            <th>Added By</th>
                                            <th>AddedOn</th>
                                            
                                            <th>Action</th>
                                            <th>Activate</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['branch_name']; ?></td>
                                            <td>256<?php echo $row['contact']; ?></td>
                                            <td><?php echo $row['location']; ?></td>
                                            <td><?php echo $row['email']; ?></td>
                                            
                                            
                                            
                                            <?php
                                            $uid = $row['recorded_by'];
                                            $sql2 = mysqli_query($con,"select name from users where id='$uid'");
                                            $row2 = mysqli_fetch_array($sql2);
                                            ?>
                                            <td><?php echo $row2['name']; ?></td>
                                            <td><?php echo $row['recorded_on']; ?></td>
                                            
                                            <td class="center">
                                            
                                            <a href="#edit<?php echo $row['branch_id']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                
                                                
                                                
                                <div id="edit<?php echo $row['branch_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 Supplier</h3>
                                        </div>
                                        <div class="modal-body">
                    <?php  
                    $sid=$row['branch_id'];
                    $values=mysqli_fetch_array(mysqli_query($con,"SELECT * from branches where branch_id='$sid'"));
                    $eid=$values['branch_id'];
                    
                    $ename=$values['branch_name'];
                    $econtact=$values['contact'];
                    $eemail=$values['email'];
                    $eaddress=$values['location'];
                    
                    ?>
                                            <form action="" method="POST">
                      <input type="hidden" name="editid" value="<?php echo $eid;?>">
                    <label style=" font-size: 20px; ">Branch 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; "> Contact</label>
                    <input type="tel" name="econtact" value="0<?php echo $econtact;?>" pattern="[0][0-9]{9}"required="required" style="width: 
                    95%; height: 30px; font-size: 20px;" minlength="9" maxlength="10">
                    <label style=" font-size: 20px; "> Location</label>
                    <input type="text" value="<?php echo $eaddress;?>" name="eaddress" class="form-control"  style="width: 
                    95%; height: 30px; font-size: 20px;">
                    <label style=" font-size: 20px; " > Email</label>
                    <input type="email" name="eemail" value="<?php echo $eemail;?>" 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>
                                        
                                            </td>
                                            <?php 
    $sus=$row['status'];
if ($sus==0) {
   ?>
   <td><a onclick = "confirmationReturn(this); return false;" href="activate_branch.php?branch_id=<?php echo $row['branch_id']; ?>"><button class="btn btn-warning"><i class="icon-ok icon-white"></i>Return</button></a>
</td>
   <?php
}else{
  ?>
  <td><a onclick = "confirmationSuspend(this); return false;" href="deactivate_branch.php?branch_id=<?php echo $row['branch_id']; ?>"><button class="btn btn-info"><i class="icon-plane icon-white"></i>suspend</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 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>

<?php


if(isset($_POST['addbranch'])){
include '../includes/connection.php';
$branchname=mysqli_real_escape_string($con,ucwords($_POST['branch']));
$contact1=mysqli_real_escape_string($con,$_POST['contact']);
$contact=substr($contact1, -9);
$address=mysqli_real_escape_string($con,ucwords($_POST['address']));
$email=mysqli_real_escape_string($con,$_POST['email']);

$id_user=$_SESSION['id'];

$sql0 = mysqli_query($con,"SELECT * from branches where branch_name='$branchname' && location='$address' && business_id='$business'") or die(mysqli_error($con));
$num0 = mysqli_num_rows($sql0);
if($num0>0){
echo"<script>alert('Failed. Branch with name $branchname and location $address already exists. Correct your inputs'),history.go(-1)</script>";
}
else{
$sql = mysqli_query($con,"INSERT INTO branches (business_id,branch_name, contact,location,email, recorded_by)
VALUES ('$business','$branchname', '$contact','$address','$email','$id_user')");

if ($sql) {


$id_user=$_SESSION['id'];
$activity="Added a new branchname ".$branchname." with a contact: ".$contact." and email: ".$email;
$log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,business_id) VALUES 
('$id_user','$activity','$business')");

echo "<script>
alert('Branch $branchname with contact $contact and email $email has been recorded successfully');
window.location='branches.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']));
$econtact1=mysqli_real_escape_string($con,$_POST['econtact']);
$econtact=substr($econtact1, -9);
$eaddress=mysqli_real_escape_string($con,ucwords($_POST['eaddress']));
$eemail=mysqli_real_escape_string($con,$_POST['eemail']);

$id_user=$_SESSION['id'];
//$time = date("Y-m-d: H:i:s");
$esql0 = mysqli_query($con,"SELECT * from branches where branch_name='$ename' && location='$eaddress' && branch_id !='$editid'");
$enum0 = mysqli_num_rows($esql0);
if($enum0>0){
echo"<script>alert('Failed. Branch with Name $ename and location $eaddress  already exists')</script>";
}
else{
$esql = mysqli_query($con,"UPDATE  branches set branch_name='$ename', contact='$econtact',location='$eaddress',email='$eemail' where branch_id='$editid'");
if ($esql) {


$id_user=$_SESSION['id'];
$eactivity="Updated Details Branch ".$ename." with a contact: ".$econtact." and email: ".$eemail;
$elog_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,business_id) VALUES 
('$id_user','$eactivity','$business')");

echo "<script>
alert('Branch $ename with contact $econtact and email $eemail has been updated successfully');
window.location='branches.php'
</script>";
} else {
echo "Error: " . $esql . "<br>" . mysqli_error($con);
}
}
mysqli_close($con);
}

?>

</html>

