@model PantheonRA.BackOffice.Models.TimeAttendanceViewModel
@using PantheonRA.WebApi.Contracts.Enums

@{
    Layout = null;
var workers = Model.TimeAttendancesViewResponse.TimeAttendances.Where(s => !string.IsNullOrEmpty(s.Worker)).GroupBy(s => s.Worker);
var records = Model.TimeAttendancesViewResponse.Records;
var filterDateStart = Model.FilterRequest.DateStart;
var filterDateEnd = Model.FilterRequest.DateEnd;
}

<table data-grid-id="time-attendance" class="table table-condensed table-striped custom-mobile-table showhidecolumns" style="margin:0px 20px 20px; border-collapse:unset;">
    <thead>
        <tr>
            <th data-column-id="Photo" data-sortable="false">
                <span style="color:transparent">-</span>
            </th>
            <th data-column-id="Code">
                @Loc.Code
            </th>
            <th data-column-id="Name">
                @Loc.LastnameAndFirstname
            </th>
            <th data-column-id="Department">
                @Loc.Department
            </th>
            <th data-column-id="CurrentStatus" class="timeattendence-hidden-column">
                @Loc.CurrentStatus
            </th>
            <th data-column-id="Arrival" class="timeattendence-hidden-column">
                @Loc.Arrival
            </th>
            <th data-column-id="ActivityForPeriod">
                @Loc.ActivityForPeriod
            </th>
            <th data-column-id="Departure" class="timeattendence-hidden-column">
                @Loc.Departure
            </th>
            <th data-column-id="Planned" class="alnright">
                @Loc.Planned
            </th>
            <th data-column-id="Actual" class="alnright">
                @Loc.Actual
            </th>
            <th data-column-id="Considered" class="alnright">
                @Loc.Considered
            </th>
            <th data-column-id="Absence" class="alnright">
                @Loc.Absence
            </th>
            <th data-column-id="Saldo" class="alnright">
                @Loc.Saldo
            </th>
            <th data-column-id="Note">
                @Loc.Note
            </th>
        </tr>
    </thead>
    <tbody>

        @foreach (var worker in workers)
        {
            <tr>
                <td>
                    @if (Model.TimeRecordsFilterData != null &&
                        Model.TimeRecordsFilterData.Employees != null &&
                        Model.TimeRecordsFilterData.Employees.Count() > 0)
                    {
                        var workerData = Model.TimeRecordsFilterData.Employees.FirstOrDefault(s => s.WorkerCode == worker.Key);

                        if (!string.IsNullOrEmpty(workerData.ProfilePicture))
                        {
                            <img class="dt-image-circle" src="@workerData.ProfilePicture" />
                        }
                        else
                        {
                            <div class="dt-text-avatar">
                                @workerData.Avatar
                            </div>
                        }
                    }
                </td>
                <td label="@Loc.Code">@worker.Key</td>
                <td label="@Loc.LastnameAndFirstname">
                    <a href="@Url.Action("Index", "TimeRecords", 
                       new { 
                           Workers = worker.Key, 
                           WorkerFromAttendence = worker.FirstOrDefault().LastName + " " + worker.FirstOrDefault().FirstName, 
                           DateStart = filterDateStart, 
                           DateEnd = filterDateEnd, 
                           Period = "CustomDate", 
                           IsWorkerFromAttendence = true, 
                           FilterDateFrom = true, 
                           FilterDateTo = true 
                       })">@worker.FirstOrDefault().LastName @worker.FirstOrDefault().FirstName</a>
                </td>
                <td label="@Loc.Department">@worker.FirstOrDefault().WorkerDept</td>
                <td label="@Loc.CurrentStatus" class="timeattendence-hidden-column">
                    @{
                        var openRecord = records.Where(s => (s.DateEnd == null) && s.Worker == worker.Key).FirstOrDefault();
                        if (openRecord == null)
                        {
                            openRecord = records.Where(s => (s.CalendarId == CalendarCodes.Absence) && s.Worker == worker.Key).FirstOrDefault();
                        }
                    }
                    @if (openRecord != null && !string.IsNullOrEmpty(openRecord.Type))
                    {
                        var currentStatus = string.Empty;
                        if (!string.IsNullOrEmpty(openRecord.TypeName))
                        {
                            currentStatus = openRecord.TypeName.Length <= 20 ? openRecord.TypeName : openRecord.TypeName.Substring(0, 20) + "...";
                        }
                        else
                        {
                            currentStatus = openRecord.Type;
                        }

                        <span class="label current-status" style="border:1px solid @openRecord.ColorBackground">
                            <i class="fa @openRecord.Icon m-l-sm" style="color:@openRecord.ColorBackground"></i> <span class="m-r-sm theme-text-color">@currentStatus</span>
                        </span>
                    }
                </td>
                <td label="@Loc.Arrival" class="timeattendence-hidden-column">
                    @if (filterDateStart.Date == filterDateEnd.Date)
                    {
                        var arrivalsCount = records
                            .Where(s => s.Date == filterDateStart.Date && s.Worker == worker.Key)
                            .Count();

                        if (arrivalsCount > 0)
                        {
                            var arrival = records
                                .Where(s => s.Date == filterDateStart.Date && s.Worker == worker.Key)
                                .First();

                            if (arrival != null)
                            {
                                <span>@arrival.DateStart.Value.ToShortTimeString()</span>
                            }
                        }
                    }
                </td>
                <td label="@Loc.ActivityForPeriod" class="color-total timeattendance-colors">
                    @{
                        var colors = worker.Where(s => s.CalendarId != CalendarCodes.Plan && s.CalendarId != null)
                            .Select(s => s.ColorBack);
                    }
                    @foreach (var color in colors)
                    {

                        var width = 100 / colors.Count();
                        var widthPerc = string.Format("{0}%", width);

                        var colorRecords = worker.Where(s => s.ColorBack == color && s.CalendarId != CalendarCodes.Plan);
                        var recordIcon = string.Format("fa {0}", colorRecords.FirstOrDefault().IconName);
                        var recordCalendarCode = colorRecords.FirstOrDefault().CalendarId;
                        var minutes = 0;
                        minutes = recordCalendarCode == CalendarCodes.ActualWork
                            ? colorRecords.Sum(s => s.ActualTimeMin)
                            : minutes = colorRecords.Sum(s => s.Absence);

                        <div class="entry-type-color"
                             style="float:left;width:@widthPerc;border:4px solid @color;background-color:@color"
                             rel="tooltip" data-toggle="tooltip" data-html="true" data-title="<i class='@recordIcon' style='color:@color'></i> @minutes.DisplayTimeFormat()"></div>
                    }
                </td>
                <td label="@Loc.Departure" class="timeattendence-hidden-column">
                    @if (filterDateStart.Date == filterDateEnd.Date)
                    {
                        var departuresCount = records
                            .Where(s => s.Date == filterDateStart.Date && s.Worker == worker.Key)
                            .Count();

                        if (departuresCount > 0)
                        {
                            var departure = records
                                .Where(s => s.Date == filterDateStart.Date && s.DateStart.HasValue && s.Worker == worker.Key)
                                .OrderByDescending(s => s.DateStart)
                                .First();

                            if (departure != null && departure.DateEnd != null)
                            {
                                <span>@departure.DateEnd.Value.ToShortTimeString()</span>
                            }
                            else
                            {
                                <span>--:--</span>
                            }
                        }
                    }
                </td>
                @{
                    var planned = worker.Where(s => s.CalendarId == CalendarCodes.Plan)
                        .Sum(s => s.PlannedForDayTimeMin);

                    var actual = Model.TimeAttendancesViewResponse.TimeAttendances
                        .Where(s => s.Worker == worker.Key && s.CalendarId != CalendarCodes.Plan && s.CalendarId != CalendarCodes.Absence)
                        .Sum(s => s.ActualTimeMin);

                    var considered = Model.TimeAttendancesViewResponse.TimeAttendances
                        .Where(s => s.Worker == worker.Key && s.CalendarId != CalendarCodes.Plan)
                        .Sum(s => s.ConsideredTimeMin);

                    var absence = Model.TimeAttendancesViewResponse.TimeAttendances
                        .Where(s => s.Worker == worker.Key)
                        .Sum(s => s.Absence);

                    var saldo = considered - planned + absence;
                }
                <td label="@Loc.Planned" class="alnright">@planned.DisplayTimeFormat()</td>
                <td label="@Loc.Actual" class="alnright">@actual.DisplayTimeFormat()</td>
                <td label="@Loc.Considered" class="alnright">@considered.DisplayTimeFormat()</td>
                <td label="@Loc.Absence" class="alnright">@absence.DisplayTimeFormat()</td>
                <td label="@Loc.Saldo" class="alnright @(saldo.ToString().Contains("-") ? "red-saldo" : string.Empty)" style="font-weight:bold">@saldo.DisplayTimeFormat()</td>
                <td label="@Loc.Note"></td>
            </tr>
        }
        @if (Model.TimeAttendancesViewResponse.TimeAttendances.Count() == 0)
        {
            <tr>
                <td colspan="14" class="text-center">
                    <div>@Loc.NoResultFound</div>
                </td>
            </tr>
        }
    </tbody>
</table>