using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.OleDb;

public partial class system_Course_courselist : System.Web.UI.Page
{
    protected int typeid = 0;
    public string strteacher = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        #region 过期和权限

        PageFunc.IfSystemLogin(this.Page);

        #endregion

        if (Request["typeid"] != null && Request["typeid"].ToString() != "")
        {
            typeid = int.Parse(Request["typeid"].ToString());
        }



        if (IsPostBack)
            return;
        TreeBind();
        BindData();
    }

    #region 绑定类别=================================
    private void TreeBind()
    {
        string strsql = "select * from NT_Data where ClassId=368 and ParentId=0";
        DataTable dt = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql, null).Tables[0];

        this.ddlClassType.Items.Clear();
        this.ddlClassType.Items.Add(new ListItem("所有课程", ""));
        foreach (DataRow dr in dt.Rows)
        {
            string Id = dr["ID"].ToString();
            string Title = dr["Title"].ToString().Trim();

            this.ddlClassType.Items.Add(new ListItem(Title, Id));
        }
    }
    #endregion

    protected void ddlClassType_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("courselist.aspx?typeid=" + ddlClassType.SelectedValue);
    }

    #region 产品绑定
    /// <summary>
    /// 数据绑定
    /// </summary>
    private void BindData()
    {
        DataSet ds = new DataSet();
        string str_ClassId = Request["vid"];
        string str_sTitle = "";
        //if (Request["ClassId"] != null)
        //{
        //    string strSql_Class = "select [ID],[ClassName] from NT_Class where ID=" + Request["ClassId"] + "";
        //    OleDbDataReader sdr_Class = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql_Class, null);
        //    if (sdr_Class.HasRows)
        //    {
        //        while (sdr_Class.Read())
        //        {
        //            str_sTitle = sdr_Class["ClassName"].ToString();
        //        }
        //    }
        //    sdr_Class.Close();
        //}


        //计算总数的语句的头部
        string sql_CountFirst = string.Empty;
        sql_CountFirst += " select";
        sql_CountFirst += " count(*)";
        sql_CountFirst += " from NT_Data ";
        sql_CountFirst += " where ClassId=368";
        //if (str_ClassId != null && str_ClassId != "")
        //{
        //    sql_CountFirst += " and ParentId=" + str_ClassId;
        //}

        //查询语句的头部
        string sql_SelectFirst = "select ID ,ClassId,Title,sTitle,TitlePicture,Intro,Content,AddTime,FilePath,OrderNum,MemberId,ParentId ";
        sql_SelectFirst += " from NT_Data ";
        sql_SelectFirst += " where ClassId=368";

        //if (str_ClassId != null && str_ClassId != "")
        //{
        //    sql_SelectFirst += " and ParentId=" + str_ClassId;
        //}

        if (typeid > 0)
        {
            sql_CountFirst += " and ParentId=" + typeid;
            sql_SelectFirst += " and ParentId=" + typeid;
            ddlClassType.SelectedValue = typeid.ToString();
        }
        else {
            sql_CountFirst += " and ParentId in (select ID from NT_Data where ClassId=368 and ParentId=0)";
            sql_SelectFirst += " and ParentId in (select ID from NT_Data where ClassId=368 and ParentId=0)";
        }

        

        sql_SelectFirst += " order by OrderNum desc, ID desc";


        DataTable dt_video = WebControlsFunc.GetDataTableByAspNetPager(AspNetPager1, sql_CountFirst, sql_SelectFirst);
        lit_video.Text = "";
        foreach (DataRow dr in dt_video.Rows)
        {

            string strSql_Class = "select * from NT_Data where ID=" + dr["ParentId"] + "";
            OleDbDataReader sdr_Class = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql_Class, null);
            if (sdr_Class.HasRows)
            {
                while (sdr_Class.Read())
                {
                    str_sTitle = sdr_Class["Title"].ToString();
                }
            }
            sdr_Class.Close();

            lit_video.Text += "<tr><td align='center'>" + dr["ID"] + "</td><td>" + dr["Title"] + "</td>";
            lit_video.Text += "<td>" + str_sTitle + "</td>";

            string strsql = "select * from NT_Member where ID=" + dr["MemberId"];
            ds = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql, null);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                lit_video.Text += "<td>" + ds.Tables[0].Rows[0]["Name"] + "</td>";
                if (ds.Tables[0].Rows[0]["Identity"].ToString() == "教师")
                {
                    strteacher = "教师";
                }
            }
            string strsql1 = "select * from NT_Member where ID=" + Session["ID1"];
            DataSet ds11 = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql1, null);
            if (ds11 != null && ds11.Tables[0].Rows.Count > 0)
            {
                if (ds11.Tables[0].Rows[0]["Identity"].ToString() == "教师")
                {
                    lit_video.Text += "<td><a href='coursedetails.aspx?vid=" + dr["ID"] + "&ClassId=" + dr["ClassId"] + "'>布置作业</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='homeworklist.aspx?vid=" + dr["ID"] + "&ClassId=" + dr["ClassId"] + "'>批改作业</a></td>";
                }
                else
                {
                    lit_video.Text += "<td><a href='homeworklist.aspx?vid=" + dr["ID"] + "&ClassId=" + dr["ClassId"] + "'>查看作业</a></td>";
                }
            }

            lit_video.Text += "</tr>";

        }


    }
    #endregion

    #region 分页事件
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }
    #endregion
}
