<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj = new my_class();
// ============

$atd_status_id;
$sub_id;
$section_id;
$today = date("Y-m-d");

extract($_GET);
?>

<style type="text/css">
    .input-append, .input-prepend {
        margin-bottom: 0px !important;
    }
    .table td {
        padding: 3px !important;
    }
</style>

<div id="view-part">


<?php
$det_in = $obj->Details_By_Cond("_int_institute_setup_vw", "section_id='$section_id'");

$flag = true;
?>


    <div class="row-fluid">
        <legend>
     <!--        <button class="btn btn-mini btn-danger" id="back_a_sub_final">
                <i class="icon-reply icon-only" id="back_a_sub_final"></i>
                  Back
    </button> -->
            <a href="#" id="back_a_sub_final" >
            <img src="asset/custom/back.png" id="back" >
            </a>

            View and Final Attendance (<?php echo 'Class: ' . $det_in['class'] . ', Department: ' . $det_in['department'] . ', Student Type: ' . $det_in['student_type'] . ', Section: ' . $det_in['section']; ?>).
        </legend>
        <input type="hidden" id="module_id" name="module_id" value="<?php echo $module_id;?>"/>
    </div>


<?php



$table_name = " _atd_daily_status_subject";
$column = "*";
$where_cond = '_atd_status = 1 && _section_id=' . $section_id . '  && DATE(_entry_date) = "' . $today . '" ';
$all_sub_id = $obj->View_all_data_row_column_By_Cond($table_name, $column, $where_cond);

$j = 1;

$total_student = 0;
$for_total_absent = 0;
$for_total_late = 0;
$for_total_leave = 0;

