public ActionResult EditFolder(CCFolder folderObj)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            CCFolder folderResponse;
            int folderType = folderObj.Type;

            if (ModelState.IsValid && accountObj != null)
            {
                folderObj.AccountGUID = accountObj.AccountGUID;
                folderObj.IsPaused = false;
                folderObj.isOverFlow = false;
                if (folderObj.Type == 3)
                    folderObj.isCrimeDiary = true;
                //Save Folder
                folderResponse = CCFolderRepository.SaveFolder(folderObj);

                var folders = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                TempData["SelectedMenu"] = "Manage";
                TempData["SelectedSubMenu"] = "ManageFolders";

                if (folderObj.FolderID > 0)
                {
                    var fields = CCFieldRepository.CCFolderFields.Where(u => u.FolderID == folderObj.FolderID & u.AccountGUID == accountObj.AccountGUID).ToList();
                    if (fields.Count == 0)
                    {
                        //Save default field
                        List<string> needtosave = new List<string>();
                        List<CCFolderField> savedobj = new List<CCFolderField>();

                        if (folderType == 1) {
                            if (accountObj.PlanID == 1)
                            {
                                needtosave = FieldsConfigHelper.GetFieldForContactSimple();
                            }
                            else if (accountObj.PlanID == 2)
                            {
                                needtosave = FieldsConfigHelper.GetFieldForContactBusiness();
                            }
                            else
                            {
                                needtosave = FieldsConfigHelper.GetFieldForContactFull();
                            }
                        }
                        else if (folderType == 2) { needtosave = FieldsConfigHelper.GetFieldForAppointmentSimple(); }
                        else if (folderType == 3) { needtosave = FieldsConfigHelper.GetFieldForCrimeDiaryAppointment(); }
                        else { }

                        foreach (var field in needtosave)
                        {
                            CCFolderField folderField = new CCFolderField();
                            folderField.FieldName = FieldsConfigHelper.GetRealName(field);
                            folderField.FieldType = FieldsConfigHelper.GetVariableType(field);
                            folderField.FolderID = folderObj.FolderID;
                            folderField.FieldCaption = field;
                            folderField.Restriction = "none";
                            folderField.AccountGUID = accountObj.AccountGUID;
                            folderField.isActive = true;
                            savedobj.Add(folderField);
                        }

                        var resp = CCFieldRepository.SaveFolderFieldsObj(savedobj);

                    }

                    // create note into grops
                    var alreadyExist = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderResponse.FolderID & fid.GroupName == "Note" & fid.AccountGUID == accountObj.AccountGUID).ToList();

                    if (alreadyExist.Count == 0)
                    {
                        CCGroup objGroup = new CCGroup();
                        objGroup.GroupName = "Note";
                        objGroup.FolderID = folderResponse.FolderID;
                        objGroup.AccountGUID = accountObj.AccountGUID;
                        var user = CCGroupRepository.SaveGroup(objGroup);
                    }

                    // create default group
                    var isDefaultGrpExist = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderResponse.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).ToList();

                    if (isDefaultGrpExist.Count == 0)
                    {
                        // add default group
                        CCGroup objGroup = new CCGroup();
                        objGroup.GroupName = "Default";
                        objGroup.FolderID = folderResponse.FolderID;
                        objGroup.AccountGUID = accountObj.AccountGUID;
                        var grp = CCGroupRepository.SaveGroup(objGroup);

                        if (grp.GroupID != 0)
                        {
                            // added all default field
                            var defaultfields = CCFieldRepository.CCFolderFields.Where(u => u.FolderID == folderObj.FolderID & u.AccountGUID == accountObj.AccountGUID).ToList();
                            int i = 1;
                            foreach (var fieldItem in defaultfields)
                            {
                                CCGroupField objgrpfield = new CCGroupField();
                                objgrpfield.FieldID = fieldItem.FieldID;
                                objgrpfield.FieldOrder = i++;
                                objgrpfield.FolderID = grp.FolderID;
                                objgrpfield.GroupID = grp.GroupID;
                                objgrpfield.AccountGUID = accountObj.AccountGUID;
                                var aa = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                            }
                        }
                    }

                    // create default layout
                    var isDefaultLayoutExist = CCLayoutRepository.CCLayouts.Where(fid => fid.FolderID == folderResponse.FolderID & fid.LayoutName == "Default" & fid.AccountGUID == accountObj.AccountGUID).ToList();

                    if (isDefaultLayoutExist.Count == 0)
                    {
                        // add default layout
                        CCLayout layout = new CCLayout();
                        layout.LayoutName = "Default";
                        layout.FolderID = folderObj.FolderID;
                        layout.AccountGUID = accountObj.AccountGUID;
                        var resLayout = CCLayoutRepository.Savelayout(layout);

                        // add default group into default layout
                        var defaultExistGrp = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == folderResponse.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                        if (defaultExistGrp != null)
                        {
                            CCLayoutGroup objLayoutGroup = new CCLayoutGroup();
                            objLayoutGroup.GroupID = defaultExistGrp.GroupID;
                            objLayoutGroup.GroupOrder = 1;
                            objLayoutGroup.FolderID = resLayout.FolderID;
                            objLayoutGroup.LayoutID = resLayout.LayoutID;
                            objLayoutGroup.AccountGUID = accountObj.AccountGUID;
                            var aa = CCLayoutGroupRepository.SavelayoutGroup(objLayoutGroup);
                        }
                    }

                }

                var folderList = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                Session["folderss"] = folderList;

                LimitationsViewModel limitationsObjMain = (LimitationsViewModel)Session["limitations"];
                HelperFunctions HF = new HelperFunctions();
                limitationsObjMain = HF.updateAccountLimitations(accountObj);
                Session["limitations"] = limitationsObjMain;

                return RedirectToAction("ManageFolders", "Folder");
            }
            else
            {
                var folderList = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                Session["folderss"] = folderList;

                LimitationsViewModel limitationsObjMain = (LimitationsViewModel)Session["limitations"];
                HelperFunctions HF = new HelperFunctions();
                limitationsObjMain = HF.updateAccountLimitations(accountObj);
                Session["limitations"] = limitationsObjMain;

                return View(folderObj);
            }
        }
        public ActionResult EditGroup(CCGroup objGroup)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];

            if (ModelState.IsValid)
            {
                objGroup.AccountGUID = accountObj.AccountGUID;
                var resGroup = CCGroupRepository.SaveGroup(objGroup);
                if (resGroup != null)
                {
                    var isDefaultExist = CCLayoutRepository.CCLayouts.Where(fid => fid.FolderID == resGroup.FolderID & fid.LayoutName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                    if (isDefaultExist != null)
                    {
                        // get layout groups last order
                        var layoutGroupsOrder = CCLayoutGroupRepository.CCLayoutGroups.Where(gid => gid.LayoutID == isDefaultExist.LayoutID & gid.AccountGUID == accountObj.AccountGUID).OrderByDescending(grp => grp.GroupOrder).FirstOrDefault();

                        // add group on default layout
                        CCLayoutGroup objLayoutGroup = new CCLayoutGroup();
                        objLayoutGroup.GroupID = resGroup.GroupID;
                        objLayoutGroup.GroupOrder = layoutGroupsOrder.GroupOrder + 1;
                        objLayoutGroup.FolderID = resGroup.FolderID;
                        objLayoutGroup.LayoutID = isDefaultExist.LayoutID;
                        objLayoutGroup.AccountGUID = accountObj.AccountGUID;
                        var aa = CCLayoutGroupRepository.SavelayoutGroup(objLayoutGroup);
                    }
                }
                return RedirectToAction("ManageLayoutsAndGroups", "Folder", new { ID = objGroup.FolderID });
            }
            else
            {
                return View(objGroup);
            }
        }
        public ActionResult SelectLayoutGroups(string groups, LayoutGroupsViewModel objLayoutField)
        {
            Account accountObj = (Account)Session["account"];
            // get Groups by Folder
            var layoutGroup = CCGroupRepository.CCGroups
                            .Where(u => u.FolderID == objLayoutField.FolderID & u.AccountGUID == accountObj.AccountGUID).ToList();

            CCLayoutGroup objLayoutGroup = new CCLayoutGroup();
            User userObj = (User)Session["user"];
            var objgroups = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(groups);
            int i = 1;

            if (objLayoutField.IsSave == false)
            {
                CCLayoutGroupRepository.DeleteLayoutGroupsByLayoutID(objLayoutField.LayoutID);
            }
            foreach (var item in objgroups)
            {
                // objLayoutGroup.AccountID = userObj.AccountID;
                var res = layoutGroup.Find(field => field.GroupName == item);
                if (res != null) objLayoutGroup.GroupID = res.GroupID;
                objLayoutGroup.GroupOrder = i++;
                objLayoutGroup.FolderID = objLayoutField.FolderID;
                objLayoutGroup.LayoutID = objLayoutField.LayoutID;
                objLayoutGroup.AccountGUID = accountObj.AccountGUID;
                var aa = CCLayoutGroupRepository.SavelayoutGroup(objLayoutGroup);
            }
            return RedirectToAction("ManageLayoutsAndGroups", "Folder", new { ID = objLayoutField.FolderID });
        }
 public CCLayoutGroup SavelayoutGroup(CCLayoutGroup layoutgroupObj)
 {
     context.CCLayoutGroups.Add(layoutgroupObj);
     context.SaveChanges();
     return layoutgroupObj;
 }