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_ExamEdit : System.Web.UI.Page
{
    //******************************************
    SqlConnection con;
    SqlCommand cmd;
    //******************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "ویرایش آزمون";
                string examName = "", examDate = "", examType = "", statements = "", questionsType = "", classTitle = "", weekDay = "", day = "", month = "", year = "";
                int maxGrade = 0, classId = 0;
                int examId = Int32.Parse(Request.QueryString["examId"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select t_Exams.examName, t_Exams.examDate, t_Classes.classId, t_Exams.examType, t_Exams.statements, t_Exams.questionsType, t_Exams.maxGrade, t_Classes.classTitle FROM t_Exams INNER JOIN t_Classes ON t_Exams.classId = t_Classes.classId Where examId = " + examId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            examName = ( rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            examDate = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            classId = (rdr.GetValue(2) != DBNull.Value ? rdr.GetInt32(2) : 0);
                            examType = (rdr.GetValue(3) != DBNull.Value ? rdr.GetString(3) : "");
                            statements = (rdr.GetValue(4) != DBNull.Value ? rdr.GetString(4) : "");
                            questionsType = (rdr.GetValue(5) != DBNull.Value ? rdr.GetString(5) : "");
                            maxGrade = (rdr.GetValue(6) != DBNull.Value ? rdr.GetInt32(6) : 0);
                            classTitle = (rdr.GetValue(7) != DBNull.Value ? rdr.GetString(7) : "");
                        }
                    }
                    rdr.Close();
                    tbExamName.Text = examName;
                    tbClassId.Text = classId.ToString();
                    tbClassTitle.Text = classTitle;
                    ListItem itemExamType = new ListItem(examType);
                    cmbExamType.SelectedIndex = cmbExamType.Items.IndexOf(itemExamType);
                    ListItem itemQuestionsType = new ListItem(questionsType);
                    cmbQuestionsType.SelectedIndex = cmbQuestionsType.Items.IndexOf(itemQuestionsType);
                    tbMaxGrade.Text = maxGrade.ToString();
                    tbStatements.Text = statements;
                    cmbQuestionsType.Enabled = (cmbExamType.SelectedIndex == 1 ? true : false);
                    ////////////
                    weekDay = C_myClass.DateSplit(examDate)[0];
                    day = C_myClass.DateSplit(examDate)[1];
                    month = C_myClass.DateSplit(examDate)[2];
                    year = C_myClass.DateSplit(examDate)[3];
                    ListItem itemWeek = new ListItem(weekDay);
                    cmbDayOfWeek.SelectedIndex = cmbDayOfWeek.Items.IndexOf(itemWeek);
                    ListItem itemDay = new ListItem(day);
                    cmbDay.SelectedIndex = cmbDay.Items.IndexOf(itemDay);
                    ListItem itemMonth = new ListItem(month);
                    cmbMonth.SelectedIndex = cmbMonth.Items.IndexOf(itemMonth);
                    ListItem itemYear = new ListItem(year);
                    cmbYear.SelectedIndex = cmbYear.Items.IndexOf(itemYear);
                }
                catch
                {
                }
                finally
                {
                }
            }
        }
        catch
        {
        }
    }
    protected void btnSelectClass_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Show();
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            if ((tbClassId.Text.Length > 0) && (tbExamName.Text.Length > 0) && (tbMaxGrade.Text.Length > 0))
            {
                int examId = Int32.Parse(Request.QueryString["examId"].ToString());
                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_ExamsUpdateCommand(examId, 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_ExamsUpdateCommand(int examId, string ExamName, string ExamDate, int ClassId, string ExamType, string Statements, string QuestionsType, int MaxGrade)
    {
        string comm = "update t_Exams SET " +
            "examName = N'" + ExamName + "' , " +
            "examDate = N'" + ExamDate + "' , " +
            "classId = " + ClassId + " , " +
            "examType = N'" + ExamType + "' , " +
            "statements = N'" + Statements + "' , " +
            "questionsType = N'" + QuestionsType + "' , " +
            "maxGrade = " + MaxGrade + " " +
            "WHERE examId = " + examId;
        return comm;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
    protected void btnStudentsCancel_Click(object sender, EventArgs e)
    {
        mpeClasses.Hide();
    }
    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 cmbExamType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbExamType.SelectedIndex == 1)
            {
                cmbQuestionsType.Enabled = true;
            }
            else
            {
                cmbQuestionsType.Enabled = false;
            }
        }
        catch
        {
        }
    }
}
