public frmPermissionDataPopUp(DataPermission resourcePer, DOResource doResource)
        {
            InitializeComponent();
            this.ResourcePer = resourcePer;
            this.doResource = doResource;
            Is_Group = doResource.ID <= -1;

            if (doResource.Creater_ID > -1 && FrameworkParams.currentUser.username != "admin" && doResource.Creater_ID != FrameworkParams.currentUser.employee_id)
            {
                HelpXtraForm.CloseFormNoConfirm(this);
                HelpMsgBox.ShowNotificationMessage(string.Format("Bạn không có quyền phân quyền trên {0} này!", resourcePer.ResName));
                return;
            }
            InitControl();
            UpdateData();
        }
        private DataTable GetDataSource(DataPermission dataPer)
        {
            if (dataPer.DMTableName_Res == "" && dataPer.DMTableName_ResGroup == "") return null;

            string sql = "";
            string ResFullBit = dataPer.Is_ResUseCreate ? "per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y'"
                : "per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y'";

            if (dataPer.Is_DM_Tree)
            {
                if (dataPer.DMTableName_Res == "")//Chỉ có nhóm danh mục
                {
                    sql = string.Format(@"  select * from (select resGroup.name, resGroup.{0} parent_id, nv.id employee_id, resGroup.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                       {1}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {2} resGroup
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={1} and per.is_group='Y'

                        union

                        select resGroup.name, resGroup.{0} parent_id,null employee_id, resGroup.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                        {1}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {2} resGroup
                        inner join department dep on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={1} and per.is_group='Y') order by is_group desc, name asc",
                        dataPer.Parent_Field, dataPer.Resource_Type_ID, dataPer.DMTableName_ResGroup);
                }
                else if (dataPer.DMTableName_ResGroup == "")//chỉ có danh mục
                {
                    sql = string.Format(@"select * from (select resGroup.name, resGroup.id parent_id, nv.id employee_id, resGroup.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({2}, 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'N' is_group,per.id,'{3}' use_create
                        from {1} resGroup
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={0} and per.is_group='N'

                        union

                        select resGroup.name, resGroup.id parent_id,null employee_id, resGroup.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({2}, 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'N' is_group,per.id,'{3}' use_create
                        from {1} resGroup
                        inner join department dep on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={0} and per.is_group='N') order by is_group desc, name asc",
                        dataPer.Resource_Type_ID, dataPer.DMTableName_Res, ResFullBit, dataPer.Is_ResUseCreate ? "Y" : "N");
                }
                else
                {
                    sql = string.Format(@"select * from (select resGroup.name, resGroup.{0} parent_id, nv.id employee_id, resGroup.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                        {1}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {2} resGroup
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={1} and per.is_group='Y'

                        union

                        select res.name, res.{3} parent_id, nv.id employee_id, res.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({5}, 'Y','N') ISFULL_BIT,
                        {1}  resource_type, 'N' is_group,per.id,'{6}' use_create
                        from {4} res
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on res.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={1} and per.is_group='N'

                        union

                        select resGroup.name, resGroup.{0} parent_id,null employee_id, resGroup.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                        {1}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {2} resGroup
                        inner join department dep on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={1} and per.is_group='Y'

                        union

                        select res.name, res.{3} parent_id,null employee_id, res.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({5}, 'Y','N') ISFULL_BIT,
                        {1}  resource_type, 'N' is_group,per.id,'{6}' use_create
                        from {4} res
                        inner join department dep on 1=1
                        left join per_resource per on res.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={1} and per.is_group='N')  order by is_group desc, name asc"
                        , dataPer.Parent_Field, dataPer.Resource_Type_ID, dataPer.DMTableName_ResGroup,
                        dataPer.RefFieldNameDM_Res_ResGroup, dataPer.DMTableName_Res, ResFullBit, dataPer.Is_ResUseCreate ? "Y" : "N");
                }
            }
            else
            {
                if (dataPer.DMTableName_Res == "")//Chỉ có nhóm danh mục
                {
                    sql = string.Format(@"select * from (select resGroup.name, resGroup.id parent_id, nv.id employee_id, resGroup.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {1} resGroup
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={0} and per.is_group='Y'

                        union

                        select resGroup.name, resGroup.id parent_id,null employee_id, resGroup.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {1} resGroup
                        inner join department dep on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={0} and per.is_group='Y') order by is_group desc, name asc",
                        dataPer.Resource_Type_ID, dataPer.DMTableName_ResGroup);
                }
                else if (dataPer.DMTableName_ResGroup == "")//chỉ có danh mục
                {
                    sql = string.Format(@"select * from (select resGroup.name, resGroup.id parent_id, nv.id employee_id, resGroup.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({2}, 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'N' is_group,per.id,'{3}' use_create
                        from {1} resGroup
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={0} and per.is_group='N'

                        union

                        select resGroup.name, resGroup.id parent_id,null employee_id, resGroup.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({2}, 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'N' is_group,per.id,'{3}' use_create
                        from {1} resGroup
                        inner join department dep on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={0} and per.is_group='N') order by is_group desc, name asc",
                        dataPer.Resource_Type_ID, dataPer.DMTableName_Res, ResFullBit, dataPer.Is_ResUseCreate ? "Y" : "N");
                }
                else
                {
                    sql = string.Format(@"select * from (select resGroup.name, resGroup.id parent_id, nv.id employee_id, resGroup.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {1} resGroup
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={0} and per.is_group='Y'

                        union

                        select res.name, res.{2} parent_id, nv.id employee_id, res.id resource_id, null department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({4}, 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'N' is_group,per.id,'{5}' use_create
                        from {3} res
                        inner join dm_nhan_vien nv on 1=1
                        left join per_resource per on res.id=per.resource_id and per.employee_id=nv.id
                        and per.resource_type={0} and per.is_group='N'

                        union

                        select resGroup.name, resGroup.id parent_id,null employee_id, resGroup.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif(per.isread_bit='Y' and per.isupdate_bit ='Y' and per.isdelete_bit='Y' and per.iscreate_bit='Y', 'Y','N') ISFULL_BIT,
                       {0} resource_type, 'Y' is_group,per.id,'Y' use_create
                        from {1} resGroup
                        inner join department dep on 1=1
                        left join per_resource per on resGroup.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type={0} and per.is_group='Y'

                        union

                        select res.name, res.{2} parent_id,null employee_id, res.id resource_id, dep.id department_id,
                        iif(per.iscreate_bit='Y','Y','N') iscreate_bit,
                        iif(per.isread_bit='Y','Y','N') isread_bit,
                        iif(per.isupdate_bit='Y','Y','N') isupdate_bit,
                        iif(per.isdelete_bit='Y','Y','N') isdelete_bit,
                        iif({4}, 'Y','N') ISFULL_BIT,
                        {0}  resource_type, 'N' is_group,per.id,'{5}' use_create
                        from {3} res
                        inner join department dep on 1=1
                        left join per_resource per on res.id=per.resource_id and per.department_id=dep.id
                        and per.resource_type= {0} and per.is_group='N')
                        order by is_group desc, name asc",
                        dataPer.Resource_Type_ID, dataPer.DMTableName_ResGroup,
                        dataPer.RefFieldNameDM_Res_ResGroup, dataPer.DMTableName_Res, ResFullBit, dataPer.Is_ResUseCreate ? "Y" : "N");
                }
            }
            if (sql != "")
            {
                DataSet ds = HelpDB.getDatabase().LoadDataSet(sql, "PER_RESOURCE");
                if (ds != null && ds.Tables.Count > 0)
                    return ds.Tables[0];
            }
            return null;
        }