Warning: Undefined array key "yPBFjS" in /home/ic3/domains/ic3.info/public_html/wp-includes/kses.php on line 1
HEX
HEX
Server: LiteSpeed
System: Linux control5.webnow.vn 4.18.0-553.83.1.lve.el8.x86_64 #1 SMP Wed Nov 12 10:04:12 UTC 2025 x86_64
User: ic3 (1169)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /home/ic3/public_html/wp-content/plugins/school-management/assets/js/modules/school-class.js
window.SchoolManagement = window.SchoolManagement || {};

// Global function for HTML onclick compatibility
function showSchoolSelection() {
  if (window.SchoolManagement.SchoolClass) {
    window.SchoolManagement.SchoolClass.showSchoolSelection();
  }
}

window.SchoolManagement.SchoolClass = {
  init: function ($) {
    this.$ = $;
    this.bindEvents();
    this.loadSchools();
    this.checkInitialLoginStatus();
    // Only call handleSchoolChange if schoolId is defined
    if (window.SchoolManagement.StudentLogin) {
      var self = this;
      window.SchoolManagement.StudentLogin.checkSessionWithCallback(
        function () {
          self.handleSchoolChangeWhenLogin();
        }
      );
    }
  },

  showSchoolSelection: function () {
    const $ = this.$ || window.SchoolManagement.$ || jQuery;

    // Show school selection (menu navigation)
    // $("#select-school").show();

    // Hide other containers
    $("#select-class").hide();
    $("#entity-container").hide();

    // Clear class title
    $("#class-title").text("");

    // Clear entity grid and hide pagination
    $("#entity-grid").empty();
    $("#pagination-container").hide();

    // Reset entity module state if it exists
    if (window.SchoolManagement.Entity) {
      window.SchoolManagement.Entity.reset();
    }
  },

  bindEvents: function () {
    const self = this;
    const $ = this.$;

    // Bind click event for navigation menu
    $(document).on("click", ".school-nav-item", function (e) {
      e.preventDefault();
      const schoolId = $(this).data("school-id");

      // Active class toggle
      $(".school-nav-item").removeClass("active");
      $(this).addClass("active");

      if (schoolId === "home") {
        // Redirect to external link when 'home' is clicked
        window.location.href = "https://ic3.info";
        return;
      } else {
        // Ensure student is logged in before handling school change
        if (window.SchoolManagement.StudentLogin) {
          window.SchoolManagement.StudentLogin.checkSessionWithCallback(
            function () {
              self.handleSchoolChange(schoolId);
            }
          );
        }
      }
    });

    $("#class-dropdown").on("change", function () {
      const classId = $(this).val();
      self.handleClassChange(classId);
    });
  },

  checkStudentOf: function (schoolId, studentOf, callback) {
    const self = this;

    window.SchoolManagement.Utils.createAjaxRequest({
      url:
        schoolManagementAjax.apiUrl +
        "school-management/v1/checkstudentof/" +
        schoolId +
        "/" +
        encodeURIComponent(studentOf),
      method: "GET",
      success: function (data) {
        const hasAccess = data && data.has_access;
        if (callback && typeof callback === "function") {
          callback(hasAccess);
        }
      },
      error: function () {
        console.error("Error checking student access to school");
        if (callback && typeof callback === "function") {
          callback(false); // Deny access on error
        }
      },
    });
  },

  handleSchoolChange: function (schoolId) {
    const self = this;
    const $ = this.$;
    const studentof = window.SchoolManagement.StudentLogin.studentOf;

    // Check if student has access to this school
    this.checkStudentOf(schoolId, studentof, function (hasAccess) {
      if (!hasAccess) {
        alert("Bạn không có quyền truy cập");
        return;
      }

      // Student has access, proceed with loading classes
      if (schoolId) {
        self.loadClasses(schoolId);
      } else {
        self.loadClasses(studentof);
      }

      $("#class-dropdown").html('<option value="">-- Chọn lớp --</option>');
      $("#entity-grid").empty();
      $("#pagination-container").hide();

      if (window.SchoolManagement.Entity) {
        window.SchoolManagement.Entity.reset();
      }
    });
  },
  handleSchoolChangeWhenLogin: function () {
    const self = this;
    const $ = this.$;
    const studentof = window.SchoolManagement.StudentLogin.studentOf;

    // Student has access, proceed with loading classes
    if (studentof && studentof.trim() !== "") {
      self.loadClasses_bystudentof(studentof);
    } else {
      console.warn("No valid studentof value found");
    }

    $("#class-dropdown").html('<option value="">-- Chọn lớp --</option>');
    $("#entity-grid").empty();
    $("#pagination-container").hide();

    if (window.SchoolManagement.Entity) {
      window.SchoolManagement.Entity.reset();
    }
  },

  handleClassChange: function (classId) {
    const $ = this.$;
    if (classId) {
      // Get the selected class name and update the title
      const selectedClassName = $("#class-dropdown option:selected").text();
      $("#class-title").text(selectedClassName);

      // Check if class has password first
      window.SchoolManagement.Password.checkClassPassword(classId);
    } else {
      $("#entity-container").hide();
      $("#entity-grid").empty();
      $("#pagination-container").hide();

      // Clear class title
      $("#class-title").text("");

      // Reset entity module state
      if (window.SchoolManagement.Entity) {
        window.SchoolManagement.Entity.reset();
      }
    }
  },

  /**
   * Load all schools from API
   */
  loadSchools: function () {
    const self = this;

    window.SchoolManagement.Utils.createAjaxRequest({
      url: schoolManagementAjax.apiUrl + "school-management/v1/schools",
      method: "GET",
      success: function (data) {
        self.populateSchoolNav(data);
      },
      error: function () {
        console.error("Error loading schools");
      },
    });
  },

  /**
   * Populate school dropdown with data
   * @param {Array} data - Schools data
   */
  populateSchoolNav: function (data) {
    const $ = this.$;
    const $navlist = $("#school-nav-list");

    if (data && data.length > 0) {
      // Sort schools naturally by title
      const sortedData = window.SchoolManagement.Utils.sortByTitle(data);

      $.each(sortedData, function (index, school) {
        $navlist.append(
          '<li class="nav-item"><a href="#" class="nav-link school-nav-item" data-school-id="' +
            school.ID +
            '">' +
            school.post_title +
            "</a></li>"
        );
      });
    }
  },

  loadClasses_bystudentof: function (schoolId) {
    const self = this;

    // Debug logging

    const url =
      schoolManagementAjax.apiUrl +
      "school-management/v1/classesbystudentof/" +
      encodeURIComponent(schoolId);

    window.SchoolManagement.Utils.createAjaxRequest({
      url: url,
      method: "GET",
      success: function (data) {
        const classId = data[0]?.ID || null;
        self.handleClassChange(classId);
      },
      error: function (xhr, status, error) {
        console.error("Error loading classes by studentof:", {
          status: status,
          error: error,
          responseText: xhr.responseText,
          url: url,
        });
      },
    });
  },

  /**
   * Load classes for a specific school
   * @param {string} schoolId - School ID
   */
  loadClasses: function (schoolId) {
    const self = this;

    window.SchoolManagement.Utils.createAjaxRequest({
      url:
        schoolManagementAjax.apiUrl +
        "school-management/v1/classes/" +
        schoolId,
      method: "GET",
      success: function (data) {
        // self.populateClassDropdown(data);
        const classId = data[0]?.ID || null;
        self.handleClassChange(classId);
      },
      error: function () {
        console.error("Error loading classes");
      },
    });
  },

  /**
   * Populate class dropdown with data
   * @param {Array} data - Classes data
   */
  populateClassDropdown: function (data) {
    const $ = this.$;
    const $dropdown = $("#class-dropdown");
    $dropdown.html('<option value="">-- Chọn lớp --</option>');

    if (data && data.length > 0) {
      // Sort classes naturally by title
      const sortedData = window.SchoolManagement.Utils.sortByTitle(data);

      $.each(sortedData, function (index, classItem) {
        $dropdown.append(
          '<option value="' +
            classItem.ID +
            '">' +
            classItem.post_title +
            "</option>"
        );
      });
    }
  },

  /**
   * Check login status on page load to show/hide logout button
   */
  checkInitialLoginStatus: function () {
    if (window.SchoolManagement.StudentLogin) {
      window.SchoolManagement.Utils.createAjaxRequest({
        url:
          schoolManagementAjax.apiUrl +
          "school-management/v1/check-student-session",
        method: "GET",
        success: function (data) {
          if (data && data.logged_in) {
            // Set studentOf first before updating login status
            window.SchoolManagement.StudentLogin.studentOf =
              data.student_of || null;
            window.SchoolManagement.StudentLogin.updateLoginStatus(
              true,
              data.student_id,
              data.student_name,
              data.student_of
            );
          } else {
            window.SchoolManagement.StudentLogin.studentOf = null;
            window.SchoolManagement.StudentLogin.updateLoginStatus(false);
          }
        },
        error: function () {
          console.error("Error checking initial login status");
        },
      });
    }
  },
};