<!DOCTYPE html>
<html>
<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>
    
    <head>
       <?php   include("../includes/header-links.php");?>
        <!-- Bootstrap -->
        <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">
        <!--[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';
         $total=0; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
           <div class="span7" id="content">

                        <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Customers</div>
                                
                                <div class="muted pull-right" >&nbsp; &nbsp; &nbsp;</div>
                                <div class="muted pull-right"><a href="crates.php"><button class="bt btn-primary">Crates</button> </a> </div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    <?php
                                    include '../includes/connection.php';
                                    $sql = mysqli_query($con,"SELECT * from customers where business_id='$business' && branch_id='$branch'");
                                    if (mysqli_num_rows($sql) > 0) {
                                       ?>
  									<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
										<thead>
											<tr>
												<th>S/NO</th>
												<th>Name</th>
												<th>Tel</th>
												
                        <th>Action</th>
											</tr>
										</thead>
										<tbody>
                                        <?php
                                        
                                         // output data of each row
                                        $nnnn=0;
                while($row = mysqli_fetch_assoc($sql)) {
                        $nnnn++;
                    ?>
                    <tr class="odd gradeX">

        
                                                <td><?php echo $nnnn; ?></td>
                                                <td><?php echo $row['name']; ?></td>
                                                <td><?php echo $row['tel']; ?></td>
                                                
                                                <td class="center">
                                                    <a href="#myModal<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-primary"><i class="icon-edit icon-white"></i>&nbsp;</a>
                                                    <a href="delete_customer.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a>
                                                    
                                                </td>
                                            <div id="myModal<?php echo $row['id']; ?>" class="modal hide">
                                            <div class="modal-header" style="background-color: #08c; color: #ffff">
                                                <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                <h3>Edit customer details</h3>
                                            </div>
                                            <div class="modal-body">
                                            <form  action="" method="POST"> 
                                      <fieldset>
                                        <div class="control-group" >
                                          <label class="control-label" for="selectError" style="font-size: 20px;">Customer Name</label>
                                          <div class="controls">
                                          
                                            <input type="hidden" class="form-control" name="editid" required="required" value="<?php echo $row['id'];?>" style="width: 80%; font-size: 20px;" />
                                            <input type="text" class="form-control" name="name" required="required" value="<?php echo $row['name'];?>" style="width: 80%; font-size: 20px;" />
                                            
                                          </div>
                                        </div>
                                        
                                        <div id="approved"> 
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Contact</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="tel" type="text"  required="required" value="<?php echo $row['tel'];?>" style=" font-size: 20px; width: 80%;">
                                          </div>
                                        </div>
                                        <div class="form-actions">
                                          <button type="submit" class="btn btn-primary" name="editcustomer"><i class="icon-save "></i>Save changes</button>
                                       
                                        </div>
                                      
                                        </div>
                                      </fieldset>
                                    </form>
                                            </div>
                                            </div>
                                             <?php
                                        include '../includes/connection.php';

                                        if(isset($_POST['editcustomer'])){
                                            $editid=$_POST['editid'];
                                            $name=$_POST['name'];
                                            $tel=$_POST['tel'];
                                            $ck=mysqli_query($con,"SELECT * from customers where name='$name' && tel='$tel' && id!='$editid' && branch_id='$branch'");
                                            if (mysqli_num_rows($ck)>0) {
                                              echo "<script>alert('Failed! Customer with Name $name and Contact $tel already exiists'),history.go(-1)</script>";
                                            }else{
                                              $sql2 = mysqli_query($con,"UPDATE  customers set name='$name', tel='$tel' where id='$editid'");
                                            
                                        

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

                                        }}
                                        
                                        ?>


                                            </tr>
                                            <?php
    }?>
    </tbody>
                                    </table>

                                    
                                    
                                    
            
                               
                                    <?php 
} else {
    echo " No Customer Has been Added Yet!";
}

//mysqli_close($conn);
?>
					 		
											
								
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

         
                </div>
                <div class="span3">
                                     <form  action="" method="POST"> 
                                      <fieldset>
                                        <legend style="text-align: center;">Add Customer</legend>
                                        <div class="control-group" >
                                          <label class="control-label" for="selectError" style="font-size: 20px;">Customer Name</label>
                                          <div class="controls">
                                          
                                            <input type="text" class="form-control" name="name" required="required" style="width: 80%; font-size: 20px;" />
                                            
                                          </div>
                                          

  
                      
                                        </div>
                                        
                                        <div id="approved"> 
                                        <div class="control-group">
                                          <label class="control-label"  for="focusedInput" style=" font-size: 20px;">Contact</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" name="tel" type="text"  required="required" style=" font-size: 20px;">
                                          </div>
                                        </div>
                                        

                                        
                                       
                                        
                                        <div class="form-actions">
                                          <button type="submit" class="btn btn-primary" name="add"><i class="icon-plus "></i>Add</button>
                                       
                                        </div>
                                      
                                        </div>
                                      </fieldset>
                                    </form>
                                    <?php
                                        include '../includes/connection.php';

                                        if(isset($_POST['add'])){
                                            $name=$_POST['name'];
                                            $tel=$_POST['tel'];
                                            $ck=mysqli_query($con,"SELECT * from customers where name='$name' && tel='$tel' && branch_id='$branch'");
                                            if (mysqli_num_rows($ck)>0) {
                                              echo "<script>alert('Failed! Customer with Name $name and Contact $tel already exiists'),history.go(-1)</script>";
                                            }else{


                                            
                                              
                                              $sql2 = mysqli_query($con,"INSERT INTO customers(name, tel,business_id,branch_id,added_by)
                                        VALUES ('$name', '$tel','$business','$branch','$id_user')");
                                            
                                        

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

                                        }}
                                        
                                        ?>

                                </div>
            </div>



<div class="row-fluid">
    
</div>

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

       <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/datatables/js/jquery.dataTables.min.js"></script>


        <script src="../assets/scripts.js"></script>
        <script src="../assets/DT_bootstrap.js"></script>


        <link href="../vendors/datepicker.css" rel="stylesheet" media="screen">
        <link href="../vendors/uniform.default.css" rel="stylesheet" media="screen">
        <link href="../vendors/chosen.min.css" rel="stylesheet" media="screen">

        <link href="../vendors/wysiwyg/bootstrap-wysihtml5.css" rel="stylesheet" media="screen">

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../vendors/jquery.uniform.min.js"></script>
        <script src="../vendors/chosen.jquery.min.js"></script>
        <script src="../vendors/bootstrap-datepicker.js"></script>

        <script src="../vendors/wysiwyg/wysihtml5-0.3.0.js"></script>
        <script src="../vendors/wysiwyg/bootstrap-wysihtml5.js"></script>

        <script src="../vendors/wizard/jquery.bootstrap.wizard.min.js"></script>
        <script type="text/javascript" src="../vendors/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="../assets/form-validation.js"></script>
        
    <script src="../assets/scripts.js"></script>
        <script>
        $(function() {
            
        });
        </script>
        <script>

    jQuery(document).ready(function() {   
       FormValidation.init();
    });
    

        $(function() {
            $(".datepicker").datepicker();
            $(".uniform_on").uniform();
            $(".chzn-select").chosen();
            $(".chzn-select1").chosen();
            $('.textarea').wysihtml5();

            $('#rootwizard').bootstrapWizard({onTabShow: function(tab, navigation, index) {
                var $total = navigation.find('li').length;
                var $current = index+1;
                var $percent = ($current/$total) * 100;
                $('#rootwizard').find('.bar').css({width:$percent+'%'});
                // If it's the last tab then hide the last button and show the finish instead
                if($current >= $total) {
                    $('#rootwizard').find('.pager .next').hide();
                    $('#rootwizard').find('.pager .finish').show();
                    $('#rootwizard').find('.pager .finish').removeClass('disabled');
                } else {
                    $('#rootwizard').find('.pager .next').show();
                    $('#rootwizard').find('.pager .finish').hide();
                }
            }});
            $('#rootwizard .finish').click(function() {
                alert('Finished!, Starting over!');
                $('#rootwizard').find("a[href*='tab1']").trigger('click');
            });
        });
        </script>

    </body>

</html>