@model PantheonRA.BackOffice.Models.TimeRecordViewModel
@using PantheonRA.WebApi.Contracts.Enums;

@{
    Layout = null;

    var days = Model.TimeRecords
        .Where(s => s.DateStart != null && s.CalendarId != CalendarCodes.Plan)
        .GroupBy(x => x.DateStart.Value.Date);

    var index = 0;

    var canDelete = Model.UserPermissions.CanDelete;
    var daysForChange = Model.UserPermissions.DaysForChange;

    var noteColumn = Model.GridSettings.Where(s => s.ColumnName == FormGridColumnNames.Note).FirstOrDefault();
    var departmentColumn = Model.GridSettings.Where(s => s.ColumnName == FormGridColumnNames.Department).FirstOrDefault();
    var costDrvColumn = Model.GridSettings.Where(s => s.ColumnName == FormGridColumnNames.CostDrv).FirstOrDefault();
    var warningColumn = Model.GridSettings.Where(s => s.ColumnName == FormGridColumnNames.Warning).FirstOrDefault();
    var consideredColumn = Model.GridSettings.Where(s => s.ColumnName == FormGridColumnNames.Considered).FirstOrDefault();
}

@foreach (var day in days)
{
    var dayOfWeek = day.Key.GetDayName();
    var dayCaption = (day.Key.Date == DateTime.Now.Date ?
        string.Format(" - {0}", Loc.Today) : string.Format("- {0}", dayOfWeek));
    var canDayDelete = canDelete && (DateTime.Now.AddDays(-daysForChange).Date <= day.Key.Date);

    var dayPlanned = Model.TimeRecords.Where(s => s.DateStart != null && s.DateStart.Value.Date == day.Key.Date).FirstOrDefault();
    var dayPlannedMin = dayPlanned == null ? 0 : dayPlanned.PlannedForDayTimeMin;

    var dayActualMin = day.Sum(s => s.ActualTimeMin);
    var dayConsideredMin = day.Sum(s => s.ConsideredTimeMin);

    <table class="table table-condensed table-striped time-record-table custom-mobile-table">
        <thead>
            <tr>
                <th style="width:30%">
                    <h4 class="panel-title">
                        <a role="button" id="timerecord-th-@index" class="text-color timerecord-th" onclick="collapseSingleRecord(@index)">
                            @day.Key.ToString(Loc.DateFormat) @dayCaption
                        </a>
                    </h4>
                </th>
                <th style="width:18%"></th>
                <th style="width:13%" class="hidden-sm"></th>
                <th style="width:12%" class="hidden-sm"></th>
                <th style="width:5%"></th>
                <th colspan="2" class="color-total color-total-th" id="color-total-@index">

                    @foreach (var color in day.GroupBy(s => s.BackgroundColor))
                    {
                        var width = 100 / day.Count();
                        var widthPerc = string.Format("{0}%", width * color.Count());

                        var colorRecords = day.Where(s => s.BackgroundColor == color.Key);
                        var recordIcon = string.Format("fa {0}", colorRecords.FirstOrDefault().IconName);
                        var minutes = colorRecords.Sum(s => s.ActualTimeMin);

                        <div class="entry-type-color"
                             style="float:left;width:@widthPerc;border:4px solid @color.Key;"
                             rel="tooltip" data-toggle="tooltip" data-html="true" data-title="<i class='@recordIcon' style='color:@color.Key'></i> @minutes.DisplayTimeFormat()"></div>
                    }

                </th>
                <th style="width:2%" class="saldo-dropdown-button">
                    @dayActualMin.DisplayTimeFormat()
                </th>
                <th style="width:2%" class="saldo-dropdown-button">
                    <div class="@consideredColumn.ColumnName @(consideredColumn.IsVisible ? string.Empty : "hidden")">
                        @Math.Abs(dayConsideredMin).DisplayTimeFormat()
                    </div>
                </th>
                <th class="text-right" style="width:8%">
                    <div class="saldo-dropdown-icon saldo-dropdown-button">
                        <a role="button" data-toggle="dropdown" class="" id="saldo-button">
                            <span class="default-icon-color"><i class="fa fa-info-circle"></i></span>
                        </a>
                        <ul class="dropdown-menu totals-info-menu">
                            @{
                                var saldo = dayConsideredMin - dayPlannedMin;
                            }
                            <li class="saldo-dropdown-list"><span>@Loc.PlannedHour</span> <strong class="pull-right">@dayPlannedMin.DisplayTimeFormat()</strong></li>
                            <li class="saldo-dropdown-list"><span>@Loc.ActualHour</span> <strong class="pull-right">@dayActualMin.DisplayTimeFormat()</strong></li>
                            <li class="saldo-dropdown-list" style="border-bottom:1px solid;">
                                <span>@Loc.ConsideredHour</span>
                                <strong class="pull-right"> @Math.Abs(dayConsideredMin).DisplayTimeFormat()</strong>
                            </li>
                            <li class="saldo-dropdown-list">
                                @Loc.Saldo
                                <strong class="pull-right @(saldo < 0 ? "red-saldo" : string.Empty)">
                                    @(saldo < 0 ? string.Format("-{0}", Math.Abs(saldo).DisplayTimeFormat()) : saldo.DisplayTimeFormat())
                                </strong>
                            </li>
                        </ul>
                    </div>
                </th>
            </tr>
        </thead>
        <tbody id="timerecord-tb-@index" class="timerecord-tb">
            @foreach (var item in day)
            {
                @Html.Hidden("DateStart-" + item.AnQId, item.DateStart)
                @Html.Hidden("DateEnd-" + item.AnQId, item.DateEnd)

                var isEditable = !item.IsAbsence && item.CalendarId != CalendarCodes.Company && item.CalendarId != CalendarCodes.Department;

                <tr class="time-record-row" style="border-left-color:@item.BackgroundColor">
                    <td label="@day.Key.ToString(Loc.DateFormat) @dayCaption">
                        <div class="row">
                            <div class="col-md-1 hidden-sm hidden-xs" id="Icon-@item.AnQId">
                                <i class="fa @item.IconName" style="color:@item.BackgroundColor"></i>
                            </div>
                            <div class="col-md-11 col-sm-11 col-xs-9 time-record-selects">

                                @if (!isEditable)
                                {
                                    <span class="m-l-sm">@item.TypeName</span>
                                }
                                else
                                {
                                    <select id="TypeId-@item.AnQId"
                                            name="Type-@item.AnQId"
                                            class="select2OnClick"
                                            onchange="editRecord(@item.AnQId, 'TypeId','TypeId-' + @item.AnQId)"
                                            filter-by="EntryTypeNumber"
                                            data-search-width="200"
                                            data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/calendar-entry-types/icon-types" })
                                            style="display: none;">
                                        <option selected value="@item.TypeCode">@item.TypeName</option>
                                    </select>
                                    <span class="select2-container select2-container--bootstrap select2OnClick duplicateToDelete"
                                          dir="ltr"
                                          id="TypeId-@item.AnQId"
                                          name="Type-@item.AnQId"
                                          onchange="editRecord(@item.AnQId, 'TypeId', 'TypeId-' + @item.AnQId)">
                                        <span class="selection">
                                            <span class="select2-selection select2-selection--single"
                                                  role="combobox"
                                                  aria-haspopup="true"
                                                  aria-expanded="false"
                                                  tabindex="0"
                                                  aria-labelledby="select2-TypeId-@item.AnQId-container">
                                                <span class="select2-selection__rendered" id="select2-TypeId-@item.AnQId-container" title="@item.TypeName">
                                                    @item.TypeName
                                                </span>
                                                <span class="select2-selection__arrow" role="presentation">
                                                    <b role="presentation"></b>
                                                </span>
                                            </span>
                                        </span>
                                    </span>
                                }
                                <i class='fa fa-spinner fa-spin fa-fw hidden'></i>
                            </div>
                            <div class="hidden-lg hidden-md hidden-sm col-xs-3 action-buttons-mobile">
                                <div style="float: right">
                                    @if (isEditable)
                                    {
                                        if (canDayDelete)
                                        {
                                            @Html.Partial("_RecordsActionButtons", item)

                                        }
                                    }
                                </div>
                            </div>
                        </div>
                    </td>
                    <td label="@Loc.Note">
                        <div class="@noteColumn.ColumnName @(noteColumn.IsVisible ? string.Empty : "hidden")">
                            @if (isEditable)
                            {
                                <button type="button" id="note-button" class="btn btn-link m-r-sm" style="padding:0px" onclick="openNoteModal(@item.AnQId)">
                                    <i class="fa fa-comment time-record-hidden-icon @(string.IsNullOrEmpty(item.Note) ? "" : "has-note")" style="color: transparent"></i>
                                </button>
                            }
                            <span id="note-span-@item.AnQId" title="@item.Note" class="timerecord-note">
                                @item.Note
                            </span>

                            <input type="hidden"
                                   id="NoteId-@item.AnQId"
                                   name="Note-@item.AnQId"
                                   onchange="editRecord(@item.AnQId)"
                                   value="@item.Note">
                        </div>
                    </td>
                    <td label="@Loc.Department" class="time-record-selects">
                        <div class="row @departmentColumn.ColumnName @(departmentColumn.IsVisible ? string.Empty : "hidden")" style="display:flex">
                            <div class="col-md-1 hidden-sm hidden-xs">
                                <button type="button" class="btn btn-link" style="padding:0px">
                                    <i class="fas fa-map-marker-alt time-record-hidden-icon" style="color: transparent"></i>
                                </button>
                            </div>
                            <div class="col-md-11">
                                @if (!isEditable)
                                {
                                    <span class="m-l-sm @(string.IsNullOrEmpty(item.CalendarDept) ? "show-placeholder" : string.Empty)">
                                        @(string.IsNullOrEmpty(item.CalendarDept) ? Loc.Department : item.CalendarDept)
                                    </span>
                                }
                                else
                                {
                                    <select id="DepartmentId-@item.AnQId"
                                            name="Department-@item.AnQId"
                                            class="select2OnClick"
                                            data-placeholder="@Loc.Department"
                                            onchange="editRecord(@item.AnQId, 'DepartmentId', 'DepartmentId-' +@item.AnQId)"
                                            data-search-width="190"
                                            data-allow-clear="true"
                                            data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/time-records/departments" })
                                            style="display: none;">
                                        @if (item.CalendarDept.IsEmpty() || item.CalendarDept == null)
                                        {
                                            <option value="" selected disabled hidden>@Loc.Department</option>
                                        }
                                        else
                                        {
                                            <option selected value="@item.CalendarDept">@item.CalendarDept</option>
                                        }
                                    </select>
                                    <span class="select2-container select2-container--bootstrap select2OnClick duplicateToDelete" 
                                          dir="ltr"
                                          id="DepartmentId-@item.AnQId"
                                          name="Department-@item.AnQId"
                                          onchange="editRecord(@item.AnQId, 'DepartmentId', 'DepartmentId-' +@item.AnQId)">
                                        <span class="selection">
                                            <span class="select2-selection select2-selection--single" 
                                                  role="combobox" 
                                                  aria-haspopup="true" 
                                                  aria-expanded="false" 
                                                  tabindex="0"
                                                  aria-labelledby="select2-DepartmentId-@item.AnQId-container">
                                                <span class="select2-selection__rendered" id="select2-DepartmentId-@item.AnQId-container" title="@item.CalendarDept">
                                                    @if (item.CalendarDept.IsEmpty() || item.CalendarDept == null)
                                                    {
                                                        <span class="select2-selection__placeholder">@Loc.Department</span>
                                                    }
                                                    else
                                                    {
                                                        <span class="select2-selection__clear">×</span>
                                                    }
                                                    @item.CalendarDept
                                                </span>
                                                <span class="select2-selection__arrow" role="presentation">
                                                    <b role="presentation"></b>
                                                </span>
                                            </span>
                                        </span>
                                    </span>
                                }
                                <i class='fa fa-spinner fa-spin fa-fw hidden'></i>
                            </div>
                        </div>
                    </td>
                    <td label="@Loc.CostCarrier" class="time-record-selects">
                        <div class="row @costDrvColumn.ColumnName @(costDrvColumn.IsVisible ? string.Empty : "hidden")" style="display:flex">
                            <div class="col-md-12">
                                @if (!isEditable)
                                {
                                    <span class="m-l-sm @(string.IsNullOrEmpty(item.CostDrv) ? "show-placeholder" : string.Empty)">
                                        @(string.IsNullOrEmpty(item.CostDrv) ? Loc.CostCarrier : item.CostDrv)
                                    </span>
                                }
                                else
                                {
                                    <select id="CostDrvId-@item.AnQId"
                                            name="CostDrv-@item.AnQId"
                                            class="select2OnClick"
                                            data-placeholder="@Loc.CostCarrier"
                                            onchange="editRecord(@item.AnQId, 'CostDrvId', 'CostDrvId-' +@item.AnQId)"
                                            data-mobile-width="190"
                                            data-allow-clear="true"
                                            data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/time-records/cost-drivers" })
                                            style="display: none;">
                                        @if (item.CostDrv.IsEmpty() || item.CostDrv == null)
                                        {
                                            <option value="" selected disabled hidden>@Loc.CostCarrier</option>
                                        }
                                        else
                                        {
                                            <option selected value="@item.CostDrv">@item.CostDrv</option>
                                        }
                                    </select>
                                    <span class="select2-container select2-container--bootstrap select2OnClick duplicateToDelete"
                                          dir="ltr"
                                          id="CostDrvId-@item.AnQId"
                                          name="CostDrv-@item.AnQId"
                                          onchange="editRecord(@item.AnQId, 'CostDrvId', 'CostDrvId-' +@item.AnQId)">
                                        <span class="selection">
                                            <span class="select2-selection select2-selection--single"
                                                  role="combobox"
                                                  aria-haspopup="true"
                                                  aria-expanded="false"
                                                  tabindex="0"
                                                  aria-labelledby="select2-CostDrvId-@item.AnQId-container">
                                                <span class="select2-selection__rendered" id="select2-CostDrvId-@item.AnQId-container" title="@item.CostDrv">
                                                    @if (item.CostDrv.IsEmpty() || item.CostDrv == null)
                                                    {
                                                        <span class="select2-selection__placeholder">@Loc.CostCarrier</span>
                                                    }
                                                    else
                                                    {
                                                        <span class="select2-selection__clear">×</span>
                                                    }
                                                    @item.CostDrv
                                                </span>
                                                <span class="select2-selection__arrow" role="presentation">
                                                    <b role="presentation"></b>
                                                </span>
                                            </span>
                                        </span>
                                    </span>
                                }
                                <i class='fa fa-spinner fa-spin fa-fw hidden'></i>
                            </div>
                        </div>
                    </td>
                    <td class="text-right hidden-xs hidden-md hidden-lg">
                        @if (isEditable)
                        {
                            if (canDayDelete)
                            {
                                @Html.Partial("_RecordsActionButtons", item)

                            }
                        }
                    </td>
                    <td label="@Loc.Warning">
                        <div class="row @warningColumn.ColumnName @(warningColumn.IsVisible ? string.Empty : "hidden")">
                            <div class="col-md-1  hidden-sm hidden-xs">
                                <button type="button" class="btn btn-link" style="padding:0px">
                                    <i class="fas fa-info time-record-hidden-icon" style="color: transparent"></i>
                                </button>
                            </div>
                            <div class="col-md-11 text-danger">

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

                    <td label="@Loc.TaBegin" class="time-record-timepickers">
                        @if (canDayDelete && isEditable)
                        {
                            <input type="time"
                                   id="TimeStartId-@item.AnQId"
                                   name="TimeStart-@item.AnQId"
                                   value="@item.DateStart.Value.ToString("HH:mm")"
                                   class="form-control tr-time-picker"
                                   onblur="editRecord(@item.AnQId, null, 'TimeStartId-' +@item.AnQId)"
                                   autocomplete="off">
                        }
                        else
                        {
                            <input type="hidden"
                                   value="@item.DateStart.Value.ToShortTimeString()"
                                   id="TimeStartId-@item.AnQId"
                                   name="TimeStart-@item.AnQId" />

                            <span style="padding-left:13px">@item.DateStart.Value.ToShortTimeString()</span>
                        }
                        <i class='fa fa-spinner fa-spin fa-fw hidden'></i>
                    </td>
                    <td label="@Loc.TaEnd" class="time-record-timepickers">
                        @if (canDayDelete && isEditable)
                        {
                            <input type="time"
                                   id="TimeEndId-@item.AnQId"
                                   name="TimeEnd-@item.AnQId"
                                   value="@(item.DateEnd != null ? item.DateEnd.Value.ToString("HH:mm") : string.Empty)"
                                   class="form-control tr-time-picker"
                                   onblur="editRecord(@item.AnQId, null, 'TimeEndId-' +@item.AnQId)"
                                   autocomplete="off">
                        }
                        else
                        {
                            <input type="hidden"
                                   id="TimeEndId-@item.AnQId"
                                   name="TimeEnd-@item.AnQId"
                                   value="@(item.DateEnd != null ? item.DateEnd.Value.ToShortTimeString() : string.Empty)">

                            <span style="padding-right:30px">@(item.DateEnd.HasValue ? item.DateEnd.Value.ToShortTimeString() : "--:--")</span>
                        }
                        <input type="hidden" id="IsOpenRecord-@item.AnQId" value="@((!item.DateEnd.HasValue).ToString())" />
                        <i class='fa fa-spinner fa-spin fa-fw hidden'></i>
                    </td>
                    <td label="@Loc.Actual">
                        @item.ActualTimeMin.DisplayTimeFormat()
                    </td>
                    <td label="@Loc.Considered">
                        <div class="@consideredColumn.ColumnName @(consideredColumn.IsVisible ? string.Empty : "hidden")">
                            @item.ConsideredTimeMin.DisplayTimeFormat()
                        </div>
                    </td>
                    <td class="text-right hidden-sm hidden-xs">
                        @if (isEditable)
                        {
                            if (canDayDelete)
                            {
                                @Html.Partial("_RecordsActionButtons", item)
                            }
                        }
                    </td>
                </tr>
            }

            @{
                index++;
            }
        </tbody>
    </table>
}

@Html.Partial("_Footer", Model.FooterData)