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

extract($_GET);

$atd_status_id;
$sub_id;
$section_id;
$today = date('Y-m-d');
?>

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


<?php
$det_in = $obj->Details_By_Cond("_int_institute_setup_vw", "section_id='$section_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'");
?>

<div class="row-fluid">
    <legend>
        Daily Subject Wise Attendance Sheet for (<?php echo 'Class: ' . $det_in['class'] . ', Department: ' . $det_in['department'] . ', Student Type: ' . $det_in['student_type'] . ', Section: ' . $det_in['section'] . ', and Subject: ' . $sub_det['_subject_full_name'];
if (!empty($sub_det['_subject_code'])) {
    echo ' (' . $sub_det['_subject_code'] . ') ';
} ?>).
    </legend>
</div>


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

    <div class="row-fluid">	
    	 <input type="hidden" name="module_id" id="module_id" value="<?php echo $module_id; ?>">
        <input type="hidden" name="atd_status_id" value="<?php echo $atd_status_id; ?>">
        <input type="hidden" name="sub_id" value="<?php echo $sub_id; ?>">
        <input type="hidden" name="section_id" value="<?php echo $section_id; ?>">					

        <table class="table table-striped table-bordered table-hover  table-final-view">
            <thead>
            <th>Sl</th>
            <th>Student ID</th>
            <th>Name</th>
            <th>Roll</th>
            <th style="text-align: center;">Present</th>
            <th style="text-align: center;">Absent</th>
            <th style="text-align: center;">Late</th>
            <th style="text-align: center;">Late Time</th>
            <th style="text-align: center;">Leave</th>
            <th style="text-align: center;">Comments</th>
            <th>Contact No</th>
            </thead>
            <tbody>
                <?php
                $i = 0;
                foreach ($obj->View_Student_List("sc._id='$section_id' AND pf_std._status='A' ORDER BY _section_roll ASC") as $value) {
                    extract($value);
                    $i++;

                    $student_id = $_id;
                    $where_con = '_section_id=' . $section_id . ' AND _std_id=' . $student_id . ' AND _subject_id=' . $sub_id . '  AND DATE(_entry_date)="' . $today . '"';

                    $std_class = $obj->Details_By_Cond("_atd_daily_by_subject", $where_con);

                    $_late_time = 0;
                    $_comments = '';
                    $present_status = '';
                    $absent_status = '';
                    $late_status = '';
                    $leave_status = '';
                    if (!empty($std_class)) {

                        $_comments = $std_class['_comments'];
                        $_late_time = $std_class['_late_time'];
                        if ($_late_time == 0 || $_late_time == '') {
                            $_late_time = 0;
                        } else {
                            $_late_time = $std_class['_late_time'];
                        }

                        $_atd_status = $std_class['_atd_status'];
                        $present_status = $_atd_status == 'P' ? 'checked' : '';
                        $absent_status = $_atd_status == 'A' ? 'checked' : '';
                        $late_status = $_atd_status == 'LA' ? 'checked' : '';
                        $leave_status = $_atd_status == 'LE' ? 'checked' : '';
                    }
                    ?>	
                    <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-hidden" />
    <?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;"><label><input type="radio" id="atd_status_<?php echo $i; ?>" name="atd_status_<?php echo $i; ?>" value="P" <?php echo $present_status; ?>   /><span class="lbl"></span></label></td>
                        <td class="td_pad" style="text-align: center;"><label><input type="radio" id="atd_status_<?php echo $i; ?>" name="atd_status_<?php echo $i; ?>" value="A" <?php echo $absent_status; ?>  /><span class="lbl"></span></label> </td>
                        <td class="td_pad" style="text-align: center;"><label><input type="radio" id="atd_status_<?php echo $i; ?>" name="atd_status_<?php echo $i; ?>" value="LA" <?php echo $late_status; ?>  /><span class="lbl"></span></label></td>
                        <td class="td_pad" style="text-align: center;">
                            <input name="late_time_<?php echo $i; ?>" type="text" class="input-mini spinner1 late_time" value="0" />

                        </td>
                        <td class="td_pad" style="text-align: center;"><label><input type="radio" name="atd_status_<?php echo $i; ?>" value="LE" <?php echo $leave_status; ?>   /> <span class="lbl"></span></label></td>
                        <td class="td_pad" style="text-align: center;"><input name="comments_<?php echo $i; ?>" class="input-small" type="text" placeholder="Comments" value="<?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='A'");
                ?>
                <tr>
            <input type="hidden" name="total_row" value="<?php echo $total_row; ?>">
            </tr>
            </tbody>	

        </table>
    </div>

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

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

        <div class="span3"><b>Mobile No:</b> <?php echo $tch_det['_contact_mobile_no']; ?></div>
    </div>

    <!-- ========================= -->
    <?php
    $table_name = "_atd_daily_status_subject";
    $where_cond = '_section_id = ' . $section_id . ' && DATE(_entry_date) = "' . $today . '" ';
    $com = $obj->Details_By_Cond($table_name, $where_cond);
    ?>

    <div class="row-fluid">	
        <div class="span3">Attendance Time
            <div class="controls">
                <div class="input-append bootstrap-timepicker">
                    <input name="atd_time" type="text" class="input-small timepicker1" value="<?php echo $com['_atd_time']; ?>" />
                    <span class="add-on">
                        <i class="icon-time"></i>
                    </span>
                </div>
            </div>
        </div>

        <div class="span3">Comments
            <div class="controls">
                <input name="remarks" class="input-large" type="text" placeholder="Comments" value="<?php echo $com['_comments']; ?>">
            </div>
        </div>
    </div>


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

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

                <div class="controls">
                    <?php if ($total_row) { ?>
                        <button class="btn btn-small btn-success" type="button" id="btn_update_atdsheet">
                            <i class="icon-ok bigger-100"></i>Save
                        </button>
                        <?php
                    } else {
                        ?>
                        <button class="btn btn-small btn-success" type="button" id="btn_update_atdsheet">
                            <i class="icon-ok bigger-100"></i>Save
                        </button>
<?php } ?>

                    &nbsp; &nbsp; &nbsp;
                    <!--<button class="btn btn-small btn-danger" type="reset" id="btn_reset">
                            <i class="icon-undo bigger-100"></i>
                            Reset
                    </button>-->
                </div>
            </div>
        </div>

    </div>


