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;

public partial class SYSadmin_Products_answer : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/sysadmin/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        else
        {
            //根据提交要求执行相关操作
            switch (Request.QueryString["action"])
            {
                case "add_test":
                    Doadmin.Execsql("Insert Into Answer_Manage (A_Class,A_Name,A_Answer,A_Hit,A_Joindate) Values('','" + Request.Form["A_Name"] + "','" + Request.Form["A_Answer"] + "',0,'" + DateTime.Now.ToString() + "')");
                    Response.Redirect("answer_manage.aspx?action=showtest");
                    break;
                case "del_test":
                    Doadmin.Execsql("Delete From Answer_Manage Where A_ID = " + Request.QueryString["id"]);
                    Response.Redirect("answer_manage.aspx?action=showtest");
                    break;
                case "del_seltest":
                    Doadmin.Execsql("Delete From Answer_Manage Where A_ID in ( " + Request.Form["id"] + ")");
                    Response.Redirect("answer_manage.aspx?action=showtest");
                    break;
                case "update_test":
                    Doadmin.Execsql("Update Answer_Manage Set A_Name = '" + Request.Form["A_Name"] + "',A_Answer='" + Request.Form["A_Answer"] + "' Where A_ID = " + Request.Form["A_ID"]);
                    Response.Redirect("answer_manage.aspx?action=showtest");
                    break;
                case "edittest":
                    SqlConnection Conn = CDatabase.wtoconn();
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand("Select * From Answer_Manage Where A_ID = " + Request.QueryString["id"], Conn);
                    SqlDataReader dataReader = Cmd.ExecuteReader();
                    dataReader.Read();
                    this.A_Name.Text = dataReader["A_Name"].ToString();
                    this.A_Answer.Text = dataReader["A_Answer"].ToString();
                    dataReader.Close();
                    Conn.Close();
                    this.testtitle.Text = "修改试题";
                    this.A_ID.Value = Request.QueryString["id"];
                    this.testlistPanel.Visible = false;
                    this.addtestPanel.Visible = true;
                    this.subjectlistPanel.Visible = false;
                    this.addsubjectPanel.Visible = false;
                    this.pagebarinfo.Visible = false;
                    break;
                case "showtest":
                    this.testlistPanel.Visible = true;
                    this.addtestPanel.Visible = false;
                    this.subjectlistPanel.Visible = false;
                    this.addsubjectPanel.Visible = false;
                    this.pagebarinfo.Visible = true;
                    break;
                case "addtest":
                    this.testlistPanel.Visible = false;
                    this.addtestPanel.Visible = true;
                    this.subjectlistPanel.Visible = false;
                    this.addsubjectPanel.Visible = false;
                    this.pagebarinfo.Visible = false;
                    break;
                case "add_subject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
                    {
                        string sOption = "", sScore = "";
                        for (int k = 1; k <= 10; k++)
                        {
                            TextBox otextbox = (TextBox)this.addsubjectPanel.FindControl("S_Option" + k);
                            if (otextbox.Text != "")
                            {
                                sOption += otextbox.Text.Trim().Replace(",", "，") + ",";
                            }
                            TextBox stextbox = (TextBox)this.addsubjectPanel.FindControl("S_Score" + k);
                            if (stextbox.Text != "")
                            {
                                sScore += stextbox.Text.Trim().Replace(",", "，") + ",";
                            }
                        }
                        sOption = sOption.Substring(0, sOption.LastIndexOf(","));
                        if (sScore.Length > 1)
                        {
                            sScore = sScore.Substring(0, sScore.LastIndexOf(","));
                        }
                        Doadmin.Execsql("Insert Into Answer_Subject (S_Class,S_Description,S_Option,S_Score) Values(" + Request.Form["S_Class"] + ",'" + Request.Form["S_Description"] + "','" + sOption + "','" + sScore + "')");
                        Response.Redirect("answer_manage.aspx?action=showsubject&sid=" + Request.QueryString["sid"]);
                    }
                    break;
                case "del_subject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
                    {
                        Doadmin.Execsql("Delete From Answer_Subject Where S_ID = " + Request.QueryString["id"]);
                        Response.Redirect("answer_manage.aspx?action=showsubject&sid=" + Request.QueryString["sid"]);
                    }
                    break;
                case "del_selsubject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
                    {
                        Doadmin.Execsql("Delete From Answer_Subject Where S_ID in ( " + Request.Form["id"] + ")");
                        Response.Redirect("answer_manage.aspx?action=showsubject&sid=" + Request.QueryString["sid"]);
                    }
                    break;
                case "update_subject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
                    {
                        string sOption = "", sScore = "";
                        for (int k = 1; k <= 10; k++)
                        {
                            TextBox otextbox = (TextBox)this.addsubjectPanel.FindControl("S_Option" + k);
                            if (otextbox.Text != "")
                            {
                                sOption += otextbox.Text.Trim().Replace(",", "，") + ",";
                            }
                            TextBox stextbox = (TextBox)this.addsubjectPanel.FindControl("S_Score" + k);
                            if (stextbox.Text != "")
                            {
                                sScore += stextbox.Text.Trim().Replace(",", "，") + ",";
                            }
                        }
                        sOption = sOption.Substring(0, sOption.LastIndexOf(","));
                        if (sScore.Length > 0)
                        {
                            sScore = sScore.Substring(0, sScore.LastIndexOf(","));
                        }
                        Doadmin.Execsql("Update Answer_Subject Set S_Description = '" + Request.Form["S_Description"] + "',S_Option='" + sOption + "',S_Score='" + sScore + "' Where S_ID = " + Request.Form["S_ID"]);
                        Response.Redirect("answer_manage.aspx?action=showsubject&sid=" + Request.QueryString["sid"]);
                    }
                    break;
                case "editsubject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0 && Convert.ToInt32(Request.QueryString["id"]) > 0)
                    {
                        SqlConnection sConn = CDatabase.wtoconn();
                        sConn.Open();
                        SqlCommand sCmd = new SqlCommand("Select * From Answer_Subject Where S_ID = " + Request.QueryString["id"], sConn);
                        SqlDataReader sDataReader = sCmd.ExecuteReader();
                        sDataReader.Read();
                        this.S_Description.Text = sDataReader["S_Description"].ToString();
                        string[] oArray = sDataReader["S_Option"].ToString().Split(',');
                        string[] sArray = sDataReader["S_Score"].ToString().Split(',');
                        for (int i = 0; i < oArray.Length; i++)
                        {
                            int j = i + 1;
                            TextBox otextbox = (TextBox)this.addsubjectPanel.FindControl("S_Option" + j);
                            otextbox.Text = oArray[i];
                            if (sArray.Length > 1)
                            {
                                TextBox stextbox = (TextBox)this.addsubjectPanel.FindControl("S_Score" + j);
                                stextbox.Text = sArray[i];
                            }
                        }
                        sDataReader.Close();
                        sConn.Close();
                        this.subjecttitle.Text = "修改题目";
                        this.S_ID.Value = Request.QueryString["id"];
                        this.S_Class.Value = Request.QueryString["sid"];
                        this.testlistPanel.Visible = false;
                        this.addtestPanel.Visible = false;
                        this.subjectlistPanel.Visible = false;
                        this.addsubjectPanel.Visible = true;
                        this.pagebarinfo.Visible = false;
                    }
                    break;
                case "showsubject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
                    {
                        this.testlistPanel.Visible = false;
                        this.addtestPanel.Visible = false;
                        this.subjectlistPanel.Visible = true;
                        this.addsubjectPanel.Visible = false;
                        this.pagebarinfo.Visible = true;
                        this.S_Class.Value = Request.QueryString["sid"];
                    }
                    break;
                case "addsubject":
                    if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
                    {
                        this.testlistPanel.Visible = false;
                        this.addtestPanel.Visible = false;
                        this.subjectlistPanel.Visible = false;
                        this.addsubjectPanel.Visible = true;
                        this.pagebarinfo.Visible = false;
                        this.S_Class.Value = Request.QueryString["sid"];
                    }
                    break;
                default:
                    this.testlistPanel.Visible = true;
                    this.addtestPanel.Visible = false;
                    this.subjectlistPanel.Visible = false;
                    this.addsubjectPanel.Visible = false;
                    break;
            }

            //加载试题列表数据
            this.FillGridView_Test();
            this.FillGridView_Subject();
        }
    }

    protected string GetSubjectCount(string sClass)
    {
        SqlConnection Conn = CDatabase.wtoconn();
        Conn.Open();
        SqlCommand Cmd = new SqlCommand("Select Count(*) From Answer_Subject Where S_Class = " + sClass, Conn);
        string sCount = Convert.ToString(Cmd.ExecuteScalar());
        Conn.Close();
        return sCount;
    }

    private void FillGridView_Test()
    {
        SqlConnection Conn = CDatabase.wtoconn();
        Conn.Open();
        SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * From Answer_Manage", Conn);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet, "Testlist");
        this.GridView_Test.DataSource = dataSet;
        this.GridView_Test.DataKeyNames = new string[] { "A_ID" };
        this.GridView_Test.DataBind();
        Conn.Close();
        if (GridView_Test.Rows.Count == 0)
        {
            this.pagebarinfo.Visible = false;
            this.notestPanel.Visible = true;
            this.testinfoPanel.Visible = false;
        }
        this.pagebarinfo.Text = "当前共有<font color='red'><b>" + dataSet.Tables["Testlist"].DefaultView.Count + "</b></font>道试题，分<font color='red'><b>" + this.GridView_Test.PageCount + "</b></font>页显示。";
    }

    protected void GridView_Test_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView_Test.PageIndex = e.NewPageIndex;
        this.FillGridView_Test();
    }

    protected void GridView_Test_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            ((HyperLink)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('你确认要删除此项吗?')");
        }
    }

    private void FillGridView_Subject()
    {
        if (Convert.ToInt32(Request.QueryString["sid"]) > 0)
        {
            SqlConnection Conn = CDatabase.wtoconn();
            Conn.Open();
            SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * From Answer_Subject Where S_Class = " + Request.QueryString["sid"], Conn);
            DataSet dataSet = new DataSet();
            dataAdapter.Fill(dataSet, "Subjectlist");
            GridView_Subject.DataSource = dataSet;
            GridView_Subject.DataKeyNames = new string[] { "S_ID" };
            GridView_Subject.DataBind();
            Conn.Close();
            if (GridView_Subject.Rows.Count == 0)
            {
                this.pagebarinfo.Visible = false;
                this.nosubjectPanel.Visible = true;
                this.subjectinfoPanel.Visible = false;
            }            
            this.pagebarinfo.Text = "当前共有<font color='red'><b>" + dataSet.Tables["Subjectlist"].DefaultView.Count + "</b></font>道题目，分<font color='red'><b>" + this.GridView_Subject.PageCount + "</b></font>页显示。";
            this.S_ClassID.Value = Request.QueryString["sid"];
        }
    }

    protected void GridView_Subject_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
            ((HyperLink)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "return confirm('你确认要删除此项吗?')");
        }
    }

    protected void GridView_Subject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_Subject.PageIndex = e.NewPageIndex;
        this.FillGridView_Subject();
    }

}
