package mes.gui;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.print.*;
import java.sql.*;
import javax.swing.table.*;

import mes.dbase.*;
import mes.gui.*;
import mes.util.*;

public class ClassProgramChecking extends MESFrame implements ActionListener{

    MainFrame parent;
    
    showClassPrograms showclassprograms;
    
    MESTable questionTable, answerTable;
    MESTable attendancetable;
    String url="jdbc:odbc:DBMaster";
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    Connection connection;
    Statement statement;
    ResultSet rs;
    PrinterJob job;
    PageFormat landscape;
    String rowCount="",strlnOpen="",strlClose="";
    int countStudent=0;
    double countLimit=0.0;
    
    JTextField IDField,FirstNameField,LastNameField,AddressField,GenderField,CourseField,SubjectField,UnitsField, totalLabUnits,daysField,timeField,roomField,limitField,instructorField;
    JTextField totalSubjectField;
    public String strID,strFName,strLName,strAddress,strGender,strCourse,strLec,strLab,strDays,strTime,strRoom,strLimit,strInstructor;
    public ClassProgramChecking(MainFrame main ,showClassPrograms paramshowPrograms, String IDCode,String FNameCode,String LNameCode,String LecID,String LabID,String daysID,String timeID,String roomID,String limitID,String instructorID){
    	
    	super(600,410,"", new String[] {"Print","Register Student","Drop Subject","Close"});
        strID = IDCode;
        strFName=FNameCode;
        strLName =LNameCode;
   		strLec=LecID;
   	    strLab=LabID;
   	    strDays=daysID;
   	    strTime=timeID;
   	    strRoom=roomID;
   	    strLimit=limitID;
   	    strInstructor=instructorID;
   	    
   		parent = main;
        setTitle("Student Class List");
        init();
        fillTable();
        setSize(580,390);
        setVisible(true);
    }
    void init(){
        addButtonActionListener(this);
        
        MAINPANEL.setLayout(null);
        //MAINPANEL.setBackground(new Color(213,229,219));
      
        JLabel t1=new JLabel("Student Enrollment File");
        MAINPANEL.add(t1).setBounds(300,5,200,25);
        t1.setFont(new Font("Verdana",Font.BOLD,14));
        
        JLabel id = new JLabel("Class No",JLabel.LEFT);
        JLabel firstname= new JLabel("Subject Code", JLabel.LEFT);
        JLabel lastname= new JLabel("Description", JLabel.LEFT);
        JLabel address= new JLabel("Instructor",JLabel.LEFT);
        JLabel gender=new JLabel("Lec. Unit(s)",JLabel.LEFT);
        JLabel course=new JLabel("Lab Unit(s)",JLabel.LEFT);
        JLabel lbltotalsubjects=new JLabel("No. of Students",JLabel.LEFT);
        lbltotalsubjects.setFont(new Font("Verdana",Font.BOLD,14));
        JLabel lbltotunits=new JLabel("Population Limit");
        lbltotunits.setFont(new Font("Verdana",Font.PLAIN,12));
        
       //new------------------------------
        
        JLabel days=new JLabel("Days",JLabel.LEFT);
        MAINPANEL.add(days).setBounds(330,60,100,24);
        days.setFont(new Font("Verdana",Font.BOLD,12));
        
        daysField=new JTextField(strDays);
        MAINPANEL.add(daysField).setBounds(375,60,100,24);  
        daysField.setFont(new Font("Verdana",Font.BOLD,11));
       
        
        JLabel time=new JLabel("Time",JLabel.LEFT);
        MAINPANEL.add(time).setBounds(480,60,100,24);
        time.setFont(new Font("Verdana",Font.BOLD,12));
        
        timeField=new JTextField(strTime);
        MAINPANEL.add(timeField).setBounds(560,60,100,24);  
        timeField.setFont(new Font("Verdana",Font.BOLD,11));
       
      
        JLabel room=new JLabel("Room#",JLabel.LEFT);
        MAINPANEL.add(room).setBounds(480,90,100,24);
        room.setFont(new Font("Verdana",Font.BOLD,12));
        
        roomField=new JTextField(strRoom);
        MAINPANEL.add(roomField).setBounds(560,90,100,24);  
        roomField.setFont(new Font("Verdana",Font.BOLD,11));
      
       //new------------------------------
        
        //## code patch 07/10/2006
        ///begin!
        JLabel labUnits = new JLabel ("Class Status", JLabel.LEFT );
        labUnits.setFont(new Font("Verdana",Font.PLAIN,14));
        //end
       
        MAINPANEL.add(id).setBounds(20,30,100,24);
        id.setFont(new Font("Verdana",Font.BOLD,13));
        
        MAINPANEL.add(firstname).setBounds(20,60,100,24);
        firstname.setFont(new Font("Verdana",Font.BOLD,11));
      
        MAINPANEL.add(lastname).setBounds(20,90,100,24);
        lastname.setFont(new Font("Verdana",Font.BOLD,11));
      
        MAINPANEL.add(address).setBounds(20,120,100,24);
        address.setFont(new Font("Verdana",Font.BOLD,11));
      
        MAINPANEL.add(gender).setBounds(270,30,100,24);
        gender.setFont(new Font("Verdana",Font.BOLD,13));
        
        MAINPANEL.add(course).setBounds(470,30,100,24);
        course.setFont(new Font("Verdana",Font.BOLD,13));
      
        MAINPANEL.add(lbltotalsubjects).setBounds(20,330,200,21);
        lbltotalsubjects.setFont(new Font("Verdana",Font.BOLD,10));
      
        MAINPANEL.add(lbltotunits).setBounds(20,360,200,21);
        lbltotunits.setFont(new Font("Verdana",Font.BOLD,10));
      
        
        MAINPANEL.add(labUnits).setBounds(420,360,200,21);
        labUnits.setFont(new Font("Verdana",Font.BOLD,10));
        
     
        IDField = new JTextField(strID);
        IDField.setFont(new Font("Verdana",Font.BOLD,11));
        FirstNameField=new JTextField(strFName);
        FirstNameField.setFont(new Font("Verdana",Font.BOLD,11));
      
        LastNameField = new JTextField(strLName);
        LastNameField.setFont(new Font("Verdana",Font.BOLD,11));
       
        AddressField = new JTextField(strInstructor);
        AddressField.setFont(new Font("Verdana",Font.BOLD,11));
       
        GenderField=new JTextField(strLec);
        GenderField.setFont(new Font("Verdana",Font.BOLD,11));

        CourseField=new JTextField(strLab);
        CourseField.setFont(new Font("Verdana",Font.BOLD,11));

        SubjectField=new JTextField();
        SubjectField.setFont(new Font("Verdana",Font.BOLD,10));

        UnitsField=new JTextField();
        UnitsField.setFont(new Font("Verdana",Font.BOLD,10));
        
        totalLabUnits = new JTextField();//Subject Status
        totalLabUnits.setFont(new Font("Verdana",Font.BOLD,10));
     
        totalSubjectField=new JTextField(strLimit);//Population Limit
        totalSubjectField.setFont(new Font("Verdana",Font.BOLD,10));
        
        IDField.setEditable(false);
        LastNameField.setEditable(false);
        FirstNameField.setEditable(false);
        AddressField.setEditable(false);
        GenderField.setEditable(false);
        CourseField.setEditable(false);
        totalLabUnits.setEditable (false);
        UnitsField.setEditable(false);
        totalLabUnits.setEditable(false);
        SubjectField.setEditable(false);
        totalSubjectField.setEditable(false);
        
        MAINPANEL.add(IDField).setBounds(130,30,140,24);
        MAINPANEL.add(FirstNameField).setBounds(130,60,190,24);
        MAINPANEL.add(LastNameField).setBounds(130,90,290,24);
        MAINPANEL.add(AddressField).setBounds(130,120,270,24);
        MAINPANEL.add(GenderField).setBounds(360,30,100,24);
        MAINPANEL.add(CourseField).setBounds(560,30,100,24);
        MAINPANEL.add(SubjectField).setBounds(170,330,70,24);//total Subjects
        //MAINPANEL.add(UnitsField).setBounds(560,330,70,24);//Capacity
        MAINPANEL.add(totalLabUnits).setBounds(560,360,70,24);//Status
        MAINPANEL.add(totalSubjectField).setBounds(170,360,70,21);
    
        questionTable = new MESTable("",new String[] {"IDNumber","FirstName","LastName","Address","Gender"});
        MAINPANEL.add(questionTable).setBounds(2,150,735,175);
        questionTable.setColumnSize(new int[]{130,170,250,240,80} );
        questionTable.useDefaultRenderer(new int[] {-1});
       
          questionTable.getTable().addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent event){
            }
        }
      );
    }
    Hashtable getQuestion(String qno, String exNo){
        DataCollection cols = parent.getCollection("classAttendance_data");
        Hashtable question = null;
        for(int i = 0; i < cols.size(); i++){
            Hashtable q = (Hashtable)cols.get(i);
            if(q.get("IDNumber").toString().equals(qno.trim()) && q.get("ClassNo").equals(exNo)){
                question = q;
                break;
            }
        }
        return  question;
    }
    void fillTable(){
        DataCollection col = parent.getCollection("classAttendance_data");
        questionTable.getTableModel().setRowCount(0);
        questionTable.getTableModel().getRowCount();
        for(int i = 0; i < col.size(); i++){
            Hashtable h = (Hashtable)col.get(i);
            if(h.get("ClassNo").toString().equals(strID)){
                Object row[] = {
                                new JLabel(""+h.get("IDNumber")+""),
                                new JLabel(""+h.get("FirstName")+""),
                                new JLabel(""+h.get("LastName")+""),
                                new JLabel(""+h.get("Address")+""),
                                new JLabel(""+h.get("Gender")+""),
                               // new JLabel(""+h.get("Course")+""),
                               // new JLabel(""+h.get("YearLevel")+""),
                                
                                
                                
       						    };
                questionTable.getTableModel().addRow(row);
     		    //---get total no. of subjects
     		 	//UnitsField.setText ( i + " "  );
//                  cntrRow=questionTable.getTableModel(); 
             }
        }
        //-- code patch 07/10/2006 
        //## begin!
        double noOfLecUnits = 0.0;
        double noOfLabUnits = 0.0;
        double totalSubjects= 0.0;
        double noOfUnits    = 0.0;
        double rowCount     = 0;
        for (int  i = 0; i < questionTable.getTableModel().getRowCount(); i++ )
        {
        //  noOfUnits += Integer.parseInt ( questionTable.getTableModel().getValueAt ( i, 2 ).toString() );
        	try
        	{
	 		//	JLabel tempLec = ( JLabel ) questionTable.getTable().getValueAt ( i, 3 );
	 	//		JLabel tempLab = ( JLabel ) questionTable.getTable().getValueAt ( i, 4 );
	 			
	 	//		noOfLecUnits += Double.parseDouble ( tempLec.getText() );
	 	//		noOfLabUnits += Double.parseDouble ( tempLab.getText() );
	 	//	    totalSubjects = noOfLecUnits+noOfLabUnits;
  	            rowCount=questionTable.getTableModel().getRowCount();
 
          SubjectField.setText(""+rowCount);//No. of Students enrolled
   	      countStudent=Integer.parseInt(SubjectField.getText());
   		  countLimit=Integer.parseInt(totalSubjectField.getText());
   	
   	        }
   	        catch ( NumberFormatException npe )
	 		{
	 		

	 		}	     

   		  if(rowCount>=countLimit)
   		  {
   		  	 strlnOpen="Open";
   		  	 totalLabUnits.setText(strlnOpen);
   		  	 totalLabUnits.setFont(new Font("Verdana",Font.BOLD,12));
   		  	 totalLabUnits.setForeground(Color.red.darker());
   		  	  
   		  }
   		  else
   		  {
   		    strlnOpen="Close";
   		    totalLabUnits.setText(strlnOpen);
   		    totalLabUnits.setFont(new Font("Verdana",Font.BOLD,12));
   		    totalLabUnits.setForeground(Color.blue.darker());
   		  }

   	  	}
   	   
   	   // UnitsField.setText ( noOfLecUnits + "" );
       // totalLabUnits.setText ( noOfLabUnits + "" );
        //totalSubjectField.setText(totalSubjects + "");
      //   SubjectField.setText(""+rowCount);
   	    
        //## end!
  }

