<br />
<br />
<section class="content-header">
            <h1>Manage Hostel Rooms </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> Rooms 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 > BLOCK </th>
                                    <th > BED SPACE </th>



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

									<?php




                  $select= " SELECT hostel_rooms.id AS id ,hostel_rooms.title AS title, hostel_rooms.description AS description,
                  hostel_rooms.bed_space AS bed_space, hostel_campuses.title AS campus, hostel_blocks.title AS block,
                  hostel_rooms.date_added as date_added, hostel_rooms.status AS status, hostel_rooms.block_id,
                  hostel_rooms.reserved AS reserved, hostel_rooms.campus_id FROM
                  hostel_campuses,hostel_rooms, hostel_blocks WHERE hostel_rooms.campus_id= hostel_campuses.id
                  AND hostel_rooms.block_id= hostel_blocks.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_added'];
											$bed_space=$row['bed_space'];
											$campus=$row['campus'];
											$block=$row['block'];

                      $Block_status = $row['reserved'];
											if($Block_status=='1'){
														$status='<label class="alert alert-danger">Reserved</label>';
													}else{
														$status='<label class="alert alert-success">Active</label>';
													}

											echo $tr = '<tr>
														<td><center>'.$sn.'</center></td>
														<td>'.$title.'</td>
														<td>'.$description.'</td>
                            <td>'.$campus.'</td>
														<td>'.$block.'</td>
														<td>'.$bed_space.'</td>

														<td>'.$date_added.'</td>
														<td>'.$status.'</td>



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

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

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

				<div class="modal fade" id="edit_room" 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 Room</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 Room ?
                                </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 Room </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($con);
            														?>

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


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

                                                                            </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 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 Room </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_room.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("Room 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_room.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("Room 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_room',function(){
			var token=$(this).attr("id");
			$('#delete').modal('show');
			document.getElementById("delete_footer").innerHTML ='<a href="#" class="btn btn-danger" onclick="delete_room('+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_room',function(){
			var token=$(this).attr("id");
			$('#edit_room').modal('show');
			document.getElementById("edit_footer").innerHTML ='<a href="#" class="btn btn-danger" onclick="load_edit_room('+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_room(token){
			//alert(token);
		 	$.post("hub/php/delete_room.php",{token:token},
			function(response,status){

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

					  swal("Room Deleted");
					  links();


				}else{
					swal(response);
				}


			});
 }


 function load_edit_room(token){


			$('#edit_room').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_room.php',{token:token},
			function(response,status){

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

			});
}

</script>
