<?php

$sl = 1;
$las =0;
$l_array_size = 0;
$emp_leave[]="";
foreach ($leave_detail as $row){
    $startDate = new DateTime($row->apply_strt_date);
    $endDate = new DateTime($row->apply_end_date);

    $difference = $endDate->diff($startDate);
    $total_days =  $difference->format("%a");
    $l_s_date = $row->apply_strt_date;
    if($total_days >= 1){
        for($j = 0; $j < $total_days+1 ; $j++){
            $emp_leave[$las++] = array($row->employee_id , $l_s_date, $row->leave_time);
            $l_s_date = date('Y-m-d', strtotime('+'.(1).' day', strtotime($l_s_date)));
        }
    }elseif ($total_days == 0){
        $emp_leave[$las++] = array($row->employee_id , $row->apply_strt_date , $row->apply_end_date);
    }
}

$l_array_size = $las;
function fll_miss_rows($sd , $ndd, $start_date,$e_id,$l_array_size,$emp_leave){
    for($i=$sd ; $i<=$ndd+1;$i++){
        $xmasDay = date('Y-m-d', strtotime('+'.($i-1).' day', strtotime($start_date)));
        $day = date("D", strtotime($xmasDay));
        $full_date= date("Y-m-d", strtotime($xmasDay));
        if($day == 'Sat' || $day == 'Sun') {
            ?>
            <td class="text-center" style="color: black"><strong>HD</strong></td>
        <?php } else{
            if($text = check_leave($l_array_size,$emp_leave,$e_id,$full_date) ){ ?>
                <td class="text-center" style="color: #0b68f4"><strong> <?php if($text == 1) echo "Hl";if($text == 2) echo "Fl";if($text == 3) echo "Ql"?> </strong></td>
            <?php }else{
                ?>
                <td class="text-center" style="color: red"><strong> A</strong></td>
                <!--            <td class="text-center" style="color: red"><strong> --><?php //echo $full_date ?><!-- AD A</strong></td>-->
                <?php
            }
        }
    }
}
function check_leave($l_array_size,$emp_leave,$e_id,$c_i_date){
    for($k = 0 ; $k < $l_array_size; $k++){
//        echo"<br>";
//        echo $emp_leave[$k][0]." ".$emp_leave[$k][1];
        if($emp_leave[$k][0] == $e_id && $emp_leave[$k][1] == $c_i_date ){
//            echo "<script> alert('".$e_id." ".$c_i_date."') </script>";
            return $emp_leave[$k][2];
        }
    }
    return false;
}
function get_total_hr($e_id,$total_working_hr){
    if(!empty($total_working_hr)){
        foreach ($total_working_hr as $row){
//            echo"<script> alert('".$row->over_time." ID ".$row->employee_id." EID ".$e_id."')  </script>";
            if($row->employee_id == $e_id){
                return $row->over_time;
            }
        }
        return 0;
    }
}
?>
<div class="row">
    <div class="col-sm-12">
        <div class="panel panel-bd">

            <div class="panel-heading">
                <div class="panel-title">
                    <h4>
                    </h4>
                </div>
            </div>

            <div class="panel-body">

                <div class="panel-title bulk_add_emp_btn float-right">
                    <a style="color: #fff;" href="<?php base_url() ?>reports_list" class="btn btn-success" id="reportLists">Reports List</a>
                </div>

                <div class="row">


                    <div class="form-group row">
                        <?php echo form_open('reports/Employee_controller/employee_by_department',array('class' => '', 'id' => 'validate'));?>
                        <label class="select col-sm-2"><?php echo display('search') ?>:</label>
                        <div class="col-sm-4">
                            <button type="button" class="btn btn-primary" data-target="#filter" data-toggle="modal"><?php echo display('filter') ?></button>
                            <!--                            <button type="submit" class="btn btn-primary">--><?php //echo display('search') ?><!--</button>-->
                            <input type="button" class="btn btn-warning" name="btnPrint" id="btnPrint" value="Print" onclick="printDiv();"/>
                            <a href="<?php base_url() ?>employee_by_department_export/<?php echo $dpt; ?>" class="btn btn-warning" id="exportReport">Export</a>
                        </div>
                        <?php echo form_close()?>
                    </div>


                </div>
            </div>
        </div>

    </div>
