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_students_pg_OnlineParticipate : System.Web.UI.Page
{
    //*******************************************
    static int rowNumber = 0;
    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 = "شرکت در آزمون";
                rowNumber = 0;
                int onlineId = Int32.Parse(Request.QueryString["onlineId"].ToString());
                SqlDataSource1.SelectCommand = "SELECT * FROM [t_OnlineQuestions] WHERE onlineId = " + onlineId + " AND answerType = N'تستی'";
                DataList1.DataBind();
                SqlDataSource2.SelectCommand = "SELECT * FROM [t_OnlineQuestions] WHERE onlineId = " + onlineId + " AND answerType = N'تشریحی'";
                DataList2.DataBind();
                if (DataList1.Items.Count == 0)
                {
                    divTest.Visible = false;
                }
                if (DataList2.Items.Count == 0)
                {
                    divExplain.Visible = false;
                }
            }
        }
        catch(Exception ex)
        {
            lblFrameTitle.Text = ex.Message;
        }
        finally
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }


    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        try
        {
            Label lblRowNumber = (Label)e.Item.FindControl("lblrowNumber");
            lblRowNumber.Text = (++rowNumber).ToString();
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            int questionId = 0, testAnswer = 0, studentId = 0;
            string explainAnswer = "";
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    UserControls_ABCD abcd = (UserControls_ABCD)DataList1.Items[i].FindControl("abcd");
                    testAnswer = GetSelectedIndex(abcd);
                    Label lblQuestionId = (Label)DataList1.Items[i].FindControl("lblQuestionId");
                    questionId = Int32.Parse(lblQuestionId.Text);
                    cmd.CommandText = t_OnlineAnswersInsert(studentId, questionId, testAnswer, explainAnswer);
                    cmd.ExecuteNonQuery();
                }
                for (int i = 0; i < DataList2.Items.Count; i++)
                {
                    TextBox tbAnswer = (TextBox)DataList2.Items[i].FindControl("tbAnswer");
                    explainAnswer = tbAnswer.Text;
                    Label lblQuestionId = (Label)DataList1.Items[i].FindControl("lblQuestionId");
                    questionId = Int32.Parse(lblQuestionId.Text);
                    testAnswer = -1;
                    cmd.CommandText = t_OnlineAnswersInsert(studentId, questionId, testAnswer, explainAnswer);
                    cmd.ExecuteNonQuery();
                }
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد.";
            }
            catch(Exception ex)
            {
                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_OnlineAnswersInsert(int StudentId, int QuestionId, int TestAnswer, string ExplainAnswer)
    {
        string comm = "insert into t_OnlineAnswers (studentId, questionId, testAnswer, explainAnswer) values(" +
            StudentId + " , " +
            QuestionId + " , " +
            TestAnswer + " , N'" +
            ExplainAnswer + "')";
        return comm;
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        try
        {
            
        }
        catch
        {
        }
    }

    private int GetSelectedIndex(UserControls_ABCD ABCD)
    {
        int res = -1;
        LinkButton btnA = (LinkButton)ABCD.FindControl("btnA");
        LinkButton btnB = (LinkButton)ABCD.FindControl("btnB");
        LinkButton btnC = (LinkButton)ABCD.FindControl("btnC");
        LinkButton btnD = (LinkButton)ABCD.FindControl("btnD");
        if (btnA.CssClass == "abcdokitem")
        {
            res = 1;
        }
        else if (btnB.CssClass == "abcdokitem")
        {
            res = 2;
        }
        else if (btnC.CssClass == "abcdokitem")
        {
            res = 3;
        }
        else if (btnD.CssClass == "abcdokitem")
        {
            res = 4;
        }
        return res;
    }
}
