@model PantheonRA.Contracts.DataContracts.DatalinkData
@{
    ViewBag.Title = Model.Id <= 0 ? Loc.CreateNewDatalink : Loc.EditDatalink;
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <a class="navbar-minimalize btn btn-primary " href="#"><i class="fa fa-bars"></i> </a>
        <h2>@ViewBag.Title</h2>
        <span class="breadcrumb-divider">&bull;</span>
        <ol class="breadcrumb">
            <li><a href="@Url.Action("Index", "SynchronizationSettings")">Home</a></li>
            <li>@Loc.AdministrationPanel</li>
            <li class="active"><strong>@ViewBag.Title</strong></li>
        </ol>
    </div>
    <div class="col-lg-2">
    </div>
</div>

<div class="wrapper wrapper-content  animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-content">
                    <div class="row m-b-sm m-t-sm">
                        <div class="col-md-12">
                            
                            @if (Model.Id > 0)
                            {
                                <h1>@PantheonRA.BackOffice.CurrentUser.DomainName <b>@Loc.SynchronizationSettings</b></h1>
                            }
                            else
                            {
                                <h2>@Html.Raw(String.Format(Loc.PleaseFillTheFormBelow, "<b>" + Loc.SynchronizationSettingsGenitiv.ToLower() + "</b>"))</h2>
                            }

                            <div class="hr-line-dashed"></div>
                            
                            @if (!Model.IsPantheonVersionValid || (Model.PantheonDocTypesStatus != null && Model.PantheonDocTypesStatus.Any(docTypeStatus => docTypeStatus.Status != PantheonRA.Contracts.DataContracts.DocTypeStatus.OK)))
                            {
                                <div class="col-md-12">
                                    <div class="alert alert-danger">
                                        @if ((Model.PantheonDocTypesStatus != null && Model.PantheonDocTypesStatus.Any(docTypeStatus => docTypeStatus.Status != PantheonRA.Contracts.DataContracts.DocTypeStatus.OK)))
                                        {
                                            <h2>@Loc.InOrderToInitializeTheSynchronizationYouMustEnterFollowingDocTypesInPantheon</h2>
                                            <p>
                                                @Loc.MissingDocTypesInPantheon
                                                <ul>
                                                    @foreach (var status in Model.PantheonDocTypesStatus.Where(docTypeStatus => docTypeStatus.Status != PantheonRA.Contracts.DataContracts.DocTypeStatus.OK))
                                                    {
                                                        <li>
                                                            <b>@status.DocType</b>
                                                        </li>
                                                    }
                                                </ul>
                                            </p>
                                        }
                                        @if(!Model.IsPantheonVersionValid)
                                        {
                                            <p>
                                                @String.Format(Loc.CurrentPantheonVersionIsXxxSynchronizationIsSupportedFromYyy, Model.PantheonVersion, Model.RequiredPantheonVersion)
                                            </p>
                                        }
                                    </div>
                                </div>
                            }

                            @using (Html.BeginForm())
                            {
                                @Html.HiddenFor(model => model.Id)
                                @Html.HiddenFor(model => model.IsPantheonVersionValid)
                                @Html.HiddenFor(model => model.PantheonVersion)
                                @Html.HiddenFor(model => model.RequiredPantheonVersion)
                                
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="row">
                                            <div class="col-xs-12">
                                                <div class="form-group">
                                                    @Html.LabelFor(model => model.DataSource, Loc.DataSource)
                                                    @Html.TextBoxFor(model => model.DataSource, new { @id = "DataSource", @class = "form-control", @placeholder = Loc.DataSource + "..." })
                                                    @Html.ValidationMessageFor(model => model.DataSource)
                                                </div>
                                            </div>
                                            <div class="col-xs-12">
                                                <div class="form-group">
                                                    @Html.LabelFor(model => model.InitialCatalog, Loc.InitialCatalog)
                                                    @Html.TextBoxFor(model => model.InitialCatalog, new { @id = "InitialCatalog", @class = "form-control", @placeholder = Loc.InitialCatalog + "..." })
                                                    @Html.ValidationMessageFor(model => model.InitialCatalog)
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="row">
                                            <div class="col-xs-12">
                                                <div class="form-group">
                                                    @Html.LabelFor(model => model.DbUserId, Loc.DbUserId)
                                                    @Html.TextBoxFor(model => model.DbUserId, new { @id = "DbUserId", @class = "form-control", @placeholder = Loc.DbUserId + "..." })
                                                    @Html.ValidationMessageFor(model => model.DbUserId)
                                                </div>
                                            </div>
                                            <div class="col-xs-12">
                                                <div class="form-group">
                                                    @Html.LabelFor(model => model.DbUserPassword, Loc.DbUserPassword)
                                                    @Html.TextBoxFor(model => model.DbUserPassword, new { @id = "DbUserPassword", @class = "form-control", @placeholder = "******" })
                                                    @Html.ValidationMessageFor(model => model.DbUserPassword)
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="row">
                                            <div class="col-xs-12">
                                                <div class="form-group">
                                                    @Html.LabelFor(model => model.StartingDateTime, "Starting Date")
                                                    @Html.TextBoxFor(m => m.StartingDateTime, "{0:dd.MM.yyyy.}", new { @class = "form-control datepicker" })
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-xs-12 col-md-offset-9 col-md-3">
                                        <button id="CheckSyncConnectivity" class="btn btn-sm btn-primary btn-block btn-outline" type="button">
                                            <div class="text-center" id="checkConnectivityLoader" style="display: none;">
                                                <div class="sk-spinner sk-spinner-three-bounce">
                                                    <div class="sk-bounce1"></div>
                                                    <div class="sk-bounce1"></div>
                                                    <div class="sk-bounce2"></div>
                                                </div>
                                            </div>
                                            <span>@Loc.CheckSyncConnectivity</span>
                                        </button>
                                    </div>
                                    <div class="col-md-12">
                                        <div>
                                            @Html.ValidationMessage("PantheonUnavailable")
                                        </div>

                                        <div class="hr-line-dashed"></div>
                                        <div class="row buttons-row">
                                            <div class="col-xs-4">
                                                <a href="@Url.Action("Index", "SynchronizationSettings")" class="btn btn-default btn-block btn-outline">@Loc.Cancel</a>
                                            </div>
                                            <div class="col-xs-1 col-xs-offset-1">

                                            </div>
                                            <div class="col-xs-6">
                                                <button class="btn btn-primary btn-block" type="submit">
                                                    <div class="text-center" style="display: none;">
                                                        <div class="sk-spinner sk-spinner-three-bounce">
                                                            <div class="sk-bounce1"></div>
                                                            <div class="sk-bounce1"></div>
                                                            <div class="sk-bounce2"></div>
                                                        </div>
                                                    </div>
                                                    <span>@Loc.Save</span>
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section pageCss{
    <link href="~/Content/plugins/datapicker/datepicker3.css" rel="stylesheet" />
    <link href="~/Content/plugins/switchery/switchery.css" rel="stylesheet" />
    <link href="~/Content/plugins/sweetAlert/sweetalert.css" rel="stylesheet" />
}

@section pageScripts{
    <script type="text/javascript" src="~/Scripts/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="~/Scripts/plugins/switchery/switchery.js"></script>
    <script src="~/Scripts/plugins/sweetAlert/sweetalert-dev.js"></script>

    <script type="text/javascript">
        $(function () {
            var language = '@PantheonRA.BackOffice.CurrentUser.Localization';
            $('.datepicker').datepicker()
                .on('click', function (e) {
                    $(this).select();
                });
        });

        $(document).ready(function () {
            $('.input-validation-error').parents('.form-group').addClass('has-error');
            $('.field-validation-error').addClass('text-danger');

            var txtDataSource = $('#DataSource');
            var txtInitialCatalog = $('#InitialCatalog');
            var txtDbUserId = $('#DbUserId');
            var txtDbUserPassword = $('#DbUserPassword');
            var btnCheckSyncConnectivity = $('#CheckSyncConnectivity');

            txtDataSource.on('keyup', _toggleCheckConnectivityButton);
            txtInitialCatalog.on('keyup', _toggleCheckConnectivityButton);
            txtDbUserId.on('keyup', _toggleCheckConnectivityButton);
            txtDbUserPassword.on('keyup', _toggleCheckConnectivityButton);
            btnCheckSyncConnectivity.on('click', _checkConnectivity);

            _toggleCheckConnectivityButton();

            var overrideCheck = false;
            function _toggleCheckConnectivityButton()
            {
                if(!overrideCheck)
                btnCheckSyncConnectivity.prop('disabled', txtDataSource.val().length === 0 || txtInitialCatalog.val().length === 0 || txtDbUserId.val().length === 0 || txtDbUserPassword.val().length === 0);
            }

            function _checkConnectivity()
            {
                overrideCheck = true;
                btnCheckSyncConnectivity.prop('disabled', true);
                $('#checkConnectivityLoader').show();
                $('#CheckSyncConnectivity > span').hide();
                $.ajax({
                    method: "POST",
                    url: "@Url.Action("_CheckSyncConnectivity", "SynchronizationSettings")",
                    data: { dataSource: txtDataSource.val(), initialCatalog: txtInitialCatalog.val(), userName: txtDbUserId.val(), password: txtDbUserPassword.val() },
                    success: function (pantheonSyncStatus) {
                        if (pantheonSyncStatus === "PANTHEON_SERVER_UNAVAILABLE")
                        {
                            swal({ title: "@Loc.ConnectionCheckFailed", text: "@Loc.PantheonUnavailable", type: "error" });
                        }
                        else if(pantheonSyncStatus.statuses.length > 0 || !pantheonSyncStatus.isVersionValid)
                        {
                            var messageText = "";
                            if (pantheonSyncStatus.statuses.length > 0)
                            {
                                messageText = "@Loc.MissingDocTypesInPantheon" + ": " + pantheonSyncStatus.statuses.join(',') + ". ";
                            }

                            if(!pantheonSyncStatus.isVersionValid)
                            {
                                messageText = messageText + "@Loc.CurrentPantheonVersionIsXxxSynchronizationIsSupportedFromYyy".replace('{0}', pantheonSyncStatus.currentVersion).replace('{1}', pantheonSyncStatus.requiredVersion);
                            }

                            swal({ title: "@Loc.ConnectionCheckFailed", text: messageText, type: "error" });
                        }
                        else
                        {
                            swal({ title: "@Loc.ConnectionCheckSuccess", text: "@Loc.TheConnectionIsAvailable", type: "success", confirmButtonColor: "#18a689" });
                        }
                    },
                    error: function ()
                    {
                        swal({ title: "@Loc.ConnectionCheckFailed", text: "@Loc.PantheonUnavailable", type: "error" });
                    },
                    complete: function () {
                        overrideCheck = false;
                        _toggleCheckConnectivityButton();
                        $('#checkConnectivityLoader').hide();
                        $('#CheckSyncConnectivity > span').show();
                    }
                });
            }
        });
    </script>
}