TeachersForm::TeachersForm(QWidget* parent): QDialog(parent)
{
	setupUi(this);
	
	currentTeacherTextEdit->setReadOnly(true);

	renameTeacherPushButton->setDefault(true);

	teachersListWidget->setSelectionMode(QAbstractItemView::SingleSelection);

	connect(renameTeacherPushButton, SIGNAL(clicked()), this, SLOT(renameTeacher()));
	connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(addTeacherPushButton, SIGNAL(clicked()), this, SLOT(addTeacher()));
	connect(sortTeachersPushButton, SIGNAL(clicked()), this, SLOT(sortTeachers()));
	connect(removeTeacherPushButton, SIGNAL(clicked()), this, SLOT(removeTeacher()));
	connect(teachersListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(teacherChanged(int)));
	connect(activateTeacherPushButton, SIGNAL(clicked()), this, SLOT(activateTeacher()));
	connect(deactivateTeacherPushButton, SIGNAL(clicked()), this, SLOT(deactivateTeacher()));
	connect(teachersListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(renameTeacher()));

	centerWidgetOnScreen(this);
	restoreFETDialogGeometry(this);
	//restore splitter state
	QSettings settings(COMPANY, PROGRAM);
	if(settings.contains(this->metaObject()->className()+QString("/splitter-state")))
		splitter->restoreState(settings.value(this->metaObject()->className()+QString("/splitter-state")).toByteArray());
	
	teachersListWidget->clear();
	for(int i=0; i<gt.rules.teachersList.size(); i++){
		Teacher* tch=gt.rules.teachersList[i];
		teachersListWidget->addItem(tch->name);
	}
	
	if(teachersListWidget->count()>0)
		teachersListWidget->setCurrentRow(0);
}
Program::Program(bool isadmin, int userid,int _id, QWidget *parent)
    : QWidget(parent)
{

    QString tempTeacherName;
    QString tempStudentName;

    this->is_admin = isadmin;
    this->user_id = userid;
    this->ID = _id;
    //setting program options
    if (this->is_admin)
        this->setFixedSize(480, 275);
    else
        this->setFixedSize(240, 245);
    this->setWindowTitle("IUST School!");
    std::vector<QString> names;
    if (is_admin)
    {
        grpTeachers = new QGroupBox(this);
        grpTeachers->setGeometry(245, 3, 233,100);

        teachersCombo = new QComboBox();

        names = sql.getTeacherNames();
        int numTeachers = names.size();
        for (int i = 0; i < numTeachers; ++i)
        {
            teachersCombo->addItem(names[i],i);
        }
        teachersCombo->setParent(grpTeachers);
        teachersCombo->setGeometry(10,2,140,20);

        tempTeacherName = teachersCombo->itemText(teachersCombo->currentIndex());

        connect(teachersCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(UpdateTeacherData()));

        label1 = new QLabel("معلم");
        label1->setGeometry(158, 2, 60, 20);
        label1->setParent(grpTeachers);

        label2 = new QLabel("نام:");
        label2->setGeometry(158, 24, 60, 20);
        label2->setParent(grpTeachers);

        label3 = new QLabel();
        label3->setGeometry(10, 24, 140, 20);
        label3->setAlignment(Qt::AlignLeft | Qt::AlignCenter);
        label3->setText(tempTeacherName);
        label3->setParent(grpTeachers);

        label4 = new QLabel("کلاس فعلی:");
        label4->setGeometry(158, 48, 60, 20);
        label4->setParent(grpTeachers);

        label5 = new QLabel();
        label5->setGeometry(10, 48, 140, 20);
        label5->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
        label5->setText(getClassName(sql.getTeacherClass(tempTeacherName)));
        label5->setParent(grpTeachers);

        classCombo = new QComboBox();
        classCombo->addItem("1/1");
        classCombo->addItem("1/2");
        classCombo->addItem("2/1");
        classCombo->addItem("2/2");
        classCombo->addItem("3/1");
        classCombo->addItem("3/2");
        classCombo->addItem("4/1");
        classCombo->addItem("4/2");
        classCombo->addItem("5/1");
        classCombo->addItem("5/2");
        classCombo->addItem("6/1");
        classCombo->addItem("6/2");
        classCombo->setParent(grpTeachers);
        classCombo->setGeometry(10,72,140,20);

        btnAddTeacherToClass = new QPushButton("تخصیص");
        btnAddTeacherToClass->setGeometry(160, 71, 60, 22);
        btnAddTeacherToClass->setParent(grpTeachers);

        connect(btnAddTeacherToClass, SIGNAL(clicked()), this, SLOT(bindTeacher()));

        grpDelays = new QGroupBox(this);
        grpDelays->setGeometry(245, 107, 233,100);

        btnFileDelayForStudent = new QPushButton("ثبت تاخیر برای دانش آموز", grpDelays);
        btnFileDelayForStudent->setGeometry(10, 5, 220, 24);

        connect(btnFileDelayForStudent, SIGNAL(clicked()), this, SLOT(addDelay()));

        delaysList = new QListWidget(grpDelays);
        delaysList->setGeometry(10, 35, 220, 60);

        grpAdminPanel = new QGroupBox(this);
        grpAdminPanel->setGeometry(245, 210, 233,54);

        btnAdminChangePassword = new QPushButton("تغییر رمز", grpAdminPanel);
        btnAdminChangePassword->setGeometry(10,1,213,22);

        connect(btnAdminChangePassword, SIGNAL(clicked()), this, SLOT(adminChangePassword()));

        btnAddTeacher = new QPushButton("اضافه کردن معلم", grpAdminPanel);
        btnAddTeacher->setGeometry(10,27,213,22);

        connect(btnAddTeacher, SIGNAL(clicked()), this, SLOT(addTeacher()));
    }

    grpStudents = new QGroupBox(this);
    if (is_admin)
        grpStudents->setGeometry(5, 3, 233,260);
    else
        grpStudents->setGeometry(5, 3, 233,230);
    studentsCombo = new QComboBox();

    std::vector<int> ids = sql.getStudentIDs(this->user_id);
    names = sql.getStudentNames(this->user_id);
    int numStudents = names.size();
    for (int i = 0; i < numStudents; ++i)
    {
        studentsCombo->addItem(names[i],ids[i]);
    }
    studentsCombo->setParent(grpStudents);
    studentsCombo->setGeometry(10,2,140,20);

    tempStudentName = studentsCombo->itemText(studentsCombo->currentIndex());

    connect(studentsCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(UpdateStudentData()));

    label1s = new QLabel("دانش آموز");
    label1s->setGeometry(158, 2, 60, 20);
    label1s->setParent(grpStudents);

    label2s = new QLabel("نام:");
    label2s->setGeometry(188, 22, 30, 18);
    label2s->setParent(grpStudents);

    label3s = new QLabel();
    label3s->setGeometry(10, 22, 190, 18);
    label3s->setAlignment(Qt::AlignLeft | Qt::AlignCenter);
    label3s->setText(studentsCombo->itemText(studentsCombo->currentIndex()));
    label3s->setParent(grpStudents);

    label4s = new QLabel("کلاس فعلی:");
    label4s->setGeometry(158, 40, 60, 18);
    label4s->setParent(grpStudents);

    label5s = new QLabel();
    label5s->setGeometry(80, 40, 70, 18);
    label5s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label5s->setText(getClassName(sql.getStudentClass(tempStudentName)));
    label5s->setParent(grpStudents);

    label6s = new QLabel("ریاضی:");
    label6s->setGeometry(180, 58, 40, 18);
    label6s->setParent(grpStudents);

    label7s = new QLabel();
    label7s->setGeometry(110, 58, 50, 18);
    label7s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label7s->setParent(grpStudents);

    label8s = new QLabel("فیزیک:");
    label8s->setGeometry(70, 58, 40, 18);
    label8s->setParent(grpStudents);

    label9s = new QLabel();
    label9s->setGeometry(10, 58, 50, 18);
    label9s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label9s->setParent(grpStudents);

    label10s = new QLabel("فارسی:");
    label10s->setGeometry(180, 76, 40, 18);
    label10s->setParent(grpStudents);

    label11s = new QLabel();
    label11s->setGeometry(110, 76, 50, 18);
    label11s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label11s->setParent(grpStudents);

    label12s = new QLabel("دینی:");
    label12s->setGeometry(70, 76, 40, 18);
    label12s->setParent(grpStudents);

    label13s = new QLabel();
    label13s->setGeometry(10, 76, 50, 18);
    label13s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label13s->setParent(grpStudents);

    label14s = new QLabel("جغرافی:");
    label14s->setGeometry(180, 94, 40, 18);
    label14s->setParent(grpStudents);

    label15s = new QLabel();
    label15s->setGeometry(110, 94, 50, 18);
    label15s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label15s->setParent(grpStudents);

    label16s = new QLabel("شیمی:");
    label16s->setGeometry(70, 94, 40, 18);
    label16s->setParent(grpStudents);

    label17s = new QLabel();
    label17s->setGeometry(10, 94, 50, 18);
    label17s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label17s->setParent(grpStudents);

    label18s = new QLabel("انظباط:");
    label18s->setGeometry(180, 112, 40, 18);
    label18s->setParent(grpStudents);

    label19s = new QLabel();
    label19s->setGeometry(110, 112, 50, 18);
    label19s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label19s->setParent(grpStudents);

    label20s = new QLabel("معدل:");
    label20s->setGeometry(70, 112, 40, 18);
    label20s->setParent(grpStudents);

    label21s = new QLabel();
    label21s->setGeometry(10, 112, 50, 18);
    label21s->setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
    label21s->setParent(grpStudents);

    update_student_scores();

    if (is_admin)
    {

        classSCombo = new QComboBox();
        classSCombo->addItem("1/1");
        classSCombo->addItem("1/2");
        classSCombo->addItem("2/1");
        classSCombo->addItem("2/2");
        classSCombo->addItem("3/1");
        classSCombo->addItem("3/2");
        classSCombo->addItem("4/1");
        classSCombo->addItem("4/2");
        classSCombo->addItem("5/1");
        classSCombo->addItem("5/2");
        classSCombo->addItem("6/1");
        classSCombo->addItem("6/2");
        classSCombo->setParent(grpStudents);
        classSCombo->setGeometry(10,140,140,24);

        btnAddStudentToClass = new QPushButton("تخصیص");
        btnAddStudentToClass->setGeometry(160, 140, 60, 24);
        btnAddStudentToClass->setParent(grpStudents);

        connect(btnAddStudentToClass, SIGNAL(clicked()), this, SLOT(bindStudent()));

        btnAddStudent = new QPushButton("اضافه کردن دانش آموز جدید");
        btnAddStudent->setGeometry(70, 170, 150, 24);
        btnAddStudent->setParent(grpStudents);

        connect(btnAddStudent, SIGNAL(clicked()), this, SLOT(addNewStudent()));

        btnRemoveStudent = new QPushButton("حذف کردن دانش آموز کنونی");
        btnRemoveStudent->setGeometry(70, 200, 150, 24);
        btnRemoveStudent->setParent(grpStudents);

        connect(btnRemoveStudent, SIGNAL(clicked()), this, SLOT(removeCurrentStudent()));

        btnScoring = new QPushButton("نمره دهی");
        btnScoring->setGeometry(140, 230, 80, 24);
        btnScoring->setParent(grpStudents);

        connect(btnScoring, SIGNAL(clicked()), this, SLOT(setStudentScore()));

        coursesCombo = new QComboBox();

        coursesCombo->addItem("انظباط", 6);

        coursesCombo->setParent(grpStudents);
        coursesCombo->setGeometry(10,230,120,24);

        update_delays();
    }
    else
    {

        btnRemoveStudent = new QPushButton("اخراج دانش آموز کنونی");
        btnRemoveStudent->setGeometry(70, 140, 150, 24);
        btnRemoveStudent->setParent(grpStudents);

        connect(btnRemoveStudent, SIGNAL(clicked()), this, SLOT(removeCurrentStudent()));

        btnScoring = new QPushButton("نمره دهی");
        btnScoring->setGeometry(140, 170, 80, 24);
        btnScoring->setParent(grpStudents);

        connect(btnScoring, SIGNAL(clicked()), this, SLOT(setStudentScore()));

        coursesCombo = new QComboBox();

        coursesCombo->addItem("ریاضی", 0);
        coursesCombo->addItem("فیزیک", 1);
        coursesCombo->addItem("فارسی", 2);
        coursesCombo->addItem("دینی", 3);
        coursesCombo->addItem("جغرافی", 4);
        coursesCombo->addItem("شیمی", 5);

        coursesCombo->setParent(grpStudents);
        coursesCombo->setGeometry(10,170,120,24);

        btnTeacherChangePassword = new QPushButton("تغییر رمز", grpStudents);
        btnTeacherChangePassword->setGeometry(70, 200, 150, 24);

        connect(btnTeacherChangePassword, SIGNAL(clicked()), this, SLOT(changeTeacherPassword()));
    }
}