using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class WebMain_answer_answer : System.Web.UI.UserControl
{
    public Cls_Operation Operation = new Cls_Operation();
    public int iCount = 0;
    public string sUrl = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.FillGridView_Test();
        }
        switch (Request.QueryString["action"])
        {
            case "checkanswer":
                if (Convert.ToInt32(Request.Form["S_Class"]) > 0)
                {
                    string sResult = "";
                    int iScore = 0;
                    int iSubjectCount = Convert.ToInt32(Request.Form["S_SubjectCount"]);
                    if (iSubjectCount > 1)
                    {
                        int z = 1;
                        Operation.OpenConn_Child();
                        SqlCommand Command = new SqlCommand("Select * From Answer_Subject Where S_Class = " + Request.Form["S_Class"], Operation.Conn_Child);
                        SqlDataReader DataReader = Command.ExecuteReader();
                        while (DataReader.Read())
                        {
                            string[] optionArray = DataReader["S_Option"].ToString().Split(',');
                            string[] scoreArray = DataReader["S_Score"].ToString().Split(',');
                            string result = Request.Form["S_Option" + z] == null ? " " : Convert.ToString(Request.Form["S_Option" + z]);
                            for (int y = 0; y < optionArray.Length; y++)
                            {
                                if (Convert.ToInt32(scoreArray[y]) > 0 && result.Contains(optionArray[y]))
                                {
                                    iScore += Convert.ToInt32(scoreArray[y]);
                                }
                            }
                            z++;
                        }
                        DataReader.Close();
                        Command.Dispose();
                        Operation.CloseConn_Child();
                    }
                    Operation.OpenConn_Child();
                    SqlCommand sCmd = new SqlCommand("Select * From Answer_Manage Where A_ID = " + Request.Form["S_Class"], Operation.Conn_Child);
                    SqlDataReader sDataReader = sCmd.ExecuteReader();
                    sDataReader.Read();
                    this.TestName.Text = sDataReader["A_Name"].ToString();
                    string[] answerArray = Regex.Split(sDataReader["A_Answer"].ToString(), "@@@", RegexOptions.IgnoreCase);
                    if (iSubjectCount > 1)
                    {
                        for (int m = answerArray.Length - 1; m >= 0; m--)
                        {
                            string[] resultArray = answerArray[m].Split('|');
                            string[] scoreArray = resultArray[0].Split('-');
                            if (scoreArray.Length == 1)
                            {
                                if (iScore >= Convert.ToInt32(scoreArray[0]))
                                {
                                    sResult = resultArray[1];
                                }
                                else
                                {
                                    if (iScore <= Convert.ToInt32(scoreArray[0]) && m != (answerArray.Length - 1))
                                    {
                                        sResult = resultArray[1];
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                if (iScore >= Convert.ToInt32(scoreArray[0]) && iScore <= Convert.ToInt32(scoreArray[1]))
                                {
                                    sResult = resultArray[1];
                                }
                            }
                            if (sResult.Length > 0)
                            {
                                break;
                            }
                        }
                        this.Subject.Text = "测试结果：<br />&nbsp;&nbsp;&nbsp;&nbsp;" + sResult;
                    }
                    else
                    {
                        for (int m = 0; m < answerArray.Length; m++)
                        {
                            string[] resultArray = answerArray[m].Split('|');
                            if (Request.Form["S_Option"].Contains(resultArray[0]))
                            {
                                this.Subject.Text = "测试结果：<br />&nbsp;&nbsp;&nbsp;&nbsp;" + resultArray[1];
                                break;
                            }
                        }
                    }
                    sDataReader.Close();
                    sCmd.Dispose();
                    Operation.CloseConn_Child();
                    this.submitPanel.Visible = false;
                    this.backPanel.Visible = true;
                    this.TestlistPanel.Visible = false;
                    this.TestPanel.Visible = true;
                }
                break;
            default:
                if (Convert.ToInt32(Request.QueryString["id"]) > 0)
                {
                    Operation.ExecSql_Child("Update Answer_Manage Set A_Hit = A_Hit + 1 Where A_ID = " + Request.QueryString["id"]);
                    Operation.OpenConn_Child();
                    SqlCommand Cmd = new SqlCommand("Select * From Answer_Subject Where S_Class = " + Request.QueryString["id"], Operation.Conn_Child);
                    if (Convert.ToInt32(Cmd.ExecuteScalar()) > 0)
                    {
                        int n = 1;
                        SqlCommand cCmd = new SqlCommand("Select Count(*) From Answer_Subject Where S_Class = " + Request.QueryString["id"], Operation.Conn_Child);
                        if (Convert.ToInt32(cCmd.ExecuteScalar()) > 1)
                        {
                            SqlDataReader dataReader = Cmd.ExecuteReader();
                            while (dataReader.Read())
                            {
                                this.Subject.Text += n + "、" + dataReader["S_Description"].ToString() + "<br />" + (char)13;
                                string[] optionArray = dataReader["S_Option"].ToString().Split(',');
                                string[] scoreArray = dataReader["S_Score"].ToString().Split(',');
                                for (int i = 0; i < optionArray.Length; i++)
                                {
                                    int x = 0;
                                    if (scoreArray.Length > 1)
                                    {
                                        for (int k = 0; k < scoreArray.Length; k++)
                                        {
                                            if (Convert.ToInt32(scoreArray[k]) > 0)
                                            {
                                                x = x + 1;
                                            }
                                        }
                                    }
                                    if (x >= 2)
                                    {
                                        this.Subject.Text += "<input type=\"checkbox\" name=\"S_Option" + n + "\" value=\"" + optionArray[i] + "\"> " + optionArray[i] + " &nbsp; " + (char)13;
                                    }
                                    else
                                    {
                                        this.Subject.Text += "<input type=\"radio\" name=\"S_Option" + n + "\" value=\"" + optionArray[i] + "\"> " + optionArray[i] + " &nbsp; " + (char)13;
                                    }
                                    if (i == optionArray.Length - 1)
                                    {
                                        this.Subject.Text += "<br /><br />";
                                    }
                                }
                                n++;
                            }
                            dataReader.Close();
                        }
                        else
                        {
                            SqlDataReader dataReader = Cmd.ExecuteReader();
                            dataReader.Read();
                            string[] optionArray = dataReader["S_Option"].ToString().Split(',');
                            this.Subject.Text = "测试：<br />&nbsp;&nbsp;&nbsp;&nbsp;" + dataReader["S_Description"].ToString() + "<br /><br />" + (char)13;
                            for (int j = 0; j < optionArray.Length; j++)
                            {
                                this.Subject.Text += "<input type=\"radio\" name=\"S_Option\" value=\"" + optionArray[j] + "\"> " + optionArray[j] + "<br />" + (char)13;
                                if (j == optionArray.Length - 1)
                                {
                                    this.Subject.Text += "<br />";
                                }
                            }
                            dataReader.Close();
                        }
                        iCount = n != 1 ? n - 1 : n;
                    }
                    else
                    {
                        this.NoSubject.Visible = true;
                        this.HaveSubject.Visible = false;
                    }
                    Cmd.Dispose();
                    Operation.CloseConn_Child();
                    this.TestName.Text = Request.QueryString["name"];
                    this.TestlistPanel.Visible = false;
                    this.TestPanel.Visible = true;
                }
                else
                {
                    this.NoSubject.Visible = false;
                    this.TestlistPanel.Visible = true;
                    this.TestPanel.Visible = false;
                }
                break;
        }
        this.sUrl = Operation.GetServerName();
    }

    protected void FillGridView_Test()
    {
        Operation.OpenConn_Child();
        SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * From Answer_Manage", Operation.Conn_Child);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet, "cu");
        this.GridView_Test.DataSource = Doadmin.psgb(dataSet, PageBar, 15, 0);
        this.GridView_Test.DataKeyNames = new string[] { "A_ID" };
        this.GridView_Test.DataBind();
        this.GridView_Test.PageSize = 15;
        if (GridView_Test.Rows.Count == 0)
        {
            this.NoTest.Visible = true;
        }
        dataAdapter.Dispose();
        dataSet.Clear();
        Operation.CloseConn_Child();
    }
}
