@using PantheonRA.WebApi.Contracts;
@using PantheonRA.WebApi.Contracts.Enums;

@model PantheonRA.WebApi.Contracts.PaWoExRegOper

@{
    var _isMobile = Request.Browser.IsMobileDevice;
    List<string> workers = new List<string>();
    if (Model.Worker != null)
    {
        workers = Model.Worker.Split(',').Select(x => x.Trim()).ToList();
    }

    PaWoExItemResources WoExItemResurce = Model.WoExItem != null ? Model.WoExItem.WoExItemResources.FirstOrDefault(x => x.ResursID.Trim() == Model.ResursID.Trim()) : null;
    int resourceQID = WoExItemResurce.QId;

    double PlanQty = (Model.WoExItem != null) && (WoExItemResurce != null) ? (double)Model.WoExItem.WoEx.PlanQty * WoExItemResurce.ExecutionPerc / 100 : 0;
    double PlanTime = (Model.WoExItem != null) && (WoExItemResurce != null) ? WoExItemResurce.CTime : 0;
    double DoneQty = (Model.WoExItem != null) && (Model.WoExItem.WoExItemWork != null) ? (double)Model.WoExItem.WoExItemWork.Where(x => x.ResursID.Trim() == Model.ResursID.Trim()).Sum(p => p.Qty) : 0;
    double DoneTime = (Model.WoExItem != null) && (Model.WoExItem.WoExItemWork != null) ? (double)Model.WoExItem.WoExItemWork.Where(x => x.ResursID.Trim() == Model.ResursID.Trim()).Sum(p => p.Time) : 0;

    if (ViewBag.ProdType == ProductionType.SE)
    {
        DoneTime = (Model.WoExItem != null) && (WoExItemResurce != null) ? WoExItemResurce.PTime : 0;
    }

    double RestQty = PlanQty - DoneQty;

    TimeSpan _planTime = TimeSpan.FromHours(PlanTime);
    var PlanTimeStr = Math.Floor(_planTime.TotalHours).ToString("00") + ":" + _planTime.Minutes.ToString("00") + ":" + _planTime.Seconds.ToString("00");

    TimeSpan _doneTime = TimeSpan.FromHours(DoneTime);
    var DoneTimeStr = Math.Floor(_doneTime.TotalHours).ToString("00") + ":" + _doneTime.Minutes.ToString("00") + ":" + _doneTime.Seconds.ToString("00");

    string ProductUM = Model.WoExItem != null ? Model.WoExItem.WoEx.UM : String.Empty;
    string timeUM = Model.WoExItem != null ? Model.WoExItem.UM : String.Empty;

}

