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_coursedetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 过期和权限

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

        #endregion

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

            #region 页面赋值 取栏目信息
            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())
                    {
                        this.lab_ClassName2.Text = sdr_Class["ClassName"].ToString();
                    }
                }
                sdr_Class.Close();
            }
            #endregion

            #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;
            //}

            string strsql = "select * from NT_Data where ClassId=368 and ParentId=0";
            DataTable dt = DbHelperOleDb.ExecuteDataSet(CommandType.Text, strsql, null).Tables[0];

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

                this.ddl_TypeName.Items.Add(new ListItem(Title, Id));
            }

            #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; }

                        //if (sdr["TitlePicture"].ToString().Length > 0)//
                        //{
                        //    this.img_TitlePicture.ImageUrl = sdr["TitlePicture"].ToString();
                        //    this.img_TitlePicture.Visible = 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();
                        }


                        this.txt_OrderNum.Text = sdr["OrderNum"].ToString();
                    }
                }
                sdr.Close();
            }

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //string str_ClassId = Request["ClassId"];
        //string str_sTitle = "";
        //if (Request["ClassId"] != 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 = this.ddl_TypeName.SelectedValue;
        //string str_TitlePicture = this.img_TitlePicture.ImageUrl;
        //#region 判断类型+保存图片
        //if (this.fu_TitlePicture != null && this.fu_TitlePicture.FileName.Length > 0)
        //{
        //    #region 判断类型
        //    string[] arrExtension = new string[] { ".gif", ".jpg", ".jpeg", ".bmp", ".png", ".JPG" };
        //    if (FileFunc.IsAllowedExtension(fu_TitlePicture, arrExtension) == false)
        //    {
        //        JavaScriptFunc.Alert(this.Page, "图片格式必须是.gif, .jpg, .jpeg, .bmp, .png, .JPG");
        //        return;
        //    }
        //    #endregion
        //    //赋值
        //    str_TitlePicture = FileFunc.CreateFile(this.Page, fu_TitlePicture);
        //}
        //#endregion
        //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 = this.txt_OrderNum.Text;

        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 = "368";
            parameters[1].Value = str_Title;
            parameters[2].Value = "";
            parameters[3].Value = "";
            parameters[4].Value = "";
            parameters[5].Value = "";
            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 = "";
            parameters[12].Value = str_FilePath;
            parameters[13].Value = "";
            parameters[14].Value = "";
            parameters[15].Value = "";
            parameters[16].Value = int.Parse(str_OrderNum);
            parameters[17].Value = int.Parse(Session["ID1"].ToString());
            parameters[18].Value = str_TypeName;
            parameters[19].Value = Request["cid"];
            //执行语句
            DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);


            #endregion

            JavaScriptFunc.AlertAndRedirect(this.Page, "修改成功", "courselist.aspx");
        }
        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 = "368";
            parameters[1].Value = str_Title;
            parameters[2].Value = "";
            parameters[3].Value = "";
            parameters[4].Value = "";
            parameters[5].Value = "";
            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 = "";
            parameters[12].Value = str_FilePath;
            parameters[13].Value = "";
            parameters[14].Value = "";
            parameters[15].Value = "";
            parameters[16].Value = int.Parse(str_OrderNum);
            parameters[17].Value = Session["ID1"];
            parameters[18].Value = str_TypeName;

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


            #endregion

            JavaScriptFunc.AlertAndRedirect(this.Page, "添加成功", "courselist.aspx");
        }
    }
}
