
<div class="row grid-margin">
  <div class="col-lg-12">
      <div class="card">
          <div class="card-body" style="height:700px; overflow:scroll">
                <div class="row">
                    <div class="col-md-2">
                        <select class="form-control" id="year" onchange="getReport()">
                            <?php 
                                $year = date('Y');
                                for($i = $year; $i < $year + 5; $i++){
                                    echo "<option value='{$i}'>{$i}</option>";
                                }
                            ?>
                        </select>    
                    </div>
                    
                    <div class="col-md-2">
                        <select class="form-control" id="month" onchange="getReport()">
                            <option value="01">January</option>
                            <option value="02">February</option>
                            <option value="03">March</option>
                            <option value="04">April</option>
                            <option value="05">May</option>
                            <option value="06">June</option>
                            <option value="07">July</option>
                            <option value="08">Auguest</option>
                            <option value="09">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>
                        </select>    
                    </div>
                    <div class="col-md-2">
                        <div class="icheck">
                            <input type="checkbox" id="distance_enable" onclick="getReport()" <?php if(isset($show_distance) && $show_distance == 1 ) echo "checked";?>/>
                            <label for="distance_enable">Distance</label>
                        </div>
                    </div>
                </div>
                <?php 
                    if(count($result)>0) {
                    foreach($result as $username=>$user) {
                ?>
                    <br/>
                    <div class="row">
                        <table class='table table-bordered'>
                            <thead>
                                <tr>
                                    <th style="width: 100px">Name</th>
                                    <th style="width: 80px">Days</th>
                                    <?php 
                                        $last_day = date('t', strtotime($year."-".$month."-01"));
                                        $user_id = $user[0]["user_id"];
                                        for($day = 1; $day <= $last_day; $day++){
                                            $date = $day > 9? $year."-".$month."-".$day : $date = $year."-".$month."-0".$day;
                                            echo "<th scope='col' style='width:80px'><a href='".site_url('track/show_track')."/{$user_id}/{$date}'>{$day}</a></th>";
                                        } 
                                    ?>
                                    <th style="width: 80px">Total</th>
                                <tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td 
                                        <?php
                                        if (isset($show_distance) && $show_distance)
                                        {
                                            echo 'rowspan="4"';
                                        }else{
                                            echo 'rowspan="3"';
                                        }
                                        ?>
                                    ><?=$username?></td>
                                    <td>On Time</td>
                                    <?php 
                                        for($day = 1; $day <= $last_day; $day++){
                                            $date = $day > 9? "".$day : "0".$day;
                                            $val = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                            foreach($user as $data){
                                                if($date == $data["day"]) {
                                                    $val = $data["on_time"];
                                                }
                                            }
                                            echo "<td>{$val}</td>";
                                        } 
                                    ?>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td>Off Time</td>
                                    
                                    <?php 
                                        for($day = 1; $day <= $last_day; $day++){
                                            $date = $day > 9? "".$day : "0".$day;
                                            $val = "";
                                            foreach($user as $data){
                                                if($date == $data["day"]) {
                                                    $val = $data["off_time"];
                                                }
                                            }
                                            echo "<td>{$val}</td>";
                                        } 
                                    ?>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td>Hours</td>
                                    <?php 
                                        for($day = 1; $day <= $last_day; $day++){
                                            $date = $day > 9? "".$day : "0".$day;
                                            $val = "";
                                            foreach($user as $data){
                                                if($date == $data["day"]) {
                                                    $val = $data["duration"];
                                                }
                                            }
                                            echo "<td>{$val}</td>";
                                        } 
                                    ?>
                                    <td>&nbsp;</td>
                                </tr>
                                <?php
                                if(isset($show_distance) && $show_distance) {
                                ?>
                                <tr>
                                    <td>Distance</td>
                                    <?php 
                                        $total = 0;
                                        for($day = 1; $day <= $last_day; $day++){
                                            $date = $day > 9? "".$day : "0".$day;
                                            $val = "";
                                            foreach($user as $data){
                                                if($date == $data["day"]) {
                                                    $val = round($data["distance"], 2);
                                                    $total += $val;
                                                }
                                            }
                                            echo "<td>{$val}</td>";
                                        }
                                        echo "<td>{$total}</td>";
                                    ?>
                                </tr>
                                <?php } ?>
                            </tbody>

                        </table>
                    </div> 
                <?php 
                }
                }else{
                    echo "<p style='width:100%; text-align:center; margin-top:20px'>No data to display. Please sync your data with server.</p>";
                }
                ?>
           
          </div>
      </div>
  </div>
</div>
<script>
    $(function(){
        $("#year").val('<?=$year?>');
        $("#month").val('<?=$month?>');
    })

    function getReport(){
        var year = $("#year").val();
        var month = $("#month").val();
        var showDistance = $("#distance_enable").is(":checked") ? 1 : 0;
        <?php 
            $level = $this->session->userdata('level');
            if(isset($level) && $level < 3) {
        ?> 
            location.href = "<?=site_url('report/get_report_of_month/')?>" + year + "/" + month + "/" + showDistance;
        <?php 
            } else {
        ?>
            location.href = "<?=site_url('report/get_user_report_of_month/')?>" + year + "/" + month + "/" + showDistance;
        <?php } ?>
    }
</script>