using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace ComResProSystem
{
    public partial class UsersMangent:BasePage
    {
        BaseBLL bll = new BaseBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", "1=1", 1, 2, "ID", "ID");
                PageCount(2);
                setEnable();
            }
        }
        public void BindUsers(Type t, string TableName, string Cloumns, string Condition, int PageIndex, int PageCount, string PK, string Order)
        {
            List<object> list = bll.SelctObjectByPage(t, TableName, Cloumns, Condition, PageIndex, PageCount, PK, Order);
            Gv_User.DataSource = list;
            Gv_User.DataBind();
            if (list.Count > 0)
            {
                preup.Attributes.Add("style", "display:block;margin-right:5%;");
            }
            else
            {
                preup.Attributes.Add("style", "display:none");
            }
        }

        protected void Ibt_select_Click(object sender, ImageClickEventArgs e)
        {
            BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", Condition(), 1, 2, "ID", "ID");
            PageCount(2);
            setEnable();
        }
        public string Condition()
        {
            string conditon = "1=1";
            if (Dd_Part.Text != "-请选择-")
            {
                conditon = conditon + " and Part='" + Dd_Part.Text + "'";
            }

            if (Dd_UserPos.Text != "-请选择-")
            {
                conditon = conditon + " and UserPos='" + Dd_UserPos.Text + "'";
              
            }
            return conditon;
        }
        public void PageCount(int Page)
        {
           Lb_Total.Text=bll.SelPageCount("Users", Condition(), Page).ToString();
           Lb_Now.Text = "1";
        }

        protected void Ibt_Firsr_Click(object sender, ImageClickEventArgs e)
        {
            BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", Condition(), 1, 2, "ID", "ID");
            PageCount(2);
            setEnable();
        }

        protected void Ibt_Pre_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Now.Text) - 1;
            BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", Condition(), pageindex, 2, "ID", "ID");
            Lb_Now.Text = pageindex.ToString();
            setEnable();
           
        }

        protected void Ibt_Next_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Now.Text) + 1;
            BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", Condition(), pageindex, 2, "ID", "ID");
            Lb_Now.Text = pageindex.ToString();
            setEnable();
        }

        protected void Ibt_Last_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Total.Text);
            BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", Condition(), pageindex, 2, "ID", "ID");
            Lb_Now.Text = pageindex.ToString();
            setEnable();
        }
        public void setEnable()
        {
            Ibt_Firsr.Enabled = Lb_Now.Text == "1" ? false : true;
            Ibt_Pre.Enabled = Lb_Now.Text == "1" ? false : true; ;
            Ibt_Next.Enabled = Lb_Now.Text == Lb_Total.Text ? false : true;
            Ibt_Last.Enabled = Lb_Now.Text == Lb_Total.Text ? false : true;
        }

        protected void Ibt_goto_Click(object sender, ImageClickEventArgs e)
        {
            if (Tb_goto.Text != "")
            {
                int go=Convert.ToInt32(Tb_goto.Text);
                int total=Convert.ToInt32(Lb_Total.Text);
                if (go >= 1 && go <= total)
                {
                    BindUsers(typeof(User), "Users", "ID,UserName,UserAge,UserPos,Part", Condition(), go, 2, "ID", "ID");
                    Lb_Now.Text = go.ToString();
                    setEnable();
                    Tb_goto.Text = "";
                }
                else
                {
                    Tb_goto.Text = "";
                }
            }
        }

        protected void Gv_User_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string ID = Gv_User.DataKeys[e.RowIndex].Value.ToString();
            System.Web.UI.ScriptManager.RegisterStartupScript(this, Page.GetType(), "ajaxjs", " var sReturn=window.showModalDialog('UserEdit.aspx?ID=" + ID + "','window','dialogWidth:600px;dialogHeight:250px;help:0;scroll:1;status:no;');",true);
        }

        protected void Gv_User_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = Gv_User.DataKeys[e.RowIndex].Value.ToString();
            if (bll.Delete0bject("Users", "ID=" + ID) == 0)
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(this, Page.GetType(), "ajaxjs", "alert('删除成功！')", true);
      
            }
        }
    }
}