<?php 
$tid=$this->session->userdata('tid');
$acedemic_session=$this->session->userdata('acedemic_session');
$class = $this->session->userdata('class_teacher');
$section=$this->session->userdata('class_teacher_section');

// $all_marked_pending_attendance = $this->AttendenceModel->marked_pending_attendance($acedemic_session,$tid);

$check_marked_pending_attendance=$this->AttendenceModel->marked_pending_attendance_by_class($acedemic_session,$class,$section);

$date='';
if($check_marked_pending_attendance){
    foreach($check_marked_pending_attendance as $marked_pending_attendance){
        $date=$marked_pending_attendance['date'];
        // $get_student_data=marked_pending_attendance_data($tid,$date);
    }
}


// $date = date('y-m-d');
$table='attendence';  
$db_check_student=$this->AttendenceModel->class_attendence($class,$section,$date,$table);
        ?> 
<div class="container mt-3 mb-3  bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Welcome">Home</a></li>
                <li class="breadcrumb-item">Attendance</li>
                <li class="breadcrumb-item">Marked Pending Attendance</li>
            </ul>    
        
           
            <div class="table-responsive" style="margin-top:30px;">
                <table class="table table-bordered table-striped">
                    <tr>
                        <td>Class : <?php echo $class; ?></td>
                        <td>Section : <?php echo $section;?></td>
                        <td>Date : <?php echo date('d-M-Y',strtotime($date));?></td>
                    </tr>    
                    <tr>
                        <td><p class="text-success font-weight-bold">Present:<br> <span class="present_students"></span></p></td>    
                        <td><p class="text-danger font-weight-bold">Absent:<br><span class="absent_students"></span></p></td>
                        <td>
                            <p class="text-info font-weight-bold">Total Student  :<?php if($db_check_student>0){echo $db_check_student; }?></p>    
                        </td>
                    </tr>    
                </table>    
            </div>

    <!--    </div>-->
    <!--</div>-->
    <!--<div class="row">-->
    <!--    <div class="col-sm-12">-->
      

            <div class="table-responsive">
                <table class="table table-stretched table-bordered" id="">
                    <thead>
                        <tr>
                            <th>Count</th>  
                            <th>Sr. No.</th>
                            <!--<th>Class</th>-->
                            <th>Name</th>
                            <!--<th>Father Name</th>-->
                            <th>Action</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody id="tabledata1">
                    </tbody>
                </table>

                <form>
                    <div class="form-group mt-3 mb-3">
                        <input type="text" class="form-control" id="reason" placeholder="Mention reason for late attendance">    
                    </div>    
                </form>

                    <!--<a style="text-align: right;" href="<?php echo base_url()?>teacher/final_attendence/<?php echo $class = $this->session->userdata('class_teacher');?>">-->
                <button class="btn btn-primary close_attendance" value="" type="submit">Submit Attendence</button>
                <!--</a>-->
            </div>


        </div>
    </div>

</div>



<script>
$(document).ready(function(){
    
var date='<?php echo $date; ?>';  
// alert(date);
function loadclassstudentdata(){
$.ajax({
url: '<?php echo base_url() ?>teacher/getallstudent_for',
type: 'POST',     
data: {date:date},
success: function(data){
$('#tabledata1').html(data);
}
});
} 
loadclassstudentdata();
 function getPresentStudentsClass(){
$.ajax({
url:'<?php echo base_url();?>teacher/getPresentStudentsinClass_for',
type:'POST',
data: { date:date},
success:function(data){
$('.present_students').html(data);
}
});  
}
function getAbsentStudentsClass(){
$.ajax({
url:'<?php echo base_url();?>teacher/getAbsentStudentsinClass_for',
type:'POST',
data: {date:date},
success:function(data){
$('.absent_students').html(data);
}
});  
}
getPresentStudentsClass();
getAbsentStudentsClass();
$(document).on("click",".absent_button",function(){
var val = 'a';
var id = $(this).attr("db_id");
var table = 'attendence';
$.ajax({
url: '<?php echo base_url(); ?>teacher/markin_old_attendance',
type: 'POST',
data: { val:val,id:id, table:table,date:date},
success: function(data){
loadclassstudentdata();
getPresentStudentsClass();
getAbsentStudentsClass(); 
}
});
});
$(document).on("click",".present_button",function(){
    
var val ='p';
var id = $(this).attr("db_id");
var table = 'attendence';

$.ajax({
url: '<?php echo base_url(); ?>teacher/markin_old_attendance',
type: 'POST',
data: { val:val,id:id, table:table,date:date},
success: function(data){
loadclassstudentdata();
getPresentStudentsClass();
getAbsentStudentsClass(); 
}
});
}); 
$(".close_attendance").click(function(){
var note = $('#reason').val();
// alert(note);
var table = 'attendence';

if(note ==''){
swal("Mention reason");    
}else{
$.ajax({
type:"POST",
url:'<?php echo base_url();?>teacher/close_old_attendance',
type: 'POST',
data: {table:table,date:date,note:note},
success: function(data){
// $("#result").html('Successfully updated record!'); 
// $("#result").addClass("alert alert-success");    
location.reload();
}
});  
}

});       
});           
</script>