using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Common;

namespace HySoft.BaseCallCenter.Web.sysmanage
{
    public partial class userAllocationRole : System.Web.UI.Page
    {
        string roleId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                roleId = Request.QueryString["ResetRole"];
                getUserRoleNmae(roleId);
            }
            //初始化方法
        }

        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="userid"></param>
        public void getUserRoleNmae(string userid) { 
               string sql = "select * from t_sys_userAccount where F_UserId ='" + userid + "'";
                DataTable dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    txtWorkingCode.Text = dt.Rows[0]["F_UserCode"].ToString();
                    txtRealName.Text = dt.Rows[0]["F_UserName"].ToString();
                }

                BindDropListRoles();
        }

        #region 绑定下拉列表
        /// <summary>
        /// 绑定下拉列表角色
        /// </summary>
        private void BindDropListRoles()
        {
            DataTable dt = new DataTable();

            try
            {
                this.DropDownListRole.Items.Clear();
                dt = new HySoft.BaseCallCenter.BLL.T_Sys_RoleInfo().GetList("1=1").Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    DropDownListRole.Items.Add(new ListItem(dr["F_RoleName"].ToString(), dr["F_RoleId"].ToString()));
                }
                this.DropDownListRole.Items.Insert(0, new ListItem("--请选择--", ""));
            }
            catch (Exception ex)
            {
                SysLog.WriteLog(ex);
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
        }
        #endregion
        /// <summary>
        /// 保存方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "";
                string sqlstr = "";
                int reslust = 0;
                if (!string.IsNullOrEmpty(DropDownListRole.Text))
                {
                    //根据用户id和原密码确定用户是否存在
                    sqlstr = "select * from t_sys_userAccount where  F_UserId='" + Request.QueryString["ResetRole"] + "'";
                    DataTable dt = DBUtility.DbHelperSQL.Query(sqlstr).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        sql = "update t_sys_userAccount set F_RoleId='" + hfRoleName.Value + "' where F_UserId ='" + Request.QueryString["ResetRole"] + "'";
                        reslust = DBUtility.DbHelperSQL.ExecuteSql(sql);
                        if (reslust > 0)
                        {
                            MessageBoxToWindow("分配角色成功", "提示！", "success");
                        }
                        else
                        {
                            MessageBoxToWindow("分配角色失败", "提示！", "error");
                        }

                    }
                    else
                    {

                        MessageBoxToWindow("不存在此用户", "提示！", "error");
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        #region 弹出对话框
        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="type"></param>
        public void MessageBoxToWindow(string title, string content, string type)
        {
            string script = "";
            switch (type)
            {
                case "error"://失败
                    type = "error";
                    script = "<script type='text/javascript'>  $.ligerDialog.alert('" + title + "','" + content + "','" + type + "');</script>";
                    break;
                case "success"://成功
                    type = "info";
                    script = "<script type='text/javascript'> $.ligerDialog.alert('" + title + "','" + content + "','" + type + "',CloseThis);</script>";
                    break;
                case "catch"://异常
                    type = "warning";
                    script = "<script type='text/javascript'>  $.ligerDialog.alert('" + title + "','" + content + "','" + type + "');</script>";
                    break;
                default:
                    type = "question";
                    break;
            }
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", script);
        }
        #endregion
    }
}