<?php
	//connect to db
	require_once("sass/connection.php");

	//if insert key is pressed then do insertion
	if($_POST['action'] == 'insert'){

		$emp_fname  = mysqli_real_escape_string($connection,$_POST['emp_fname']);
		$emp_mname = mysqli_real_escape_string($connection,$_POST['emp_mname']);
		$emp_lname = mysqli_real_escape_string($connection,$_POST['emp_lname']);
		$emp_email = mysqli_real_escape_string($connection,$_POST['emp_email']);
		$emp_contact = mysqli_real_escape_string($connection,$_POST['emp_contact']);
		$emp_designation = mysqli_real_escape_string($connection,$_POST['emp_designation']);
		$emp_gender = mysqli_real_escape_string($connection,$_POST['emp_gender']);
		$emp_country = mysqli_real_escape_string($connection,$_POST['emp_country']);
		$emp_comments = mysqli_real_escape_string($connection,$_POST['emp_comments']);
		
		$sql   = "insert into employee (emp_fname, emp_mname, emp_lname, emp_email, emp_contact, emp_designation, emp_gender, emp_country, emp_comments) values ('$emp_fname', '$emp_mname', '$emp_lname', '$emp_email', '$emp_contact', '$emp_designation', '$emp_gender', '$emp_country', '$emp_comments')";
		$query = mysqli_query($connection,$sql);
		 $used_id = mysqli_insert_id();
		if($query){
		echo "<div class='alert alert-success alert-dismissible' role='alert'>Successfully Inserted !";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";
			echo "<table id='example1' class='table table-bordered table-striped'>
                                        <thead>
                                            <tr>
                                               <th>First Name</th>
                                                <th>Middle Name</th> 
                                                <th>Last name</th>                    
                                                <th>Email</th>                    
                                                <th>Contact No</th>                    
                                                <th>Designation</th>                    
                                                <th>Gender</th>                    
                                                <th>Country</th>                    
                                                <th>Comments</th>                                  
                                                <th>Action</th>
                                            </tr>

                                        </thead>
                                        <tbody>"; 
		 $query2 = "SELECT * FROM employee";
                                                $result = mysqli_query($connection,$query2);
                                                
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                 $i++;                     
										 $employee_id = $row['employee_id'];
											$emp_fname = $row['emp_fname'];
											$emp_mname = $row['emp_mname'];
											$emp_lname = $row['emp_lname'];
											$emp_email = $row['emp_email'];
											$emp_contact = $row['emp_contact'];
											$emp_designation = $row['emp_designation'];
											$emp_gender = $row['emp_gender'];
											$emp_country = $row['emp_country'];
											$emp_comments = $row['emp_comments'];
											
										
                                        
                                        
                                        echo "<tr>
                                        <td>$emp_fname
										<input type='hidden' id='emp_fname$i' value='$emp_fname'/>
										</td>
                                        <td>$emp_mname
										<input type='hidden' id='emp_mname$i' value='$emp_mname'/>
										</td>
                                        <td>$emp_lname
										<input type='hidden' id='emp_lname$i' value='$emp_lname'/>
										</td>
                                        <td>$emp_email
										<input type='hidden' id='emp_email$i' value='$emp_email'/>
										</td>
                                        <td>$emp_contact
										<input type='hidden' id='emp_contact$i' value='$emp_contact'/>
										</td>
                                        <td>$emp_designation
										<input type='hidden' id='emp_designation$i' value='$emp_designation'/>
										</td>
                                        <td>$emp_gender
										<input type='hidden' id='emp_gender$i' value='$emp_gender'/>
										</td>
                                        <td>$emp_country
										<input type='hidden' id='emp_country$i' value='$emp_country'/>
										</td>
                                        <td>$emp_comments
										<input type='hidden' id='emp_comments$i' value='$emp_comments'/>
										</td>
                                        <td>
                                       <input type='hidden' id='zipsearch$i' value='$employee_id'/> <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-edit'></i> &nbsp;Edit</button>
									   
									   <button type='button' class='btn btn-danger' data-toggle='modal' data-target='#delmodel$i'><i class='fa fa-times'></i> &nbsp;Delete</button>
                                        </td> 
                                        </tr>";
										
										
echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  
  
  var emp_fnamecollect = document.getElementById('emp_fname$i');
  var emp_fnameentry = document.getElementById('emp_fname_Last');
  
  
  var emp_mnamecollect = document.getElementById('emp_mname$i');
  var emp_mnameentry = document.getElementById('emp_mname_Last');
  
  
  var emp_lnamecollect = document.getElementById('emp_lname$i');
  var emp_lnameentry = document.getElementById('emp_lname_Last');
  
  
  var emp_emailcollect = document.getElementById('emp_email$i');
  var emp_emailentry = document.getElementById('emp_email_Last');
  
  
  var emp_contactcollect = document.getElementById('emp_contact$i');
  var emp_contactentry = document.getElementById('emp_contact_Last');
  
  
  var emp_designationcollect = document.getElementById('emp_designation$i');
  var emp_designationentry = document.getElementById('emp_designation_Last');
  
  
  var emp_gendercollect = document.getElementById('emp_gender$i');
  var emp_genderentry = document.getElementById('emp_gender_Last');
  
  
  var emp_countrycollect = document.getElementById('emp_country$i');
  var emp_countryentry = document.getElementById('emp_country_Last');
  
  
  var emp_commentscollect = document.getElementById('emp_comments$i');
  var emp_commentsentry = document.getElementById('emp_comments_Last');
  
  
  
  
  entry.value = collect.value;
  emp_fnameentry.value = emp_fnamecollect.value;
  emp_mnameentry.value = emp_mnamecollect.value;
  emp_lnameentry.value = emp_lnamecollect.value;
  emp_emailentry.value = emp_emailcollect.value;
  emp_contactentry.value = emp_contactcollect.value;
  emp_designationentry.value = emp_designationcollect.value;
  emp_genderentry.value = emp_gendercollect.value;
  
  
  emp_countryentry.value = emp_countrycollect.value;
  
  emp_commentsentry.value = emp_commentscollect.value;
  
   
}
</script> ";

echo "<div class='modal fade' id='delmodel$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Delete Package</h4>
      </div>
      <div class='modal-body'>
    Are You sure you want to delete  $emp_fname ?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='delete.php?tbl=employee&idcol=employee_id&id=$employee_id&redirect=ajax_main'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";
												
												}
												echo "</tbody>
                                        <tfoot>
                                            <tr>
                                                <th>First Name</th>
                                                <th>Middle Name</th> 
                                                <th>Last name</th>                    
                                                <th>Email</th>                    
                                                <th>Contact No</th>                    
                                                <th>Designation</th>                    
                                                <th>Gender</th>                    
                                                <th>Country</th>                    
                                                <th>Comments</th>                                  
                                                <th>Action</th>
                                            </tr>

                                        </tfoot>
                                    </table>";
									
								echo "<script src='js/plugins/datatables/jquery.dataTables.js' type='text/javascript'></script>
        <script src='js/plugins/datatables/dataTables.bootstrap.js' type='text/javascript'></script>
        
        <script type='text/javascript'>
            $(function() {
                $('#example1').dataTable();
                $('#example2').dataTable({
                    'bPaginate': true,
                    'bLengthChange': false,
                    'bFilter': false,
                    'bSort': true,
                    'bInfo': true,
                    'bAutoWidth': false
                });
            });
        </script>";	
									
									
		}else {
			echo "Something Wrong!";
		}
	}
	//if show key is pressed show records
	if($_POST['action'] == 'update'){
	$employee_id  = mysqli_real_escape_string($connection,$_POST['employee_id']);
	$emp_fname  = mysqli_real_escape_string($connection,$_POST['emp_fname']);
		$emp_mname = mysqli_real_escape_string($connection,$_POST['emp_mname']);
		$emp_lname = mysqli_real_escape_string($connection,$_POST['emp_lname']);
		$emp_email = mysqli_real_escape_string($connection,$_POST['emp_email']);
		$emp_contact = mysqli_real_escape_string($connection,$_POST['emp_contact']);
		$emp_designation = mysqli_real_escape_string($connection,$_POST['emp_designation']);
		$emp_gender = mysqli_real_escape_string($connection,$_POST['emp_gender']);
		$emp_country = mysqli_real_escape_string($connection,$_POST['emp_country']);
		$emp_comments = mysqli_real_escape_string($connection,$_POST['emp_comments']);
		$sql   = "UPDATE employee SET emp_fname ='$emp_fname', emp_mname ='$emp_mname', emp_lname ='$emp_lname' 
, emp_email ='$emp_email', emp_contact ='$emp_contact', emp_designation ='$emp_designation', emp_gender ='$emp_gender', emp_country ='$emp_country', emp_comments ='$emp_comments' WHERE employee_id = '$employee_id'";
		$query = mysqli_query($connection,$sql);
		
		
		if($query){
		echo "<div class='alert alert-success alert-dismissible' role='alert'>Successfully Updated !";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";
			echo "<table id='example1' class='table table-bordered table-striped'>
                                        <thead>
                                            <tr>
                                               <th>First Name</th>
                                                <th>Middle Name</th> 
                                                <th>Last name</th>                    
                                                <th>Email</th>                    
                                                <th>Contact No</th>                    
                                                <th>Designation</th>                    
                                                <th>Gender</th>                    
                                                <th>Country</th>                    
                                                <th>Comments</th>                                  
                                                <th>Action</th>
                                            </tr>

                                        </thead>
                                        <tbody>"; 
		 $query2 = "SELECT * FROM employee";
                                                $result = mysqli_query($connection,$query2);
                                                
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                 $i++;                     
										 $employee_id = $row['employee_id'];
											$emp_fname = $row['emp_fname'];
											$emp_mname = $row['emp_mname'];
											$emp_lname = $row['emp_lname'];
											$emp_email = $row['emp_email'];
											$emp_contact = $row['emp_contact'];
											$emp_designation = $row['emp_designation'];
											$emp_gender = $row['emp_gender'];
											$emp_country = $row['emp_country'];
											$emp_comments = $row['emp_comments'];
											
										
                                        
                                        
                                        echo "<tr>
                                        <td>$emp_fname
										<input type='hidden' id='emp_fname$i' value='$emp_fname'/>
										</td>
                                        <td>$emp_mname
										<input type='hidden' id='emp_mname$i' value='$emp_mname'/>
										</td>
                                        <td>$emp_lname
										<input type='hidden' id='emp_lname$i' value='$emp_lname'/>
										</td>
                                        <td>$emp_email
										<input type='hidden' id='emp_email$i' value='$emp_email'/>
										</td>
                                        <td>$emp_contact
										<input type='hidden' id='emp_contact$i' value='$emp_contact'/>
										</td>
                                        <td>$emp_designation
										<input type='hidden' id='emp_designation$i' value='$emp_designation'/>
										</td>
                                        <td>$emp_gender
										<input type='hidden' id='emp_gender$i' value='$emp_gender'/>
										</td>
                                        <td>$emp_country
										<input type='hidden' id='emp_country$i' value='$emp_country'/>
										</td>
                                        <td>$emp_comments
										<input type='hidden' id='emp_comments$i' value='$emp_comments'/>
										</td>
                                        <td>
                                       <input type='hidden' id='zipsearch$i' value='$employee_id'/> <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-edit'></i> &nbsp;Edit</button>
									   
									   <button type='button' class='btn btn-danger' data-toggle='modal' data-target='#delmodel$i'><i class='fa fa-times'></i> &nbsp;Delete</button>
                                        </td> 
                                        </tr>";
										
										
echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  
  
  var emp_fnamecollect = document.getElementById('emp_fname$i');
  var emp_fnameentry = document.getElementById('emp_fname_Last');
  
  
  var emp_mnamecollect = document.getElementById('emp_mname$i');
  var emp_mnameentry = document.getElementById('emp_mname_Last');
  
  
  var emp_lnamecollect = document.getElementById('emp_lname$i');
  var emp_lnameentry = document.getElementById('emp_lname_Last');
  
  
  var emp_emailcollect = document.getElementById('emp_email$i');
  var emp_emailentry = document.getElementById('emp_email_Last');
  
  
  var emp_contactcollect = document.getElementById('emp_contact$i');
  var emp_contactentry = document.getElementById('emp_contact_Last');
  
  
  var emp_designationcollect = document.getElementById('emp_designation$i');
  var emp_designationentry = document.getElementById('emp_designation_Last');
  
  
  var emp_gendercollect = document.getElementById('emp_gender$i');
  var emp_genderentry = document.getElementById('emp_gender_Last');
  
  
  var emp_countrycollect = document.getElementById('emp_country$i');
  var emp_countryentry = document.getElementById('emp_country_Last');
  
  
  var emp_commentscollect = document.getElementById('emp_comments$i');
  var emp_commentsentry = document.getElementById('emp_comments_Last');
  
  
  
  
  entry.value = collect.value;
  emp_fnameentry.value = emp_fnamecollect.value;
  emp_mnameentry.value = emp_mnamecollect.value;
  emp_lnameentry.value = emp_lnamecollect.value;
  emp_emailentry.value = emp_emailcollect.value;
  emp_contactentry.value = emp_contactcollect.value;
  emp_designationentry.value = emp_designationcollect.value;
  emp_genderentry.value = emp_gendercollect.value;
  
  
  emp_countryentry.value = emp_countrycollect.value;
  
  emp_commentsentry.value = emp_commentscollect.value;
  
   
}
</script> ";

echo "<div class='modal fade' id='delmodel$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>Delete Package</h4>
      </div>
      <div class='modal-body'>
    Are You sure you want to delete  $emp_fname ?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='delete.php?tbl=employee&idcol=employee_id&id=$employee_id&redirect=ajax_main'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";
												
												}
												echo "</tbody>
                                        <tfoot>
                                            <tr>
                                                <th>First Name</th>
                                                <th>Middle Name</th> 
                                                <th>Last name</th>                    
                                                <th>Email</th>                    
                                                <th>Contact No</th>                    
                                                <th>Designation</th>                    
                                                <th>Gender</th>                    
                                                <th>Country</th>                    
                                                <th>Comments</th>                                  
                                                <th>Action</th>
                                            </tr>

                                        </tfoot>
                                    </table>";
						
echo "<script src='js/plugins/datatables/jquery.dataTables.js' type='text/javascript'></script>
        <script src='js/plugins/datatables/dataTables.bootstrap.js' type='text/javascript'></script>
        
        <script type='text/javascript'>
            $(function() {
                $('#example1').dataTable();
                $('#example2').dataTable({
                    'bPaginate': true,
                    'bLengthChange': false,
                    'bFilter': false,
                    'bSort': true,
                    'bInfo': true,
                    'bAutoWidth': false
                });
            });
        </script>";

								 		
									
									
									
		}else {
			echo "Something Wrong!";
		}
		
		
	}
?>