@using PAW.Contracts.Enums
@using PantheonRA.BackOffice.Enums

<div class="modal inmodal fade" id="createAdvancePaymentModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-50" role="document">
        <div class="modal-content">
            <div class="modal-body">
                @Html.Hidden("CreateAdvPayIsFiscal")
                <div class="row">
                    <h3 class="col-xs-9">@Loc.CreateAdvancePayment</h3>
                    <div class="col-xs-3 m-t-xs">
                        <i class="fa fa-times clickable-icon padding pull-right m-t-3" data-dismiss="modal" title="@Loc.Close"></i>
                        <a class="btn-transparent pull-right m-r-xs" onclick="openCreateAdvancePaymentModalHelp()" title="@Loc.CheckInstructions"><i class="fas fa-question clickable-icon"></i></a>
                        <a class="btn-transparent pull-right m-r-xs" onclick="openUserManualHelp()" title="@Loc.CheckSamples"><i class="fas fa-book-reader"></i></a>
                    </div>

                    <div class="col-xs-12">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.ExpectedDeliveryDate</h4>
                            </div>
                            <div class="col-xs-7">
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="far fa-calendar alt"></i>
                                    </span>
                                    @Html.TextBox("AdvancePaymentDateBeforeVAT", DateTime.Now.ToString(Loc.DateFormat),
                                        HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "AdvancePaymentDateBeforeVAT", false, Loc.ExpectedDeliveryDate))
                                    @Html.ValidationMessage("DateBeforeVATValidation", "", new { @class = "text-danger", @id = "ap-DateBeforeVAT-validation" })
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.AdvancePaymentDate</h4>
                            </div>
                            <div class="col-xs-7">
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="far fa-calendar alt"></i>
                                    </span>
                                    @Html.TextBox("AdvancePaymentDate", DateTime.Now.ToString(Loc.DateFormat),
                                        HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "AdvancePaymentDate", false, Loc.AdvancePaymentDate))
                                    @Html.ValidationMessage("DateValidation", "", new { @class = "text-danger", @id = "ap-Date-validation" })
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.DocType</h4>
                            </div>
                            <div class="col-xs-7">
                                @Html.DropDownList("AdvancePaymentDocType", new SelectList(ViewBag.AdvancePaymentDocTypes != null ? ViewBag.AdvancePaymentDocTypes : new List<string>(), "IdView", "Value"),
                                    HtmlAttributesHelper.CreateInputAttributes("form-control custom-select", "AdvancePaymentDocType", false, Loc.DocType, true))
                                @Html.ValidationMessage("AdvancePaymentDocTypeDocTypeValidation", "", new { @class = "text-danger", @id = "ap-DocType-validation" })
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.Ident</h4>
                            </div>
                            <div class="col-xs-5 typeahead-ident-filter padding-right-none" id="AdvancePaymentIdentContainer">
                                <input type="hidden" id="AdvancePaymentIdent" title="@Loc.Ident" />
                                <input type="text" class="typeahead_3 form-control" id="AdvancePaymentIdentSearch" dataUrl="@Url.Action("SearchIdents", "SetItem")"
                                       placeholder="@Loc.EnterIdentProduct" autocomplete="off" />
                            </div>
                            <div class="col-xs-2 m-t-xs" id="AdvancePaymentIdentCheckedContainer">
                                <input type="checkbox" id="AdvancePaymentIdentChecked" class="form-control js-switch js-switch-ad-pay" />
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.Percent</h4>
                            </div>
                            <div class="col-xs-7">
                                <div class="input-group">
                                    <input id="AdvancePaymentPercent" class="form-control numberInput wizardInput" value="100" autocomplete="off" onchange="onChangeAdvancePaymentPercent()" />
                                    <span class="input-group-addon">%</span>
                                </div>
                                @Html.ValidationMessage("PercentValidation", "", new { @class = "text-danger", @id = "ap-Percent-validation" })
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.OrderValue</h4>
                            </div>
                            <div class="col-xs-7">
                                <div class="input-group">
                                    <input id="AdvancePaymentOrderValue" class="form-control numberInput" autocomplete="off" disabled />
                                    <span class="input-group-addon currencyAddon disabled"></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5 padding-right-none">
                                <h4 class="m-t-sm">@Loc.AdvancePaymentValue</h4>
                            </div>
                            <div class="col-xs-7">
                                <div class="input-group">
                                    <input id="AdvancePaymentValue" class="form-control numberInput wizardInput" autocomplete="off" onchange="onChangeAdvancePaymentValue()" />
                                    <span class="input-group-addon currencyAddon"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12 m-t-xs">
                        <div class="row">
                            <div class="col-xs-5">
                                <h4>@Loc.CreateAdvancePaymentWithDifferentVat</h4>
                            </div>
                            <div class="col-xs-7">
                                <div class="m-t-sm">
                                    <input type="checkbox" id="AdvancePaymentByVatChecked" class="form-control js-switch js-switch-ad-pay" />
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-ghost" data-dismiss="modal"><i class="fa fa-times"></i><span class="m-l-xs">@Loc.Cancel</span></button>
                <button type="button" class="btn btn-primary m-r-sm" onclick="checkCreateAdvancePayment()" id="createAdvancePaymentBtn"><i class="fa fa-check m-r-xs"></i>@Loc.Create</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        initializeIdentBasicTypeahead($('#AdvancePaymentIdentSearch').attr('dataUrl'), $('#AdvancePaymentIdentSearch'), $('#AdvancePaymentIdent'),
            { item: '@Loc.Item', name: '@Loc.Name' }, ("@PantheonRA.BackOffice.CurrentUser.Localization" === "@Localization.Code.RS" ? $("#CreateAdvPayIsFiscal") : undefined), function (item) {
                $("#AdvancePaymentIdent").val(item.id);
                $("#AdvancePaymentIdentSearch").val(item.id);
                return { selected: true, value: item.id };
            });

        var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch-ad-pay'));
        elems.forEach(function (html) {
            var switchery = new Switchery(html, { size: 'small' });
        });
    });

    function onChangeAdvancePaymentPercent() {
        var value = toNumber($("#AdvancePaymentOrderValue").val()) / 100 * toNumber($("#AdvancePaymentPercent").val());
        $("#AdvancePaymentValue").val(value);
        formatNumberInput($("#AdvancePaymentValue"));
    }

    function onChangeAdvancePaymentValue() {
        var value = 100 * (toNumber($("#AdvancePaymentValue").val()) / toNumber($("#AdvancePaymentOrderValue").val()));
        $("#AdvancePaymentPercent").val(value);
        formatNumberInput($("#AdvancePaymentPercent"));
    }

    function displayAdvancePaymentValidationErrors(response) {
        for (var i = 0; i < response.errors.length; i++) {
            var $span = $("#ap-" + response.errors[i].Key + "-validation");
            $span.text(response.errors[i].Value);
        }
    }
</script>