<style>
     .action_subject_addbtn {
         background: #032DA4;
         border-radius: 35px;
         color: #fff;
         padding: 5px 15px;
         border: none;
         font-size: 12px;
         cursor: pointer;
         text-decoration: none;
     }

     form label.error, #secNameerr {
        color : red;
    }
    .cierr {
        font-size:14px;
    }

    .form-group .bootstrap-select > .dropdown-toggle {
        padding: 9px 9px !important;
        font-size: 12px !important;
        border: 1px solid #c8c8c8 !important;
        background: #fff !important;
    }
 </style>
    <div class="wraper responsive-width w-90">  
        <main class="" id="main">
            
            <div class="admin_tempblock">
                <div class="admin_tempsec">
                    <div class="admin_sec">
                        <div class="subsec_sec">
                            <div class="subject_r subject_r_lng">
                                <form method="post" action="<?php echo base_url(); ?>admin/assign-additional-subjects" id="assignAdditionalSubToStudentsForm">
                                <input type="hidden" name="clicked_on" id="clicked_on" value="Filter">
                                <div class="subject_lsec">
                                    <div class="subject_lhead">
                                        Assign Additional Subjects To The Students
                                        <p id="success" style="color:green;"></p>
                                    </div>

                                    <?php
                                    $batchId=''; $courseId=''; $sectionIds='';
                                    if(count($_POST)>0)
                                    {
                                        $batchId = $_POST['filter_by_batch'];
                                        $courseId = $_POST['filter_by_course'];

                                        if(isset($_POST['filter_by_section']) && !empty($_POST['filter_by_section']))
                                            $sectionIds = implode(',',$_POST['filter_by_section']);
                                    }
                                    ?>                                
                                    
                                        <div class="filter_lbl">
                                             <i class="fas fa-filter"></i>
                                             Filter
                                         </div>
                                         
                                         <div class="row">
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="mb-0" style="font-size: 13px;">
                                                        Batch <span style="font-weight:bold;color:red;"> * </span>
                                                    </label>
                                                    <select class="form-control" name="filter_by_batch" id="filter_by_batch" onchange="batch_is_changed()">
                                                    <option value="">Select Batch</option>
                                                    <?php
                                                    if (isset($batch) && !empty($batch))
                                                    {
                                                        foreach ($batch as $row)
                                                        {
                                                    ?>
                                                         <option value="<?php echo $row['id'] ?>" <?php if($row['id']==$batchId){?> selected <?php } ?> >
                                                             <?php echo $row['b_name']; ?>
                                                        </option>
                                                    <?php
                                                        }
                                                    }
                                                    ?>
                                                    </select>
                                                    <div id='err_filter_by_batch' class="text-danger cierr"><?php echo form_error('filter_by_batch'); ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="mb-0" style="font-size: 13px;">
                                                        Course <span style="font-weight:bold;color:red;"> * </span>
                                                    </label>                                         
                                                    <select class="form-control" name="filter_by_course" id="filter_by_course" onchange="fetch_sections_of_course('onchange')">
                                                    <option value="">Select Course</option>
                                                    <?php
                                                    if (isset($course) || !empty($course))
                                                    {
                                                        foreach ($course as $row)
                                                        {
                                                    ?>
                                                        <option value="<?php echo $row['id'] ?>" <?php if($row['id']==$courseId){?> selected <?php } ?> >
                                                            <?php echo $row['course_name']; ?>
                                                        </option>
                                                    <?php
                                                        }
                                                    }
                                                    ?>
                                                    </select>
                                                    <div id='err_filter_by_course' class="text-danger cierr"><?php echo form_error('filter_by_course'); ?></div>
                                                </div>
                                            </div>
                                            <div class="col-md-3 pl-0">
                                                <div class="form-group">
                                                    <label class="mb-0" style="font-size: 13px;">Section</label>
                                                    <select class="form-control selectpicker" multiple data-live-search="true" name="filter_by_section[]" id="filter_by_section">
                                                        <option value="">Select Section</option>                                                      
                                                    </select>
                                                    <div class="text-danger cierr"><?php echo form_error('filter_by_section'); ?></div>
                                                </div>
                                            </div>
                                             
                                            <div class="col-md-3">
                                                <div class="form-group" style="margin-top: 1.5rem;">
                                                    <input type="button" name="filterSubmit" onclick="validate_filter()" id="filter_reset" class="btn btn-success" value="Filter">
                                                     
                                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <a href="<?php echo base_url() ?>admin/assign-additional-subjects"><button type="button" name="reset_filter" id="reset_filter" value="reset_filter" class="subject_addbtn subject_addbtn_nobg p-0"><i class="fas fa-redo "></i>Reset</button></a>
                                                </div>
                                            </div>
                                        </div>  
                                    

                                    <?php if($this->session->flashdata('success')) { ?>
                                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                                            <p><?php echo $this->session->flashdata('success') ?></p>
                                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                    <?php } ?>

                                    <?php if($this->session->flashdata('failed')) { ?>
                                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                            <p><?php echo $this->session->flashdata('failed') ?></p>
                                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                    <?php } ?>

                                    <?php
                                    /*$showHideTable = "style='display:none;'";
                                    if(isset($theResults) && !empty($theResults))
                                        $showHideTable = "style=''";  */                                  

                                    $showHideTableUpdate = "style='display:none;'";
                                    if(isset($theResults) && !empty($theResults) && !empty($theOptSubs))
                                        $showHideTableUpdate = "style='margin-top: 1.5rem;'";
                                    ?>
                                    
                                    <div id='theTableHere'>
                                        <table id="tbl" class="display table-responsive" >
                                            <thead>
                                                <tr>
                                                    <th width='50px;'>Sl No</th>
                                                    <th width='50px;'>Roll No</th>
                                                    <th width='150px;'>Student Name</th>
                                                    <?php
                                                    $theColSpan = 3;
                                                    if($batchId == $currentBatch)
                                                    {
                                                        if(!empty($theOptSubs))
                                                        {
                                                            foreach ($theOptSubs as $osub)
                                                            {
                                                                $theColSpan++;
                                                    ?>
                                                    <th>
                                                        <input type="checkbox" id='chk_unchk_all_<?php echo $osub['id'];?>' onclick="check_uncheck_column(<?php echo $osub['id'];?>)" ><br/>
                                                        <?php echo $osub['sub_name'];?>                                                      
                                                    </th>
                                                    <?php
                                                            }
                                                        }
                                                        else
                                                        {
                                                            $theColSpan++;
                                                    ?>
                                                        <th>No additional subjects present for the selected Batch & Course</th>
                                                    <?php
                                                        }
                                                    }
                                                    else
                                                    {
                                                        $theColSpan++;
                                                    ?>
                                                    <th>Additional Subject Details</th>
                                                    <?php
                                                    }
                                                    ?>                                               
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(isset($theResults) && !empty($theResults))
                                            {
                                                $slno=1;                                                
                                                foreach ($theResults as $key => $std)
                                                {
                                                    $osArr = array();
                                                    if(isset($std['the_add_subjects']) && $std['the_add_subjects']!='')
                                                        $osArr = explode(',', $std['the_add_subjects']);
                                                    
                                            ?>
                                                <tr>
                                                    <td><?php echo $slno;?></td>
                                                    <td><?php echo $std['roll_no'];?></td>
                                                    <td><?php echo $std['name'];?></td>
                                                    <?php
                                                    if($batchId == $currentBatch)
                                                    {
                                                        if(!empty($theOptSubs))
                                                        {
                                                            foreach ($theOptSubs as $ss=>$osub)
                                                            {
                                                                if(empty($osArr))
                                                                {
                                                    ?>
                                                                <td>
                                                                    <input type="checkbox" class='c_uc_os_col_<?php echo $osub['id'];?>' name="student[<?php echo $std['id'];?>][<?php echo $osub['id'];?>]" id='cb_std_<?php echo $std['id']."_".$osub['id'];?>' onclick="manage_the_checkboxes(<?php echo $std['id'];?>, <?php echo $osub['id'];?>)" >
                                                                    <input type="hidden" class='txt_c_uc_os_col_<?php echo $osub['id'];?>' size="5px" name="student[<?php echo $std['id'];?>][<?php echo $osub['id'];?>]" id='hidden_std_<?php echo $std['id']."_".$osub['id'];?>' value='no'>
                                                                </td>
                                                    <?php
                                                                }
                                                                else
                                                                {
                                                                    $checked = ''; $chkVal='no';
                                                                    if(in_array($osub['id'], $osArr)){
                                                                        $checked = 'checked';
                                                                        $chkVal='yes';
                                                                    }                                                                
                                                    ?>
                                                                <td>
                                                                    <?php //echo $osub['id'].' = '.$checked;?>
                                                                    <input type="checkbox" class='c_uc_os_col_<?php echo $osub['id'];?>' name="student[<?php echo $std['id'];?>][<?php echo $osub['id'];?>]" id='cb_std_<?php echo $std['id']."_".$osub['id'];?>' <?php echo $checked;?> onclick="manage_the_checkboxes(<?php echo $std['id'];?>, <?php echo $osub['id'];?>)" >
                                                                    <input type="hidden" class='txt_c_uc_os_col_<?php echo $osub['id'];?>' size="5px" name="student[<?php echo $std['id'];?>][<?php echo $osub['id'];?>]" id='hidden_std_<?php echo $std['id']."_".$osub['id'];?>' value='<?php echo $chkVal;?>'>
                                                                </td>
                                                    <?php
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                    ?>
                                                    <td> - </td>
                                                    <?php
                                                        }                                                    
                                                    }
                                                    else
                                                    {
                                                    ?>
                                                    <td><?php echo $std['the_add_subject_details'];?></td>
                                                    <?php
                                                    }
                                                    ?>
                                                </tr>
                                            <?php
                                                $slno++;
                                                }
                                            }
                                            else
                                            {
                                            ?>
                                            <tr>
                                                <td colspan="<?php echo $theColSpan;?>">-- No data to display --</td>
                                            </tr>
                                            <?php
                                            }
                                            ?>                                        
                                            </tbody>
                                        </table>

                                        <div class="col-md-3"  >
                                            <div id='tbl-update-btn' class="form-group" <?php echo $showHideTableUpdate;?> >
                                                <input type="button" name="assignSubmit" onclick="submit_the_form_here()" class="btn btn-success" value="Update">
                                            </div>
                                            <div id='tbl-loading-btn' class="form-group" style='display:none;'>
                                                <i class="fa-solid fa-loader"></i>Loading.... Please wait
                                            </div>                                            
                                        </div>
                                    </div>                                  
                                </div>

                                
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>  
    </div>
    

    <script defer src="<?php echo base_url(); ?>assets_admin/js/bootstrap-select.min.js"></script>

    <script>
    var jsCurrBatch = '<?php echo $currentBatch;?>';
    var jsSeldSections = "<?php echo $sectionIds;?>";
    
    $(document).ready(function(){

        fetch_sections_of_course('onload');

        //table js
        /*$('#tbl').DataTable( {
            "lengthMenu": [[50, 75, 100, -1], [50, 75, 100, "All"]],
            "ordering": false,
        });*/
        $('#tbl').DataTable( {
            "lengthMenu": [[-1], ["All"]],
            "ordering": false,
        });
        //table js end
        $('#filter_by_section').selectpicker();
        
    });

    function batch_is_changed()
    {
        $('#filter_by_course').val('');
        $('#filter_by_section').empty().selectpicker("refresh");

        $('#theTableHere').hide();
    }

    function fetch_sections_of_course(calledOn)
    {
        if(calledOn=='onchange')
            $('#theTableHere').hide();

        $('#filter_by_section').empty().selectpicker("refresh");
        var divData = "<option value='' disabled> Select Section </option>";
        
        if($('#filter_by_course').val()!='')
        {
            $.ajax({
                url: "<?php echo base_url(); ?>Admin/get_section_by_course",
                method: "POST",
                data: {
                    course_id: $('#filter_by_course').val()
                },
                dataType: "json",
                success: function(response) {            
                    //divData += "<option value='' disabled> Select Section </option>";
                    if(response.sections != '')
                    {
                        var ssArr = [];
                        if(jsSeldSections!='')
                            ssArr = jsSeldSections.split(",");

                        $.each(response.sections, function(i, sectiondata) {
                            if(ssArr.includes(sectiondata.section_id))
                                divData += "<option value='"+sectiondata.section_id+"' selected >"+sectiondata.section_name+"</option>";
                            else
                                divData += "<option value='"+sectiondata.section_id+"'>"+sectiondata.section_name+"</option>";
                        });
                    }
                    else
                        divData += "<option value=''> -- No Section's Found --</option>";
                    
                    $('#filter_by_section').append(divData).selectpicker("refresh");
                }
            });
        }        
    }

    function validate_filter()
    {
        $errCnt=0;
        $("#err_filter_by_batch").hide();
        $("#err_filter_by_course").hide();

        if($('#filter_by_batch').val()=='')
        {
            $('#err_filter_by_batch').html('Please select the batch.');
            $('#err_filter_by_batch').show();
            $errCnt++;
        }

        if($('#filter_by_course').val()=='')
        {
            $('#err_filter_by_course').html('Please select the course.');
            $('#err_filter_by_course').show();
            $errCnt++;
        }

        $('#clicked_on').val('Filter');

        if($errCnt==0)
            $('#assignAdditionalSubToStudentsForm').submit();

        return false;
    }

    function check_uncheck_column(optSubId)
    {
        if($('#chk_unchk_all_'+optSubId).prop('checked'))
        {
            $('.c_uc_os_col_'+optSubId).each(function(){
                $(this).prop('checked', true);                
            });

            $('.txt_c_uc_os_col_'+optSubId).each(function(){
                $(this).val('yes');                
            });
        }
        else
        {
            $('.c_uc_os_col_'+optSubId).each(function(){
                $(this).prop('checked', false);
            });

            $('.txt_c_uc_os_col_'+optSubId).each(function(){
                $(this).val('no');                
            });
        }
    }

    function manage_the_checkboxes(stdId, osId)
    {
        if($('#cb_std_'+stdId+'_'+osId).prop('checked'))
            $('#hidden_std_'+stdId+'_'+osId).val('yes');
        else
            $('#hidden_std_'+stdId+'_'+osId).val('no');
    }

    function submit_the_form_here()
    {
        $('#clicked_on').val('UpdateData');
        $('#assignAdditionalSubToStudentsForm').submit();

        $('#tbl-update-btn').hide();
        $('#tbl-loading-btn').show();
    }
    </script>