@* Desktop buttons *@
@if (!_isMobile)
{
    if (PantheonRA.BackOffice.CurrentUser.CanAccess(Permissions.PRODUCTION))
    {
        <div style="display:flex" class="padding-left-none">

            @* Start *@
            <a id="start" role="button" style="margin-left: 0 !important; font-size: 24px; line-height: 2; padding: 10px 24px;"
               class="btn start-btn  @(!Model.EventType.IsEmpty() ? "hidden" : string.Empty)"
               data-event-type="@WorkRegEventType.OperationStart"
               data-start-caption="@Loc.TaBegin"
               data-milliseconds="@Model.Milliseconds"
               onclick="startWorkRecording(this, true)"
               data-icon="far fa-stop-circle"
               data-color="#7EAF3F">
                <i class="fas fa-play"></i>
                <span>@Loc.TaBegin</span>
            </a>

            @* Pause *@
            <a id="pause" role="button" style="margin-left: 0 !important; font-size: 24px; line-height: 2; padding: 10px 24px;"
               class="btn pause-btn @(Model.EventType.IsEmpty() || Model.EventType == WorkRegEventType.OperationPause || Model.EventType == WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
               data-event-type="@WorkRegEventType.OperationPause"
               data-pause-caption="@Loc.Pause"
               data-milliseconds="@Model.Milliseconds"
               onclick="startWorkRecording(this, true)"
               data-icon="far fa-pause-circle"
               data-color="goldenrod">
                <i class="far fa-pause-circle"></i>
                <span class="hidden-xxs">@Loc.Pause</span>
            </a>

            @* Continue *@
            <a id="continue" role="button" style="margin-left: 0 !important; font-size: 24px; line-height: 2; padding: 10px 24px;"
               class="btn continue-btn @(Model.EventType.IsEmpty() || Model.EventType == WorkRegEventType.OperationStart || Model.EventType == WorkRegEventType.OperationContinue || Model.EventType == WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
               data-event-type="@WorkRegEventType.OperationContinue"
               data-pause-caption="@Loc.Continue"
               data-milliseconds="@Model.Milliseconds"
               onclick="startWorkRecording(this, true)"
               data-icon="far fa-play-circle"
               data-color="#7EAF3F">
                <i class="far fa-play-circle"></i>
                <span class="hidden-xxs">@Loc.Continue</span>
            </a>

            @if (PantheonRA.BackOffice.CurrentUser.CanAccess(Permissions.PRODUCTION))
            {
                @* Stop *@
                <a id="stop" role="button" style="font-size: 24px; line-height: 2; padding: 10px 24px;"
                   class="btn stop-btn @(Model.EventType.IsEmpty() || Model.EventType == WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
                   data-event-type="@WorkRegEventType.OperationEnd"
                   data-end-caption="@Loc.TaEnd"
                   data-milliseconds="@Model.Milliseconds"
                   onclick="startWorkRecording(this, true)"
                   data-icon="far fa-stop-circle"
                   data-color="#BB5534">
                    <i class="far fa-stop-circle"></i>
                    <span class="hidden-xxxs">@Loc.TaEnd</span>
                </a>
            }

            @* Cancel *@
            <a id="cancelButton" role="button" style="background-color: #BB5534; border-color: #BB5534;min-height: 66px; font-size: 24px; line-height: 2; padding: 10px 24px;"
               class="btn cancel-btn btn-primary @(!(Model.EventType == WorkRegEventType.OperationEnd) ? "hidden" : string.Empty)"
               onclick="deleteWorkReg(true)">
                <i class="far fa-window-close"></i>
                <span>@Loc.Cancel</span>
            </a>

            @if (PantheonRA.BackOffice.CurrentUser.CanAccess(Permissions.PRODUCTION))
            {
                @* Confirm *@
                <a id="confirmButton" role="button" style="min-height: 66px; font-size: 24px; line-height: 2; padding: 10px 24px;"
                   class="btn confirm-btn btn-primary @(!(Model.EventType == WorkRegEventType.OperationEnd) ? "hidden" : string.Empty)"
                   onclick="confirmOperationWork(true)">
                    <i class="fa fa-dolly"></i>
                    <span>@Loc.Confirm</span>
                </a>
            }
        </div>
    }
}
else
{
    @* Mobile buttons *@
    <div class="@(!_isMobile ? "hidden" : string.Empty)">
        <a href="@Url.Action("WorkOrderOperations", "Manufacturing", new { key = Model.AcKey })" id="back" class="btn btn-light btn-rounded text-primary btn-back">
            <i class="fas fa-chevron-left"></i>
        </a>
        @if (PantheonRA.BackOffice.CurrentUser.CanAccess(Permissions.PRODUCTION))
        {
            <div class="cont-right" style="right:0px;">

                @* Stop button *@
                <a id="stop" role="button" style="border-radius:0 50px 50px 0 !important;"
                   class="btn pull-right stop-btn btn-rounded @(Model.EventType.IsEmpty() || Model.EventType == WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
                   data-event-type="@WorkRegEventType.OperationEnd"
                   data-end-caption="@Loc.TaEnd"
                   data-milliseconds="@Model.Milliseconds"
                   onclick="startWorkRecording(this, true)"
                   data-icon="far fa-stop-circle"
                   data-color="#BB5534">
                    <i class="far fa-stop-circle"></i>
                    <span class="hidden-xxxs">@Loc.TaEnd</span>
                </a>

                @* Pause button *@
                <a id="pause" role="button" style="border-radius: 50px 0 0 50px !important;"
                   class="btn pull-right pause-btn btn-rounded @(Model.EventType.IsEmpty() || Model.EventType == WorkRegEventType.OperationPause || Model.EventType == WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
                   data-event-type="@WorkRegEventType.OperationPause"
                   data-pause-caption="@Loc.Pause"
                   data-milliseconds="@Model.Milliseconds"
                   onclick="startWorkRecording(this, true)"
                   data-icon="far fa-pause-circle"
                   data-color="goldenrod">
                    <i class="far fa-pause-circle"></i>
                    <span class="hidden-xxs">@Loc.Pause</span>
                </a>

                @* Continue button *@
                <a id="continue" role="button" style="border-radius: 50px 0 0 50px !important;"
                   class="btn pull-right continue-btn btn-rounded @(Model.EventType.IsEmpty() || Model.EventType == WorkRegEventType.OperationStart ||
                        Model.EventType == WorkRegEventType.OperationContinue || Model.EventType == WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
                   data-event-type="@WorkRegEventType.OperationContinue"
                   data-pause-caption="@Loc.Continue"
                   data-milliseconds="@Model.Milliseconds"
                   onclick="startWorkRecording(this, true)"
                   data-icon="far fa-play-circle"
                   data-color="#7EAF3F">
                    <i class="far fa-play-circle"></i>
                    <span class="hidden-xxs">@Loc.Continue</span>
                </a>

                @* Start button *@
                <a id="start" role="button"
                   class="btn pull-right start-btn  btn-rounded @(!Model.EventType.IsEmpty() ? "hidden" : string.Empty)"
                   data-event-type="@WorkRegEventType.OperationStart"
                   data-start-caption="@Loc.TaBegin"
                   data-milliseconds="@Model.Milliseconds"
                   onclick="startWorkRecording(this, true)"
                   data-icon="far fa-stop-circle"
                   data-color="#7EAF3F">
                    <i class="fas fa-play"></i>
                    <span>@Loc.TaBegin</span>
                </a>

                @if (PantheonRA.BackOffice.CurrentUser.CanAccess(Permissions.PRODUCTION))
                {
                    @* Confirm button *@
                    <a id="confirmButton" role="button"
                       class="btn confirm-btn pull-right btn-primary btn-rounded m-l-sm @(Model.EventType != WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
                       onclick="confirmOperationWork(true)">
                        <i class="fa fa-dolly"></i>
                        <span>@Loc.Confirm</span>
                    </a>

                    @* Cancel button *@
                    <a id="cancelButton" role="button" style="background-color: #BB5534; border-color: #BB5534"
                       class="btn cancel-btn pull-right btn-primary btn-rounded m-l-sm @(Model.EventType != WorkRegEventType.OperationEnd ? "hidden" : string.Empty)"
                       onclick="deleteWorkReg(true)">
                        <i class="far fa-window-close"></i>
                        <span>@Loc.Cancel</span>
                    </a>
                }

                @* Options button *@
                <button id="options" type="button" class="btn pull-right btn-rounded">
                    <i class="fas fa-ellipsis-h"></i>
                </button>
            </div>
        }
    </div>
}