<br />
<br />
<section class="content-header">
            <h1>Manage School </h1>
            <ol class="breadcrumb">
                <li class="breadcrumb-item pt-1"><a href="dashboard"><i class="fa fa-fw fa-home"></i> Dashboard</a>
                </li>
                
            </ol>
        </section>
        <!-- Main content -->
        <section class="content p-l-r-15">
            <div class="row">
                <div class="col-12">
                    <div class="card border-primary ">
                        <div class="card-header text-white  bg-primary">
                            <h4 class="card-title d-inline">
                                <i class="fa fa-fw fa-News"></i> School List
                            </h4>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive table-responisve-lg table-responsive-xl">
                                <table class="table table-bordered" id="table">
                                    <thead>
                                    <tr class="filters">
                                        <th width="5%"><a href="#" class="button button-3d button-primary button-rounded btn_3d" data-toggle="modal" data-target="#myModal">+
											</a></th>
                                        
                                   
                                    <th > TITTLE </th>
                                    
                                    <th > CODE </th>
									
                                    <th> EMAIL </th>
									
                                    <th > PHONE NUMBER </th>
										<th>Created At</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                    </thead>
                                    <tbody>
									
									<?php
									
							
									
									
									$select="SELECT * FROM schools WHERE school_status ='1'";
									$sql_select = mysqli_query($con,$select);
									$num_rows=mysqli_num_rows($sql_select);
									if($num_rows > 0){
										$sn=1;
										while($row=mysqli_fetch_array($sql_select)){
											
											$school_id=$row['school_id'];
											$title=$row['title'];
											$code=$row['code'];
											$email=$row['email'];
											$phone_contacts=$row['phone_contacts'];
											$date_added=$row['date_added'];
											$school_status=$row['school_status'];
											if($school_status=='0'){
														$status='Delete';
													}else if($school_status=="1"){
														$status='Active';
													}else{
														$status='Suspend';
													}
											
											echo $tr = '<tr>
														<td><center>'.$sn.'</center></td>
														<td>'.$title.'</td>
														<td>'.$code.'</td>
														<td>'.$email.'</td>
														
														<td>'.$phone_contacts.'</td>
														<td>'.$date_added.'</td>
														<td>'.$status.'</td>
														
														
														
														<td><a href="#" class="edit_school" id="'.$school_id.'"><i
																class="fa fa-fw fa-pencil text-primary actions_icon " title="Edit School"></i></a>
																<a
																href="#"
																id="'.$school_id.'"
																data-toggle="modal"
																
																class="delete_school"
																><i
																class="fa fa-fw fa-times text-danger actions_icon" title="Delete School"></i>
														</a></td>
																	
													</tr>';
													$sn++;
											
										}
									}
								
									?>
                                    </tbody>
                                </table>
								
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="Heading"
                     aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title custom_align" id="Heading">Delete School</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>
                            <div class="modal-body">
                                <div class="alert alert-warning">
                                    <span class="fa fa-exclamation-triangle"></span> Are you sure you want to
                                    delete this School?
                                </div>
                            </div>
                            <div class="modal-footer" id="delete_footer">
                                
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                </div>
				
				<div class="modal fade" id="edit_school" tabindex="-1" role="dialog" aria-labelledby="Heading"
                     aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title custom_align" id="Heading">Edit School</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>
                            <div class="modal-body">
                                <div class="alert alert-warning">
                                    <span class="fa fa-exclamation-triangle"></span> Are you sure you want to
                                    edit this School ?
                                </div>
                            </div>
                            <div class="modal-footer" id="edit_footer">
                                
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                </div>
				
				
            </div>
            <!-- row-->
            
        </section>
		
 <!-- The Modal -->
  <div class="modal fade" id="myModal">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h2 class="modal-title"><span class="badge badge-pill badge-danger"> ADD SCHOOL </span></h2>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
		<form method="POST" id="form_school" >
          <div class="form-group row">
				<label for="title" class="col-sm-3 col-form-label text-right">Title : (*)
				</label>
					<div class="col-sm-9">
						<input id="title" name="title" type="text"
						placeholder="Title " class="form-control required" required />
					</div>
             </div>
			 
			 <div class="form-group row">
				<label for="code" class="col-sm-3 col-form-label text-right">Code : (*)
				</label>
					<div class="col-sm-9">
						<input id="Code" name="code" type="text"
						placeholder="Code " class="form-control required" required />
					</div>
             </div>

			 <div class="form-group row">
				<label for="email" class="col-sm-3 col-form-label text-right">Email 
				</label>
					<div class="col-sm-9">
						<input id="email" name="email" type="text"
						placeholder="email " class="form-control required" />
					</div>
             </div>
			 
			 
			 <div class="form-group row">
				<label for="location" class="col-sm-3 col-form-label text-right">Location 
				</label>
					<div class="col-sm-9">
						<input id="text" name="location" type="text"
						placeholder="Location " class="form-control required"  />
					</div>
             </div>

			 <div class="form-group row">
				<label for="Phone Number" class="col-sm-3 col-form-label text-right">Phone Number 
				</label>
					<div class="col-sm-9">
						<input id="text" name="pn" type="text"
						placeholder="Phone Number " class="form-control required"  />
					</div>
             </div>
							<div class="form-group row">
												<label class="col-sm-3 col-form-label text-right"> Description</label>
									<div class="col-sm-9">
										<textarea class="form-control" rows="2" name="description" id="comment"></textarea>
									</div> 
							</div>
											
				
			 
						<div id="button">
							<center>
														<button class="button button-3d button-primary button-rounded btn_3d" onclick="add_sub_cat()">Save
												</button>
						</div>
             </form>               
						
						
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
			<center>
				<div id="output_error">
				
				</div>
			</center>
			
        </div>
        
      </div>
    </div>
