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

<div class="tabs-container">
    @Html.Partial("~/Views/Company/_CompanySettingsTabNavigation.cshtml")
    <div class="row buttons-tabbed">
        <button type="button" onclick="openImportMailboxModal()"
                title="@Loc.ImportMailboxes" class="btn btn-danger btn-outline pull-right">
            <i class="far fa-paper-plane m-r-xs"></i>
            <span class="visible-lg-inline visible-md-inline">@Loc.ImportMailboxes</span>
        </button>
    </div>

    <div class="divided-content clearfix">
        <div class="divided-content-main divided-content-full">
            <div class="row">
                <div class="col-lg-6 col-md-6 col-xs-12">
                    <div class="form-horizontal form-md m-t-md">
                        <h3>@ViewBag.Title</h3>
                        <div class="form-group">
                            <div class="col-lg-4 col-md-5 font-bold">
                                @Html.LabelFor(model => model.DocumentDropDownCurrentValue, @Loc.DocType, new { @class = "control-label" })
                            </div>
                            <div class="col-lg-5 col-md-7">
                                @Html.DropDownListFor(model => model.DocumentDropDownCurrentValue, Model.DocumentTypesList != null ? Model.DocumentTypesList.Select(x => new SelectListItem { Text = x.DocType + " - " + x.Name, Value = x.DocType }) : new List<SelectListItem>(), "", 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">
                            <div class="col-lg-4 col-md-5 font-bold">
                                @Html.LabelFor(model => model.ClassificationDropDownCurrentValue, @Loc.Classification, new { @class = "control-label" })
                            </div>
                            <div class="col-lg-5 col-md-7">
                                @Html.DropDownListFor(model => model.ClassificationDropDownCurrentValue, Model.ClasificationList != null ? Model.ClasificationList.Select(x => new SelectListItem { Text = x.AcType + " - " + x.AcTypeDesc, Value = x.AcType }) : new List<SelectListItem>(), "", new { @id = "ClassificationSelectList", @onchange = "ChangeClassification(this.value)", @class = "form-control custom-select" })
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12" style="height:calc(100vh - 270px)">
                    <h3>@Loc.Mailboxes</h3>
                    <div class="project-list">
                        <div class="document-list">
                            <table id="grid-data" data-grid-id="users" data-url="@Url.Action("GetMailboxes", "Mailbox")" class="list-table">
                                <thead>
                                    <tr>
                                        <th data-url="@Url.Action("Edit", "Mailbox")?id=">@Loc.Name</th>
                                        <th>@Loc.Subject</th>
                                        <th>@Loc.Classification</th>
                                        <th>@Loc.Login</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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

        $(document).ajaxStart(function () {
                loading.show();
                documentDd.prop("disabled", true);
                classificationDd.prop("disabled", true);
        }).ajaxStop(function () {
            loading.hide();
            documentDd.prop("disabled", false);
            classificationDd.prop("disabled", false);
        });

            $dataTable = $('#grid-data');
            $dataTable.DataTable({
                ajax: {
                    url: $dataTable.attr('data-url')
                },
                columns: [
                    { data: "Name", name: "AcName", filtertype: "text", render: $.fn.dataTable.render.link("AnQid") },
                    { data: "Subject", name: "AcSubject", filtertype: "text" },
                    { data: "ClassificationName", name: "AnClassificationQid" },
                    { data: "LoginName", name: "AnLoginsQid" }
                ]

            });
        });

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

        function ChangeClassification(val) {
            var url = '@Url.Action("SaveSettings", "Settings")';

            $.ajax({
                url: url,
                type: "post",
                data: {
                    dropDownName: "@DefaultSettingsTypes.DefaultClassification",
                    selectedValue: val
                },
                success: function () {
                    toastr.success("@Loc.ClassificationSettingsDropDownAjaxSuccess");
                },
                error: function () {
                    toastr.warning("@Loc.ClassificationSettingsDropDownAjaxFail");
                }
            });
        }

        function openImportMailboxModal() {
            let tableTitles = ['@Loc.Name', '@Loc.Subject', '@Loc.Icon', '@Loc.ClassificationQId', '@Loc.LoginQid'];
            let tableDataNames = ["Name", "Subject", "Icon", "AnClassificationQId", "AnLoginsQId"];
            let localization = {
                importType: "@Loc.ImportType",
                forCountry: "@Loc.ForCountry",
                dowloadFromServer: "@Loc.DownloadFromServer",
                importIntoCodebook: "@Loc.ImportIntoCodebook",
                importTypeAdding: "@Loc.ImportTypeAdding",
                importTypeAddEdit: "@Loc.ImportTypeAddEdit",
                yesNo: "@Loc.Yes/@Loc.No"
            };

            openImportCodebookModal("@Loc.ImportMailboxes", tableTitles, tableDataNames,
                '@Url.Action("GetImportMailboxes", "Mailbox")', '@Url.Action("ImportMailboxes", "Mailbox")', '@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.ImportMailboxes)',
                localization,
                '@PantheonRA.BackOffice.CurrentUser.Localization',
                function () {
                    $.fn.dataTable.reload($('#grid-data'));
                })
        }
    </script>
}
