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_OnLineExamAdd : 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)
            {
                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");
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    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
        {
        }
    }

    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
        {
            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_OnlineExamsInsertCommand(onlineName, onlineType, testNum, explainNum, testScore, explainScore, designer, userCreator);
                int onlineId = Int32.Parse(cmd.ExecuteScalar().ToString());
                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_OnlineExamsInsertCommand(string OnlineName, string OnlineType, int TestNum, int ExplainNum, double TestScore, double ExplainScore, string Designer, string UserCreator)
    {
        string comm = "insert into t_OnlineExams (onlineName, onlineType, testNum, explainNum, testScore, explainScore, designer, userCreator) values (N'" +
            OnlineName + "', N'" +
            OnlineType + "', " +
            TestNum + " , " +
            ExplainNum + " , " +
            TestScore + " , " +
            ExplainScore + " , N'" +
            Designer + "', N'" +
            UserCreator + "') " +
            "SELECT SCOPE_IDENTITY() AS Last_Id";
        return comm;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void btnNewClass_Click(object sender, EventArgs e)
    {
        
        try
        {
            mpeClasses.Show();
        }
        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 btnStudentsCancel_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Hide();
        }
        catch
        {
        }
    }
}
