<br />
<br />
<section class="content-header">
            <h1>Manage Hostel Blocks </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> Block 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 > DESCRIPTION </th>

                                    <th> CAMPUS </th>

                                    <th > BED SPACE </th>
                                    <th > GENDER</th>
                                    <th > AMOUNT </th>

										                <th>Created At</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                    </thead>
                                    <tbody>

									<?php




                  $select= " SELECT hostel_blocks.id AS id ,hostel_blocks.title AS title, hostel_blocks.description AS description,
                  hostel_blocks.bed_space AS bed_space, hostel_campuses.title AS campus, hostel_blocks.gender AS gender,
                  hostel_blocks.hostel_amount AS amount, hostel_blocks.date_added as date, hostel_blocks.status AS status,
                  hostel_blocks.reserved AS reserved, hostel_blocks.campus_id FROM
                  hostel_campuses,hostel_blocks WHERE hostel_blocks.campus_id= hostel_campuses.id
                  AND hostel_blocks.status='1'  ORDER BY hostel_blocks.campus_id";
									$sql_select = mysqli_query($conn_1,$select);
									$num_rows=mysqli_num_rows($sql_select);
									if($num_rows > 0){
										$sn=1;
										while($row=mysqli_fetch_array($sql_select)){

											$id=$row['id'];
											$title=$row['title'];
											$description=$row['description'];
											$date_added=$row['date'];
											$bed_space=$row['bed_space'];
											$campus=$row['campus'];
											$gender=$row['gender'];
                      $amount = $row['amount'];
                      $Block_status = $row['reserved'];
											if($Block_status=='1'){
														$status='Reserved';
													}else{
														$status='Not Reserved';
													}

											echo $tr = '<tr>
														<td><center>'.$sn.'</center></td>
														<td>'.$title.'</td>
														<td>'.$description.'</td>
														<td>'.$campus.'</td>
														<td>'.$bed_space.'</td>
                            <td>'.$gender.'</td>
                            <td>'.$amount.'</td>
														<td>'.$date_added.'</td>
														<td>'.$status.'</td>



														<td><a href="#" class="edit_block" id="'.$id.'"><i
																class="fa fa-fw fa-pencil text-primary actions_icon " title="Edit Campus"></i></a>
																<a
																href="#"
																id="'.$id.'"
																data-toggle="modal"

																class="delete_block"
																><i
																class="fa fa-fw fa-times text-danger actions_icon" title="Delete Campus"></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 Block</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 Block?
                                </div>
                            </div>
                            <div class="modal-footer" id="delete_footer">

                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                </div>

				<div class="modal fade" id="edit_block" 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 Block</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 Block ?
                                </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 Block </span></h2>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <!-- Modal body -->
        <div class="modal-body">
		<form method="POST" id="form_faculty" >

                      <div class="form-group row">
            				<label for="category" class="col-sm-3 col-form-label text-right">Campus *</label>
                                                            <div class="col-sm-9">
                                                                <select class="form-control select21" id="school"
                                                                        title="Select School..." name="campus_id">
                                                                    <option disabled selected>Select Campus</option>
            														<?php
            														echo get_all_campus($conn_1);
            														?>

                                                                </select>
                                                            </div>
            											</div>

			 <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">Description : (*)
				</label>
					<div class="col-sm-9">
						<input id="description" name="description" type="text"
						placeholder="Description " class="form-control required" required />
					</div>
             </div>

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


			 <div class="form-group row">
				<label for="location" class="col-sm-3 col-form-label text-right">Gender
				</label>
					<div class="col-sm-9">
						<select class="form-control required" name="gender">
              <option value="">----select ----</option>
              <option value="male">Male</option>
              <option value="female">Female</option>
            </select>
					</div>
             </div>

			 <div class="form-group row">
				<label for="Phone Number" class="col-sm-3 col-form-label text-right">Amount
				</label>
					<div class="col-sm-9">
						<input id="text" name="amount" type="text"
						placeholder="Amount " class="form-control required"  />
					</div>
             </div>
							<div class="form-group row">
												<label class="col-sm-3 col-form-label text-right"> Added By</label>
									<div class="col-sm-9">
                    <input id="text" name="added_by" type="text"
        						placeholder="Added By " class="form-control required"  />
									</div>
							</div>

              <div class="form-group row">
       				<label for="Phone Number" class="col-sm-3 col-form-label text-right">Status
       				</label>
       					<div class="col-sm-9">
       						<select class="form-control required" name="status" id="status">
                     <option value="">-----Select ----</option>
                     <option value="0">Reserved</option>
                     <option value="1">Active</option>
                     <option value="1">Block</option>

                   </select>
       					</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 Block </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_faculty" >

							</form>

                            </div>
                            <div class="modal-footer" id="edit_footer">

                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                </div>

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



		$("#form_faculty").submit(function(event){
			event.preventDefault(); //prevent default action
			var post_url = "hub/php/add_block.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_faculty')[0].reset();
						swal("Block 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_faculty").submit(function(event){

			event.preventDefault(); //prevent default action
			var post_url = "hub/php/update_block.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_faculty')[0].reset();
						swal("Block 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 faculty modal
		$(document).on('click','.delete_block',function(){
			var token=$(this).attr("id");
			$('#delete').modal('show');
			document.getElementById("delete_footer").innerHTML ='<a href="#" class="btn btn-danger" onclick="delete_block('+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 faculty end

		// load edit faculty start modal
		$(document).on('click','.edit_block',function(){
			var token=$(this).attr("id");
			$('#edit_block').modal('show');
			document.getElementById("edit_footer").innerHTML ='<a href="#" class="btn btn-danger" onclick="load_edit_block('+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 faculty end




});

function links(){
	location.reload();
 }


  function delete_block(token){
			//alert(token);
		 	$.post("hub/php/delete_block.php",{token:token},
			function(response,status){

				if(response == 1 ){
					$('#delete').modal('hide');

					  swal("Block Deleted");
					  links();


				}else{
					swal(response);
				}


			});
 }


 function load_edit_block(token){


			$('#edit_block').modal('hide');
			$('#edit_loading').modal('show');

			document.getElementById("edit_form_faculty").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_block.php',{token:token},
			function(response,status){

			document.getElementById("edit_form_faculty").innerHTML =response;

			});
}

</script>
