<style>
   .error {
        color:red !important;
    }
</style>
<div class="wraper responsive-width">
    <main class="long-tbl-main" id="main">
        <div class="displaydata pt-5 form-group_lng" style="display:flex">
            <div class="col-md-6 pull-left" style="padding:0">
                <!-- <a href="<?php // echo base_url(); ?>admin/classroom" class="pull-left"><button type="button" class="btn subject_addbtn bg-dark">Back</button></a> -->
            </div>
         <?php
        $search_batchid=(!empty($search_batch_id)) ? $search_batch_id :'';
        $search_classroomid=(!empty($search_classroom_id)) ? $search_classroom_id :'';

        ?>
        </div>
        <div class="admin_tempblock">
            <div class="admin_tempsec">
                <div class="admin_sec">
                    <div class="subsec_sec">
                        <div class="subject_r subject_r_lng">
                            <div class="subject_lsec">
                                <div class="subject_lhead"><?php echo $title; ?> <p id="success" style="color:green;"></p>
                                </div>

                                <div class="bootstrap_alert">

                                </div>

                                

                                <form method="post" action="<?php echo base_url(); ?>admin/assign-to-hostel">
                                     
                                     <div class="filter_lbl">
                                         <i class="fas fa-filter"></i>
                                         Filter
                                     </div>
                                   
                                     <div class="row">
                                         <div class="col-md-4">
                                             <div class="form-group">
                                             <label class="mb-0" style="font-size: 13px;">Batch</label>
 
                                            <select id="search_batch" class="form-control" placeholder="Select Batch"
                                             name="search_batch">
                                                    <option value=""> Select Batch</option>
                                                    <?php 
                                                    if(isset($batchs) && !empty($batchs)){
                                                    foreach($batchs as $bvalue){ 
                                                        if($search_batchid == $bvalue['id'])
                                                            $bchecked='selected';
                                                        else 
                                                            $bchecked=''; 
                                                        ?>
                                                    <option value="<?php echo $bvalue['id']; ?>" <?=$bchecked; ?>><?php echo $bvalue['b_name']; ?></option>
                                                    <?php } } ?>
                                                    
                                            </select>
                                            <span class="error">
                                            <?php echo form_error('search_batch', '<div class="error">', '</div>'); ?>
                                        </span>
                                            </div>
                                         </div>
                                         <div class="col-md-4">
                                             <div class="form-group">
                                             <label class="mb-0" style="font-size: 13px;">Classroom</label>
                                                 <select id="search_classroom" class="form-control" placeholder="Select Classroom"
                                             name="search_classroom">
                                                     <option value=""> Select Classroom</option>
                                                     <?php 
                                                    if(isset($classrooms) && !empty($classrooms)){
                                                    foreach($classrooms as $clvalue){
                                                        if($search_classroomid == $clvalue['id'])
                                                        $clchecked='selected';
                                                    else 
                                                        $clchecked=''; 
                                                        ?>
                                                    <option value="<?php echo $clvalue['id']; ?>" <?=$clchecked; ?> ><?php echo $clvalue['classroom_name']; ?></option>
                                                    <?php } } ?>
                                                 </select>
                                             </div>
                                         </div>
                                       
                                         <div class="col-md-4">
                                             <div class="form-group" style="margin-top: 1.5rem;">
                                                 <input type="submit" name="filterSubmit" id="filter_reset"
                                             class="btn btn-success" value="Filter">
                                                 <a href="<?php echo base_url() ?>admin/assign-routes"><button type="button"
                                                         name="reset_filter" id="reset_filter" value="reset_filter"
                                                         class="subject_addbtn subject_addbtn_nobg"><i
                                                             class="fas fa-redo"></i>Reset</button></a>
                                             </div>
                                         </div>

                                     </div>                                        
                                     
                             </form>

                         
                                <!--<div class="view_del-subjects"><a href="<?= base_url(); ?>admin/classroom_deleted">View Deleted Items</a></div>-->
                                <table id="tbl" class="display table-responsive">
                                    <thead>
                                        <tr>

                                            <th>Sl.No</th>
                                            <th>Student Id</th>
                                            <th>Student Name</th>
                                         
                                            <th>Action</th>
                                           
                                             
                                            
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if(isset($studentlist) && !empty($studentlist) ){ 
                                            $sno=1;
                                            foreach($studentlist as $student){
                                            ?>
                                    <tr>
                                        <td><?=  $sno; ?></td>
                                        <td><?= $student['studentId']; ?></td>
                                        <td><?= $student['name']; ?></td>
                                    
                                        <td>
                                        <?php if(lcfirst($student['hostel']) == 'yes' && $student['hostel_plan_id'] != 0 ){ ?>
                                          
                                            <button class="btn btn-success btn-xs route_modal" style="padding:6px !important;" data-id="<?php echo $student['id']; ?>"> <i class="fas fa-check-circle "></i> Re-Assign</button>
                                            <?php } else { ?>
                                               <button class="btn btn-primary btn-xs route_modal" style="padding:6px !importants;" data-id="<?php echo $student['id']; ?>">Assign</button>
                                             <?php } ?> 
                                        </td>
                                     
                                    </tr>
                                    <?php 
                               $sno++; }
                                } ?>
                                    </tbody>

                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>

