@model List<PantheonRA.WebApi.Contracts.PrsnEvaluation>
@using PantheonRA.WebApi.Contracts.Enums;

@{
    var prsnEvalIndex = 0;
    var evaluationName = "Evaluations[{0}].{1}";
    var evaluated = string.Empty;
    var inProgress = string.Empty;
}

@Html.Hidden("CurrentWorker", Model.Select(q => q.Worker).FirstOrDefault())

@if (Model.Count > 0)
{
    foreach (var item in Model)
    {
        var grade = string.Format("{0:n2}", Math.Round(Convert.ToDecimal(item.Revalue), 2));

        using (Ajax.BeginForm("Edit", "Evaluation", null,
            new AjaxOptions
            {
                OnSuccess = "reloadWorkersTable()"
            },
            new {
                @enctype = "multipart/form-data",
                @id = "eval-form-" + prsnEvalIndex,
                @novalidate = "novalidate"
            }))
        {
            var evalIndex = 0;

            <div id="evaluation-accordion-@prsnEvalIndex" class="panel evaluation-panel panel-default">
                @*Evaluation header*@
                <table class="table table-bordered table-fixed evaluation-table">
                    <tbody>
                        <tr>
                            @Html.Hidden("AnQId", item.AnQId, new { Id = string.Format("{0}-{1}", prsnEvalIndex, "AnQId") })
                            @Html.Hidden("JobId", item.JobId, new { Id = string.Format("{0}-{1}", prsnEvalIndex, "JobId") })
                            @Html.Hidden("Period", item.Period, new { Id = string.Format("{0}-{1}", prsnEvalIndex, "Period") })
                            @Html.Hidden("Revalue", item.Revalue, new { Id = string.Format("{0}-{1}", prsnEvalIndex, "Revalue") })
                            @Html.Hidden("Worker", item.Worker, new { Id = string.Format("{0}-{1}", prsnEvalIndex, "Worker") })
                            @Html.Hidden("Updated", item.Updated, new {Id = string.Format("{0}-{1}", prsnEvalIndex, "Updated") })
                            <td width="15%">
                                <div class="row eval-row">
                                    <div class="col-md-3 col-xs-1 open-position" data-toggle="collapse" href="#collapse-@prsnEvalIndex">
                                        <div class="panel-heading" role="tab" id="heading-@prsnEvalIndex">
                                            <h4 class="panel-title">
                                                <a class="text-success collapsed"
                                                   role="button"
                                                   data-toggle="collapse"
                                                   href="#collapse-@prsnEvalIndex"
                                                   aria-controls="ariaCollapse-@prsnEvalIndex">
                                                </a>
                                            </h4>
                                        </div>
                                    </div>
                                    <div class="open-position" data-toggle="collapse" href="#collapse-@prsnEvalIndex" title="@Loc.EvaluationPeriod">
                                        <span>@Loc.From @item.Period @Loc.To.ToLower() @item.Period</span>
                                    </div>
                                </div>
                            </td>

                            <td width="9%" title="@Loc.EvaluationPeriodicals">
                                <div class="row eval-row">
                                    <i class="far fa-clock alt text-success"></i>
                                    <span>
                                        <input id=@string.Format("{0}-{1}", prsnEvalIndex, "Periodical")
                                               name="Periodical"
                                               type="text" 
                                               maxlength="2" 
                                               class="eval-period" 
                                               value="@item.Periodical" 
                                               onchange="submitEvalForm(@prsnEvalIndex, @evalIndex, true)"/> 
                                        @Loc.Months
                                    </span>
                                </div>
                            </td>

                            <td width="13%" title="@Loc.Leader/@Loc.Appraiser">
                                <div class="row eval-row">
                                    <div class="col-md-1 col-xs-1">
                                        <i class="fas fa-user-check alt text-success"></i>
                                    </div>
                                    <div class="col-md-10 col-xs-1">
                                        <select id=@string.Format("{0}-{1}", prsnEvalIndex, "Appraiser")
                                                name="Appraiser"
                                                class="form-control select2withAPI"
                                                data-placeholder="@Loc.Appraiser"
                                                data-column-names="@Loc.Appraiser"
                                                data-bootstrap-columns="col-md-12"
                                                data-search-width="250"
                                                onchange="submitEvalForm(@prsnEvalIndex, @evalIndex, true)"
                                                data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/employees/get-all-employees" })>
                                            <option selected value="@item.Appraiser" title="@Loc.Leader/@Loc.Appraiser">@item.Appraiser</option>
                                        </select>
                                    </div>
                                </div>
                            </td>

                            <td width="18%" title="@Loc.EvaluationDescription">
                                <div class="row eval-row">
                                    <i class="fa fa-comment alt text-success"></i>
                                    <input type="text"
                                           id="@string.Format("{0}-{1}", prsnEvalIndex, "Description")"
                                           name="Description"
                                           value="@item.Description"
                                           onblur="submitEvalForm(@prsnEvalIndex, @evalIndex, true)"
                                           placeholder="@Loc.Description..." />
                                </div>
                            </td>

                            <td width="9%" title="@Loc.Status">
                                <div class="row eval-row">
                                    <div class="col-md-1 col-xs-1">
                                        <i class="fas fa-tasks alt text-success"></i>
                                    </div>
                                    <div class="col-md-10 col-xs-1">
                                        <select id=@string.Format("{0}-{1}", prsnEvalIndex, "Status")
                                                name="Status"
                                                class="form-control select2"
                                                data-placeholder="@Loc.Status"
                                                onchange="submitEvalForm(@prsnEvalIndex, @evalIndex, true)"
                                                data-search-width="200">
                                            @{
                                                evaluated = item.Status == EvaluationStatus.Finished ? "selected" : string.Empty;
                                                inProgress = item.Status == EvaluationStatus.InProgress
                                                    || string.IsNullOrEmpty(item.Status)
                                                    || string.IsNullOrWhiteSpace(item.Status) ? "selected" : string.Empty;
                                            }
                                            <option></option>
                                            <option @evaluated value="@EvaluationStatus.Finished" title="@Loc.Status">@Loc.Evaluated</option>
                                            <option @inProgress value="@EvaluationStatus.InProgress" title="@Loc.Status">@Loc.InWork</option>
                                        </select>
                                    </div>
                                </div>
                            </td>

                            <td width="5%" title="@Loc.EvaluationPercent">
                                <div class="row eval-row">
                                    <span class="badge grade" id="@string.Format("{0}-{1}", prsnEvalIndex, "Grade")">
                                        @grade
                                    </span>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>

                @*Evaluation main*@
            <div id="collapse-@prsnEvalIndex" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-professionalism-@prsnEvalIndex">
                <table class="table table-bordered">
                    <thead>
                        <tr class="row">
                            <th width="25%">@Loc.Name</th>
                            <th width="20%">@Loc.Grade</th>
                            <th width="10%">@Loc.Weight</th>
                            <th width="10%" class="text-right">@Loc.Points/@Loc.Percent</th>
                            <th width="10%">@Loc.Note</th>
                            <th width="10%">@Loc.NoteFromAppraiser</th>
                            <th width="15%">@Loc.Appraiser</th>
                        </tr>
                    </thead>
                </table>

                @foreach (var category in item.Evaluations.Where(q => q.ParId == 0))
                {
                    <div id="partial-accordion-@prsnEvalIndex-@evalIndex">
                        <div class="panel evaluation-subpanel">
                            <div class="panel-heading" id="partial-heading-@prsnEvalIndex-@evalIndex">
                                <table class="eval-category-header">
                                    <tr>
                                        <td width="25%">
                                            <h4 class="panel-title">
                                                <a role="button" id="expand-@prsnEvalIndex-@evalIndex" class="text-success" data-toggle="collapse"
                                                   data-parent="#partial-accordion-@prsnEvalIndex-@evalIndex"
                                                   href="#partial-collapse-@prsnEvalIndex-@evalIndex">
                                                    @category.SetEval
                                                </a>
                                            </h4>
                                        </td>
                                        <td width="20%"></td>
                                        <td width="10%"></td>
                                        <td width="10%" class="text-right">
                                            @Math.Round(Convert.ToDecimal(category.Points), 0)
                                        </td>
                                        <td width="10%"></td>
                                        <td width="10%"></td>
                                        <td width="15%"></td>
                                    </tr>
                                </table>

                            </div>

                            <div id="partial-collapse-@prsnEvalIndex-@evalIndex" class="panel-collapse collapse in">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="panel-body evaluation-subpanel-body">
                                            <table class="table table-bordered table-fixed">
                                                <tbody>
                                                    @foreach (var eval in item.Evaluations.Where(q => q.ParId == category.IntId))
                                                    {
                                                        <tr>
                                                            <td class="hidden">
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "AnQId"), eval.AnQId, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "AnQId") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "Type"), eval.Type, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Type") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "Type2"), eval.Type2, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Type2") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "JobId"), eval.JobId, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "JobId") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "Period"), eval.Period, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Period") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "Worker"), eval.Worker, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Worker") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "Description"), eval.Description, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Description") })
                                                                @Html.Hidden(string.Format(evaluationName, evalIndex, "Updated"), eval.Updated, new { Id = string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Updated") })
                                                            </td>
                                                            <td width="25%">
                                                                <input id=@string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "SetEval")
                                                                       name="@string.Format(evaluationName, evalIndex, "SetEval")"
                                                                       value="@eval.SetEval"
                                                                       class="input-full-width"
                                                                       readonly />
                                                            </td>
                                                            <td width="20%" class="bg-white">
                                                                <div class="col-md-12 col-xs-1">
                                                                    <select id=@string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "SetEval2")
                                                                            name="@string.Format(evaluationName, evalIndex, "SetEval2")"
                                                                            class="form-control select2withAPI"
                                                                            data-placeholder="@Loc.Grade"
                                                                            data-search-width="400"
                                                                            onchange="calculateEvalPoints($(this).val(), @eval.Ponder, @prsnEvalIndex, @evalIndex);
                                                                                      submitEvalForm(@prsnEvalIndex, @evalIndex);"
                                                                            data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/evaluation/grades-lookup-items" })>
                                                                        <option selected value="@eval.SetEval2">@eval.SetEval2</option>
                                                                    </select>
                                                                </div>
                                                            </td>
                                                            <td width="10%">
                                                                <input id=@string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Ponder")
                                                                       name=@string.Format(evaluationName, evalIndex, "Ponder")
                                                                       value="@eval.Ponder.ToString("0.##")"
                                                                       class="eval-nums"
                                                                       readonly />
                                                            </td>
                                                            <td width="10%">
                                                                <input id=@string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Points")
                                                                       name=@string.Format(evaluationName, evalIndex, "Points")
                                                                       value="@eval.Points.ToString("0.##")"
                                                                       class="eval-nums"
                                                                       readonly />

                                                            </td>
                                                            <td width="10%" class="text-center description">
                                                                <button type="button"
                                                                        class="btn btn-link m-r-sm"
                                                                        title="@eval.Description"
                                                                        onclick="openNoteModal(@prsnEvalIndex, @evalIndex, false, '@eval.Description')">
                                                                    <i class="fa fa-comment description-hidden-icon @(string.IsNullOrEmpty(eval.Description) ? "transparent-color" : "theme-color" )"></i>
                                                                </button>
                                                            </td>
                                                            <td width="10%" class="text-center bg-white description">
                                                                <button type="button"
                                                                        class="btn btn-link m-r-sm"
                                                                        title="@eval.DescApp"
                                                                        onclick="openNoteModal(@prsnEvalIndex, @evalIndex, true, '@eval.DescApp')">
                                                                    <i class="fa fa-comment description-hidden-icon desc-icon-@prsnEvalIndex-@evalIndex @(string.IsNullOrEmpty(eval.DescApp) ? "transparent-color" : "theme-color" )"></i>
                                                                </button>
                                                                <input id="@string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "DescApp")"
                                                                       name="@string.Format(evaluationName, evalIndex, "DescApp")"
                                                                       value="@eval.DescApp"
                                                                       class="hidden" />
                                                            </td>
                                                            <td width="15%">
                                                                <input id=@string.Format("{0}-{1}-{2}", prsnEvalIndex, evalIndex, "Appraiser")
                                                                       name=@string.Format(evaluationName, evalIndex, "Appraiser")
                                                                       value="@eval.Appraiser"
                                                                       class="text-center"
                                                                       readonly />
                                                            </td>
                                                        </tr>
                                                        evalIndex++;
                                                    }
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                }
            </div>
            </div>
            prsnEvalIndex++;
        }
    }
}
else
{
    <div>
        <h4>@Loc.WorkerDoesNotHaveEvaluations</h4>
    </div>
}
