@model PantheonRA.WebApi.Contracts.TravelOrderAllData

@{
    var firstDistance = Model.Distances.OrderBy(x => x.OrdinalNumber).FirstOrDefault();
    var lastDistance = Model.Distances.OrderByDescending(x => x.OrdinalNumber).FirstOrDefault();

    string startLocation = string.Empty;
    string endLocation = string.Empty;
    int startKm = 0;
    int endKm = 0;
    int totalKm = 0;


    if (firstDistance != null)
    {
        startLocation = firstDistance.Node;
        startKm = firstDistance.KMCounterBefore.HasValue ? firstDistance.KMCounterBefore.Value : 0;
    }

    if (lastDistance != null)
    {
        endLocation = lastDistance.Node;
        endKm = lastDistance.KMCounterAfter.HasValue ? lastDistance.KMCounterAfter.Value : 0;
        totalKm = endKm - startKm;
    }
}

<div class="form-group col-md-12 col-sm-6">
    @Html.LabelFor(m => m.Header.Date, Loc.Date, new { @class = "col-md-4 control-label", @title = Loc.Date })
    <div class="col-md-8">
        <div class="input-group">
            <span class="input-group-addon"><i class="far fa-calendar alt"></i></span>
            @Html.TextBox("Header.Date", Model.Header.Date, new { @class = "form-control custom-datepicker" })
            @Html.ValidationMessageFor(m => m.Header.Date, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

<div class="form-group col-md-12 col-sm-6">
    @Html.LabelFor(m => m.Header.DateCalc, Loc.DateOfCalculation, new { @class = "col-md-4 control-label", @title = Loc.DateOfCalculation })
    <div class="col-md-8">
        <div class="input-group">
            <span class="input-group-addon"><i class="far fa-calendar alt"></i></span>
            @Html.TextBox("Header.DateCalc", Model.Header.DateCalc, new { @class = "form-control custom-datepicker" })
        </div>
    </div>
</div>

<div class="form-group col-md-12 col-sm-6">
    @{ var approvedByString = string.IsNullOrEmpty(Model.Header.ApprovedByFullName) ? string.Empty : Model.Header.ApprovedByFullName; }
    @Html.LabelFor(m => m.Header.ApprovedBy, Loc.ApprovedBy, new { @class = "col-md-4 control-label", @title = Loc.ApprovedBy })
    <div class="col-md-8">
        <select id="HeaderApprovedBy"
                name="Header.ApprovedBy"
                class="form-control select2withAPI"
                data-placeholder="@Loc.ApprovedBy"
                data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/travel-orders/approvers-lookupitems" })>
            <option selected value="@(Model.Header.ApprovedBy == 0 ? null : Model.Header.ApprovedBy)">@approvedByString</option>
        </select>
    </div>
</div>

<div class="form-group col-md-12 col-sm-6">
    @Html.LabelFor(m => m.Header.Payer, Loc.Payer, new { @class = "col-md-4 control-label", @title = Loc.Payer })
    <div class="col-md-8">
        <select id="HeaderPayer"
                name="Header.Payer"
                class="form-control select2withAPI"
                data-placeholder="@Loc.Payer"
                data-column-names="@Loc.Company,@Loc.Name,@Loc.Address,@Loc.Post"
                data-bootstrap-columns="col-md-4,col-md-3,col-md-3,col-md-2"
                data-search-width="350"
                data-search-left="-120"
                data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/subject/searchsubjects-lookupitems" })>
            <option selected value="@Model.Header.Payer">@Model.Header.Payer</option>
        </select>
    </div>
</div>

<hr class="hr-dashed" />

<div class="form-group col-md-12 col-sm-6">
    @Html.Label("StartLocation", Loc.StartLocation, new { @class = "col-md-4 control-label", @title = Loc.StartLocation })
    <div class="col-md-8">
        <select id="StartLocation"
                name="Header.AcPlaceStart"
                class="form-control select2withAPI"
                data-placeholder="@Loc.StartLocation"
                data-search-width="350"
                data-search-left="-120"
                data-column-names="@Loc.Company,@Loc.Name,@Loc.Address,@Loc.Post"
                data-bootstrap-columns="col-md-3,col-md-3,col-md-3,col-md-3"
                data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/subject/searchsubjects-lookupitems" })>
            <option selected value="@Model.Header.AcPlaceStart">@Model.Header.AcPlaceStart</option>
        </select>
    </div>
</div>

<div class="form-group col-md-12 col-sm-6">
    @Html.Label("EndLocation", Loc.FinalLocation, new { @class = "col-md-4 control-label", @title = Loc.FinalLocation })
    <div class="col-md-8">
        <select id="EndLocation"
                name="Header.AcPlaceFinish"
                class="form-control select2withAPI"
                data-placeholder="@Loc.FinalLocation"
                data-search-width="350"
                data-search-left="-120"
                data-column-names="@Loc.Company,@Loc.Name,@Loc.Address,@Loc.Post"
                data-bootstrap-columns="col-md-3,col-md-3,col-md-3,col-md-3"                
                data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/subject/searchsubjects-lookupitems" })>
            <option selected value="@Model.Header.AcPlaceFinish">@Model.Header.AcPlaceFinish</option>
        </select>
    </div>
</div>

<div class="form-group col-md-12 col-sm-4">
    @Html.Label("CounterBefore", Loc.StartCounter, new { @class = "col-md-4 control-label", @title = Loc.StartCounter })
    <div class="col-md-8">
        @Html.TextBox("Header.CounterBefore", startKm.ToString("N0"), new { @class = "form-control thousands-no-decimals", @readonly="readonly" })
    </div>
</div>

<div class="form-group col-md-12 col-sm-4">
    @Html.Label("CounterAfter", Loc.FinalCounter, new { @class = "col-md-4 control-label", @title = Loc.FinalCounter })
    <div class="col-md-8">
        @Html.TextBox("Header.CounterAfter", endKm.ToString("N0"), new { @class = "form-control thousands-no-decimals", @readonly = "readonly" })
    </div>
</div>

<div class="form-group col-md-12 col-sm-4">
    @Html.Label("TotalKm", Loc.DrivenKm, new { @class = "col-md-4 control-label", @title = Loc.DrivenKm })
    <div class="col-md-8">
        @Html.TextBox("Header.TotalKm", totalKm.ToString("N0"), new { @class = "form-control thousands-no-decimals", @readonly = "readonly" })
    </div>
</div>

<hr class="hr-dashed" />

<div class="form-group">
    @Html.LabelFor(m => m.Header.Job, Loc.Job, new { @class = "col-md-4 control-label", @title = Loc.Job })
    <div class="col-md-8">
        <select id="HeaderJob"
                name="Header.Job"
                class="form-control select2withAPI"
                filter-by="HeaderWorker"
                data-placeholder="@Loc.Job"
                onchange="dropDownChange('HeaderWorker', ['HeaderDept'], [setDepartmentDropDown], 'filterBy')"
                data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/travel-orders/worker-jobs-lookupitems" })>
            <option selected value="@Model.Header.Job">@Model.Header.Job</option>
        </select>
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Header.Dept, Loc.Department, new { @class = "col-md-4 control-label", @title = Loc.Department })
    <div class="col-md-8">
        <select id="HeaderDept"
                name="Header.Dept"
                class="form-control select2withAPI"
                data-placeholder="@Loc.Department"
                data-change-url="@Url.Action("GetSingleValue", "GlobalHelper", new { apiPath = "/v1/employees/GetById" })"
                data-url=@Url.Action("GetDepartments", "Employees")>
            <option selected value="@Model.Header.Dept">@Model.Header.Dept</option>
        </select>
    </div>
</div>