@model SectionModel
@using Nop.Core.Domain.Catalog;
@using Nop.Services
@using Nop.Services.Stores

@using Nop.Plugin.Misc.SimpleLMS.Areas.Admin.Models


@{

}


<form asp-controller="Course" asp-action="CreateSection" method="post" id="section-form">
    <div class="card-body" id="create-or-edit-section">

        <input type="hidden" id="Id" name="Id" value="@Model.Id" />
        <input type="hidden" id="CourseId" name="CourseId" value="@Model.CourseId" />
        <input type="hidden" id="DisplayOrder" name="DisplayOrder" value="@Model.DisplayOrder" />
        <div class="form-group row">
            <div class="col-md-3">
                <nop-label asp-for="Title" />
            </div>
            <div class="col-md-9">
                <nop-editor asp-for="Title" asp-required="true" />
                <span asp-validation-for="Title"></span>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-md-3">
                <nop-label asp-for="IsFree" />
            </div>
            <div class="col-md-9">
                <nop-editor asp-for="IsFree" asp-required="false" />
                <span asp-validation-for="IsFree"></span>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-md-3">

            </div>
            <div class="col-md-9">
                <button type="button" class="btn btn-primary btn-sm" id="add-section-submit">
                    @(Model.Id>0 ?  T("SimpleLMS.Update") + " " + @T("SimpleLMS.Section") : T("SimpleLMS.Add") + " " + @T("SimpleLMS.Section"))
                </button>
            </div>
        </div>
    </div>
    <script type="text/javascript">

        $('#add-lesson-submit').off();

    $('#add-section-submit').on('click',
        function (e) {

            e.preventDefault();

            var _form = $(this).closest("form");
            _form.removeData('validator');
            _form.removeData('unobtrusiveValidation');
            $.validator.unobtrusive.parse(_form);

            var isValid = $(_form).validate().form();

            if (!isValid) {
                return false;
            }


                //alert($('#create-or-edit-section').serialize());
             $(this).prop('disabled', true);



             $.ajax({
                    type: "POST",
                    url: '@Url.Action((Model.Id > 0 ? "EditSection" : "CreateSection"), "Course")',
                    //data: $('#create-or-edit-section').serialize(),
                    data: {
                        id: $("#create-or-edit-section").find("#Id").val(),
                        courseId: $("#create-or-edit-section").find("#CourseId").val(),
                        displayOrder: $("#create-or-edit-section").find("#DisplayOrder").val(),
                        title: $("#create-or-edit-section").find("#Title").val(),
                        isFree: $("#create-or-edit-section").find("#IsFree").is(":checked")
                    },
                    headers: {
                        "RequestVerificationToken":
                            $('input:hidden[name="__RequestVerificationToken"]').val()
                    },
                    success: function (response) {
                        $('#create-or-edit-section-modal').dialog('destroy').remove();
                        refreshSections();
                    },
                 failure: function (response) {

                      alert(JSON.stringify(response));
                     $(this).prop('enabled', false);

                    },
                 error: function (response) {
                        alert(JSON.stringify(response));
                     $(this).prop('enabled', false);
                    }
              });
        });



    </script>
</form>