</div>
<div class="row">
    <!--  table area -->

    <div class="panel panel-bd">

        <div class="panel-heading">
            <div class="panel-title">
                <h4>
                    <?php echo "Employee Attendance Summary " ?>
                </h4>
            </div>
        </div>

        <div class="panel-body" id="printArea">
            <?php if($difference > 0){ ?>

                <div class="row" style="margin-bottom: 15px">
                    <div class="col-sm-4 text-center"> <h3> Attendance Summary </h3> </div>
                    <div class="col-sm-4 text-center"> <h3> Period: <?php echo $start_date.' - '.$to_date ?> </h3> </div>
                    <div class="col-sm-4 text-center"> <h3> <?php echo  "Work Location ".$work_location?> </h3> </div>
                    <div class="col-sm-4 text-center"> <h3> <?php
                            if($department == '0'){
                                echo "Department All";
                            }else{
                                echo "Department ".$department;
                            }
                            ?> </h3> </div>
                    <div class="col-sm-4 text-center"> <h3> <?php
                            if($duty_types == '0'){
                                echo "Duty Type All";
                            }else{
                                echo "Duty Type ".$duty_types;
                            }
                            ?> </h3> </div>
                    <div class="col-sm-4 text-center"> <h3>
                            <?php
                            if ($employees_status == 0){
                                echo "Employee Status Inactive";
                            }elseif( $employees_status == 1 ){
                                echo "Employee Status Active";
                            }elseif ( $employees_status == 2){
                                echo "Employee Status On Leave";
                            }elseif ($employees_status == 3){
                                echo "Employee Status Not Started ";
                            }elseif ($employees_status == 4){
                                echo "Employee Status Cancelled ";
                            }else{
                                echo "Employee Status All";
                            }
                            ?>
                        </h3> </div>
                </div>
            <?php } ?>
            <div class="table-responsive">
                <?php if($difference > 0){ ?>
                    <table class="table table-bordered table-hover">
                        <thead>
                        <tr>
                            <th><?php echo display('cid') ?></th>
                            <th><?php echo "Name" ?></th>
                            <th><?php echo "Department" ?></th>
                            <?php
                            for($i=1 ; $i<=$day_difference+1;$i++){
                                $xmasDay = date('Y-m-d', strtotime('+'.($i-1).' day', strtotime($start_date)));
                                $day = date("D", strtotime($xmasDay));
                                $date= date("d", strtotime($xmasDay));

                                ?>
                                <th class="text-center"> <?php echo $date."<br>".$day[0] ?> </th>
                            <?php } ?>
                            <th> <?php echo "Total Hours"?> </th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        if (!empty($attendance_report)) {
                            ?>
                            <?php $sl = 1; ?>
                            <?php
                            $ndd = $day_difference;
                            $sd = 1;
                            $nr = true;
                            $r = 0;
                            $a_r_size = 0;
                            foreach ($attendance_report as $row) {
                                $a_r_size++;
                                ?>
                                <?php if($nr){ ?>
                                    <tr class="<?php echo ($sl & 1)?"odd gradeX":"even gradeC" ?>">

                                    <td><?php echo $sl; ?></td>
                                    <td><?php echo $row->employee_name ?></td>
                                    <td><?php echo $row->department_name ?></td>
                                    <?php
                                    $ndd = $day_difference;
                                    $nr = false;
                                }

                                for($i=$sd ; $i<=$ndd+1;$i++){
                                    $xmasDay = date('Y-m-d', strtotime('+'.($i-1).' day', strtotime($start_date)));
                                    $day = date("D", strtotime($xmasDay));
                                    $full_date = date("Y-m-d", strtotime($xmasDay));

//                                    echo "<script> alert(' date ".$full_date." I ".$i." NDD ".($ndd +1)."'); </script>";
                                    if($day == 'Sat' || $day == 'Sun'){ ?>
                                        <td class="text-center" style="color: black"><strong> HD</strong></td>
                                        <!--                                        <td class="text-center" style="color: black"><strong> --><?php //echo $full_date ." ".$i  ?><!-- HD</strong></td>-->
                                        <?php
//                                        echo "<script> alert('SSB ".$i." ".$row->check_in_date." ".$full_date." ') </script>";
                                        if($row->check_in_date == $full_date){
//                                            echo "<script> alert('SSA ".$i." ') </script>";
//                                            $r++;
//                                            break;
                                            if($attendance_report[$r]->employee_id != $attendance_report[$r+1]->employee_id ) {
//                                                echo "<script> alert('".$attendance_report[$r]->employee_id." Not Same ". $attendance_report[$r+1]->employee_id."'); </script>";
                                                fll_miss_rows(($i+1),$ndd,$start_date,$attendance_report[$r]->employee_id,$l_array_size,$emp_leave);
                                                $nr = true;
                                                $tr = true;
//                                                if($a_r_size == sizeof($attendance_report)){
//                                                    $sd = $ndd +1;
//                                                }else{
//                                                    $sd = 1;
//                                                }
                                                $r++;
                                                $iii = false;

                                                break;
                                            }
                                            $nr = false;
                                            $tr = false;
                                            $sd = $i+1;
                                            $r++;
                                            break;
                                        }
                                        ?>
                                    <?php } elseif($row->check_in_date == $full_date ) {
//                                        echo "<script> alert('".$row->check_in_date." ". $full_date."'); </script>";
                                        ?>
                                        <td class="text-center" style="color: green"><strong> P</strong></td>
                                        <!--                                        <td class="text-center" style="color: green"><strong> --><?php //echo $full_date ." ".$i  ?><!-- P</strong></td>-->
                                        <?php
//                                        echo "<script> alert('".$attendance_report[$r]->employee_id." check ".$r ." ". $attendance_report[$r+1]->employee_id." Dates ".$row->check_in_date." & ".$full_date."'); </script>";
                                        if($attendance_report[$r]->employee_id != $attendance_report[$r+1]->employee_id ) {
//                                         echo "<script> alert('".$attendance_report[$r]->employee_id." Not Same ". $attendance_report[$r+1]->employee_id."'); </script>";
                                            fll_miss_rows(($i+1),$ndd,$start_date,$attendance_report[$r]->employee_id,$l_array_size,$emp_leave);
                                            $nr = true;
                                            $tr = true;
                                            if($a_r_size == sizeof($attendance_report)){
                                                $sd = $ndd +1;
                                            }else{
                                                $sd = 1;
                                            }
                                            $r++;
                                            $iii = false;

                                            break;
                                        }
//                                        echo "<script> alert('".$attendance_report[$r]->employee_id." Same ". $attendance_report[$r+1]->employee_id."'); </script>";
                                        $nr = false;
                                        $tr = false;
                                        $sd = $i+1;
                                        $r++;
                                        break;
                                    } else {
                                        if($text = check_leave($l_array_size,$emp_leave,$row->employee_id,$full_date) ){ ?>
                                            <td class="text-center" style="color: #0b68f4"><strong> <?php if($text == 1) echo "Hl";if($text == 2) echo "Fl";if($text == 3) echo "Ql"?> </strong></td>
                                        <?php }else{?>
                                            <td class="text-center" style="color: red"><strong> A</strong></td>
                                            <!--                                            <td class="text-center" style="color: red"><strong> --><?php //echo $full_date ." ".$i  ?><!-- A</strong></td>-->
                                        <?php }}
                                    if($i == $ndd+1){
//                                    echo "<script> alert('RC'); </script>";
                                        $nr = true;
                                        $tr = true;
                                        if($iii){
//                                        echo "<script> alert('RCN'); </script>";
                                            $sd = 1;
                                            $r++;
                                        }
                                    }
                                }
                                ?>
                                <?php if ($tr){?>
                                    <td> <?php echo get_total_hr($row->employee_id,$total_working_hr)?> </td>
                                    </tr>

                                    <?php $sl++; ?>
                                <?php } ?>
                                <?php $employee_id = $row->employee_id; } ?>
                            <?php if($sd < $ndd){
                                fll_miss_rows($sd ,$ndd, $start_date,$employee_id,$l_array_size,$emp_leave );
                                ?>
                                <td> <?php echo get_total_hr($row->employee_id,$total_working_hr)?> </td>
                                </tr>
                                <?php $sl++; ?>
                            <?php }?>

                        <?php  }
                        if (!empty($remaining_emp)){
                            foreach ($remaining_emp as $row){ ?>
                                <tr class="<?php echo ($sl & 1)?"odd gradeX":"even gradeC" ?>">
                                    <td><?php echo $sl; ?></td>
                                    <td><?php echo $row->employee_name ?></td>
                                    <td><?php echo $row->department_name ?></td>
                                    <?php
                                    $ndd = $day_difference;

                                    for($i=1 ; $i<=$ndd+1;$i++){
                                        $xmasDay = date('Y-m-d', strtotime('+'.($i-1).' day', strtotime($start_date)));
                                        $day = date("D", strtotime($xmasDay));
                                        $full_date = date("Y-m-d", strtotime($xmasDay));

                                        if($day == 'Sat' || $day == 'Sun'){
                                            ?>
                                            <td class="text-center" style="color: black"><strong>HD</strong></td>
                                        <?php } else{
                                            fll_miss_rows($i , $ndd, $start_date,$row->employee_id,$l_array_size,$emp_leave);
                                            break;
                                            ?>
                                            <?php
                                        }
                                    } ?>
                                    <td> <?php echo get_total_hr($row->employee_id,$total_working_hr)?> </td>
                                </tr>
                                <?php $sl++; }
                        }
                        ?>
                        </tbody>
                    </table>  <!-- /.table-responsive -->
                <?php } ?>
            </div>
            <div class="biopagination">  <?php echo  $links ?></div>
        </div>
    </div>

