@using PantheonRA.BackOffice.Enums

<div class="modal inmodal fade modal-z-index-3000" id="iris4791Modal" tabindex="-1" role="dialog" aria-hidden="true" data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <div class="iris-title m-b-sm">
                    <div class="row">
                        <div class="col-xs-11 padding-right-none">
                            <span><strong>@Loc.IRIS4791Title</strong></span>
                        </div>
                        <div class="col-xs-1 pull-right">
                            <i class="fa fa-times clickable-icon m-l-xs" data-dismiss="modal" title="@Loc.Close"></i>
                        </div>
                    </div>
                </div>
                <span>@Loc.IRIS4791Text</span>
                <div class="well well-sm well-bg clickable-icon m-t-sm m-b-sm" data-dismiss="modal" onclick="recalculateTax()">
                    <div class="row">
                        <div class="col-xs-2">
                            <div class="icon-arrow-right"></div>
                        </div>
                        <div class="col-xs-10 well-text-margin">
                            <strong>@Loc.RecalculateTax</strong>
                            <p class="no-margins">@Loc.RecalculateTaxSubText</p>
                        </div>
                    </div>
                </div>
                <div class="well well-sm well-bg clickable-icon m-b-sm" data-dismiss="modal" onclick="abandonTaxRecalc()">
                    <div class="row">
                        <div class="col-xs-2">
                            <div class="icon-arrow-right"></div>
                        </div>
                        <div class="col-xs-10 well-text-margin">
                            <strong>@Loc.Continue</strong>
                            <p class="no-margins">@Loc.AbandonTaxRecalculateChangeWayOfProcurement</p>
                        </div>
                    </div>
                </div>
                <div class="well well-sm well-bg clickable-icon m-b-sm" onclick="window.open('@Loc.HelpUrl@HelpIds.IRIS4791', '_blank')">
                    <div class="row">
                        <div class="col-xs-2">
                            <div class="icon-help icon-full-size"></div>
                        </div>
                        <div class="col-xs-10 well-text-margin">
                            <strong>@Loc.ViewInstructions</strong>
                            <p class="no-margins">@Loc.ViewInstructionsDescription</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $('#iris4791Modal .modal-dialog').draggable();
    });

    function recalculateTax() {
        swapLoadingIcon(true, "saveButton", "fa fa-save");
        swapLoadingIcon(true, "printButton", "far fa-print");
        swapLoadingIcon(true, "add-position", "fa fa-plus");

        $.get("@Url.Action("MoveChangeVATIn", "Move")" + "?acKey=" + $("#AcKey").val(), function (response) {
            swapLoadingIcon(false, "saveButton", "fa fa-save");
            swapLoadingIcon(false, "printButton", "far fa-print");
            swapLoadingIcon(false, "add-position", "fa fa-plus");
            abandonTaxRecalc();
        });

    }

    function abandonTaxRecalc() {
        swapLoadingIcon(true, "add-position", "fa fa-plus");

        var model = {
            AcKey: $("#AcKey").val(),
            WayOfSale: $("#WayOfSale").val(),
            FxRate: 1,
            Author: $("#AuthorType").val()
        };

        $.ajax({
            type: "POST",
            data: JSON.stringify(model),
            url: '@Url.Action("RecalculatePositionsWithFxRate", "Currency")',
            contentType: "application/json"
        }).done(function (response) {
            if (response) {
                if ($("#AuthorType").val() === "@AuthorType.OrdersSupplier" || $("#AuthorType").val() === "@AuthorType.OrdersBuyer") {
                    $.get('@Url.Action("OrderItems", "Order")?acKey=' + $("#AcKey").val(),
                        function (data) {
                            invoiceItems = [];

                            for (var i = 0; i < data.length; i++) {
                                data[i].DeliveryDeadline = data[i].DeliveryDeadline ? moment(data[i].DeliveryDeadline) : null;
                                data[i].OriginalPrice = data[i].Price;
                                data[i].OriginalValue = data[i].Value;

                                invoiceItems.push(data[i]);
                                $("#price-" + data[i].AnNo).val(data[i].Price);
                                $("#vat-" + data[i].AnNo).val(data[i].VATCode);
                                $("#vatPercent-" + data[i].AnNo).val(data[i].VAT);
                                $("#totalPrice-" + data[i].AnNo).val(data[i].TotalPrice);
                                $("#value-" + data[i].AnNo).val(data[i].Value);
                                $("#lastPrice-" + data[i].AnNo).val(data[i].LastPrice);
                                $("#retailPrice-" + data[i].AnNo).val(data[i].RetailPrice);
                                changeIdentData(data[i].AnNo);
                            }

                            initializeNumberInputs();
                            swapLoadingIcon(false, "add-position", "fa fa-plus");
                        }, "json");
                } else {
                    $.post('@Url.Action("MoveItemsList", "MoveItem")?id=' + $("#AcKey").val(),
                        function (data) {
                            invoiceItems = [];

                            for (var i = 0; i < data.length; i++) {
                                data[i].TotalPrice = data[i].TotalPriceForReceivedItem;
                                data[i].TaxPrice = data[i].TaxPrice + data[i].Excise;
                                invoiceItems.push(data[i]);

                                $("#price-" + data[i].AnNo).val(data[i].Price);
                                $("#vat-" + data[i].AnNo).val(data[i].VATCodeTR);
                                $("#vatPercent-" + data[i].AnNo).val(data[i].VATIn);
                                $("#priceForValue-" + data[i].AnNo).val(data[i].PriceForValue);
                                $("#basisForTax-" + data[i].AnNo).val(data[i].BasisForTax);
                                $("#taxPrice-" + data[i].AnNo).val(data[i].TaxPrice);
                                $("#totalPrice-" + data[i].AnNo).val(data[i].TotalPrice);
                                $("#retailPrice-" + data[i].AnNo).val(data[i].RTPrice);
                                var priceRebate = data[i].Price - (data[i].Price * data[i].Rabate / 100);
                                $("#priceRebate-" + data[i].AnNo).val(priceRebate);
                            }

                            updatePriceTable();
                            initializeNumberInputs();
                            swapLoadingIcon(false, "add-position", "fa fa-plus");
                        }, "json");
                }
            }
        });
 }
</script>