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_OnlineExamEdit : System.Web.UI.Page
{
    //*****************************************
    SqlConnection con;
    SqlCommand cmd;
    SqlTransaction tr;
    static DataTable dt;
    //*****************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                string onlineName = "", onlineType = "", designer = "", userCreator = "";
                int testNum = 0, explainNum = 0;
                double testScore = 0.0, explainScore = 0.0;
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "ویرایش آزمون آنلاین";
                dt = new DataTable();
                dt.Columns.Add("classId");
                dt.Columns.Add("classTitle");
                dt.Columns.Add("courseName");
                dt.Columns.Add("teacherName");
                int onlineId = Int32.Parse(Request.QueryString["onlineId"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select onlineName, onlineType, testNum, explainNum, testScore, explainScore, designer, userCreator FROM t_OnlineExams Where onlineId = " + onlineId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            onlineName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            onlineType = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            testNum = (rdr.GetValue(2) != DBNull.Value ? rdr.GetInt32(2) : 0);
                            explainNum = (rdr.GetValue(3) != DBNull.Value ? rdr.GetInt32(3) : 0);
                            testScore = (rdr.GetValue(4) != DBNull.Value ? Math.Round(rdr.GetDouble(4), 2) : 0.0);
                            testScore = (rdr.GetValue(5) != DBNull.Value ? Math.Round(rdr.GetDouble(5), 2) : 0.0);
                            designer = (rdr.GetValue(6) != DBNull.Value ? rdr.GetString(6) : "");
                            userCreator = (rdr.GetValue(7) != DBNull.Value ? rdr.GetString(7) : "");
                        }
                    }
                    rdr.Close();
                    tbOnlineName.Text = onlineName;
                    ListItem itemType = new ListItem(onlineType);
                    cmbOnlineType.SelectedIndex = cmbOnlineType.Items.IndexOf(itemType);
                    tbTestNum.Text = testNum.ToString();
                    tbExplianNum.Text = explainNum.ToString();
                    tbTestScore.Text = testScore.ToString();
                    tbExplainScore.Text = explainScore.ToString();
                    tbDesigner.Text = designer;
                    if (onlineType == "تستی")
                    {
                        tbTestScore.Enabled = true;
                        tbTestNum.Enabled = true;
                        tbExplainScore.Enabled = false;
                        tbExplianNum.Enabled = false;
                    }
                    else if (onlineType == "تشریحی")
                    {
                        tbTestScore.Enabled = false;
                        tbTestNum.Enabled = false;
                        tbExplainScore.Enabled = true;
                        tbExplianNum.Enabled = true;
                    }
                    else if (onlineType == "تستی-تشریحی")
                    {
                        tbTestScore.Enabled = true;
                        tbTestNum.Enabled = true;
                        tbExplainScore.Enabled = true;
                        tbExplianNum.Enabled = true;
                    }
                    else
                    {
                        tbTestScore.Enabled = false;
                        tbTestNum.Enabled = false;
                        tbExplainScore.Enabled = false;
                        tbExplianNum.Enabled = false;
                    }
                    ///////////////////////////////////////////////////////// FillGridView;
                    cmd.CommandText = "select t_OnlineAccess.classId, t_Classes.classTitle, t_Courses.courseName, t_Teachers.fullName FROM " +
                        "t_OnlineAccess INNER JOIN t_Classes ON t_OnlineAccess.classId = t_Classes.classId " +
                        "INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId " +
                        "INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId " +
                        "WHERE t_OnlineAccess.onlineId = " + onlineId;
                    SqlDataReader rdr2 = cmd.ExecuteReader();
                    if (rdr2.HasRows)
                    {
                        while (rdr2.Read())
                        {
                            DataRow dr = dt.NewRow();
                            dr["classId"] = (rdr2.GetValue(0) != DBNull.Value ? rdr2.GetInt32(0) : 0);
                            dr["classTitle"] = (rdr2.GetValue(1) != DBNull.Value ? rdr2.GetString(1) : "");
                            dr["courseName"] = (rdr2.GetValue(2) != DBNull.Value ? rdr2.GetString(2) : "");
                            dr["teacherName"] = (rdr2.GetValue(3) != DBNull.Value ? rdr2.GetString(3) : "");
                            dt.Rows.Add(dr);
                        }
                    }
                    rdr2.Close();
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    

    protected void btnNewClass_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Show();
        }
        catch
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            int onlineId = Int32.Parse(Request.QueryString["onlineId"].ToString());
            string onlineName = "", onlineType = "", designer = "", userCreator = "";
            int testNum = 0, explainNum = 0;
            double testScore = 0.0, explainScore = 0.0;
            userCreator = User.Identity.Name;
            onlineName = tbOnlineName.Text;
            onlineType = (cmbOnlineType.SelectedIndex > 0 ? cmbOnlineType.SelectedItem.ToString() : "");
            designer = tbDesigner.Text;
            testNum = (tbTestNum.Text.Length > 0 ? Int32.Parse(tbTestNum.Text) : 0);//Int32.Parse(tbTestNum.Text);
            explainNum = (tbExplianNum.Text.Length > 0 ? Int32.Parse(tbExplianNum.Text) : 0);//Int32.Parse(tbExplianNum.Text);
            testScore = (tbTestScore.Text.Length > 0 ? Math.Round(Double.Parse(tbTestScore.Text), 2) : 0);
            explainScore = (tbExplainScore.Text.Length > 0 ? Math.Round(Double.Parse(tbExplainScore.Text), 2) : 0);
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandText = t_OnlineExamsUpdateCommand(onlineId, onlineName, onlineType, testNum, explainNum, testScore, explainScore, designer, userCreator);
                cmd.ExecuteNonQuery();
                cmd.CommandText = "delete from t_OnlineAccess Where onlineId = " + onlineId;
                cmd.ExecuteNonQuery();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    DropDownList cmbOp = (DropDownList)GridView1.Rows[i].FindControl("cmbOp");
                    if (cmbOp.SelectedItem.ToString() == "ثبت")
                    {
                        int classId = Int32.Parse(GridView1.Rows[i].Cells[1].Text);
                        cmd.CommandText = "insert into t_OnlineAccess (onlineId, classId) values (" +
                            onlineId + " , " +
                            classId + ")";
                        cmd.ExecuteNonQuery();
                    }
                }
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد." + "<br />" + "برای طراحی سوالات و کلید در لیست آزمون های آنلاین گزینه سوالات را انتخاب نمایید.";
            }
            catch (Exception ex)
            {
                tr.Rollback();
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = ex.Message;
            }
            finally
            {
                con.Close();
            }
        }
        catch (Exception exc)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = exc.Message;
        }
    }

    private string t_OnlineExamsUpdateCommand(int OnlineId, string OnlineName, string OnlineType, int TestNum, int ExplainNum, double TestScore, double ExplainScore, string Designer, string UserCreator)
    {
        string comm = "UPDATE t_OnlineExams SET " +
            "onlineName = N'" + OnlineName + "' , " +
            "onlineType = N'" + OnlineType + "' , " +
            "testNum = " + TestNum + " , " +
            "explainNum = " + ExplainNum + " , " +
            "testScore = " + TestScore + " , " +
            "explainScore = " + ExplainScore + " , " +
            "designer = N'" + Designer + "' , " +
            "userCreator = N'" + UserCreator + "' " +
            "WHERE onlineId = " + OnlineId;
        return comm;
    }

    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 courseName = "", teacherName = "", classTitle = "";
                int classId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    if (!IsInList(classId))
                    {
                        con.Open();
                        cmd.CommandText = "select t_Courses.courseName, t_Teachers.fullName, t_Classes.classTitle FROM " +
                            "t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId " +
                            "INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId " +
                            "WHERE t_Classes.classId = " + classId;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                courseName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                                teacherName = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                                classTitle = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            }
                        }
                        rdr.Close();
                        DataRow row = dt.NewRow();
                        row["classId"] = classId.ToString();
                        row["courseName"] = courseName;
                        row["teacherName"] = teacherName;
                        row["classTitle"] = classTitle;
                        dt.Rows.Add(row);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                        UpdatePanel2.Update();
                    }
                    mpeClasses.Hide();

                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch
        {
        }
    }

    private bool IsInList(int ClassId)
    {
        bool res = false;
        try
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (ClassId == Int32.Parse(GridView1.Rows[i].Cells[1].Text))
                    res = true;
            }
        }
        catch
        {
        }
        return res;
    }

    protected void cmbOnlineType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbOnlineType.SelectedIndex == 1)
            {
                tbExplianNum.Text = "0";
                tbExplainScore.Text = "0";
                tbTestNum.Text = "";
                tbTestScore.Text = "";
                tbTestScore.Enabled = true;
                tbExplainScore.Enabled = false;
                tbTestNum.Enabled = true;
                tbExplianNum.Enabled = false;
            }
            else if (cmbOnlineType.SelectedIndex == 2)
            {
                tbTestScore.Enabled = false;
                tbExplainScore.Enabled = true;
                tbTestNum.Enabled = false;
                tbExplianNum.Enabled = true;
                tbTestNum.Text = "0";
                tbTestScore.Text = "0";
                tbExplianNum.Text = "";
                tbExplainScore.Text = "";
            }
            else if (cmbOnlineType.SelectedIndex == 3)
            {
                tbTestScore.Enabled = true;
                tbExplainScore.Enabled = true;
                tbTestNum.Enabled = true;
                tbExplianNum.Enabled = true;
                tbTestNum.Text = "";
                tbTestScore.Text = "";
                tbExplianNum.Text = "";
                tbExplainScore.Text = "";
            }
            else
            {
                tbTestScore.Enabled = false;
                tbExplainScore.Enabled = false;
                tbTestNum.Enabled = false;
                tbExplianNum.Enabled = false;
                tbTestNum.Text = "0";
                tbTestScore.Text = "0";
                tbExplianNum.Text = "0";
                tbExplainScore.Text = "0";
            }
        }
        catch
        {
        }
    }
}
