@model PantheonRA.BackOffice.Models.StockRecalcModel

@{
    Layout = null;
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "stockRecalcForm" }))
{
    <div class="divided-content clearfix">
        <div class="divided-content-full">
            <div class="tab-content">
                <div id="stockRecalcTab" class="tab-pane fade in active m-t-md">
                    <div class="form-horizontal form-md">
                        <div class="col-xs-12">
                            <div class="form-group">
                                @Html.LabelFor(model => model.StockRecalcDate, Loc.Date, new {
                                    @class = "col-md-2 control-label",
                                    @title = Loc.Date
                                })
                                <div class="col-md-3">
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="far fa-calendar alt"></i>
                                        </span>
                                        @Html.TextBoxFor(model => model.StockRecalcDate, Model.StockRecalcDate.HasValue ? Model.StockRecalcDate.Value.ToString(Loc.DateFormat) : null,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "StockRecalcDate", true, Loc.Date))
                                    </div>
                                </div>
                                <div class="col-md-7 m-t-sm">
                                    <span>@Loc.EndOfMaterialTransactions</span>
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.StockRecalcIdent, Loc.Ident, new {
                                    @class = "col-md-2 control-label",
                                    @title = Loc.Ident
                                })
                                <div class="col-md-10">
                                    <input type="hidden" id="StockRecalcIdent" />
                                    <input type="text" class="typeahead_3 form-control" id="StockRecalcIdentSearch" dataUrl="@Url.Action("SearchIdentsIncludeSerial", "SetItem")"
                                           placeholder="@Loc.EnterIdentProduct" autocomplete="off" />
                                </div>
                            </div>
                            <div class="form-group">
                                @Html.LabelFor(model => model.StockRecalcWarehouse, Loc.Warehouse, new {
                                    @class = "col-md-2 control-label",
                                    @title = Loc.Warehouse
                                })
                                <div class="col-md-10">
                                    @Html.DropDownListFor(model => model.StockRecalcWarehouse,
                                        new SelectList(ViewBag.Departments != null ? ViewBag.Departments : new List<string>(), "Id", "Name"), Loc.Select,
                                        HtmlAttributesHelper.CreateInputAttributes("form-control custom-select", null, false, Loc.Warehouse, true))
                                </div>
                            </div>

                            <h4 class="m-t-xl">@Loc.RecoveryType</h4>
                            <div class="form-group">
                                <div class="col-xs-1 m-t-xs">
                                    <input type="checkbox" class="form-control js-switch" id="stockRecalcSwitch" />
                                </div>
                                <label class="control-label col-xs-11 p-b-5">@Loc.StockRecalc</label>
                            </div>
                            <div class="form-group">
                                <div class="col-xs-1 m-t-xs">
                                    <input type="checkbox" class="form-control js-switch" id="recalcValuationPricesSwitch" />
                                </div>
                                <label class="control-label col-xs-11 p-b-5">@Loc.RecalculationOfValuationPrices</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div id="stockRecalcErrorsTab" class="tab-pane fade m-t-md">
                    <div class="col-xs-12">
                        <div class="project-list">
                            <table id="stockRecalcErrors-grid" class="list-table">
                                <thead>
                                    <tr>
                                        <th width="20%">@Loc.Warehouse</th>
                                        <th width="20%">@Loc.Ident</th>
                                        <th width="20%">@Loc.Document</th>
                                        <th width="40%">@Loc.Error</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

<input type="hidden" value="@ViewBag.ReturnUrl" id="ReturnUrl" name="ReturnUrl" />

