using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using JuSNS.Model;
using JuSNS.UI.Control;

namespace JuSNS.Web.system.user
{
    public partial class index : JuSNS.UI.Page.ManagePage
    {
        JuSNS.Home.User us = new JuSNS.Home.User();
        public static string KeyValue = "";
        protected string sort = "user";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string pg = "1";
                string type = Request.QueryString["Type"];
                if (!string.IsNullOrEmpty(Request.QueryString["f"]))
                {
                    KeyValue = "";
                }
                if (type == "del")
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["pg"]))
                    {
                        pg = Request.QueryString["pg"];
                    }
                    if (us.DelUser(int.Parse(Request.QueryString["id"]), 0) == 1)
                    {
                        Response.Redirect("index.aspx?Page=" + pg + "");
                    }
                    else
                    {
                        PageError("删除会员信息失败!", "index.aspx");
                    }
                }
                else if (type == "delAll")
                {
                    if (us.DelUserAll() > 0)
                    {
                        Response.Redirect("index.aspx");
                    }
                }
                else if (type == "Recommended")
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["pg"]))
                    {
                        pg = Request.QueryString["pg"];
                    }
                    if (us.DelUser(int.Parse(Request.QueryString["id"]), int.Parse(Request.QueryString["flag"])) == 1)
                    {
                        Response.Redirect("index.aspx?Page=" + pg + "");
                    }
                    else
                    {
                        PageError("操作失败!", "index.aspx");
                    }
                }
                else
                {
                    if (!IsPostBack)
                    {
                        LoadList();
                    }
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            int r, p, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = null;
            int s = 10;
            if (!string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                s =Convert.ToInt32(Request.QueryString["s"]);
            }
            if (s == 10 && string.IsNullOrEmpty(this.Key.Text))
            {
                dt = JuSNS.Home.UtilPage.GetPage("UserList_u_aspx", PageIndex, PAGESZ, out r, out p, null);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.Key.Text))
                {
                    SqlConditionInfo[] st = new SqlConditionInfo[2];
                    st[0] = new SqlConditionInfo("@Key", Key.Text, TypeCode.String);
                    st[1] = new SqlConditionInfo("@s", s, TypeCode.Int32);
                    st[0].Blur = 3;

                    dt = JuSNS.Home.UtilPage.GetPage("UserList_Name_aspx", PageIndex, PAGESZ, out r, out p, st);
                    KeyValue = Key.Text;
                }
                else
                {
                    SqlConditionInfo st = new SqlConditionInfo("@s", s, TypeCode.Int32);
                    dt = JuSNS.Home.UtilPage.GetPage("UserList_aspx", PageIndex, PAGESZ, out r, out p, st);
                    KeyValue = "";
                }
            }
            if (dt != null)
            {
                Rpt_UserList.DataSource = dt;
                Rpt_UserList.DataBind();
            }
            //判断是否调用分页
            if (r > PAGESZ)
            {
                DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "index.aspx");
            }
            else
            {
                DivPage.InnerHtml = "";
            }
        }

        /// <summary>
        /// 执行搜索过程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
                LoadList();
        }
    }
}
