private List<cls_Module_Authority_Admin> getList(DataTable dt)
        {
            try
            {
                List<cls_Module_Authority_Admin> list = new List<cls_Module_Authority_Admin>();
                cls_Module_Authority_Admin mAAmin;

                DataTable dtModule = new cls_Modules().Select();

                for (int i = 0; i < dtModule.Rows.Count; i++)
                {
                    mAAmin = new cls_Module_Authority_Admin();
                    mAAmin.Company = "";

                    DataRow[] _Row = dt.Select("ModuleId=" + dtModule.Rows[i]["Id"].ToString());

                    for (int j = 0; j < _Row.Count(); j++)
                    {
                        mAAmin.Company += _Row[j]["CompanyId"].ToString() + ";";
                    }

                    mAAmin.Company = mAAmin.Company.Substring(0, mAAmin.Company.Length - 1);
                    if (mAAmin.Company == "0") mAAmin.Company = "";

                    mAAmin.AdminType = Convert.ToInt16(_Row[0]["AdminType"]);
                    mAAmin.IsUse = Convert.ToInt16(_Row[0]["IsUse"]);
                    mAAmin.ModuleName = _Row[0]["ModuleName"].ToString();
                    mAAmin.ModuleId = Convert.ToInt16(_Row[0]["ModuleId"]);
                    mAAmin.Id = 0;

                    list.Add(mAAmin);
                }
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        private bool checkAdminHTAuthority()
        {
            cls_Module_Authority_Admin maAdmin = new cls_Module_Authority_Admin();
            maAdmin.EmployeeId = Convert.ToInt32(Session["UserId"]);
            int result = maAdmin.SelectAuthority();

            if (result > 0) return true;
            else return false;
        }
        private void bindRepeater(int empId)
        {
            try
            {
                //LAY DANH SACH CONG TY
                DataTable dt = new cls_Catalog_Company().Select((short)1, Convert.ToInt32(Session["UserId"]));
                ddlCompanySelect.DataSource = dt;
                ddlCompanySelect.DataBind();
                ViewState["DT_COMPANY"] = dt;

                bindCboEmployee(empId);

                cls_Module_Authority_Admin aAdmin = new cls_Module_Authority_Admin();
                aAdmin.EmployeeId = empId;

                DataTable dtEAuthority = aAdmin.SelectByEId();
                List<cls_Module_Authority_Admin>  list = getList(dtEAuthority);
                ViewState["DT_EAUTHORITY"] = dtEAuthority;

                rptData.DataSource = list;
                rptData.DataBind();

                if (list[0].AdminType == 1) chkAdminFull.Checked = true;
                setVisibleRepeater();
            }
            catch (Exception ex)
            {
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtCompany = (DataTable)ViewState["DT_COMPANY"];
                DataTable dtEModule = new DataTable();
                dtEModule.Columns.Add("Id", typeof(int));
                dtEModule.Columns.Add("EmployeeId");
                dtEModule.Columns.Add("ModuleId");
                dtEModule.Columns.Add("CompanyId");
                dtEModule.Columns.Add("AdminType");
                dtEModule.Columns.Add("IsUse");
                dtEModule.Columns.Add("CreatedTime");

                int e_id;
                e_id = Convert.ToInt32(cboEmployee.SelectedValue);

                if (e_id == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                "MyScript", "alert('Bạn phải chọn Nhân viên cần phân quyền trước !');", true);
                    cboEmployee.Focus();
                    return;
                }

                DataTable dtEAuthority = (DataTable)ViewState["DT_EAUTHORITY"];

                for (int i = 0; i < rptData.Items.Count; i++)
                {
                    RepeaterItem item = rptData.Items[i];

                    var chkSelectM = (CheckBox)item.FindControl("chkSelectM");
                    var lbModuleName = (Label)item.FindControl("lbModuleName");
                    var lblModuleId = (Label)item.FindControl("lblModuleId");
                    var txtComName = (TextBox)item.FindControl("txtComName");
                    var lblEMId = (Label)item.FindControl("lblEMId");

                    if(chkAdminFull.Checked ==false)
                        if ((chkSelectM.Checked) && (txtComName.Text.Length <= 0))
                        {
                            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                                "MyScript", "alert('Bạn phải chọn công ty cho Module \""+lbModuleName.Text+"\" !');", true);
                            txtComName.Focus();
                            return;
                        }

                    DataTable dtCompanyId;

                    if(chkAdminFull.Checked)
                        dtCompanyId = dtCompany;
                    else
                        dtCompanyId = getCompanyIdList(txtComName.Text, dtCompany);

                    for (int j = 0; j < dtCompanyId.Rows.Count; j++)
                    {
                        DataRow dRow = dtEModule.NewRow();
                        DataRow[] _Row = dtEAuthority.Select("ModuleId=" + lblModuleId.Text + " AND CompanyId=" + dtCompanyId.Rows[j]["Id"].ToString());

                        if (_Row.Count() > 0)
                            dRow["Id"] = _Row[0]["Id"];
                        else
                            dRow["Id"] = "0";

                        dRow["EmployeeId"] = e_id;
                        dRow["ModuleId"] = lblModuleId.Text;
                        dRow["CompanyId"] = dtCompanyId.Rows[j]["Id"];
                        dRow["AdminType"] = (chkAdminFull.Checked) ? 1 : 2;

                        dRow["IsUse"] = (short)((chkAdminFull.Checked) ? 1 : 0);
                        if (dRow["IsUse"].ToString() == "0") dRow["IsUse"] = (short)((chkSelectM.Checked) ? 1 : 0);
                        dRow["CreatedTime"] = DateTime.Now;

                        dtEModule.Rows.Add(dRow);
                    }
                }

                int result = new cls_Module_Authority_Admin().ExecuteUpdateByPId(dtEModule, e_id);
                if (result > 0)
                {
                    cls_Module_Authority_Admin aAdmin = new cls_Module_Authority_Admin();
                    aAdmin.EmployeeId = e_id;
                    result = aAdmin.UpdateByEId();

                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                        "MyScript", "alert('Cập nhật quyền thao tác với Module cho Nhân sự thành công!');", true);
                    Response.Redirect("Admin_System.aspx");
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(),
                        "MyScript", "alert('Có lỗi trong quá trình cập nhật, vui lòng thử lại !!!');", true);
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void bindListAdmin()
        {
            try
            {
                //BIND CAC MODULE
                DataTable dt = new cls_Modules().Select();

                if (dt.Rows.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(" <table class=\"basic-table\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">" +
                              "  <tbody><tr>" +
                              "      <td>" +
                              "          <table class=\"form-table\" border=\"0\" cellpadding=\"4\"><tbody>" +
                              "              <tr class=\"gray\">" +
                              "                  <td colspan=\"" + (dt.Rows.Count + 1).ToString() + "\">PHÂN QUYỀN NGƯỜI DÙNG</td>" +
                              "                  <td align=\"right\" colspan=\"2\"><a href=\"Admin_System.aspx?A_action=2\">+Bổ sung</a></td>" +
                              "              </tr>" +
                              "              <tr bgcolor=\"whitesmoke\">" +
                              "                  <td class=\"Normal\" width=\"180px\"> <u>Người đã phân quyền </u></td>" +
                              "                  <td class=\"Normalred\"><b>Admin HT</b></td>");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sb.Append("                  <td class=\"NormalBold\" align=\"center\">Module " + dt.Rows[i]["ModuleName"].ToString() + "</td>");
                    }
                    sb.Append("                  <td class=\"normal\">Cập nhật</td>"+
                              "              </tr>");

                    DataTable dtEmp = new cls_Module_Authority_Admin().SelectEmpByCom(Convert.ToInt16(ddlCompany.SelectedValue));
                    if (dtEmp.Rows.Count > 0)
                    {
                        DataTable dtAModule = new cls_Module_Authority_Admin().SelectByCompany(Convert.ToInt16(ddlCompany.SelectedValue));

                        for (int i = 0; i < dtEmp.Rows.Count; i++)
                        {
                            DataRow[] _Row = dtAModule.Select("EmployeeId=" + dtEmp.Rows[i]["EmployeeId"].ToString(), "ModuleCode asc");

                            sb.Append("<tr>"+
                                      "     <td class=\"NormalBold\"><img src=\"../images/MRoom/user.gif\" /> "+_Row[0]["EmployName"].ToString()+"</td>");

                            if (_Row[0]["AdminType"].ToString() == "1")
                                sb.Append("     <td class=\"normal\" align=\"center\"><img src=\"../images/Update.gif\" align=\"absmiddle\"></td>");
                            else
                                sb.Append("     <td class=\"normal\" align=\"center\">&nbsp;</td>");

                            for (int j = 0; j < _Row.Count(); j++)
                            {
                                if (_Row[j]["IsUse"].ToString() == "1")
                                    sb.Append("     <Td class=\"normal\" align=\"center\"><img src=\"../images/Update.gif\" align=\"absmiddle\"> </td>");
                                else
                                    sb.Append("     <Td class=\"normal\" align=\"center\">&nbsp;</td>");
                            }

                            sb.Append("     <td class=\"normal\" align=\"center\"><a href=\"Admin_System.aspx?A_action=2&E_Id=" + dtEmp.Rows[i]["EmployeeId"].ToString() + "\">Sửa</a></td>" +
                                      "</tr>");
                        }
                    }

                    sb.Append("              </tbody>" +
                              "          </table>" +
                              "      </td>" +
                              "  </tr>" +
                            "</tbody></table>");

                    ltlListAdmin.Text = sb.ToString();
                }
            }
            catch (Exception ex)
            {
            }
        }
        protected void cboEmployee_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            int empId = Convert.ToInt32(cboEmployee.SelectedValue);

            if (empId > 0)
            {
                cls_Module_Authority_Admin aAdmin = new cls_Module_Authority_Admin();
                aAdmin.EmployeeId = empId;

                DataTable dtEAuthority = aAdmin.SelectByEId();
                List<cls_Module_Authority_Admin> list = getList(dtEAuthority);
                ViewState["DT_EAUTHORITY"] = dtEAuthority;

                rptData.DataSource = list;
                rptData.DataBind();

                if (list[0].AdminType == 1) chkAdminFull.Checked = true;
                setVisibleRepeater();
            }
        }