@model IEnumerable<PantheonRA.WebApi.Contracts.TravelOrderDailyAllow>
@using PantheonRA.BackOffice.Enums
@using PantheonRA.WebApi.Contracts.Enums
@using PAW.Contracts.Enums;

@{
    var dailyAllowsNum = (int)ViewBag.Enumerator;
    var dailyAllowsId = "DistanceViewModels" + dailyAllowsNum + "DailyAllows{0}{1}{2}";
    var dailyAllowsName = "DistanceViewModels[" + dailyAllowsNum + "].DailyAllows[{0}].{1}";
    var type = "DailyAllows";
    bool isDomestic = (bool)ViewBag.IsDomestic;
    var hideOnDomestic = isDomestic ? "hidden" : string.Empty;

    bool isLocalizationBA = Loc.CurrentLocalization == Localization.Code.BA;
    bool isLocalizationMK = Loc.CurrentLocalization == Localization.Code.MK;
    bool isLocalizationSL = Loc.CurrentLocalization == Localization.Code.SI;
    bool isLocalizationME = Loc.CurrentLocalization == Localization.Code.ME;

    var hideOnLocalizationBA = isLocalizationBA ? "hidden" : string.Empty;
    var hideOnLocalizationMK = isLocalizationMK ? "hidden" : string.Empty;
    var hideOnLocalizationME = isLocalizationME ? "hidden" : string.Empty;

    var workerLegislation = (string)ViewBag.Legislation;
    var hideOnLegislationBA = workerLegislation == Legislation.BA ? "hidden" : string.Empty;

    var hideOnDomesticMK = isDomestic && isLocalizationMK ? "hidden" : string.Empty;
    var hideOnDomesticNotBA = isDomestic && !isLocalizationBA ? "hidden" : string.Empty;

    bool isSLInternational = !isDomestic && isLocalizationSL ? true : false;

}

@Html.Hidden("DailyAllowsPrefix-" + dailyAllowsNum, string.Format(dailyAllowsName, "OrdinalNumberNo", "Property"))
@Html.Hidden("DefaultFactor", DailyAllowTypes.DefaultFactor)
@Html.Hidden("FactorMinus5", DailyAllowTypes.FactorMinus5)
@Html.Hidden("FactorMinus20", DailyAllowTypes.FactorMinus20)
@Html.Hidden("FactorMinus30", DailyAllowTypes.FactorMinus30)
@Html.Hidden("FactorMinus50", DailyAllowTypes.FactorMinus50)
@Html.Hidden("FactorMinus80", DailyAllowTypes.FactorMinus80)
@Html.Hidden("FactorPlus70", DailyAllowTypes.FactorPlus70)
@Html.Hidden("MinusFive", DailyAllowTypes.MinusFive)
@Html.Hidden("MinusTwenty", DailyAllowTypes.MinusTwenty)
@Html.Hidden("MinusThirty", DailyAllowTypes.MinusThirty)
@Html.Hidden("MinusFifty", DailyAllowTypes.MinusFifty)
@Html.Hidden("MinusEighty", DailyAllowTypes.MinusEighty)
@Html.Hidden("PlusSeventy", DailyAllowTypes.PlusSeventy)
@Html.Hidden("isSLInternational", isSLInternational)

