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_completehomework : System.Web.UI.Page
{
    public DataRow dm;

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

        PageFunc.IfSystemLogin(this.Page);
        //PubPageFunc.PageHavePurview(this.Page, "2001");

        if (Session["ID1"] != null)
        {
            string strsql = "select * from NT_Member where ID=" + Session["ID1"];
            DataSet ds = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql, null);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                dm = ds.Tables[0].Rows[0];
            }
        }

        #endregion

        if (!IsPostBack)
        {
            //取当前日期
            this.txt_AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");

            

            #region 绑定DropDownList
            //string sql1 = "select ID,TypeName,OrderNum from NT_ClassType where PrantId=" + Request["ClassId"] + " order by OrderNum asc";
            //WebControlsFunc.BindToDropDownList(ddl_TypeName, sql1, "TypeName", "ID", "请选择", "");
            //if (ddl_TypeName.Items.Count < 2)//没有设置的话就隐藏
            //{
            //    this.tr_TypeName.Visible = false;
            //}
            //else
            //{
            //    this.tr_TypeName.Visible = true;
            //}
            #endregion

            //页面赋值  取数据信息
            if (Request["cid"] != null)
            {

                StringBuilder strSql = new StringBuilder();
                strSql.Append("select [Title],[sTitle],[TypeName],[IsTop],[IsRec],[IsHot],[TitlePicture],[Intro],[Keywords],[Content],[AddTime],[FilePath],[Video1],[Video2],[Address],[OrderNum] from NT_Data ");
                strSql.Append(" where ID=@ID");
                OleDbParameter[] parameters = { new OleDbParameter("@ID", OleDbType.Integer, 4) };
                parameters[0].Value = Request["cid"];

                OleDbDataReader sdr = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql.ToString(), parameters);
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        this.txt_Title.Text = sdr["Title"].ToString();//

                        //this.ddl_TypeName.SelectedValue = sdr["TypeName"].ToString();


                        //if (sdr["IsTop"].ToString() == "是") { this.cb_IsTop.Checked = true; }
                        //if (sdr["IsRec"].ToString() == "是") { this.cb_IsRec.Checked = true; }
                        //if (sdr["IsHot"].ToString() == "是") { this.cb_IsHot.Checked = true; }

                        
                        this.txt_Intro.Text = sdr["Intro"].ToString();//
                        this.txt_Keywords.Text = sdr["Keywords"].ToString();//
                        this.FileContent.Text = sdr["Content"].ToString();//
                        this.txt_AddTime.Text = DateTime.Parse(sdr["AddTime"].ToString()).ToString("yyyy-MM-dd");//
                        if (sdr["FilePath"].ToString().Length > 0)
                        {
                            lab_FilePath.Visible = true;
                            this.lab_FilePath.Text = "<a  href='" + sdr["FilePath"].ToString() + "' target='_blank'>下载</a>";

                        }


                    }
                }
                sdr.Close();

                string strsql1 = "select * from NT_Video where DataId=" + Request["cid"];
                DataTable dt = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql1, null).Tables[0];
                if (dt != null && dt.Rows.Count > 0) {
                    txt_pinglun.Text = dt.Rows[0]["Content"].ToString();
                }

            }

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string str_ClassId = "";
        string str_ParentId = "";

        if (Request["cid"] != null && Request["cid"] != "")
        {
            #region 操作数据库

            string strsql11 = "select * from NT_Data where ID=" + Request["cid"];
            DataTable dt11 = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql11, null).Tables[0];
            if (dt11 != null && dt11.Rows.Count > 0)
            {
                str_ClassId = dt11.Rows[0]["ClassId"].ToString();
                str_ParentId = dt11.Rows[0]["ParentId"].ToString();
            }


            string strsql1 = "select * from NT_Video where DataId=" + Request["cid"] + " and MemberId=" + Session["ID1"];
            DataTable dt = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql1, null).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {

                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append("update NT_Video set ");
                strSql1.Append("MemberId=@MemberId,");
                strSql1.Append("DataId=@DataId,");
                strSql1.Append("Content=@Content,");
                strSql1.Append("AddTime=@AddTime ");
                strSql1.Append(" where ID=@ID");
                OleDbParameter[] parameters1 = {
                    new OleDbParameter("@MemberId", OleDbType.Integer,4),
					new OleDbParameter("@DataId", OleDbType.Integer,4),
                    new OleDbParameter("@Content", OleDbType.VarChar),
                    new OleDbParameter("@AddTime", OleDbType.VarChar),
                    new OleDbParameter("@ID", OleDbType.Integer,4)};
                parameters1[0].Value = Session["ID1"];
                parameters1[1].Value = Request["cid"];
                parameters1[2].Value = txt_pinglun.Text;
                parameters1[3].Value = DateTime.Now;
                parameters1[4].Value = int.Parse(dt.Rows[0]["ID"].ToString());
                DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql1.ToString(), parameters1);
            }
            else {
                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append("insert into NT_Video(");
                strSql1.Append("MemberId,DataId,Content,AddTime)");
                strSql1.Append(" values (");
                strSql1.Append("@MemberId,@DataId,@Content,@AddTime)");
                OleDbParameter[] parameters1 = {
                    new OleDbParameter("@MemberId", OleDbType.Integer,4),
					new OleDbParameter("@DataId", OleDbType.Integer,4),
                    new OleDbParameter("@Content", OleDbType.VarChar),
                    new OleDbParameter("@AddTime", OleDbType.VarChar)};
                parameters1[0].Value = Session["ID1"];
                parameters1[1].Value = Request["cid"];
                parameters1[2].Value = txt_pinglun.Text;
                parameters1[3].Value = DateTime.Now;
                DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql1.ToString(), parameters1);
            }
            #endregion

            JavaScriptFunc.AlertAndRedirect(this.Page, "批改成功", "worklist.aspx?vid=" + str_ParentId + "&ClassId=" + str_ClassId);
        }
        else
        {
            #region 操作数据库
            //执行语句

            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("insert into NT_Video(");
            strSql1.Append("MemberId,DataId,Content,AddTime)");
            strSql1.Append(" values (");
            strSql1.Append("@MemberId,@DataId,@Content,@AddTime)");
            OleDbParameter[] parameters1 = {
                    new OleDbParameter("@MemberId", OleDbType.Integer,4),
					new OleDbParameter("@DataId", OleDbType.Integer,4),
                    new OleDbParameter("@Content", OleDbType.VarChar),
                    new OleDbParameter("@AddTime", OleDbType.VarChar)};
            parameters1[0].Value = Session["ID1"];
            parameters1[1].Value = Request["cid"];
            parameters1[2].Value = txt_pinglun.Text;
            parameters1[3].Value = DateTime.Now;
            DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql1.ToString(), parameters1);

            #endregion

            JavaScriptFunc.AlertAndRedirect(this.Page, "添加成功", "courselist.aspx?vid=" + str_ParentId + "&ClassId=" + str_ClassId);
        }
    }
}
