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_teachers_pg_OnlineExamQuestions : System.Web.UI.Page
{
    //*********************************
    static DataTable dt;
    SqlConnection con;
    SqlCommand cmd;
    SqlTransaction tr;
    //*********************************
    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("context");
                dt.Columns.Add("barom");
                dt.Columns.Add("answerType");
                dt.Columns.Add("testAnswer");
                dt.Columns.Add("answerA");
                dt.Columns.Add("answerB");
                dt.Columns.Add("answerC");
                dt.Columns.Add("answerD");
                dt.Columns.Add("manfi");
                int onlineId = Int32.Parse(Request.QueryString["onlineId"].ToString());
                FillDataGrid(onlineId);
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    private void FillDataGrid(int OnlineId)
    {
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "select context, barom, answerType, testAnswer, answerA, answerB, answerC, answerD, manfi FROM t_OnlineQuestions Where onlineId = " + OnlineId;
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        DataRow dr = dt.NewRow();
                        dr["context"] = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                        dr["barom"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetDouble(1) : 0.00);
                        dr["answerType"] = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                        dr["testAnswer"] = (rdr.GetValue(3) != DBNull.Value ? rdr.GetInt32(3) : -1);
                        dr["answerA"] = (rdr.GetValue(4) != DBNull.Value ? rdr.GetString(4) : "");
                        dr["answerB"] = (rdr.GetValue(5) != DBNull.Value ? rdr.GetString(5) : "");
                        dr["answerC"] = (rdr.GetValue(6) != DBNull.Value ? rdr.GetString(6) : "");
                        dr["answerD"] = (rdr.GetValue(7) != DBNull.Value ? rdr.GetString(7) : "");
                        dr["manfi"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetDouble(1) : 0.00);
                        dt.Rows.Add(dr);
                    }
                }
                rdr.Close();
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
    }

    protected void btnNewClass_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Show();
        }
        catch
        {
        }
    }

    protected void btnStudentsCancel_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Hide();
        }
        catch
        {
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbAnswerType.SelectedIndex == 0)
            {
                testAnswer.Visible = true;
                lblTestAnswer.Visible = true;
                tbAnswerA.Enabled = true;
                tbAnswerB.Enabled = true;
                tbAnswerC.Enabled = true;
                tbAnswerD.Enabled = true;
            }
            else if (cmbAnswerType.SelectedIndex == 1)
            {
                testAnswer.Visible = false;
                lblTestAnswer.Visible = false;
                tbAnswerA.Enabled = false;
                tbAnswerB.Enabled = false;
                tbAnswerC.Enabled = false;
                tbAnswerD.Enabled = false;
            }
        }
        catch
        {
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            if (IsFilled())
            {
                string context = tbContext.Text;
                double barom = Math.Round( Double.Parse(tbBarom.Text), 2);
                double manfi = Math.Round(Double.Parse(tbManfi.Text), 2);
                string answerType = (cmbAnswerType.SelectedIndex >= 0 ? cmbAnswerType.SelectedItem.ToString() : "");
                int testAnswer = UserControls_ABCD.SelectedIndex;
                string answerA = tbAnswerA.Text;
                string answerB = tbAnswerB.Text;
                string answerC = tbAnswerC.Text;
                string answerD = tbAnswerD.Text;
                DataRow dr = dt.NewRow();
                dr["context"] = context;
                dr["barom"] = barom;
                dr["manfi"] = manfi;
                dr["answerType"] = answerType;
                dr["testAnswer"] = testAnswer;
                dr["answerA"] = answerA;
                dr["answerB"] = answerB;
                dr["answerC"] = answerC;
                dr["answerD"] = answerD;
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                UpdatePanel1.Update();
                tbContext.Text = "";
                tbBarom.Text = "";
                tbManfi.Text = "";
                tbAnswerA.Text = "";
                tbAnswerB.Text = "";
                tbAnswerC.Text = "";
                tbAnswerD.Text = "";
                cmbAnswerType.SelectedIndex = 0;
                mpeClasses.Hide();
            }
        }
        catch
        {
        }
    }

    private bool IsFilled()
    {
        bool filled = false;
        try
        {
            if ((tbContext.Text.Length > 0) && (tbBarom.Text.Length > 0) && (tbManfi.Text.Length > 0))
            {
                if (cmbAnswerType.SelectedIndex == 0)
                {
                    if ((tbAnswerA.Text.Length > 0) && (tbAnswerB.Text.Length > 0) && (tbAnswerC.Text.Length > 0) && (tbAnswerD.Text.Length > 0) && (UserControls_ABCD.SelectedIndex > 0))
                    {
                        filled = true;
                    }
                }
                else
                {
                    filled = true;
                }
            }
        }
        catch
        {
        }
        return filled;
    }

    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
        {
            double totalTest = 0.0, totalExplain = 0.0;
            int onlineId = Int32.Parse(Request.QueryString["onlineId"].ToString());
            string context = "", answerType = "", answerA = "", answerB = "", answerC = "", answerD = "";
            double barom = 0.0, manfi = 0.0;
            int testAnswer = -1;
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandText = "delete from t_OnlineQuestions Where onlineId = " + onlineId;
                cmd.ExecuteNonQuery();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    DropDownList cmbOp = (DropDownList)GridView1.Rows[i].FindControl("cmbOp");
                    if (cmbOp.SelectedIndex == 0)
                    {
                        context = GridView1.Rows[i].Cells[1].Text;
                        answerType = GridView1.Rows[i].Cells[2].Text;
                        barom = Math.Round(Double.Parse(GridView1.Rows[i].Cells[3].Text), 2);
                        manfi = Math.Round(Double.Parse(GridView1.Rows[i].Cells[4].Text), 2);
                        totalTest += (answerType == "تستی" ? barom : 0 );
                        totalExplain += (answerType == "تشریحی" ? barom : 0);
                        answerA = (GridView1.Rows[i].Cells[5].Text.Length > 0 ? GridView1.Rows[i].Cells[5].Text : "");
                        answerB = (GridView1.Rows[i].Cells[6].Text.Length > 0 ? GridView1.Rows[i].Cells[6].Text : "");
                        answerC = (GridView1.Rows[i].Cells[7].Text.Length > 0 ? GridView1.Rows[i].Cells[7].Text : "");
                        answerD = (GridView1.Rows[i].Cells[8].Text.Length > 0 ? GridView1.Rows[i].Cells[8].Text : "");
                        testAnswer = (answerType == "تستی" ? Int32.Parse(GridView1.Rows[i].Cells[9].Text) : -1);
                        cmd.CommandText = "insert into t_OnlineQuestions (onlineId, context, barom, answerType, testAnswer, answerA, answerB, answerC, answerD, manfi) values (" +
                            onlineId + " , N'" +
                            context + "', " +
                            barom + " , N'" +
                            answerType + "', " +
                            testAnswer + " , N'" +
                            answerA + "', N'" +
                            answerB + "', N'" +
                            answerC + "', N'" +
                            answerD + "', " +
                            manfi + " )";
                        cmd.ExecuteNonQuery();
                    }
                }
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد." + "<br />" +
                    "جمع بارم تستی : " + Math.Round(totalTest, 2).ToString() + "<br />" +
                    "جمع بارم تشریحی : " + Math.Round(totalExplain, 2).ToString();
            }
            catch(Exception ex)
            {
                tr.Rollback();
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = ex.Message;
                //lblMessage.Text = "خطا در ذخیره سازی.";
            }
            finally
            {
                con.Close();
            }
        }
        catch(Exception exc)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            //lblMessage.Text = exc.Message;
            lblMessage.Text = "خطا در ذخیره سازی.";
        }
    }
}
