using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using CRM.BLL;
using CRM.Model;
using CRM.Common;

public partial class Pages_System_Employees_Employees_sxt : System.Web.UI.Page
{
    private void Bind()
    {
        string sql = "select 所属权限组,boss from 员工表 where eid="+Session["LoginID"].ToString();

        DataTable dt = CRM.DBUtility.SqlHelper.ExecuteDataSet(CRM.DBUtility.SqlHelper.connstr,CommandType.Text,sql,null).Tables[0];
        if (dt.Rows.Count > 0)
        {
            string rightName = CRM.Common.InitData.GetRightNameByID(dt.Rows[0]["所属权限组"].ToString());
            switch (rightName)
            {
                case "管理员": //如果是管理员，列出一般客户名单
                    sql = "select * from 员工表 where boss=0 and cid=" + Session["CID"].ToString() + " and 所属权限组=(select id from 员工权限表 where 权限组名称='一般客户'and cid=" + Session["CID"].ToString() + ")";
                    break;
                case "代理":
                    sql = "select * from 员工表 where 部门=" + Session["LoginID"].ToString() + " and eid!=" + Session["LoginID"].ToString();
                    break;
                default:
                    sql = "select * from 员工表 where cid=" + Session["CID"].ToString();
                    break;
            }

            if (dt.Rows[0]["boss"].ToString() == "1")
            {
                sql = "select * from 员工表 where cid=" + Session["CID"].ToString() + " and 所属权限组=(select id from 员工权限表 where 权限组名称='一般客户' and cid=" + Session["CID"].ToString() + ")";
            }
        }

        GridView1.DataSource = CRM.DBUtility.SqlHelper.ExecuteDataSet(CRM.DBUtility.SqlHelper.connstr,CommandType.Text,sql,null);
        GridView1.DataKeyNames = new string[] { "eid","boss" };
        GridView1.DataBind();

    }
    protected void InitData()
    {
        ddlRight.Items.Clear();
        ddlRight.Items.Add(new ListItem("请选择", "-1"));
        DataTable dtRight = CRM.Common.InitData.GetRightByCid(Session["CID"].ToString());
        if (dtRight.Rows.Count > 0)
        {
            for (int i = 0; i < dtRight.Rows.Count; i++)
            {
                ddlRight.Items.Add(new ListItem(dtRight.Rows[i]["权限组名称"].ToString(), dtRight.Rows[i]["id"].ToString()));
            }
        }
        ddlRight.SelectedIndex = 3;

        ddlDepartment.Items.Clear();
        ddlDepartment.Items.Add(new ListItem("请选择", "-1"));
        DataTable dtDepartment = CRM.Common.InitData.GetDepartmentByCid(Session["CID"].ToString());
        if (dtDepartment.Rows.Count > 0)
        {
            for (int i = 0; i < dtDepartment.Rows.Count; i++)
            {
                ddlDepartment.Items.Add(new ListItem(dtDepartment.Rows[i]["部门名称"].ToString(), dtDepartment.Rows[i]["id"].ToString()));
            }
        }

        ddlRole.Items.Clear();
        ddlRole.Items.Add(new ListItem("请选择", "-1"));
        DataTable dtRole = CRM.Common.InitData.GetRoleByCid(Session["CID"].ToString());
        if (dtRole.Rows.Count > 0)
        {
            for (int i = 0; i < dtRole.Rows.Count; i++)
            {
                ddlRole.Items.Add(new ListItem(dtRole.Rows[i]["角色名称"].ToString(), dtRole.Rows[i]["id"].ToString()));
            }
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int index = e.NewSelectedIndex;


        IList<EmployeesInfo> employeesinfo = new List<EmployeesInfo>();

        Employees employees = new Employees();
        employeesinfo = employees.GetEmployeesById(GridView1.DataKeys[index].Values["eid"].ToString());

        if (employeesinfo.Count > 0)
        {
            txtEmployeesLoginName.Text = employeesinfo[0].登录名;
            txtEmployeesName.Text = employeesinfo[0].姓名;
            txtEmployeesPWD.Text = employeesinfo[0].密码;
            txtEmployeesPWD.Attributes.Add("Value", employeesinfo[0].密码);
            ddlDepartment.SelectedValue = "-1";
            ddlRole.SelectedValue ="-1";
            txtMobile.Text = employeesinfo[0].手机;
            txtTel.Text = employeesinfo[0].住宅电话;
            ddlRight.SelectedValue = employeesinfo[0].所属权限组.ToString();
            ddlStatus.SelectedValue = employeesinfo[0].用户状态.ToString();
            txtBz.Text = employeesinfo[0].备注;
        }
        Lable1.Text = "";

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;
        Employees employees = new Employees();
        employees.EmployeesDeleteById(GridView1.DataKeys[index].Values["eid"].ToString());

        Bind();

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        int index = e.Row.RowIndex;
        if (index != -1) {
            if (GridView1.DataKeys[index]["boss"].ToString()=="1")
                ((ImageButton)e.Row.Cells[10].Controls[0]).Visible = false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (LoginName_TextChanged() == "1")
        {
            return;
        }
        #region 增加员工
        else
        {

            EmployeesInfo employeesinfo = new EmployeesInfo();

            employeesinfo.登录名 = txtEmployeesLoginName.Text.Trim();
            employeesinfo.姓名 = txtEmployeesName.Text.Trim();
            employeesinfo.密码 = txtEmployeesPWD.Text.Trim();
            employeesinfo.部门 = Int32.Parse(Session["LoginID"].ToString());
            employeesinfo.角色 = Int32.Parse(ddlRole.SelectedValue.ToString());
            employeesinfo.手机 = txtMobile.Text;
            employeesinfo.住宅电话 = txtTel.Text;
            ddlRight.SelectedIndex = 3;
            employeesinfo.所属权限组 = Int32.Parse(ddlRight.SelectedValue.ToString());
            employeesinfo.用户状态 = Int32.Parse(ddlStatus.SelectedValue.ToString());
            employeesinfo.备注 = txtBz.Text;
            employeesinfo.CID = Int32.Parse(Session["CID"].ToString());

            string existence = CRM.Common.InitData.CheckLoginNameExistence(txtEmployeesLoginName.Text.Trim());
            if (existence == "")
            {
                Employees employees = new Employees();
                employees.EmployeesAdd(employeesinfo);

                if (!Page.ClientScript.IsClientScriptBlockRegistered("AddSales"))
                {
                    Page.RegisterClientScriptBlock("AddSales", "<script>alert('添加成功')</script>");
                }
            }
            else
            {
                if (!Page.ClientScript.IsClientScriptBlockRegistered("SalesExistence"))
                {
                    Page.RegisterClientScriptBlock("SalesExistence", "<script>alert('用户已存在!')</script>");
                }
            }

            Bind();
        }
        #endregion
    }
    protected string LoginName_TextChanged() {
        string temp="";
        string existence=CRM.Common.InitData.CheckLoginNameExistence(txtEmployeesLoginName.Text.Trim());
        if (existence != "")
        {
            Lable1.Text = "登录名已存在，请修改！";
            txtEmployeesLoginName.Text = "";
            temp="1";
        }
        else {
            Lable1.Text = "";
        }
        return temp;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int index = GridView1.SelectedIndex;

        EmployeesInfo employeesinfo = new EmployeesInfo();

        employeesinfo.登录名 = txtEmployeesLoginName.Text.Trim();
        employeesinfo.姓名 = txtEmployeesName.Text.Trim() ;
        employeesinfo.密码 = txtEmployeesPWD.Text.Trim();
        employeesinfo.部门 = Int32.Parse(Session["LoginID"].ToString()); 
        employeesinfo.角色 = -1;
        employeesinfo.手机 = txtMobile.Text;
        employeesinfo.住宅电话 = txtTel.Text;
        employeesinfo.所属权限组 = Int32.Parse(ddlRight.SelectedValue.ToString());
        employeesinfo.用户状态 = Int32.Parse(ddlStatus.SelectedValue.ToString());
        employeesinfo.备注 = txtBz.Text;
        employeesinfo.CID = Int32.Parse(Session["CID"].ToString());
        employeesinfo.EID =Int32.Parse(GridView1.DataKeys[index].Values["eid"].ToString());

        Employees employees = new Employees();
        employees.EmployeesUpdateById(employeesinfo);

        Bind();
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CRM.Common.Login.IsLogin();
        if (!IsPostBack)
        {
            Bind();
            InitData();
        }
    }
}
