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.Data.SqlClient;

public partial class manage_Company_Managers : BasePage
{
    EasyAccess AccessHelper = new EasyAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AdminUser();
        }
    }
    private void AdminUser()
    {
        string sql = "select * from admin order by id";
        GridView1.DataSource = AccessHelper.ExecuteDataSet(sql);
        GridView1.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
        {
            JWindow.ShowDialog("管理员帐号或密码均不可为空！");
            return;
        }
        string sql = "insert into admin(uid,upwd,addtime) values ('"+TextBox1.Text.Trim()+"','"+TextBox2.Text.Trim().DESEncrypt()+"','"+DateTime.Now.ToString()+"')";
        bool isOK = AccessHelper.ExecuteNonQuery(sql);
        if (isOK)
        {
            JWindow.ShowDialog("管理员添加成功！");
            AdminUser();
        }
        else
        {
            JWindow.ShowDialog("管理员添加失败！");
        }


    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        AdminUser();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        AdminUser();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string key = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string sql = "delete from admin where id="+key.ToInt32()+"";
        bool IsOK = AccessHelper.ExecuteNonQuery(sql);
        if (IsOK)
        {
            JWindow.ShowDialog("管理员删除成功！");
            AdminUser();
        }
        else
        {
            JWindow.ShowDialog("管理员删除失败！");
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         string key = GridView1.DataKeys[e.RowIndex].Value.ToString();
         string uid = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim();
         string upwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text.Trim();
         string sql = "update admin set uid='" + uid + "',upwd='" + upwd.DESEncrypt() + "' where id=" + key.ToInt32() + "";

         bool IsOK = AccessHelper.ExecuteNonQuery(sql);
         if (IsOK)
        {
            GridView1.EditIndex = -1;
            JWindow.ShowDialog("管理员修改成功！");
         
        }
        else
        {
            JWindow.ShowDialog("管理员修改失败！");
        }
         AdminUser();
    }
}