</div>

<div id="filter" class="modal fade" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <center><strong><h4><i class='fa fa-book' aria-hidden='true'></i>Filter Reports</h4></strong></center>
            </div>
            <div class="modal-body">


                <div class="row">
                    <div class="col-sm-12 col-md-12">
                        <div class="panel">
                            <div class="panel-heading">
                                <div class="panel-title">

                                </div>
                            </div>
                            <div class="panel-body">

                                <?php echo  form_open_multipart('reports/Employee_controller/filter_attendance_summary') ?>

                                <div class="row">
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for="work_location" class="">
                                                <?php echo "Work Location:" ?></label>
                                            <?php
                                            echo form_dropdown('work_location', $country_list, (!empty($data->work_in_state) ? $data->work_in_state : "New York"), ' class="form-control"')

                                            ?>
                                        </div>
                                    </div>
                                    <div class="col-sm-3" id="start_date_div">
                                        <div class="form-group">
                                            <label for="from_date" class="">
                                                <?php echo "from Date" ?></label>
                                            <input type="text" name="start_date" placeholder="<?php echo "Enter Start Date" ?>" class="form-control datepicker">
                                        </div>

                                    </div>
                                    <div class="col-sm-3" id="end_date_div">
                                        <div class="to_date">
                                            <label for="courses_name" class="">
                                                <?php echo "To Date" ?></label>
                                            <input type="text" name="to_date" placeholder="<?php echo "Enter End date" ?>" class="form-control datepicker">
                                        </div>

                                    </div>

                                </div>
                                <div class="row">

                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for="department" class="">
                                                <?php echo "Department:" ?></label>
                                            <!--                                            <select name="department" id="department" onchange="select_employees_by_department(this.value)" class="form-control select2-hidden-accessible" tabindex="-1" aria-hidden="true" autocomplete="off">-->
                                            <select name="department" id="department" class="form-control select2-hidden-accessible" tabindex="-1" aria-hidden="true" autocomplete="off">
                                                <option value="0"> All</option>
                                                <?php
                                                foreach ($dropdowndept as $dpts) {
                                                    echo '<option value="' . $dpts['dept_id'] . '">' . $dpts['department_name'] . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>

                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for="duty_types" class="">
                                                <?php echo "Duty Types:" ?></label>
                                            <select name="duty_types" id="duty_types" class="form-control select2-hidden-accessible" tabindex="-1" aria-hidden="true" autocomplete="off">

                                                <option value="0"> All</option>
                                                <option value="1"> Full Time</option>
                                                <option value="2"> Part Time</option>
                                                <option value="3"> Contructual</option>
                                                <option value="4"> Other</option>
                                                <option value="5"> Intern</option>
                                            </select>
                                        </div>

                                    </div>
                                </div>
                                <div class="row">
                                </div>
                                <div class="row">
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for="employees_status" class="">
                                                <?php echo "Employees Status:" ?></label>
                                            <select name="employees_status" id="employees_status" class="form-control select2-hidden-accessible" tabindex="-1" aria-hidden="true" autocomplete="off">
                                                <option value="">Select Status</option>
                                                <option value="4">Cancelled </option>
                                                <option value="3">Not Started </option>
                                                <option value="2">On Leave </option>
                                                <option value="1">Active </option>
                                                <option value="0">Inactive</option>
                                            </select>
                                        </div>

                                    </div>
                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label for="employees" class="">
                                                <?php echo "Employees:" ?></label>
                                            <select name="employees" id="employees" class="form-control select2-hidden-accessible" tabindex="-1" aria-hidden="true" autocomplete="off">
                                                <option value="0">All</option>
                                                <?php foreach ($dropdownemp as $row) { ?>
                                                    <option value="<?php echo $row['employee_id'] ?>"><?php echo $row['first_name'].' '.$row['last_name']; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>

                                    </div>
                                </div>

                                <div class="form-group form-group-margin text-right">
                                    <!--                                    <button type="reset" class="btn btn-primary w-md m-b-5">--><?php //echo display('reset') ?><!--</button>-->
                                    <button type="submit" class="btn btn-success w-md m-b-5"><?php echo 'View Reports' ?></button>
                                </div>
                                <?php echo form_close() ?>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">

        </div>
    </div>
</div>

<script>
    // function check_attendance_status(){
    //     var x = document.getElementById("attendance_status").value;
    //     alert(x);
    //
    //     if(x == "1") {
    //         alert(x);
    //         // #start_date{ display:none; };
    //         document.getElementById("start_date").style.display = "none";
    //     }
    //     else if(x=="2"){
    //         alert(x);
    //         document.getElementById("end_date_div").element.style.visibility = 'hidden';
    //     }
    // }
</script>