@model PantheonRA.BackOffice.Models.CashRegisterSettingsViewModel
@using PantheonRA.WebApi.Contracts
@using PAW.Contracts.Enums;
@using PantheonRA.BackOffice.Enums
@{
    ViewBag.Title = @Loc.DefaultSettings;
}

<div class="tabs-container">
    @Html.Partial("~/Views/Company/_CompanySettingsTabNavigation.cshtml")
    <div class="tab-content">
        <div class="divided-content clearfix">
            <div class="divided-content-main divided-content-full">
                <div class="form-horizontal form-md m-t-md">
                    <h3>@ViewBag.Title</h3>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.LabelFor(model => model.DocType, @Loc.DocType, new { @class = "control-label" })
                        </div>
                        <div class="col-md-3">
                            @Html.DropDownListFor(model => model.DocType, Model.DocumentTypesList.Select(x => new SelectListItem { Text = x.Value, Value = x.Id }), "",
                                new { @id = "DocumentSelectList", @onchange = "ChangeDocument(this.value)", @class = "form-control custom-select" })
                        </div>
                        <div class="col-md-1">
                            <i class='fa fa-spinner fa-spin'></i>
                        </div>
                    </div>
                    <div class="form-group"
                         @(PantheonRA.BackOffice.CurrentUser.Localization != Localization.Code.SI && PantheonRA.BackOffice.CurrentUser.Localization != Localization.Code.HR ? "hidden" : "")>
                        <div class="col-md-3">
                            @Html.LabelFor(model => model.AutomaticallyFiscalize, @Loc.AutomaticallyFiscalizeUnconfirmedBills)
                        </div>
                        <div class="col-md-3">
                            @Html.CheckBoxFor(model => model.AutomaticallyFiscalize, new { @id = "AutomaticallyFiscalize", @class = "form-control js-switch cashReg-js-switch" })
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section pageScripts{
    <script>
        $(document).ready(function () {
            var documentDd = $("#DocumentSelectList");
            var loading = $(".fa-spinner").hide();

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

            $(document)
                .ajaxStart(function () {
                    loading.show();
                    documentDd.prop("disabled", true);

                })
                .ajaxStop(function () {
                    loading.hide();
                    documentDd.prop("disabled", false);
                });

            $("#AutomaticallyFiscalize").on('change', function () {
                $.ajax({
                    url: '@Url.Action("SaveSettings", "Settings")',
                    type: "post",
                    data: {
                        dropDownName: "@DefaultSettingsTypes.AutomaticallyFiscalizeUnconfirmedCashRegDocs",
                        selectedValue: $(this).is(':checked') ? "@Status.Confirmed" : "@Status.NotConfirmed"
                    },
                    success: function () {
                        toastr.success("@Loc.ClassificationSettingsDropDownAjaxSuccess");
                    },
                    error: function () {
                        toastr.warning("@Loc.ClassificationSettingsDropDownAjaxFail");
                    }
                });
            });
        });

        function ChangeDocument(val) {
            var url = '@Url.Action("SaveSettings", "Settings")';
            $.ajax({
                url: url,
                type: "post",
                data: {
                    dropDownName: "@DefaultSettingsTypes.DefaultCashRegDocumentType",
                    selectedValue: val
                },
                success: function () {
                    toastr.success("@Loc.DocumentSettingsDropDownAjaxSuccess");
                },
                error: function () {
                    toastr.warning("@Loc.DocumentSettingsDropDownAjaxFail");
                }
            });
        }
    </script>
}