</form>


<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 () {
            });



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


    $(".table-final-view tbody tr").each(function () {
        $this = $(this);
        var check = $this.find(".late-time-hidden").length;
        if (check > 0) {
            var late_time_hidden = $(this).find(".late-time-hidden").val();

            $this.find(".late_time").val(late_time_hidden);
        }

    });

    $("#btn_update_atdsheet").click(function () {
		var module_id = $('#module_id').val();
        var atd_status_id = $('input[name="atd_status_id"]').val();
        var sub_id = $('input[name="sub_id"]').val();
        var section_id = $('input[name="section_id"]').val();
        var remarks = $('input[name="remarks"]').val();
        var atd_time = $('input[name="atd_time"]').val();
        var total_row = $('input[name="total_row"]').val();
        var operation = 'save_atd';
        var operation2 = 'up_status';

        //var rs = false;

        for (i = 1; i <= total_row; i++) {

            var std_id = $('input[name="std_id_' + i + '"]').val();
            var atd_status = $('input:radio[name="atd_status_' + i + '"]:checked').val();
            var late_time = $('input[name="late_time_' + i + '"]').val();
            var comments = $('input[name="comments_' + i + '"]').val();

            var dataStr = "operation=" + operation + "&section_id=" + section_id + "&sub_id=" + sub_id + "&std_id=" + std_id + "&atd_status=" + atd_status + "&late_time=" + late_time + "&comments=" + comments;

            var dataStr2 = "operation2=" + operation2 + "&atd_status_id=" + atd_status_id + "&atd_time=" + atd_time + "&remarks=" + remarks;

            var dataStr3 = "module_id=" + module_id +"&section_id=" + section_id + "&sub_id=" + sub_id + "&atd_status_id=" + atd_status_id;

            //alert(late_time);

            $.ajax({
                type: "post",
                url: "module/attendance/update_atd_data_subject.php",
                data: dataStr,
                success: function (result) {
                }
            });
        }

        $.ajax({
            type: "post",
            url: "module/attendance/update_atd_data_subject.php",
            data: dataStr2,
            success: function (st) {
                //alert(st);
                $("#view-part").load("module/attendance/attendance_sheet_subject_final_view.php", dataStr3);

                $('#first_part').remove();
                $('#second_part').css("display", "block");
            }
        });

    });



// =================================



    $("#btn_save_atd").click(function () {

        var atd_status_id = $('input[name="atd_status_id"]').val();
        var section_id = $('input[name="section_id"]').val();
        var remarks = $('input[name="remarks"]').val();
        var atd_time = $('input[name="atd_time"]').val();
        var operation2 = 'up_status';

        var dataStr4 = "operation2=" + operation2 + "&atd_status_id=" + atd_status_id + "&atd_time=" + atd_time + "&remarks=" + remarks;

        var dataStr5 = "section_id=" + section_id;


        $.ajax({
            type: "post",
            url: "module/attendance/update_atd_data_subject.php",
            data: dataStr4,
            success: function (st) {
                $("#view-part").load("module/attendance/attendance_sheet_subject_final_view.php", dataStr5);

                $('#first_part').remove();
                $('#second_part').css("display", "block");
            }
        });

    });





</script>