<!-- modal start -->
<!-- Modal -->
<div class="modal fade " id="HostelDetailModal" tabindex="-1"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
 
    <div class="modal-content">
    
        <div class="routes_body">

        
        </div>
        <div class="modal-footer">
        <button type="button" class="btn btn-primary" id="assign_route_btn">Assign</button>
      </div>
        <input type="hidden" id="student_id" name="student_id" class="form-control" />
    
    </div>
  </div>
</div>
<!-- modal end -->

<script>
$(document).ready(function(){
    $('#tbl').DataTable({
            "lengthMenu": [
                [25, 50, 100, 150, -1],
                [25, 50, 100, 150, "All"]
            ]
        });
        // $('#tbl1').DataTable({
        //     "lengthMenu": [
        //         [25, 50, 100, 150, -1],
        //         [25, 50, 100, 150, "All"]
        //     ]
        // });
});

$(".route_modal").click(function(){
    var studentid=$(this).data('id');
    var batchid= "<?php echo $search_batchid; ?>";
    var divdata='';
    if(batchid !=''){
        $.ajax({
        url:"<?php echo base_url(); ?>admin/gethostelDetails",
        method:"POST",
        data:{
            batch_id:batchid,
            student_id:studentid
        },
        success: function(htmldata){
            $('#student_id').val(studentid);
            $(".routes_body").html(htmldata);
            $('#HostelDetailModal').modal({
                    backdrop:'static',
                    keyboard:false,
                    show:true
            });
            let assignBtn=$('#modalStudentPayment').data('payment-status');
            if(assignBtn === 1){
                $("#assign_route_btn").addClass("d-none");
            }
            else {
                $("#assign_route_btn").removeClass("d-none");
            }
     
        }
    });
    }
    else {
        alert("Please Select Batch");
    }
});



$("#assign_route_btn").click(function(){
var selectedVal = "";
var alertsection='';
var selected = $("input[type='radio'][name='hostel_plan_id']:checked");
if (selected.length > 0) {
    selectedVal = selected.val();
    var studentid1=$('#student_id').val();
    if(studentid1 != ''){
        $.ajax({
        url:"<?php echo base_url(); ?>admin/hostelAssignToStudent",
        method:"POST",
        data:{
            hostel_plan_id:selectedVal,
            student_id:studentid1
        },
        success: function(result){
            $('#HostelDetailModal').modal("hide");
            if(result == 1){
                swal({
                    title: "Good job", 
                    text: "Hostel has been assigned successfully!", 
                    type: "success"
                },
                function(){ 
                location.reload();
                });
                // $(".bootstrap_alert").html('<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">&times;</a><strong>Success!</strong> Route Assigned Successfully </div>');
            }
            else {
                swal({
                    title: "Sorry", 
                    text: "Hostel assigning failed!", 
                    type: "error",
                },
                function(){ 
                location.reload();
                });
                // $(".bootstrap_alert").html('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert">&times;</a><strong>Danger!</strong> Route Assiging Failed </div>');
            }
        }
        });
    }
    else 
    {
        alert("Student Id : "+studentid);
    }
}
else {
    alert("Please Select Route");
}
})



</script>