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_correcthomework : 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");


            //页面赋值  取数据信息
            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();//



                        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)
                        {
                            this.lab_FilePath.Text = "<a  href='" + sdr["FilePath"].ToString() + "' target='_blank'>下载</a>";
                            //this.img_PicturePath.ImageUrl = sdr["PicturePath"].ToString();
                            //this.img_PicturePath.Visible = true;
                            this.lab_FilePathBak.Text = sdr["FilePath"].ToString();
                        }


                    }
                }
                sdr.Close();
            }

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string str_ClassId = Request["vid"];
        //string str_sTitle = "";
        //if (Request["vid"] != null)
        //{
        //    string strSql_Class = "select * from NT_Data where ID=" + Request["vid"] + "";
        //    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();
        //}

        string str_Title = this.txt_Title.Text.Trim();
        string str_IsTop = this.cb_IsTop.Checked == true ? "是" : "否";
        string str_IsRec = this.cb_IsRec.Checked == true ? "是" : "否";
        string str_IsHot = this.cb_IsHot.Checked == true ? "是" : "否";
        string str_TypeName = "";
        string str_TitlePicture = "";
        
        string str_Intro = this.txt_Intro.Text.Trim();
        string str_Keywords = this.txt_Keywords.Text.Trim();
        string str_Content = this.FileContent.Text.Trim();
        string str_AddTime = this.txt_AddTime.Text.Trim();

        string str_FilePath = this.lab_FilePathBak.Text;
        #region 附件
        if (this.fu_FilePath != null && this.fu_FilePath.FileName.Length > 0)
        {
            //赋值
            str_FilePath = FileFunc.CreateFile(this.Page, fu_FilePath);
        }
        #endregion



        string str_OrderNum = "";

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


            StringBuilder strSql = new StringBuilder();
            strSql.Append("update NT_Data set ");
            strSql.Append("ClassId=@ClassId,");
            strSql.Append("Title=@Title,");
            strSql.Append("sTitle=@sTitle,");
            strSql.Append("TitlePicture=@TitlePicture,");
            strSql.Append("Intro=@Intro,");
            strSql.Append("Keywords=@Keywords,");
            strSql.Append("Content=@Content,");
            strSql.Append("IsTop=@IsTop,");
            strSql.Append("IsRec=@IsRec,");
            strSql.Append("IsHot=@IsHot,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("TypeName=@TypeName,");
            strSql.Append("FilePath=@FilePath,");
            strSql.Append("Video1=@Video1,");
            strSql.Append("Video2=@Video2,");
            strSql.Append("Address=@Address,");
            strSql.Append("OrderNum=@OrderNum, ");
            strSql.Append("MemberId=@MemberId,");
            strSql.Append("ParentId=@ParentId ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ClassId", OleDbType.Integer,4),
					new OleDbParameter("@Title", OleDbType.VarChar),
                    new OleDbParameter("@sTitle", OleDbType.VarChar),
                    new OleDbParameter("@TitlePicture", OleDbType.VarChar),
                     new OleDbParameter("@Intro", OleDbType.VarChar),
                     new OleDbParameter("@Keywords", OleDbType.VarChar),
                    new OleDbParameter("@Content", OleDbType.VarChar),
                     new OleDbParameter("@IsTop", OleDbType.VarChar),
                      new OleDbParameter("@IsRec", OleDbType.VarChar),
                       new OleDbParameter("@IsHot", OleDbType.VarChar),
					new OleDbParameter("@AddTime", OleDbType.VarChar),
                    new OleDbParameter("@TypeName", OleDbType.VarChar),
                    new OleDbParameter("@FilePath", OleDbType.VarChar),
                    new OleDbParameter("@Video1", OleDbType.VarChar),
                    new OleDbParameter("@Video2", OleDbType.VarChar),
                    new OleDbParameter("@Video2", OleDbType.VarChar),
                    new OleDbParameter("@Address", OleDbType.VarChar),
                    new OleDbParameter("@OrderNum", OleDbType.Integer,4),
                    new OleDbParameter("@MemberId", OleDbType.Integer,4),
                    new OleDbParameter("@ParentId", OleDbType.Integer,4),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = str_ClassId;
            parameters[1].Value = str_Title;
            parameters[2].Value = "";
            parameters[3].Value = str_TitlePicture;
            parameters[4].Value = str_Intro;
            parameters[5].Value = str_Keywords;
            parameters[6].Value = str_Content;
            parameters[7].Value = str_IsTop;
            parameters[8].Value = str_IsRec;
            parameters[9].Value = str_IsHot;
            parameters[10].Value = str_AddTime;
            parameters[11].Value = str_TypeName;
            parameters[12].Value = str_FilePath;
            parameters[13].Value = "";
            parameters[14].Value = "";
            parameters[15].Value = "";
            parameters[16].Value = 0;
            parameters[17].Value = int.Parse(Session["ID1"].ToString());
            parameters[18].Value = int.Parse(Request["vid"].ToString());
            parameters[19].Value = Request["cid"];
            //执行语句
            DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);


            #endregion

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into NT_Data(");
            strSql.Append("[ClassId],[Title],[sTitle],[TitlePicture],[Intro],[Keywords],[Content],[IsTop],[IsRec],[IsHot],[AddTime],[TypeName],[FilePath],[Video1],[Video2],[Address],[OrderNum],[MemberId],[ParentId])");
            strSql.Append(" values (");
            strSql.Append("@ClassId,@Title,@sTitle,@TitlePicture,@Intro,@Keywords,@Content,@IsTop,@IsRec,@IsHot,@AddTime,@TypeName,@FilePath,@Video1,@Video2,@Address,@OrderNum,@MemberId,@ParentId)");
            OleDbParameter[] parameters = 
            {
                new OleDbParameter("@ClassId", OleDbType.Integer,4),
                new OleDbParameter("@Title", OleDbType.VarChar),
                new OleDbParameter("@sTitle", OleDbType.VarChar),
                new OleDbParameter("@TitlePicture", OleDbType.VarChar),
                new OleDbParameter("@Intro", OleDbType.VarChar),
                new OleDbParameter("@Keywords", OleDbType.VarChar),
                new OleDbParameter("@Content", OleDbType.VarChar),
                new OleDbParameter("@IsTop", OleDbType.VarChar),
                new OleDbParameter("@IsRec", OleDbType.VarChar),
                new OleDbParameter("@IsHot", OleDbType.VarChar),
                new OleDbParameter("@AddTime", OleDbType.VarChar),
                new OleDbParameter("@TypeName", OleDbType.VarChar),
                new OleDbParameter("@FilePath", OleDbType.VarChar),
                new OleDbParameter("@Video1", OleDbType.VarChar),
                new OleDbParameter("@Video2", OleDbType.VarChar),
                new OleDbParameter("@Address", OleDbType.VarChar),
                new OleDbParameter("@OrderNum", OleDbType.Integer,4),
                new OleDbParameter("@MemberId",OleDbType.Integer,4),
                new OleDbParameter("@ParentId", OleDbType.Integer,4)
            };
            parameters[0].Value = str_ClassId;
            parameters[1].Value = str_Title;
            parameters[2].Value = "";
            parameters[3].Value = str_TitlePicture;
            parameters[4].Value = str_Intro;
            parameters[5].Value = str_Keywords;
            parameters[6].Value = str_Content;
            parameters[7].Value = str_IsTop;
            parameters[8].Value = str_IsRec;
            parameters[9].Value = str_IsHot;
            parameters[10].Value = str_AddTime;
            parameters[11].Value = str_TypeName;
            parameters[12].Value = str_FilePath;
            parameters[13].Value = "";
            parameters[14].Value = "";
            parameters[15].Value = "";
            parameters[16].Value = 0;
            parameters[17].Value = int.Parse(Session["ID1"].ToString());
            parameters[18].Value = int.Parse(Request["vid"].ToString());

            DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);


            #endregion

            JavaScriptFunc.AlertAndRedirect(this.Page, "添加成功", "worklist.aspx?vid=" + Request["vid"] + "&ClassId=" + str_ClassId);
        }
    }
}
