using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Text;
public partial class BackCenter_PropertyName_PropertyNameIndex : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        #region 过期和权限

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

        #endregion
        if (!IsPostBack)
        {
            BindData();
        }
    }

    //绑定
    public void BindData()
    {

        //计算总数的语句的头部
        string sql_CountFirst = string.Empty;
        sql_CountFirst += " select";
        sql_CountFirst += " count(*)";
        sql_CountFirst += " from NT_PropertyName";
        sql_CountFirst += " where 1=1";


        //查询语句的头部
        string sql_SelectFirst = "select ";
        sql_SelectFirst += " Id,";
        sql_SelectFirst += " Type,";
        sql_SelectFirst += " Name,";
        sql_SelectFirst += " OrderNum";
        sql_SelectFirst += " from NT_PropertyName";
        sql_SelectFirst += " where 1=1";

        //排序
        string sql_SelectLast = " order by OrderNum asc,Id desc";


        //查询条件
        string sql_Conditions = string.Empty;

        #region 查询条件

        string str_Name = StringFunc.DataFilter(this.txt_Name.Text.Trim());
        if (str_Name.Length > 0)
        {
            sql_Conditions += " and Name like '%" + str_Name + "%'";
        }
        string str_Type = this.ddls_Type.SelectedValue;
        if (str_Type.Length > 0)
        {
            sql_Conditions += " and Type ='" + str_Type + "'";
        }

        #endregion

        //计算总数的语句
        string str_Count = sql_CountFirst + sql_Conditions;

        //查询语句
        string str_Select = sql_SelectFirst + sql_Conditions + sql_SelectLast;



        //AspNetPager
        //WebControlsFunc.BindToAspNetPager(this.AspNetPager1, CommandType.Text, str_count, null);

        //GridView
        //WebControlsFunc.BindToGridView(this.GridView1, this.AspNetPager1, CommandType.Text, str_select, null);

        WebControlsFunc.BindToAspNetPagerAndGridView(GridView1, AspNetPager1, str_Count, str_Select);
    }

    //查询
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

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

    //转向修改页
    protected void btnUpdate_Command(object sender, CommandEventArgs e)
    {
        JavaScriptFunc.Redirect("PropertyNameEdit.aspx?Id=" + e.CommandArgument.ToString());
    }

    //转向设置参数值页
    protected void btnSetPropertyValue_Command(object sender, CommandEventArgs e)
    {
        JavaScriptFunc.Redirect("SetPropertyValue.aspx?PropertyNameId=" + e.CommandArgument.ToString());
    }

    //删除
    protected void btnDelete_Command(object sender, CommandEventArgs e)
    {

        //删除关联
        string sql1 = "select count(*) from NT_PropertyValue where PropertyNameId=" + e.CommandArgument.ToString() + "";
        object obj1 = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql1.ToString(), null);
        if (obj1.ToString() != "0")
        {
            JavaScriptFunc.Alert(this.Page, "该参数名有参数值不能删除！");
            return;
        }

        string sql2 = "select count(*) from NT_ClassProperty where PropertyNameId=" + e.CommandArgument.ToString() + "";
        object obj2 = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql2.ToString(), null);
        if (obj2.ToString() != "0")
        {
            JavaScriptFunc.Alert(this.Page, "有栏目使用该属性名不能删除！");
            return;
        }

        string sql3 = "select count(*) from NT_DataProperty where PropertyNameId=" + e.CommandArgument.ToString() + "";
        object obj3 = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql3.ToString(), null);
        if (obj3.ToString() != "0")
        {
            JavaScriptFunc.Alert(this.Page, "有数据使用该属性名不能删除！");
            return;
        }

        StringBuilder strSql = new StringBuilder();
        strSql.Append("delete from NT_PropertyName ");
        strSql.Append(" where Id=@Id");
        OleDbParameter[] parameters = {
					new OleDbParameter("@ID",e.CommandArgument.ToString())
			};


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

        BindData();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         int RowIndex = e.Row.RowIndex;
         if (RowIndex > -1)
         {
             //根据类型显示隐藏设置列
             //if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer))
             if (e.Row.RowType != DataControlRowType.Header)
             {
                 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[1].Text.Trim() == "DropDownList")
                 {
                     e.Row.Cells[4].Enabled = true;
                 }
                 else
                 {
                     e.Row.Cells[4].Enabled = false;
                     e.Row.Cells[4].Text = "";
                 }
                 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[1].Text.Trim() == "TextBox")
                 {
                     e.Row.Cells[1].Text = "文本";
                 }
                 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[1].Text.Trim() == "TextArea")
                 {
                     e.Row.Cells[1].Text = "多行文本";
                 }
                 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[1].Text.Trim() == "DropDownList")
                 {
                     e.Row.Cells[1].Text = "下拉框";
                 }
                 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[1].Text.Trim() == "DateTime")
                 {
                     e.Row.Cells[1].Text = "日期";
                 }

             }
         }

    } 
}