if (!empty($all_sub_id)) {
    foreach ($all_sub_id as $subValu) {



        $sub_id = $subValu['_subject_id'];
        $status_comments = $subValu['_comments'];
        $_atd_time = $subValu['_atd_time'];
        $approved_status = $subValu['_approve_satus'];
        $atd_status_id = $subValu['_id'];
		

        $tch_id = $obj->Details_By_Cond("_r_class_routine", "_section_id='$section_id' AND _subject_id='$sub_id' AND _teacher_id!='0' GROUP BY _subject_id");
        $teacher_id = $tch_id['_teacher_id'];

        $tch_det = $obj->Details_By_Cond("_pf_emp_personal_info", "_id='$teacher_id'");
        $sub_det = $obj->Details_By_Cond("_int_subject", "_id='$sub_id'");

        $sub_code = '';
        $sub_code_str = '';
        if (!empty($sub_det['_subject_code'])) {
            $sub_code_str = ' (' . $sub_det['_subject_code'] . ') ';
            $sub_code = $sub_det['_subject_code'];
        }
		$subject_name = $sub_det['_subject_full_name'];
        ?>

            <div class="row-fluid homework_conformation" id="attendance_sheet" >   

                <div class="row-fluid">
                    <legend>
            <?php echo 'Subject: ' . $sub_det['_subject_full_name'] . $sub_code_str; ?>.
                    </legend>
                </div>


                <!-- ==================================== -->
                <form class="form-vertical" id="attendance_sheet">

                    <div class="row-fluid">	


                        <table class="table table-striped table-bordered table-hover">
                            <thead>
                            <th>Sl</th>
                            <th>Student ID</th>
                            <th>Name</th>
                            <th>Roll</th>
                            <th style="text-align: center;">Status</th>
                            <th style="text-align: center;">Late Time</th>
                            <th style="text-align: center;">Comments</th>
                            <th>Contact No</th>
                            </thead>
                            <tbody>
        <?php
        $i = 0;
        $p = 0;
        $ab = 0;
        $la = 0;
        $le = 0;
        foreach ($obj->View_Student_List_atttd_with_comment_subject_wise("sc._id='$section_id' AND DATE(hmc._entry_date) ='".$today."' && pf_std._status!='D' AND _subject_id = $sub_id ORDER BY _section_roll ASC") as $value) {
            extract($value);
            $i++;

            $_late_time = $value['_late_time'];


            if ($_atd_status == 'P') {
                $atd_status = 'Present';
            } elseif ($_atd_status == 'A') {
                $atd_status = 'Absent';
                $ab = $ab + 1;
            } elseif ($_atd_status == 'LA') {
                $atd_status = 'Late';
                $la = $la + 1;
            } elseif ($_atd_status == 'LE') {
                $atd_status = 'Leave';
                $le = $le + 1;
            }
            ?>	
                                    <tr>
                                        <td class="td_pad">
                                            <input type="hidden" name="std_id_<?php echo $i; ?>" value="<?php echo $value['_id']; ?>">
                                            <input type="hidden" class="late-time-hidden" value="<?php echo $_late_time ?>" name="late-time" />
                                    <?php echo $i; ?>
                                        </td>
                                        <td class="td_pad"><?php echo $value['_uniq_id']; ?></td>
                                        <td class="td_pad"><?php echo $value['_full_name'];
                        if (!empty($value['_nick_name'])) {
                            echo ' (' . $value['_nick_name'] . ')';
                        } ?></td>
                                        <td class="td_pad"><?php echo $value['_section_roll']; ?></td>
                                        <td class="td_pad" style="text-align: center;"> <?php echo $atd_status; ?> </td>
                                        <td class="td_pad" style="text-align: center;">	 <?php echo $_late_time ?> </td>
                                        <td class="td_pad" style="text-align: center;"><?php echo $_comments; ?></td>

                                        <td class="td_pad"><?php echo $value['_contact_mobile']; ?></td>
                                    </tr>	
                                    <?php
                                }
                                $total_row = $obj->View_Student_List_rows("sc._id='$section_id' AND pf_std._status!='D'");
                                $selected_student = $i;


                                $present = $total_row - ($ab + $la + $le);
                                $prsent_persentage = round(($present * 100) / $total_row, 1);


                                $sing_ab_persentage = round(($ab * 100) / $total_row, 1);
                                $sing_la_persentage = round(($la * 100) / $total_row, 1);
                                $single_le_persentage = round(($le * 100) / $total_row, 1);


                                /// count at every loop, this is for counting total info of all
                                $for_total_absent = $for_total_absent + $ab;
                                $for_total_late = $for_total_late + $la;
                                $for_total_leave = $for_total_leave + $le;
                                $total_student = $total_student + $total_row;
                                ?>
                                <tr>
                            <input type="hidden" name="total_row" value="<?php echo $total_row; ?>">
                            <td colspan="8" rowspan="1" style="text-align:center; vertical-align:middle; padding-bottom:5px !important; ">
                                <b>Total Stuednt: </b> <?php echo $total_row . ", "; ?>   <b> Preseant:</b>  <?php echo $total_row - ($ab + $la + $le) . " ( " . $prsent_persentage . "%), "; ?> &nbsp; &nbsp;  <b>Absent: </b><?php echo $ab . " ( " . $sing_ab_persentage . "%), "; ?>    &nbsp; &nbsp; <b> Late: </b> <?php echo $la . " ( " . $sing_la_persentage . "%), "; ?>  &nbsp; &nbsp;  <b>Leave: </b> <?php echo $le . " ( " . $single_le_persentage . "%) "; ?>




                            </td>
                            </tbody>	

                        </table>
                    </div>

                    <!-- ========================== -->

                    <div class="row-fluid">	
                        <div class="span3"><b>Class Teacher name:</b> <?php echo $tch_det['_full_name'];
                        if (!empty($tch_det['_nick_name'])) {
                            echo ' (' . $tch_det['_nick_name'] . ')';
                        } ?></div>

                        <div class="span2"><b>Mobile No:</b> <?php echo $tch_det['_contact_mobile_no']; ?></div>
                        <div class="span3"><b>Attendence Time :</b> <?php echo date("H:i", strtotime($_atd_time)); ?> </div>

                        <div class="span4"><b>Comments: </b>
                            <input name="remarks" class="input-large" type="text" placeholder="Comments" style="display:none" value="<?php echo $status_comments; ?>" />
        <?php echo $status_comments; ?>


                        </div>




                    </div>

                    <!-- ========================= -->



                    <div class="row-fluid">									
                        <div class="span12">			
                            <div class="control-group">
                                <label class="control-label" for="form-field-1">&nbsp;</label>

                                <input type="hidden" name="atd_status_id" value="<?php echo $atd_status_id; ?>">
                                <input type="hidden" name="sub_id" class="sub_id" value="<?php echo $sub_id; ?>">
                                <input type="hidden" name="subject_name" class="subject_name" value="<?php echo $subject_name; ?>">
                               
                                <input type="hidden" name="section_id" class="section_id" value="<?php echo $section_id; ?>">	
								
                                <div class="controls">





                                    &nbsp; &nbsp; &nbsp;
        <?php
		if($_SESSION['edit']=='Y'){
        if ($approved_status != 1) {
            $flag = false;
            ?>  
                                        <button class="btn btn-small btn-success btn_save_final_view" type="button" >
                                            <i class="icon-ok bigger-100"></i>
                                            Approve
                                        </button>
                                        <button class="btn btn-small btn-warning btn_reset " type="reset" onclick="attendence_edit_sub('<?php echo $module_id;?>','<?php echo $section_id; ?>','<?php echo $sub_id; ?>','<?php echo $atd_status_id; ?>','<?php echo $sub_det['_subject_full_name']; ?>','<?php echo $sub_code; ?>'  )" >
                                            <i class="icon-edit bigger-100"></i>
                                            Edit
                                        </button>
        <?php } else { ?>
                                        <button class="btn btn-small btn-success" type="button" >
                                            <i class="icon-ok bigger-100"></i>
                                            Approved
                                        </button>

        <?php } 
		
		
	}
		
		
		?>
                                </div>
                            </div>
                        </div>

                    </div>
                </form>
                                    <?php
                                }
                                echo '</div>';
                            }
                            ?>
        <!-- =========================== -->







        <?php
        if (!empty($all_sub_id)) {
            ?>

            <div class="row-fluid">
                <legend>
                    <strong>Summary  : </strong>
                </legend>
            </div>
            <div class="row-fluid">	


                <table class="table table-striped table-bordered table-hover table-final-view">
                    <thead>
                    <th>Total Student </th>
                    <th>Preseant</th>
                    <th>Absent</th>
                    <th>Late</th>
                    <th style="text-align: center;">Leave</th>
                    </thead>
                    <tbody>
    <?php
    $total_present = $total_student - ($for_total_absent + $for_total_late + $for_total_leave);
    $prsent_persentage = round(($total_present * 100) / $total_student, 1);
    $absent_persentage = round(($for_total_absent * 100) / $total_student, 1);
    $late_persentage = round(($for_total_late * 100) / $total_student, 1);
    $leave_persentage = round(($for_total_leave * 100) / $total_student, 1);
    ?>	
                        <tr>

                            <td class="td_pad"><?php echo $total_student; ?></td>
                            <td class="td_pad"><?php echo $total_present . " ( " . $prsent_persentage . '%)'; ?></td>
                            <td class="td_pad"><?php echo $for_total_absent . " ( " . $absent_persentage . '%)'; ?></td>

                            <td class="td_pad" style="text-align: center;"> <?php echo $for_total_late . " ( " . $late_persentage . '%)'; ?> </td>
                            <td class="td_pad" style="text-align: center;">	 <?php echo $for_total_leave . " ( " . $leave_persentage . '%)' ?> </td>


                        </tr>

                    </tbody>	

                </table>
            </div>

                        <?php
                if($_SESSION['edit']=='Y'){    
						if (!$flag) {
                            ?>

                <div class="row-fluid">
                    <div class="span12">			
                        <div class="control-group">
                            <div class="controls" style="text-align:center">
                                <button class="btn btn-small btn-success" type="button" id="btn_save_all_value">
                                    <i class="icon-ok bigger-100"></i>All Approve
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
        <?php
    }
  }
	
} else {
    echo 'There is no students attendence available at section: ' . $det_in['section'];
}
?>


    </div>

    <script type="text/javascript">

    $('.spinner1').ace_spinner({value:0,min:0,max:80,step:1, btn_up_class:'btn-info' , btn_down_class:'btn-info'})
    .on('change', function(){
    });
 

    $(".table-final-view tbody tr").each(function(){
                     var late_time_hidden = parseInt($(this).find(".late-time-hidden ").val());
                      $(this).find(".late_time").val(late_time_hidden);
            });


    $('.timepicker1').timepicker({
            minuteStep: 1,
            showSeconds: false,
            showMeridian: false
    })



    $(".btn_save_final_view").click(function(){
			var module_id = $('#module_id').val();
            var section_id = $(this).parent().parent().find(".section_id ").val();	
            var sub_id = $(this).parent().parent().find(".sub_id").val();
			var subject_name = $(this).parent().parent().find(".subject_name").val();
			
	
            $this = $(this);
            if(section_id){
                    var dtstr = "section_id="+section_id+"&sub_id="+sub_id+"&module_id="+module_id+"&subject_name="+subject_name ;
                    $.ajax({
                               type: "POST",		   
                               url: "module/attendance/attendence_subject_approve_single.php?"+dtstr,		   
                               success: function(result){
                                           $this.parent().find(".btn_reset").remove(); 
                                         $this.html('<i class="icon-ok bigger-100"></i>Approved'); 
                                           $this.unbind( "click" );
                                    },
                                    error : function(result){
			  	
                                    }			   	
                    });
            }

    });

    $("#btn_save_all_value").click(function(){
            $this = $(this) ;
			var module_id = $('#module_id').val();
            var data = {"module_id":module_id,"AllSection":[]};	
            var allform = $(".homework_conformation");
            $(allform).each(function(){
                    var section_id = $(this).find(".section_id ").val();
					var subject_name = $(this).find(".subject_name ").val();
                    var sub_id = $(this).find(".sub_id ").val();
                    products = {"section_id":section_id,"sub_id":sub_id,"subject_name":subject_name};
                    data.AllSection.push(products);
            });
            var p = JSON.stringify(data);
            var intIndexOfMatch = p.indexOf( "&" );		
            while (intIndexOfMatch != -1){
                    p = p.replace("&", "%26");
                    intIndexOfMatch = p.indexOf( "&" );
            }	
	
            $.ajax({
                       type: "POST",
                       dataType: "json",
                       url: "module/attendance/attendence_subject_approve_all.php",
                       data: "p="+p,	   
                      success: function(result){
			
                                  /*  $(allform).each(function(){
                                        //    $(this).find(".btn_reset").remove(); 
                                         //   $(this).find(".btn_save_final_view").html('<i class="icon-ok bigger-100"></i>Approved'); 
                                         //   $(this).find(".btn_save_final_view").unbind( "click" );
										 
                                    });*/
									 $(".btn_save_final_view").html('<i class="icon-ok bigger-100"></i>Approved'); 
									  $(".btn_save_final_view").unbind( "click" );
									  $(".btn_reset").remove();
                                    alert(result.msg);
                      },
                      error : function(result){
		  
                      }			   	
            });
    });


    function attendence_edit_sub(mod_id, sc_id,sub_id,atd_status_id, sub_name,sub_code){
            var datastr="module_id="+mod_id+"&section_id="+sc_id+"&sub_id="+sub_id+"&subject_name="+sub_name+"&sub_code="+sub_code+"&atd_status_id="+atd_status_id;
             $("#view-part").load("module/attendance/edit_attendance_final_sheet_subject.php",datastr);
    }

    </script>

    <script type="text/javascript">
    $('#back_a_sub_final').click(function(){

    $("#main_content_div").load("module/attendance/attendance_approve_choose_subject.php");

    });
</script>