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_User_UserIndex : 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_User";
        sql_CountFirst += " where CreateUserId=" + Session["ID"] + " ";


        //查询语句的头部
        string sql_SelectFirst = string.Empty;
        sql_SelectFirst += " select [ID],[Account],[Password],[RealName],";
        sql_SelectFirst += " (select top 1 Name from NT_Role where [RoleId]=NT_Role.ID) as RoleName,";
        sql_SelectFirst += " (select top 1 Name from NT_Department where [DepartmentId]=NT_Department.ID) as DepartmentName,";
        sql_SelectFirst += " [CreateUserId] ";
        sql_SelectFirst += " from NT_User ";
        sql_SelectFirst += " where Account<>'admin' and CreateUserId=" + Session["ID"] + " ";
        //查询条件
        string sql_Conditions = string.Empty;

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




        #region 查询条件

        //if (Session["Account"].ToString() != "admin")
        //{
        //    sql_Conditions += " and CreateUserId="+Session["ID"]+" ";
        //}

        string str_Account = StringFunc.DataFilter(this.txt_Account.Text.Trim());
        string str_RealName = StringFunc.DataFilter(this.txt_RealName.Text.Trim());
        if (str_Account.Length > 0)
        {
            sql_Conditions += " and Account like '%" + str_Account + "%'";

        }
        if (str_RealName.Length > 0)
        {
            sql_Conditions += " and RealName like '%" + str_RealName + "%'";
        }


        #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 btnUpdate_Command(object sender, CommandEventArgs e)
    {
        JavaScriptFunc.Redirect("UserEdit.aspx?Id=" + e.CommandArgument.ToString());
    }
    //删除
    protected void btnDelete_Command(object sender, CommandEventArgs e)
    {
        //删除关联
        string sql1 = "select count(*) from NT_Site where UserId=" + e.CommandArgument.ToString() + "";
        object obj1 = DbHelperOleDb.ExecuteScalar(CommandType.Text, sql1.ToString(), null);
        ;
        if (obj1.ToString() != "0")
        {
            JavaScriptFunc.Alert(this.Page, "该用户有" + obj1.ToString() + "个网站使用，不能删除！");
            return;
        }


        StringBuilder strSql = new StringBuilder();
        strSql.Append("select * from NT_User Where CreateUserId=" + e.CommandArgument.ToString());
        bool isExist = DbHelperOleDb.Exists(strSql.ToString());
        if (isExist)
        {
            JavaScriptFunc.Alert(this.Page, "请先删除该用户创建的账户！");
            return;
        }
        strSql = new StringBuilder();
        string[] strSqlList = new string[2];
        strSqlList[0] = string.Format("delete from NT_Role where CreateUserId={0}", e.CommandArgument.ToString());
        strSqlList[1] = string.Format("delete from NT_User where ID={0}", e.CommandArgument.ToString());
        DbHelperOleDb.ExecuteSqlTran(strSqlList);
        BindData();
    }

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

    //重置密码
    protected void btnReSet_Command(object sender, CommandEventArgs e)
    {

        string strSql = "update NT_User set [Password]='" + PageFunc.GetMd5("888888") + "' where ID=" + e.CommandArgument.ToString() + "";

        DbHelperOleDb.ExecuteNonQuery(CommandType.Text, strSql.ToString(), null);
        JavaScriptFunc.Alert(this.Page, "重置为888888");

        BindData();
    }
}