<script>
    var $errorsDataTable;

    $(document).ready(function () {
        initializeIdentBasicTypeahead($('#StockRecalcIdentSearch').attr('dataUrl'), $('#StockRecalcIdentSearch'), $('#StockRecalcIdentSearch'), { item: '@Loc.Item', name: '@Loc.Name' }, null, function (item) {
            $("#StockRecalcIdent").val(item.id);
            $("#StockRecalcIdentSearch").val(item.id);
            return { selected: true, value: item.id };
        });

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

        $('.modal-dialog .partialStockRecalcModalContent').draggable();

        $('.partialStockRecalcModalContent').resizable({
            alsoResize: ".modal-stock-recalc",
            minHeight: 463,
            minWidth: 530,
        });

        $('#stockRecalcModal').on('show.bs.modal', function () {
            $(this).find('.stockRecalcModalBody').css({
                'max-height': '100%',
                'min-height': 'calc(100% - 131px)',
            });
        });

        $(".partialStockRecalcModalContent").resize(function () {
            jsRenderDataTable("stockRecalcErrors-grid", "stockRecalcModalBody", 60, true);
        });

        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            let modalBodyMinHeight = "";
            if ($(e.target).attr("href") === "#stockRecalcErrorsTab") {
                $("#stockRecalcModalFooter").attr("hidden", true);
                modalBodyMinHeight = "calc(100% - 67px)";
            } else {
                $("#stockRecalcModalFooter").attr("hidden", false);
                modalBodyMinHeight = "calc(100% - 131px)";
            }

            $("#stockRecalcModal").find('.stockRecalcModalBody').css({
                'min-height': modalBodyMinHeight,
            });

            if ($.fn.dataTable.isDataTable('#stockRecalcErrors-grid')) {
                $('#stockRecalcErrors-grid').DataTable().columns.adjust();
            }
        });
    });

    function stockRecalc() {
        if (!$("#stockRecalcSwitch").is(":checked") && !$("#recalcValuationPricesSwitch").is(":checked")) {
            $().customModal({
                title: "",
                destroyOnClose: true,
                showCloseButton: true,
                type: "error",
                text: "@Loc.RecoveryComplete",
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: '@Loc.Confirm',
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
            return;
        }

        swapLoadingIcon(true, "stockRecalcBtn", "fa fa-plus");

        $.post('@Url.Action("StockRecalc", "Reports")', {
            Ident: $("#StockRecalcIdent").val(),
            Warehouse: $("#StockRecalcWarehouse").val(),
            StockRecalc: $("#stockRecalcSwitch").is(":checked"),
            RecalcValuationPrices: $("#recalcValuationPricesSwitch").is(":checked"),
            MoveOpenDate: $("#StockRecalcDate").val(),
        }).done(function (response) {
            $().customModal({
                title: "",
                destroyOnClose: true,
                showCloseButton: true,
                type: "error",
                text: "@Loc.RecoveryComplete",
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: '@Loc.Confirm',
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });

            if (response && response.length > 0) {
                loadErrors(response);
            }

            swapLoadingIcon(false, "stockRecalcBtn", "fa fa-plus");
        }).fail(function () {
            $().customModal({
                title: "",
                destroyOnClose: true,
                showCloseButton: true,
                type: "error",
                text: "@Loc.GeneralError",
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: '@Loc.Confirm',
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
            swapLoadingIcon(false, "stockRecalcBtn", "fa fa-plus");
        });
    }

    function loadErrors(items) {
        $errorsDataTable = $('#stockRecalcErrors-grid');
        if ($.fn.dataTable.isDataTable('#stockRecalcErrors-grid'))
            $errorsDataTable.DataTable().destroy();

        $errorsDataTable.DataTable({
            serverSide: false,
            ajax: undefined,
            scroller: false,
            responsive: false,
            paging: false,
            info: false,
            data: items,
            columns: [
                { data: "Warehouse" },
                { data: "Ident" },
                { data: "AcKey" },
                {
                    data: "Error", render: function (data, type, full) {
                        if (type === 'display') {
                            switch (data) {
                                case '1': return "@Loc.IRIS6651"; break;
                                case '2': return "@Loc.IRIS6652"; break;
                                case '3': return "@Loc.IRIS6656"; break;
                                case '10': return "@Loc.IRIS6653"; break;
                                case '11': return "@Loc.IRIS6654"; break;
                                case '12': return "@Loc.IRIS6655"; break;
                                default: ""; break;
                            }
                        }
                    }
                }
            ],
            initComplete: function (settings, json) {
                $errorsDataTable.DataTable().rows().recalcHeight().draw();
            }
        });
    }
</script>