using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;




public partial class personal_Staffmanagemen : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            DataBindRePeat();

        }
    }

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);

    private void DataBindRePeat()
    {

        int temp1 = ConvertHelper.GetInteger(lblCurrentPage.Text);

        List<department> Stus = GetTestData();

        Stus = Stus.Skip((temp1 - 1) * 15).Take(15).ToList();

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();

        int count;
        List<department> Stu = GetTestData();
        count = ConvertHelper.GetInteger(Stu.Count());

        int j = count / 15;
        int i = count % 15;


        if (j >= 1)
        {
            if (i > 0)
            {
                j = j + 1;
            }

        }
        else
        {
            j = 1;

        }

        lblPageCount.Text = j.ToString();
    }


    private List<department> GetTestData()
    {
        List<department> stus = new List<department>();

        try
        {
            string sql = "SELECT [Number], [PersonnelName], [LoginName],  [WorkName], [Sex], [IdCardNum],[Birthday],[CallPhone],[Pid],[state] FROM [v_tabp_work] where approvestate = 0 ";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    department s = new department();
                    
                    s.Number = ConvertHelper.GetString(dr["Number"]);

                    s.PersonnelName = ConvertHelper.GetString(dr["PersonnelName"]);
                    s.LoginName = ConvertHelper.GetString(dr["LoginName"]);

                    s.WorkName = ConvertHelper.GetString(dr["WorkName"]);
                    s.Sex = ConvertHelper.GetInteger(dr["Sex"]);
                    s.IdCardNum = ConvertHelper.GetString(dr["IdCardNum"]);
                    s.Birthday = ConvertHelper.GetDateTime(dr["Birthday"]);
                    s.CallPhone = ConvertHelper.GetString(dr["CallPhone"]);
                    s.Pid = ConvertHelper.GetInteger(dr["Pid"]);
                    s.state = ConvertHelper.GetInteger(dr["state"]);

                    stus.Add(s);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.Close();
        }
        return stus;
    }


    public class department
    {
        public string Number { get; set; }
        public string PersonnelName { get; set; }
        public string LoginName { get; set; }
        public string WorkName { get; set; }
        public int Sex { get; set; }
        public string IdCardNum { get; set; }
        public DateTime Birthday { get; set; }
        public string CallPhone { get; set; }
        public int Pid { get; set; }
        public int state { get; set; }
    }



    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        string strsql;
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);

        int str = ConvertHelper.GetInteger(e.CommandArgument.ToString());
        string strid = (e.Item.FindControl("HiddenField1") as HiddenField).Value;


        if (e.CommandName == "Delete")
        {

            if (str == 1)
            {
                strsql = "update tabP_Personnel set state= 0 where  pid ='" + ConvertHelper.GetInteger(strid) + "'";
            }
            else
            {
                strsql = "update tabP_Personnel set state= 1 where  pid ='" + ConvertHelper.GetInteger(strid) + "'";
            }

            sqlcon.Open();


            SqlCommand mycmd = new SqlCommand(strsql, sqlcon);

            mycmd.ExecuteNonQuery();
            mycmd.Dispose();



            Response.Write("<script>alert('执行成功！')</script>");

            string indexTopMainStr = "SELECT [Number], [PersonnelName], [LoginName], [Pwd],  [WorkName], [Sex], [IdCardNum],[Birthday],[CallPhone],[Pid],[state] FROM [v_tabp_work] where approvestate = 0 ";
            SqlDataAdapter rst = new SqlDataAdapter(indexTopMainStr, sqlcon);
            DataSet rdt = new DataSet();
            rst.Fill(rdt);
            this.Repeater1.DataSourceID = null;
            Repeater1.DataSource = rdt;//数据绑定 
            Repeater1.DataBind();

            sqlcon.Close();


        }

        if (e.CommandName == "updateUser")
        {
            string url;
            url = "Modifyuser.aspx?name=" + strid;
            Response.Redirect(url);

        }

        if (e.CommandName == "detaileduser")
        {

            string url;
            url = "detaileduser.aspx?name=" + strid + "&name1=1";
            Response.Redirect(url);

        }
        if (e.CommandName == "powerControl")//权限
        {
            int pid = ConvertHelper.GetInteger((e.Item.FindControl("hdPid") as HiddenField).Value);//人员主键
            Response.Redirect("../PowerManage/PowerManage.aspx?pid=" + pid + "");
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        int temp = ConvertHelper.GetInteger(this.lblCurrentPage.Text);

        if (temp > 1)
        {
            List<department> Stus = GetTestData();

            this.lblCurrentPage.Text = (--temp).ToString();

            Stus = Stus.Skip((temp - 1) * 15).Take(15).ToList();

            this.Repeater1.DataSource = Stus;
            this.Repeater1.DataBind();
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int temp = ConvertHelper.GetInteger(this.lblCurrentPage.Text);

        if (temp < ConvertHelper.GetInteger(lblPageCount.Text))
        {
            List<department> Stus = GetTestData();
            this.lblCurrentPage.Text = (++temp).ToString();

            Stus = Stus.Skip((temp - 1) * 15).Take(15).ToList();

            this.Repeater1.DataSource = Stus;
            this.Repeater1.DataBind();
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {



        List<department> Stus = GetTestData();

        Stus = Stus.Skip(0).Take(15).ToList();

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
        lblCurrentPage.Text = "1";
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        int intendpage = ConvertHelper.GetInteger(lblPageCount.Text);

        List<department> Stus = GetTestData();

        Stus = Stus.Skip((intendpage - 1) * 15).Take(15).ToList();

        this.Repeater1.DataSource = Stus;
        this.Repeater1.DataBind();
        lblCurrentPage.Text = lblPageCount.Text;
    }
}


