<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Contractor App </title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

  <!-- Theme style -->
  <link rel="stylesheet" href="<?php echo base_url();?>assets/css/AdminLTE.min.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
  <link rel="stylesheet" href="<?php echo base_url();?>assets/css/custom.css" type="text/css" media="screen"/>


  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <?php  include"includes/header.php";  ?>
  <!-- Left side column. contains the logo and sidebar -->
  <aside class="main-sidebar">
    <!-- sidebar: style can be found in sidebar.less -->
    <?php  include"includes/sidebar.php";  ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper ">
	<section class="content-header">
      <h1>Clients</h1>
    </section>
  
 
		<section class="content">
			
		<div class="backf pt30">
		
		
			
			<div class="mb20">
				<button class="btn btn-warning mb20" data-toggle="modal" data-target="#add-client">Add Client</button>
			</div>
			
			<div class="table-responsive">
				<table  class="table table-striped datatble">
					<thead>
					  <tr>
                                              <th>Sr.No</th>
						<th>First Name</th>
						<th>Last Name</th>
						<th>Email</th>
					<!--	<th>Password</th>-->
						<th>Phone</th>
						<!-- <th>Address</th> -->
						<th>Action</th>
					  </tr>
					</thead>
                                         <?php 
					$i=1;
                                        $ans=0;
					?>
					<tbody>
					 
					 <?php 
				
					$query = $this->db->query("SELECT * FROM  clients_tbl");
					foreach($query->result_array('clients_tbl') as $row){
                                             $ans = $ans+$i;
					?>
					  <tr>
                                               <td><?php echo $ans;?></td>
						<td><?php echo $row['first_name'];?></td>
						<td><?php echo $row['last_name'];?></td>
						<td><?php echo $row['email'];?></td>
						<?php /*?><td><?php echo $row['password'];?></td><?php */?>
						<td><?php echo $row['phone'];?></td>
						
						<td>
							<button class="btn btn-info editclient" data-toggle="modal" data-target="#edit-client" client_id="<?php echo $row['client_id'];?>">Edit</button>
							<button class="btn btn-danger delete" del="<?php echo $row['client_id'];?>">Delete</button>
						</td>
					  </tr>
					   <?php } $ans=$i+1;?> 
					</tbody>
				  </table>
			</div>

      </div>
		
		</section>
		
		
		
  </div>
  
  
  
  
  <!-- Modal -->
<div id="add-client" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
     <form  method="post" id="add_client">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add Client</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
		  <label for="usr">First Name:</label>
		  <input type="text" class="form-control" id="first_name" name="first_name" required>
		</div>
		 <div class="form-group">
		  <label for="usr">Last Name:</label>
		  <input type="text" class="form-control" id="last_name" name="last_name" required>
		</div>
		 <div class="form-group">
		  <label for="usr">Email:</label>
		  <input type="text" class="form-control" id="email" name="email" required>
		</div>
		 <div class="form-group">
		  <label for="usr">Password:</label>
		  <input type="password" class="form-control" id="password" name="password" required>
		</div>
		 <div class="form-group">
		  <label for="usr">Phone:</label>
		  <input type="text" class="form-control" id="phone" name="phone" required>
		</div>
		 <!-- <div class="form-group">
		  <label for="usr">Address:</label>
		  <input type="text" class="form-control" id="usr">
		</div> -->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
       
         <input type="submit" class="btn btn-success" name="sub" value="Save">
      </div>
      </form>
    </div>

  </div>
</div>
  
  
  <!-- Modal -->
<div id="edit-client" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
     <form  method="post" id="edit_client">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Edit Client</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
		  <label for="usr">First Name:</label>
		  <input type="text" class="form-control" id="first_name1" name="first_name1" required>
           <input type="hidden" id="client_id" name="client_id">
		</div>
		 <div class="form-group">
		  <label for="usr">Last Name:</label>
		  <input type="text" class="form-control" id="last_name1" name="last_name1" required>
		</div>
		 <div class="form-group">
		  <label for="usr">Email:</label>
		  <input type="text" class="form-control" id="email1" name="email1" required>
		</div>
		 <div class="form-group">
		  <label for="usr">Password:</label>
		  <input type="password" class="form-control" id="password1" name="password1">
		</div>
		 <div class="form-group">
		  <label for="usr">Phone:</label>
		  <input type="text" class="form-control" id="phone1" name="phone1" required>
		</div>
		 <!-- <div class="form-group">
		  <label for="usr">Address:</label>
		  <input type="text" class="form-control" id="usr">
		</div> -->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        
         <input type="submit" class="btn btn-success update" value="Update">
      </div>
      </form>
    </div>

  </div>
</div>
 <?php  include"includes/footer.php";  ?>
 <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
 <script>
 $('form[id="add_client"]').validate({
 rules: {

	first_name:{
		required:true,
		},
	last_name:{
		required:true,
		},
	email:{
		email: true,
		required:true,
		},
	phone:{
		required:true,
		},
	password:{
		required:true,
		},
	
		
	},
	
 submitHandler: function(form) {
  var data = $("#add_client").serialize();
 $.ajax({                                      
			  url: '<?php echo base_url() ?>index.php/admin/add_client',                 //the script to call to get data  
			  type:'post',
			  data: data,//you can insert url argumnets here to pass to api.php
				  success: function(data)          //on recieve of reply
				  {

					if(data == 1){
					alert('Data inserted Successfully');
					location.reload();
					} 
                                        else if(data == 3){
                                            alert('Email is already exist, Please use other one');
                                        }
				 } 
			  });
 }	
});
 
  $('.editclient').click(function(){
     var client_id = $(this).attr('client_id');
   
         $.ajax({
                type: "POST",
                url: '<?php echo base_url() ?>index.php/admin/edit_client',
                data: {client_id:client_id},
				dataType:"json",
                success:function(data)
                {
			      $("#client_id").val(data.client_id);
				  $("#first_name1").val(data.first_name);  
				  $("#last_name1").val(data.last_name);
				  $("#email1").val(data.email);
				 // $("#password1").val(data.password);
				  $("#phone1").val(data.phone);
				 },
                error:function()
                {
                    alert('fail');
                }
            });
   
   
   });
  
  
  
$(".update").click(function () {
$('form[id="edit_client"]').validate({
  rules: {
    first_name1: {
      required: true,
    },
	last_name1: {
      required: true,
    },
	email1: {
	 email : true,
      required: true,
    },
	
	phone1: {
      required: true,
    },
	
	},

  submitHandler: function(form) {
	var client_id   = $('#client_id').val();
	var data = $("#edit_client").serialize();
	$.ajax({                                      
				  url: '<?php echo base_url() ?>index.php/admin/update_client_details',      //the script to call to get data  
				  type:'post',
				 // dataType: 'json',
				  data: data,//you can insert url argumnets here to pass to api.php
					  success: function(data)          //on recieve of reply
					  {
						if(data == 1){
					 alert("Data Updated Successfully");
						location.reload();
						}
                                                 else if(data == 3){
                                            alert('Email is already exist, Please use other one');
                                        }
						
					  } 
				  });
  }
});
});


$(".delete").click(function(){
	
	var client_id= $(this).attr('del');
if (confirm("Do you really want to delete?")){
	   $.ajax({
                type: "POST",
                url: '<?php echo base_url() ?>index.php/admin/delete_client',
                data: {client_id:client_id},
                success:function(data)
                {
				    
                    alert('Data deleted successfully!!');
					location.reload();
					
                },
                error:function()
                {
                    alert('fail');
                }
            });
}
	
	
	});

 </script>
</body>
</html>
