// Fees update method
  private void updateFeesData(long id) {
    String columns[] = {"Course", "Fees Payed", "Total fees", "Installments"};
    try {
      Database db = new Database();

      panel_7.removeAll();

      feestablemodel = new MyTableModel(db.getFeeData(id), columns);

      feestable = new JTable(feestablemodel);
      feestable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      feestable.getSelectionModel().addListSelectionListener(this);

      feesscrollpane = new JScrollPane(feestable);
      panel_7.add(feesscrollpane);

      // change fees payed label
      feespayedlabel.setText("Fees Payed");
      feesduelabel.setText("Fees Due");
      totalfeeslabel.setText("Total Fees");

      panel_7.revalidate();
    } catch (Exception e) {
      JOptionPane.showMessageDialog(this, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
    }
  }
  // Add fee method
  private void addFees() {
    try {
      float feesadded = Float.parseFloat(addfeestextfield.getText());

      int id = table.getSelectionModel().getMinSelectionIndex();
      long studentid = (long) table.getValueAt(id, 0);

      int index = feestable.getSelectionModel().getMinSelectionIndex();
      String coursename = (String) feestable.getValueAt(index, 0);

      Database db = new Database();
      db.addFees(feesadded, studentid, coursename);

      updateFeesData(studentid);

      addfeestextfield.setText("");
    } catch (Exception e) {
      JOptionPane.showMessageDialog(this, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
    }
  }
  // Add course method
  public void addCourse() {
    try {
      Database db = new Database();

      String coursename = (String) coursecombobox.getSelectedItem();
      if (coursecombobox.getSelectedIndex() == 0) {
        throw new Exception("No course selected");
      }

      float fees = db.getCoursefees(coursename);
      float totalfees = fees + (ims.main.Settings.getInstallment() * (int) spinner.getValue());

      long id = (long) table.getValueAt((int) table.getSelectionModel().getMinSelectionIndex(), 0);

      db.addCourseToCurrentStudent(id, totalfees, (int) spinner.getValue(), coursename);

      updateFeesData(id);
      courseReset();
    } catch (Exception e) {
      JOptionPane.showMessageDialog(this, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
    }
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  private void initializeComponent() {
    // Initialize panels
    panel_1 = new JPanel();
    panel_2 = new JPanel();
    panel_3 = new JPanel();
    panel_4 = new JPanel();
    panel_5 = new JPanel();
    panel_6 = new JPanel();
    panel_7 = new JPanel();

    // Initialize spinner
    spinnermodel = new SpinnerNumberModel(0, 0, 10, 1);
    spinner = new JSpinner(spinnermodel);

    // Initialize combo box
    searchcombobox = new JComboBox();
    searchcombobox.addItem("All");
    searchcombobox.addItem("By Name");
    searchcombobox.addItem("By Guardian Name");
    searchcombobox.addItem("By Course");
    searchcombobox.addItem("Due Fees");
    searchcombobox.setSelectedIndex(0);

    coursecombobox = new JComboBox();
    coursecombobox.addItem("Select Course");
    Database db = new Database();
    String courses[] = db.getCourses();

    if (courses != null) {
      for (String x : courses) {
        coursecombobox.addItem(x);
      }
    }

    // Initialize text field
    searchtextfield = new JTextField(10);
    addfeestextfield = new JTextField(8);

    // Initialize button
    searchbutton = new JButton("Search");
    addfeesbutton = new JButton("Add fees");
    addcoursebutton = new JButton("Add Course");

    // Initialize check box
    allfieldcheckbox = new JCheckBox("Show all fields");
    allfieldcheckbox.setSelected(true);

    // Initialize label
    totalstudentlabel = new JLabel("Total Student");
    feespayedlabel = new JLabel("Fees Payed");
    feesduelabel = new JLabel("Fees due");
    totalfeeslabel = new JLabel("Total fees");

    // Add Item listener to check box
    allfieldcheckbox.addItemListener(this);

    // Add Action Listener to button
    searchbutton.addActionListener(this);
    addfeesbutton.addActionListener(this);
    addcoursebutton.addActionListener(this);

    // Add Key Listener to button
    searchbutton.addKeyListener(this);
    addfeesbutton.addKeyListener(this);
    addcoursebutton.addKeyListener(this);
  }
  // Method to update Table and related components
  private void update() {
    Database db = new Database();
    String column[] = {
      "ID",
      "NAME",
      "GENDER",
      "GUARDIAN_ROLE",
      "GUARDIAN_NAME",
      "PRESENT_ADDRESS",
      "PRESENT_CITY",
      "PRESENT_PHONE"
    };

    try {
      // Update table
      if (searchcombobox.getSelectedIndex() == 0 && allfieldcheckbox.isSelected()) {
        mytablemodel = new MyTableModel(db.getAllStudent(), db.getColumnNameFromStudent());
      } else if (searchcombobox.getSelectedIndex() == 0 && !allfieldcheckbox.isSelected()) {
        mytablemodel = new MyTableModel(db.getSomeFieldsFromStudent(), column);
      } else if (searchcombobox.getSelectedIndex() == 1 && allfieldcheckbox.isSelected()) {
        String query =
            "SELECT * FROM Student WHERE NAME = \'" + searchtextfield.getText().trim() + "\'";
        mytablemodel = new MyTableModel(db.getData(query), db.getColumnNameFromStudent());
      } else if (searchcombobox.getSelectedIndex() == 1 && !allfieldcheckbox.isSelected()) {
        String query =
            "SELECT ID, NAME, GENDER, GUARDIAN_ROLE, GUARDIAN_NAME, PRESENT_ADDRESS, PRESENT_CITY, PRESENT_PHONE FROM Student WHERE NAME = \'"
                + searchtextfield.getText().trim()
                + "\'";
        mytablemodel = new MyTableModel(db.getData(query), column);
      } else if (searchcombobox.getSelectedIndex() == 2 && allfieldcheckbox.isSelected()) {
        String query =
            "SELECT * FROM Student WHERE GUARDIAN_NAME = \'"
                + searchtextfield.getText().trim()
                + "\'";
        mytablemodel = new MyTableModel(db.getData(query), db.getColumnNameFromStudent());
      } else if (searchcombobox.getSelectedIndex() == 2 && !allfieldcheckbox.isSelected()) {
        String query =
            "SELECT ID, NAME, GENDER, GUARDIAN_ROLE, GUARDIAN_NAME, PRESENT_ADDRESS, PRESENT_CITY, PRESENT_PHONE FROM Student WHERE GUARDIAN_NAME = \'"
                + searchtextfield.getText().trim()
                + "\'";
        mytablemodel = new MyTableModel(db.getData(query), column);
      } else if (searchcombobox.getSelectedIndex() == 3 && allfieldcheckbox.isSelected()) {
        String query =
            "SELECT * FROM Student WHERE ID = ANY(SELECT SID FROM Fee WHERE CID = ANY(SELECT ID FROM CourseInfo WHERE NAME = \'"
                + searchtextfield.getText().trim()
                + "\'))";
        mytablemodel = new MyTableModel(db.getData(query), db.getColumnNameFromStudent());
      } else if (searchcombobox.getSelectedIndex() == 3 && !allfieldcheckbox.isSelected()) {
        String query =
            "SELECT ID, NAME, GENDER, GUARDIAN_ROLE, GUARDIAN_NAME, PRESENT_ADDRESS, PRESENT_CITY, PRESENT_PHONE FROM Student WHERE ID = ANY(SELECT SID FROM Fee WHERE CID = ANY(SELECT ID FROM CourseInfo WHERE NAME = \'"
                + searchtextfield.getText().trim()
                + "\'))";
        mytablemodel = new MyTableModel(db.getData(query), column);
      } else if (searchcombobox.getSelectedIndex() == 4 && allfieldcheckbox.isSelected()) {
        String query =
            "SELECT * FROM Student WHERE ID = ANY(SELECT SID FROM Fee WHERE TOTAL_FEES - FEES_PAYED >= 0)";
        mytablemodel = new MyTableModel(db.getData(query), db.getColumnNameFromStudent());
      } else {
        String query =
            "SELECT ID, NAME, GENDER, GUARDIAN_ROLE, GUARDIAN_NAME, PRESENT_ADDRESS, PRESENT_CITY, PRESENT_PHONE FROM Student WHERE ID = ANY(SELECT SID FROM Fee WHERE TOTAL_FEES - FEES_PAYED >= 0)";
        mytablemodel = new MyTableModel(db.getData(query), column);
      }

      table = new JTable(mytablemodel);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      // Add list selection listener to table
      table.getSelectionModel().addListSelectionListener(this);

      TableColumn col = null;
      for (int i = 3; i < table.getColumnCount(); i++) {
        col = table.getColumnModel().getColumn(i);
        col.setPreferredWidth(200);
      }

      scrollpane = new JScrollPane(table);

      panel_3.removeAll();
      panel_3.add(scrollpane);

      // Update total student label
      int total = db.getTotalStudent();
      totalstudentlabel.setText("Total Student = " + total);

      // Clear search combo box
      searchtextfield.setText("");

      this.revalidate();
    } catch (Exception e) {
      JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
  }