<div class="section">
    <div class="row">
        <div class="small-7 columns">
            <h1><i class="fa fa-file"></i>Time Sheets <small>Active</small></h1>
        </div>
        <div class="small-5 columns text-right">
            <h1 class="subheader"><?php echo $this->monthName ?> <?php echo $this->year?></h1>
        </div>
    </div>
    <div class="row">
        <div class="small-12 columns">
            <ul class="button-group radius even-6">
                <?php

                $year = $this->year;

                if($this->month == 12)
                {
                    $year = $this->nextYear;
                }

                if($this->month == 1)
                {
                    $year = $this->previousYear;
                }


                ?>
                <li><a class="button small secondary" href="
                    <?php
                    switch ($this->month)
                    {
                        case 01:
                            echo $this->link(array('reports',$this->previousYear, $this->previousMonth));
                            break;
                        default:
                            echo $this->link(array('reports',$this->year, $this->previousMonth));
                    }
                    ?>
                    "><i class="fa fa-caret-left"></i> Previous</a></li>
                <li><a class="button small secondary" href="
                    <?php
                    switch ($this->month)
                    {
                        case 12:
                            echo $this->link(array('reports',$this->nextYear, $this->nextMonth));
                            break;
                        default:
                            echo $this->link(array('reports',$this->year, $this->nextMonth));
                    }
                    ?>
                    ">Next <i class="fa fa-caret-right"></i></a></li>


                <li><a id="showAll" class="button small secondary" href="#"><i class="fa fa-eye"></i> Show All</a></li>
                <li><a id="hideAll" class="button small secondary" href="#"><i class="fa fa-eye-slash"></i> Hide All</a></li>
                <li><a class="button small" href="<?php echo $this->link(array('reports','weekly')) ?>"><i class="fa fa-file"></i> Week Report</a></li>
                <li><a class="button small" data-reveal-id="print"  href="#"><i class="fa fa-print"></i> Print</a></li>
            </ul>
        </div>
    </div>
    <div class="row">
        <div class="small-12 columns">
            <?php
            $i = 0;
            foreach($this->report as $user=>$timesheet)
            {
                $userId = $timesheet['id'];
                echo "<h3 id='user'.$i.'' class='timeTitle'>$user <i class='fa fa-chevron-down right'></i></h3>";
                echo "
        <div class=\"wrapper hide\">";

                if(count($timesheet) > 0)
                {
                    echo"
                    <table width='100%'>
                        <thead>
                        <tr>
                            <th>Date</th>
                            <th>In</th>
                            <th>Out</th>
                            <th>Less Time</th>
                            <th>Hours</th>
                            <th>Code</th>
                            <th>Date Stamp</th>
                            <th>Validated</th>";
                            if($this->accountLevel >= 900)
                            {
                                echo "<th>Action</th>";
                            }
                        echo"</tr>
                        </thead>
                    ";
                }

                $totalValidated = 0;
                $totalInvalid = 0;
                $totalVacation = 0;
                $totalSick = 0;

                foreach($timesheet as $key=>$entry)
                {
                   if($key != 'id')
                   {
                        echo "
                            <tr>
                                   <td>".date("l, F jS Y",strtotime($entry['date']))."</td>
                                   <td>".date("g:i A",$entry['inTime'])."</td>
                                   <td>".date("g:i A",$entry['outTime'])."</td>
                                   <td>".$entry['lessTime']." <small>Hours</small></td>
                                   <td>".$entry['timeWorked']."</td>
                                   <td>".$entry['code']."</td>
                                   <td>".date("M. jS Y @ G:i A",strtotime($entry['timestamp']))."</td>
                                   <td><div class='text-center'>";

                        if($entry['validated'] == 1)
                        {
                            echo "<i class=\"fa fa-check green\"></i>";
                        }
                        else
                        {
                            echo "<i class=\"fa fa-close red\"></i>";
                        }

                        echo "</td>";

                        if($this->accountLevel >= 900)
                        {
                            echo "<td><a href=\"".$this->link(array('timesheet','remove',$key))."\"><i class=\"fa fa-trash\"></i> Remove</a></td>";
                        }

                        echo "</tr>";

                        if(strlen($entry['note']) > 0)
                        {
                            echo "
                                <tr>
                                    <td colspan='9'>
                                        <b>Note:</b> ".$entry['note']."
                                    </td>
                                </tr>
                            ";
                        }

                        if($entry['validated'] == 1)
                        {
                            $totalValidated += $entry['timeWorked'];
                        }

                        if($entry['validated'] == 0)
                        {
                            $totalInvalid += $entry['timeWorked'];
                        }

                        if($entry['code'] == "Vacation")
                        {
                            $totalVacation += $entry['timeWorked'];
                        }

                        if($entry['code'] == "Sick")
                        {
                            $totalSick += $entry['timeWorked'];
                        }

                        if($entry['code'] == "Sick (Family)")
                        {
                            $totalSick += $entry['timeWorked'];
                        }
                   }
                }

                if(count($timesheet) > 0)
                {
                echo "</table>";

                echo "<div class=\"row\">";
                echo "<div class=\"small-6 medium-4 large-3 columns\">";
                echo "<div class=\"card successBg\">
                    <div class=\"title\">Validated</div>
                    <div class=\"value\">".$totalValidated." <small>Hours</small></div>
                </div>";
                echo "</div>";
                echo "<div class=\"small-6 medium-4 large-3 columns\">";
                echo "<div class=\"card warning\">
                    <div class=\"title\">Not Validated</div>
                    <div class=\"value\">".$totalInvalid." <small>Hours</small></div>
                </div>";
                echo "</div>";
                echo "<div class=\"small-6 medium-4 large-3 columns\">";
                echo "<div class=\"card\">
                        <div class=\"title\">Sick</div>
                        <div class=\"value\">".$totalSick." <small>Hours</small></div>
                </div>";
                echo "</div>";
                echo "<div class=\"small-6 medium-4 large-3 columns\">";
                echo "<div class=\"card\">
                    <div class=\"title\">Vacation</div>
                    <div class=\"value\">".$totalVacation." <small>Hours</small></div>
                </div>";
                echo "</div>";

                echo "<div class=\"small-12 columns text-center\">";

                if(array_key_exists($user, $this->reviewed))
                {
                    echo "<h4><i class='fa fa-check'></i> Reviewed by <b>".$this->reviewed[$user]['supervisor']."</b> on ".$this->reviewed[$user]['reviewDateFormatted']."</h4>";
                }
                else
                {
                    echo "<a class='button radius' href='".$this->link(array('reports','reviewed',$userId,$this->year,$this->month))."'>Mark as reviewed</a>";
                }

                echo"</div>";
                echo "</div>";

                }
                else
                {
                    echo "<div class=\"text-center\">No time submitted</div>";
                }

                $i++;
                echo "</div><hr>";
            }
            ?>
        </div>
    </div>
</div>
<div id="yearForm" class="reveal-modal small" data-reveal aria-labelledby="Change Year" aria-hidden="true" role="dialog">
    <h2 id="modalTitle">Select a Year</h2>
    <?php echo $this->yearForm ?>
    <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</div>

<div id="print" class="reveal-modal small" data-reveal aria-labelledby="Print Report" aria-hidden="true" role="dialog">
    <h2>Print Individual Time Sheet</h2>
    <?php echo $this->printTimeSheetForm ?>
    <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</div>

<script>
    $(function() {

        $(".timeTitle").click(function() {
            $(this).next(".wrapper").slideToggle("slow");
            $(this).find("i").toggleClass("fa-chevron-up fa-chevron-down");
            return false;
        });

        $("#hideAll").click(function() {
            $(".wrapper").slideUp();
            $(".timeTitle").find("i").removeClass("fa-chevron-up");
            $(".timeTitle").find("i").addClass("fa-chevron-down");
            return false;
        });

        $("#showAll").click(function() {
            $(".wrapper").slideDown();
            $(".timeTitle").find("i").removeClass("fa-chevron-down");
            $(".timeTitle").find("i").addClass("fa-chevron-up");
            return false;
        });

    });
</script>