</div>



<div class="modal fade" id="edit_loading" tabindex="-1" role="dialog" aria-labelledby="Heading"
                     aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h2 class="modal-title custom_align" id="Heading"> <span class="badge badge-pill badge-danger"> Editing School </span> </h2>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>
                            <div class="modal-body" id="edit_modal_body">
							<form method="POST" id="edit_form_school" >
							
							</form>
                                
                            </div>
                            <div class="modal-footer" id="edit_footer">
                                
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                </div>

<script type="text/javascript">
$(document).ready(function(e){



		$("#form_school").submit(function(event){
			event.preventDefault(); //prevent default action
			var post_url = "hub/php/add_school.php"; //get form action url
			var request_method = $(this).attr("method"); //get form GET/POST method

			$("#output_error").html('<div style="margin-top:10px"><center>login, please wait...<br/><img src="images/ajax-loader1.gif" width="20%"/></center></div>');
			document.getElementById("button").style.display = "none";
			var form_data = new FormData(this); //Creates new FormData object


				$.ajax({
					url : post_url,
					type: request_method,
					data : form_data,
					contentType: false,
					cache: false,
					processData:false
				}).done(function(response){ //

					if(response==1){
					 
						$('#form_school')[0].reset();
						swal("School saved successfully");
						document.getElementById("button").style.display = "block";
						$("#output_error").html("");
						links();
						
					}else{

					$("#output_error").html(response);
					document.getElementById("button").style.display = "block";

					}
				});

		});
		
		
		
		
		$("#edit_form_school").submit(function(event){
			
			event.preventDefault(); //prevent default action
			var post_url = "hub/php/update_school.php"; //get form action url
			var request_method = $(this).attr("method"); //get form GET/POST method

			$("#output_error_edit").html('<div style="margin-top:10px"><center>login, please wait...<br/><img src="images/ajax-loader1.gif" width="20%"/></center></div>');
			document.getElementById("button_edit").style.display = "none";
			var form_data = new FormData(this); //Creates new FormData object


				$.ajax({
					url : post_url,
					type: request_method,
					data : form_data,
					contentType: false,
					cache: false,
					processData:false
				}).done(function(response){ //

					if(response==1){
					 
						$('#edit_form_school')[0].reset();
						swal("School Updated successfully");
						document.getElementById("button_edit").style.display = "block";
						$("#output_error_edit").html("");
						links();
						
					}else{

					$("#output_error_edit").html(response);
					document.getElementById("button_edit").style.display = "block";

					}
				});

		});
		
		
		
		
		
		
		
		// load delete school modal
		$(document).on('click','.delete_school',function(){
			var token=$(this).attr("id");
			$('#delete').modal('show');
			document.getElementById("delete_footer").innerHTML ='<a href="#" class="btn btn-danger" onclick="delete_school('+token+')"><span class="fa fa-check"></span> Yes</a><button type="button" class="btn btn-success" data-dismiss="modal"><span class="fa fa-close"></span> No</button>';
			
			
		});
		// delete school end
		
		// load edit school start modal
		$(document).on('click','.edit_school',function(){
			var token=$(this).attr("id");
			$('#edit_school').modal('show');
			document.getElementById("edit_footer").innerHTML ='<a href="#" class="btn btn-danger" onclick="load_edit_school('+token+')"><span class="fa fa-check"></span> Yes</a><button type="button" class="btn btn-success" data-dismiss="modal"><span class="fa fa-close"></span> No</button>';
		
			
		});
		// edit school end
		
		
		
		
});

function links(){
	location.reload();
 }
 
 
  function delete_school(token){
			//alert(token);
		 	$.post("hub/php/delete_school.php",{token:token},
			function(response,status){
				
				if(response == 1 ){
					$('#delete').modal('hide');
					 
					  swal("School Deleted");
					  links();
					 
					
				}else{
					swal(response);
				}
			
			 
			});
 }
 
 
 function load_edit_school(token){
	
		
			$('#edit_school').modal('hide');
			$('#edit_loading').modal('show');
			
			document.getElementById("edit_form_school").innerHTML ='<div style="margin-top:10px"><center>loading Data, please wait...<br/><img src="images/ajax-loader1.gif" width="10%"/></center></div>';
	
			$.post('hub/php/load_edit_school.php',{token:token},
			function(response,status){
			
			document.getElementById("edit_form_school").innerHTML =response;
		
			});
}
 
</script>
