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

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

        #endregion

        //修改
        if (Request["ClassId"] != null && Request["DataId"] != null)
        {
            #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_ClassName1.Text = sdr_Class["ClassName"].ToString();
                    }
                }
                sdr_Class.Close();
            }
            #endregion

            //页面赋值
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.[ID],a.[ClassId],a.[PropertyNameId],b.Type,b.Name ");
            strSql.Append(" from NT_ClassProperty a");
            strSql.Append(" left join NT_PropertyName b on a.PropertyNameId=b.Id");
            strSql.Append(" where a.ClassId=@ClassId");
            OleDbParameter[] parameters = { new OleDbParameter("@ClassId", OleDbType.Integer, 4) };
            parameters[0].Value = Request["ClassId"];

            OleDbDataReader sdr = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql.ToString(), parameters);
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    this.lab_NO.Visible = false;

                    Label l = new Label();
                    l.Text = "<br/>";

                    #region 文本
                    if (sdr["Type"].ToString() == "TextBox")//文本
                    {
                        Label l_Name = new Label();
                        l_Name.Text = sdr["Name"].ToString() + "：";
                        PlaceHolder1.Controls.Add(l_Name);

                        TextBox tb = new TextBox();
                        tb.ID = "tb_" + sdr["ID"].ToString();

                        #region 赋值
                        if (Request["DataId"] != null)
                        {
                            string sql = "select PropertyValue from NT_DataProperty where DataId=" + Request["DataId"] + " and PropertyNameId=" + sdr["PropertyNameId"].ToString() + "";
                            object obj = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql, null);
                            tb.Text = obj != null ? obj.ToString() : "";
                        }
                        #endregion

                        PlaceHolder1.Controls.Add(tb);
                        PlaceHolder1.Controls.Add(l);
                    }
                    #endregion

                    #region 多行文本
                    if (sdr["Type"].ToString() == "TextArea")//多行文本
                    {
                        Label l_Name = new Label();
                        l_Name.Text = sdr["Name"].ToString() + "：";
                        PlaceHolder1.Controls.Add(l_Name);

                        TextBox tb = new TextBox();
                        tb.ID = "tb_" + sdr["ID"].ToString();

                        //tb.CssClass = "ckeditor";
                        tb.TextMode = TextBoxMode.MultiLine;
                        tb.Rows = 4;
                        tb.Columns = 60;
                        #region 赋值
                        if (Request["DataId"] != null)
                        {
                            string sql = "select PropertyValue from NT_DataProperty where DataId=" + Request["DataId"] + " and PropertyNameId=" + sdr["PropertyNameId"].ToString() + "";
                            object obj = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql, null);
                            tb.Text = obj != null ? obj.ToString() : "";
                        }
                        #endregion

                        PlaceHolder1.Controls.Add(tb);
                        PlaceHolder1.Controls.Add(l);
                    }
                    #endregion

                    #region 日期
                    if (sdr["Type"].ToString() == "DateTime")//日期
                    {
                        Label l_Name = new Label();
                        l_Name.Text = sdr["Name"].ToString() + "：";
                        PlaceHolder1.Controls.Add(l_Name);

                        TextBox tb = new TextBox();
                        tb.ID = "tb_" + sdr["ID"].ToString();
                        tb.Attributes.Add("onclick", "WdatePicker()");

                        #region 赋值
                        if (Request["DataId"] != null)
                        {
                            string sql = "select PropertyValue from NT_DataProperty where DataId=" + Request["DataId"] + " and PropertyNameId=" + sdr["PropertyNameId"].ToString() + "";
                            object obj = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql, null);
                            tb.Text = obj != null ? obj.ToString() : "";
                        }
                        #endregion


                        PlaceHolder1.Controls.Add(tb);
                        PlaceHolder1.Controls.Add(l);
                    }
                    #endregion 下拉框

                    #region 下拉框
                    if (sdr["Type"].ToString() == "DropDownList")//下拉框
                    {
                        Label l_Name = new Label();
                        l_Name.Text = sdr["Name"].ToString() + "：";
                        PlaceHolder1.Controls.Add(l_Name);

                        DropDownList ddl = new DropDownList();

                        //属性复选框
                        string sql1 = "select Id,Name from NT_PropertyValue where PropertyNameId=" + sdr["PropertyNameId"].ToString() + " order by OrderNum asc";
                        WebControlsFunc.BindToDropDownList(ddl, sql1, "Name", "Name", "请选择", "");
                        ddl.ID = "ddl_" + sdr["ID"].ToString();
                        PlaceHolder1.Controls.Add(ddl);

                        #region 赋值
                        if (Request["DataId"] != null)
                        {
                            string sql = "select PropertyValue from NT_DataProperty where DataId=" + Request["DataId"] + " and PropertyNameId=" + sdr["PropertyNameId"].ToString() + "";
                            object obj = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql, null);
                            WebControlsFunc.SelectDropDownList(ddl, obj != null ? obj.ToString() : "");

                        }
                        #endregion
                        PlaceHolder1.Controls.Add(l);
                    }
                    #endregion
                }
            }
            sdr.Close();
        }

    }

    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Request["ClassId"] != null && Request["DataId"] != null)
        {

            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("select a.[ID],a.[ClassId],a.[PropertyNameId],b.Type ");
            strSql1.Append(" from NT_ClassProperty a");
            strSql1.Append(" left join NT_PropertyName b on a.PropertyNameId=b.Id");
            strSql1.Append(" where a.ClassId=@ClassId");
            OleDbParameter[] parameters1 = { new OleDbParameter("@ID", OleDbType.Integer, 4) };
            parameters1[0].Value = Request["ClassId"];

            //删除原来的属性
            string[] sqllist = new string[1];
            sqllist[0] = "delete from NT_DataProperty where DataId=" + Request["DataId"] + "";
            //循环保存
            OleDbDataReader sdr = DbHelperOleDb.ExecuteDataReader(CommandType.Text, strSql1.ToString(), parameters1);
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    //循环PlaceHolder里的控件
                    ControlCollection cc = this.PlaceHolder1.Controls;
                    foreach (Control c in cc)
                    {
                        string str_DataId = Request["DataId"];
                        string str_PropertyNameId = sdr["PropertyNameId"].ToString();
                        string str_PropertyValue = string.Empty;

                        #region 文本
                        if (sdr["Type"].ToString() == "TextBox")//文本
                        {
                            if (c.ID == "tb_" + sdr["ID"].ToString())
                            {
                                TextBox t = (TextBox)PlaceHolder1.FindControl(c.ID);


                                str_PropertyValue = t.Text;

                                string sql = "INSERT INTO NT_DataProperty ([DataId] ,[PropertyNameId],[PropertyValue]) ";
                                sql += " VALUES (" + str_DataId + "," + str_PropertyNameId + ",'" + str_PropertyValue + "')";
                                sqllist = StringFunc.ArrayAddItem(sqllist, sql);
                            }
                        }
                        #endregion

                        #region 日期
                        if (sdr["Type"].ToString() == "DateTime")//日期
                        {
                            if (c.ID == "tb_" + sdr["ID"].ToString())
                            {
                                TextBox t = (TextBox)PlaceHolder1.FindControl(c.ID);

                                str_PropertyValue = t.Text;
                                string sql = "INSERT INTO NT_DataProperty ([DataId] ,[PropertyNameId],[PropertyValue]) ";
                                sql += " VALUES (" + str_DataId + "," + str_PropertyNameId + ",'" + str_PropertyValue + "')";
                                sqllist = StringFunc.ArrayAddItem(sqllist, sql);
                            }
                        }
                        #endregion

                        #region 多行文本

                        if (sdr["Type"].ToString() == "TextArea")//多行文本
                        {
                            if (c.ID == "tb_" + sdr["ID"].ToString())
                            {
                                TextBox t = (TextBox)PlaceHolder1.FindControl(c.ID);

                                str_PropertyValue = t.Text;
                                string sql = "INSERT INTO NT_DataProperty ([DataId] ,[PropertyNameId],[PropertyValue]) ";
                                sql += " VALUES (" + str_DataId + "," + str_PropertyNameId + ",'" + str_PropertyValue + "')";
                                sqllist = StringFunc.ArrayAddItem(sqllist, sql);
                            }
                        }
                        #endregion

                        #region 下拉框
                        if (sdr["Type"].ToString() == "DropDownList")//下拉框
                        {
                            if (c.ID == "ddl_" + sdr["ID"].ToString())
                            {
                                DropDownList t = (DropDownList)PlaceHolder1.FindControl(c.ID);
                                str_PropertyValue = t.SelectedValue;
                                string sql = "INSERT INTO NT_DataProperty ([DataId] ,[PropertyNameId],[PropertyValue]) ";
                                sql += " VALUES (" + str_DataId + "," + str_PropertyNameId + ",'" + str_PropertyValue + "')";
                                sqllist = StringFunc.ArrayAddItem(sqllist, sql);

                            }
                        }
                        #endregion

                    }
                }
            }
            sdr.Close();
            DbHelperOleDb.ExecuteSqlTran(sqllist);

            JavaScriptFunc.AlertAndRedirect(this.Page, "保存成功", "DataIndex.aspx?PageIndex=true&Conditions=true&ClassId=" + Request["ClassId"] + "");
        }
    }
}