<table id="DailyAllowsTable-@dailyAllowsNum" class="table table-bordered input-no-bottom-border to-panel-table allows-expenses-table">
    <thead>
        <tr>
            <th style="width:15%">@Loc.DailyAllow</th>
            <th>@Loc.DailyAllowNo</th>
            <th>@Loc.DailyAllowAmount</th>
            <th id="th-breakfast" class="@hideOnLocalizationME @hideOnDomesticMK">@Loc.Breakfast</th>
            <th id="th-lunch" class="@hideOnLocalizationME @hideOnDomesticMK @hideOnDomesticNotBA">@Loc.Lunch</th>
            <th id="th-dinner" class="@hideOnLocalizationME @hideOnDomesticMK @hideOnDomesticNotBA @hideOnLegislationBA da-dinner legislationRSBD">@Loc.Dinner</th>
            <th class="@hideOnLocalizationME @hideOnDomesticMK">@Loc.Factor</th>
            <th class="@hideOnLocalizationME @hideOnLocalizationMK @hideOnLocalizationBA">@Loc.DailyAllowBonus</th>
            <th>@Loc.Value</th>
            <th style="width:10%">@Loc.Currency</th>
            <th>@Loc.FxRate</th>
            <th id="th-button"></th>
        </tr>
    </thead>
    <tbody>
        @Html.Partial("_DailyAllowsCreate")

        @for (var i = 0; i < Model.Count(); i++)
        {
            <tr id="@i">
                <td class="hidden">
                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "Deleted", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "Deleted")"
                           value="@Model.ElementAt(i).Deleted" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "AnNo", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "AnNo")"
                           value="@Model.ElementAt(i).AnNo" />

                    <input type="hidden"
                            id="@string.Format(dailyAllowsId, i, "AcKey", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "AcKey")"
                           value="@Model.ElementAt(i).AcKey" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "anId", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "anId")"
                           value="@Model.ElementAt(i).anId" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "Country", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "Country")"
                           value="@Model.ElementAt(i).Country" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "CZakonodaja", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "CZakonodaja")"
                           value="@Model.ElementAt(i).CZakonodaja" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "DailyAllowOrdinalNumber", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "DailyAllowOrdinalNumber")"
                           value="@Model.ElementAt(i).DailyAllowOrdinalNumber" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "IsDomestic", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "IsDomestic")"
                           value="@Model.ElementAt(i).IsDomestic" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "IsFromSixToEight", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "IsFromSixToEight")"
                           value="@Model.ElementAt(i).IsFromSixToEight" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "lIsInstallGE", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "lIsInstallGE")"
                           value="@Model.ElementAt(i).lIsInstallGE" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "PriceFullDailyAllow", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "PriceFullDailyAllow")"
                           value="@Model.ElementAt(i).PriceFullDailyAllow" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "PriceHalfDailyAllow", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "PriceHalfDailyAllow")"
                           value="@Model.ElementAt(i).PriceHalfDailyAllow" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "PriceReducedDailyAllow", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "PriceReducedDailyAllow")"
                           value="@Model.ElementAt(i).PriceReducedDailyAllow" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "QId", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "QId")"
                           value="@Model.ElementAt(i).QId" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "TotalHours", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "TotalHours")"
                           value="@Model.ElementAt(i).TotalHours" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "DateHourFrom", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "DateHourFrom")"
                           value="@Model.ElementAt(i).DateHourFrom" />

                    <input type="hidden"
                           id="@string.Format(dailyAllowsId, i, "DateHourTo", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "DateHourTo")"
                           value="@Model.ElementAt(i).DateHourTo" />
                </td>

                <td label="@Loc.DailyAllow">
                    <div style="width: 100%" onclick="expandTravelOrderRow('DailyAllowsTable-@dailyAllowsNum', @i, true);">
                        <select id="@string.Format(dailyAllowsId, i, "DailyAllow", Model.ElementAt(i).AnNo)"
                                name="@string.Format(dailyAllowsName, i, "DailyAllow")"
                                class="form-control select2withAPI"
                                data-placeholder="@Loc.DailyAllow"
                                data-column-names="@Loc.DailyAllow,@Loc.Country"
                                data-bootstrap-columns="col-md-6,col-md-6"
                                data-search-width="400"
                                data-dropdown-height="300"
                                data-selector-separator=", "
                                onchange="SetDailyAllowFxRate(
                                             '@dailyAllowsNum',
                                             '@string.Format(dailyAllowsId, i, "Currency", Model.ElementAt(i).AnNo)',
                                             '@string.Format(dailyAllowsId, i, "anFxRate", Model.ElementAt(i).AnNo)',
                                             '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)',
                                             '@string.Format(dailyAllowsId, i, "anPriceDailyAllow", Model.ElementAt(i).AnNo)',
                                             '@string.Format(dailyAllowsId, i, "Value", Model.ElementAt(i).AnNo)',
                                             'DailyAllowsTable-@dailyAllowsNum',
                                             '@i',
                                             '@type');"
                                data-url=@Url.Action("GetDailyAllowTypes", "TravelOrdersDailyAllows")>
                            <option selected value="@Model.ElementAt(i).DailyAllow">@Model.ElementAt(i).DailyAllow</option>
                        </select>
                    </div>
                </td>

                <td label="@Loc.DailyAllowNo">
                    <input type="text"
                           id="@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "anDailyAllowNo")"
                           class="form-control thousands-with-decimals"
                           onchange="SetDailyAllowFxRate(
                                        '@dailyAllowsNum',
                                        '@string.Format(dailyAllowsId, i, "Currency", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anFxRate", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anPriceDailyAllow", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "Value", Model.ElementAt(i).AnNo)',
                                        'DailyAllowsTable-@dailyAllowsNum',
                                        '@i',
                                        '@type');"
                           value="@Model.ElementAt(i).anDailyAllowNo.ToString("N2")" />
                </td>

                <td label="@Loc.DailyAllowAmount">
                    <input type="text"
                           id="@string.Format(dailyAllowsId, i, "anPriceDailyAllow", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "anPriceDailyAllow")"
                           class="form-control thousands-with-decimals"
                           onchange="SetDailyAllowFxRate(
                                        '@dailyAllowsNum',
                                        '@string.Format(dailyAllowsId, i, "Currency", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anFxRate", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anPriceDailyAllow", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "Value", Model.ElementAt(i).AnNo)',
                                        'DailyAllowsTable-@dailyAllowsNum',
                                        '@i',
                                        '@type');"
                           value="@Model.ElementAt(i).anPriceDailyAllow.ToString("N2")" />
                </td>


                <td label="@Loc.Breakfast" class="text-center @hideOnLocalizationME @hideOnDomesticMK">
                    @{
                        var isChecked = Model.ElementAt(i).IsBreakfastIncluded ? "checked" : string.Empty;
                    }
                    <input type="checkbox"
                           class="travelorders-checkbox custom-checkbox"
                           id="@string.Format(dailyAllowsId, i, "IsBreakfastIncluded", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "IsBreakfastIncluded")"
                           value="@Model.ElementAt(i).IsBreakfastIncluded.ToString()"
                           checked="@Model.ElementAt(i).IsBreakfastIncluded"
                           onchange="setCheckedValue(this.id,
                                        '@string.Format(dailyAllowsId, i, "IsLunchIncluded", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "IsDinnerIncluded", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)');
                                    setPercentage(@dailyAllowsNum, @i, @Model.ElementAt(i).AnNo);
                                    updateTravelOrderInput('DailyAllowsTable-@dailyAllowsNum', '@i', '@type');" />
                </td>

                <td label="@Loc.Lunch" class="text-center @hideOnLocalizationME @hideOnDomesticMK @hideOnDomesticNotBA">
                    <input type="checkbox"
                           class="travelorders-checkbox custom-checkbox"
                           id="@string.Format(dailyAllowsId, i, "IsLunchIncluded", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "IsLunchIncluded")"
                           value="@Model.ElementAt(i).IsLunchIncluded.ToString()"
                           checked="@Model.ElementAt(i).IsLunchIncluded"
                           onchange="setCheckedValue(this.id,
                                        '@string.Format(dailyAllowsId, i, "IsBreakfastIncluded", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "IsDinnerIncluded", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)');
                                    setPercentage(@dailyAllowsNum, @i, @Model.ElementAt(i).AnNo);
                                    updateTravelOrderInput('DailyAllowsTable-@dailyAllowsNum', '@i', '@type');" />
                </td>

                <td label="@Loc.Dinner" class="text-center legislationRSBD @hideOnLocalizationME @hideOnDomesticMK @hideOnDomesticNotBA @hideOnLegislationBA">
                    <input type="checkbox"
                           class="travelorders-checkbox custom-checkbox"
                           id="@string.Format(dailyAllowsId, i, "IsDinnerIncluded", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "IsDinnerIncluded")"
                           value="@Model.ElementAt(i).IsDinnerIncluded.ToString()"
                           checked="@Model.ElementAt(i).IsDinnerIncluded"
                           onchange="setCheckedValue(this.id,
                                        '@string.Format(dailyAllowsId, i, "IsBreakfastIncluded", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "IsLunchIncluded", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)');
                                    setPercentage(@dailyAllowsNum, @i, @Model.ElementAt(i).AnNo);
                                    updateTravelOrderInput('DailyAllowsTable-@dailyAllowsNum', '@i', '@type');" />
                </td>

                <td label="@Loc.Factor" class="disabled-td @hideOnLocalizationME @hideOnDomesticMK">
                    <input type="text"
                           id="@string.Format(dailyAllowsId, i, "CalcFactor", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "CalcFactor")"
                           class="form-control"
                           value="@Model.ElementAt(i).CalcFactor.ToString("N2")" readonly />
                </td>

                <td label="@Loc.DailyAllowBonus" class="@hideOnLocalizationME @hideOnLocalizationMK @hideOnLocalizationBA">
                    <input type="text"
                           id="@string.Format(dailyAllowsId, i, "anPriceFieldBonus", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "anPriceFieldBonus")"
                           class="form-control thousands-with-decimals"
                           value="@(Model.ElementAt(i).anPriceFieldBonus.HasValue ? Model.ElementAt(i).anPriceFieldBonus.Value.ToString("N2") : string.Empty)"
                           onchange="updateTravelOrderInput('DailyAllowsTable-@dailyAllowsNum', '@i', '@type');" />
                </td>

                <td label="@Loc.Value">
                    <input type="text"
                           id="@string.Format(dailyAllowsId, i, "Value", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "Value")"
                           class="form-control"
                           value="@(Model.ElementAt(i).Value.HasValue ? Model.ElementAt(i).Value.Value.ToString("N2") : string.Empty)" 
                           onchange="updateTravelOrderInput('DailyAllowsTable-@dailyAllowsNum', '@i', '@type');" />
                </td>

                <td label="@Loc.Currency">
                    <select id="@string.Format(dailyAllowsId, i, "Currency", Model.ElementAt(i).AnNo)"
                            name="@string.Format(dailyAllowsName, i, "Currency")"
                            class="form-control select2withAPI"
                            onchange="SetDailyAllowFxRate(
                                        '@dailyAllowsNum',
                                        '@string.Format(dailyAllowsId, i, "Currency", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anFxRate", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anDailyAllowNo", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "anPriceDailyAllow", Model.ElementAt(i).AnNo)',
                                        '@string.Format(dailyAllowsId, i, "Value", Model.ElementAt(i).AnNo)',
                                        'DailyAllowsTable-@dailyAllowsNum',
                                        '@i',
                                        '@type');"
                            data-url=@Url.Action("GetDropDownValues", "GlobalHelper", new { apiPath = "/v1/travel-orders/expenses/currencies-lookupitems" })>
                        <option selected value="@Model.ElementAt(i).Currency">@Model.ElementAt(i).Currency</option>
                    </select>
                </td>

                <td label="@Loc.FxRate">
                    <input type="text"
                           id="@string.Format(dailyAllowsId, i, "anFxRate", Model.ElementAt(i).AnNo)"
                           name="@string.Format(dailyAllowsName, i, "anFxRate")"
                           class="form-control"
                           value="@Model.ElementAt(i).anFxRate" 
                           onchange="updateTravelOrderInput('DailyAllowsTable-@dailyAllowsNum', '@i', '@type');" />
                </td>

                <td>
                    <button type="button" onclick='DeleteRow("DailyAllowsTable-@dailyAllowsNum", @i)'
                            class="btn btn-outline add-del-allow-expense">
                        <span class="far fa-trash-alt"></span>
                    </button>
                </td>

            </tr>
        }
    </tbody>
</table>