using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookDemo.QA.Admin
{
    public partial class Questions : System.Web.UI.Page
    {

        private BookDemoEntities ctx = new BookDemoEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBindToGvQuestions();
            }
        }

        /// <summary>
        /// 绑定数据到questionList
        /// </summary>
        private void DataBindToGvQuestions()
        {
            List<QAQuestion> questionList = (from q
                                             in ctx.QAQuestion           //从QAQuestion中搜索
                                                   .Include("QAUserInfo")//关联QAUserInfo搜索
                                                   .Include("QACatalog") //关联QACatalog搜索
                                             orderby q.Id descending     //按照提问ID倒叙排列
                                             select q).ToList();         //转为List<QAQuestion>类型

            gvQuestions.DataSource = questionList;              //将questionList设为gvQuestions数据源
            gvQuestions.DataKeyNames = new string[] { "Id" };   //将Id加入DataKeyNames中主键
            gvQuestions.DataBind();                             //将数据绑定到gvQuestions
        }

        protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                QAQuestion quest = (QAQuestion)e.Row.DataItem;//获取当前行数据实体
                Label lblUserName = e.Row.FindControl("lblUserName") as Label;//查找lblUserName控件
                Label lblStatus = e.Row.FindControl("lblStatus") as Label;//查找lblStatus控件
                Label lblCatalog = e.Row.FindControl("lblCatalog") as Label;//查找lblCatalog控件

                lblUserName.Text = quest.QAUserInfo.UserName;//用户名
                lblCatalog.Text = quest.QACatalog.Name;//目录名
                //判断状态
                switch (quest.State)
                {
                    case 0:
                        lblStatus.Text = "未解决";
                        break;
                    case 1:
                        lblStatus.Text = "已解决";
                        break;
                    case 2:
                        lblStatus.Text = "已关闭";
                        break;
                    default:
                        break;
                }
            }
        }

        protected void gvQuestions_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //通过DataKeys获得当前行对应实体的ID
            int questionId = Convert.ToInt32(gvQuestions.DataKeys[e.RowIndex]["Id"].ToString());
            //通过ID获取QAQuestion实体
            QAQuestion quest = ctx.QAQuestion.First(q => q.Id == questionId);
            List<QAAnswer> answerList = quest.QAAnswer.ToList();//获取该提问下的所有回答
            //便利删除提问下的所有回答
            foreach (var answer in answerList)
            {
                ctx.DeleteObject(answer);//删除回答
            }
            ctx.DeleteObject(quest);//删除提问
            ctx.SaveChanges();//执行所有数据库变化
            DataBindToGvQuestions();//重新绑定数据到gvQuestions
        }
    }
}
