using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class rl_admins_pg_ExamAdd : System.Web.UI.Page
{
    //*************************
    SqlConnection con;
    SqlCommand cmd;
    //*************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "تعریف آزمون کلاسی";
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void btnSelectClass_Click(object sender, EventArgs e)
    {
        try
        {
            SqlDataSource1.SelectCommand = "SELECT t_Classes.classId, t_Classes.classTitle, t_Classes.status, t_Teachers.fullName, t_Courses.courseName FROM t_Classes INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId WHERE t_Teachers.teacherId = " + Int32.Parse(Session["iden"].ToString());
            GridClasses.DataBind();
            mpeClasses.Show();
        }
        catch
        {
        }
    }
    protected void btnStudentsCancel_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Hide();
        }
        catch
        {
        }
    }
    protected void GridClasses_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "SelectPress")
            {
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                int classId = Int32.Parse(e.CommandArgument.ToString());
                string classTitle = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Classes", "classTitle", "classId", classId);
                tbClassId.Text = classId.ToString();
                tbClassTitle.Text = classTitle;
                UpdatePanel1.Update();
                mpeClasses.Hide();
            }
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            System.Threading.Thread.Sleep(2000);
            if ((tbClassId.Text.Length > 0) && (tbExamName.Text.Length > 0) && (tbMaxGrade.Text.Length > 0))
            {
                int classId = Int32.Parse(tbClassId.Text);
                string examName = tbExamName.Text;
                string examType = (cmbExamType.SelectedIndex > 0 ? cmbExamType.SelectedItem.ToString() : "");
                string statements = tbStatements.Text;
                string questionsType = (cmbQuestionsType.SelectedIndex > 0 ? cmbQuestionsType.SelectedItem.ToString() : "");
                int maxGrade = Int32.Parse(tbMaxGrade.Text);
                string weekDay = (cmbDayOfWeek.SelectedIndex > 0 ? cmbDayOfWeek.SelectedItem.ToString() : "");
                string day = (cmbDay.SelectedIndex > 0 ? cmbDay.SelectedItem.ToString() : "");
                string month = (cmbMonth.SelectedIndex > 0 ? cmbMonth.SelectedItem.ToString() : "");
                string year = (cmbYear.SelectedIndex > 0 ? cmbYear.SelectedItem.ToString() : "");
                string examDate = weekDay + " " + day + " " + month + " " + year;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = t_ExamsInsertCommand(examName, examDate, classId, examType, statements, questionsType, maxGrade);
                    cmd.ExecuteNonQuery();
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "با موفقیت ذخیره شد.";
                }
                catch
                {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = "ذخیره سازی با شکست مواجه شد.";
                }
                finally
                {
                    con.Close();
                }
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "مقادیر اجباری را وارد کنید.";
            }
        }
        catch
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "ذخیره سازی با شکست مواجه شد.";
        }
    }

    private string t_ExamsInsertCommand(string ExamName, string ExamDate, int ClassId, string ExamType, string Statements, string QuestionsType, int MaxGrade)
    {
        string comm = "insert into t_Exams (examName, examDate, classId, examType, statements, questionsType, maxGrade) values(N'" +
            ExamName + "', N'" +
            ExamDate + "', " +
            ClassId + " , N'" +
            ExamType + "', N'" +
            Statements + "', N'" +
            QuestionsType + "', " +
            MaxGrade + " )";
        return comm;
    }

    protected void cmbExamType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbExamType.SelectedIndex == 1)
            {
                cmbQuestionsType.Enabled = true;
            }
            else
            {
                cmbQuestionsType.Enabled = false;
            }
        }
        catch
        {
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
    protected void GridClasses_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridClasses.PageIndex = e.NewPageIndex;
            SqlDataSource1.SelectCommand = "SELECT t_Classes.classId, t_Classes.classTitle, t_Classes.status, t_Teachers.fullName, t_Courses.courseName FROM t_Classes INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId WHERE t_Teachers.teacherId = " + Int32.Parse(Session["iden"].ToString());
            GridClasses.DataBind();
        }
        catch
        {
        }
    }
}