/*
   void delete(){
        if(questionTable2.getTable().getSelectedRow() == -1){
            JOptionPane.showMessageDialog(parent, "Select the question to delete.", "Delete question", JOptionPane.WARNING_MESSAGE);
            return;
        }
        int del = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this question?", "Delete", 
                                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE
                                        );
        if(del ==0 ){    
            Hashtable question = getQuestion(questionTable2.getValueAtSelectedRow(0).trim(), strID);
            String qNumber = question.get("ClassNo").toString();
            
            String sql = "DELETE FROM enrollment_data WHERE IDNumber = '"+strID+"' AND ClassNo = '"+qNumber+"'";
            int delete = parent.database.executeUpdate(sql);
            if(delete == 0){
                DataCollection col = parent.getCollection("enrollment_data");
                col.remove(question);
                fillTable();
            }
        }
    }
  */
    public void actionPerformed(ActionEvent event){
        String act = event.getActionCommand();
        if(act.equals("Close"))
        {
            dispose();
        }
         else if(act.equals("Register Student"))
         {
         parent.addFrame4(new enlist_students_to_subject(parent, this));
         }  
         else if(act.equals("Drop Subject"))
         {
        // delete();	
         }
        else
        {
        fillTable();	
        }
    }

}//\\\\\\\\\\\\\\\\\\\END OF CLASS DSAChecking.java\\\\\\\\\\\\\//
