/**
 * Open centered pop-up window
 * By Arthur Gareginyan (https://www.arthurgareginyan.com)
 * For full source code, visit https://mycyberuniverse.com
 *
 * @param URL - specifies the URL of the page to open. If no URL is specified, a new window with about:blank will be opened
 * @param windowName - specifies the target attribute or the name of the window (_blank, _parent, _self, _top, name)
 * @param windowWidth - the window width in pixels (integer)
 * @param windowHeight - the window height in pixels (integer)
 */
function popUpWindow(URL, windowName, windowWidth, windowHeight) {
  var centerLeft = screen.width / 2 - windowWidth / 2;
  var centerTop = screen.height / 2 - windowHeight / 2;
  var windowFeatures =
    "toolbar=no, location=no, directories=no, status=no, menubar=no, titlebar=no, scrollbars=no, resizable=no, ";
  return window.open(
    URL,
    windowName,
    windowFeatures +
      " width=" +
      windowWidth +
      ", height=" +
      windowHeight +
      ", top=" +
      centerTop +
      ", left=" +
      centerLeft
  );
}

$(document).ready(function (e) {
  $("#sidebarCollapse").on("click", function () {
    $("#sidebar").toggleClass("active");
  });

  $("#first").on("submit", function (e) {
    e.preventDefault();
    let result = confirm(
      "Are you sure you want to Register the selected courses?"
    );

    if (result === false) {
      return;
    }

    let form = document.getElementById("first");

    document.getElementById("firstSubmit").disabled = true;
    document.getElementById("firstSpinner").classList.add("spinner-grow");
    document.getElementById("firstSpinner").classList.add("spinner-grow-sm");

    $.ajax({
      type: "POST",
      url: "./api/reg.php",
      data: new FormData(form),
      contentType: false,
      cache: false,
      processData: false,
      success: function (resp) {
        resp = JSON.parse(resp);
        if (resp.data == 1) {
          alert(resp.message);
          window.location = "dashboard.php";
        } else {
          alert("Something Wrong Happened! Please contact Support!");
        }
      },
      fail: function (xhr, textStatus, errorThrown) {
        alert("Registration failed");
      },
      error: function (e) {
        alert("Registration failed");
        done("first");
      },
    }).done(function () {
      done("first");
    });
  });

  // ////////////////////////////////////////

  $("#second").on("submit", function (e) {
    e.preventDefault();
    let result = confirm(
      "Are you sure you want to Register the selected 2nd semester courses?"
    );

    if (result === false) {
      return;
    }

    let form = document.getElementById("second");

    document.getElementById("secondSubmit").disabled = true;
    document.getElementById("secondSpinner").classList.add("spinner-grow");
    document.getElementById("secondSpinner").classList.add("spinner-grow-sm");

    $.ajax({
      type: "POST",
      url: "./api/reg.php",
      data: new FormData(form),
      contentType: false,
      cache: false,
      processData: false,
      success: function (resp) {
        resp = JSON.parse(resp);
        if (resp.data == 1) {
          alert(resp.message);
          window.location = "dashboard.php";
        } else {
          alert("Something Wrong Happened! Please contact Support!");
        }
      },
      fail: function (xhr, textStatus, errorThrown) {
        alert("Registration failed");
      },
      error: function (e) {
        alert("Registration failed");
        done("second");
      },
    }).done(function () {
      done("second");
    });
  });
  
  ///////////////////////////////////////////
  
  $("#third").on("submit", function (e) {
    e.preventDefault();
    let result = confirm(
      "Are you sure you want to Register the selected 3rd semester courses?"
    );

    if (result === false) {
      return;
    }

    let form = document.getElementById("third");

    document.getElementById("thirdSubmit").disabled = true;
    document.getElementById("thirdSpinner").classList.add("spinner-grow");
    document.getElementById("thirdSpinner").classList.add("spinner-grow-sm");

    $.ajax({
      type: "POST",
      url: "./api/reg.php",
      data: new FormData(form),
      contentType: false,
      cache: false,
      processData: false,
      success: function (resp) {
        resp = JSON.parse(resp);
        if (resp.data == 1) {
          alert(resp.message);
          window.location = "dashboard.php";
        } else {
          alert("Something Wrong Happened! Please contact Support!");
        }
      },
      fail: function (xhr, textStatus, errorThrown) {
        alert("Registration failed");
      },
      error: function (e) {
        alert("Registration failed");
        done("third");
      },
    }).done(function () {
      done("third");
    });
  });
});

function done(form) {
  document.getElementById(`${form}Submit`).disabled = false;
  document.getElementById(`${form}Spinner`).classList.remove("spinner-grow");
  document
    .getElementById(`${form}Spinner`)
    .classList.remove("spinner-grow-sm");
}
