public ActionResult SelectFolderFields(string fields, FolderFieldsSelectModel objFolderFields)
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            List<string> needtosave = new List<string>();
            List<CCFolderField> savedobj = new List<CCFolderField>();
            List<CCFolderField> activeFields = new List<CCFolderField>();
            List<CCFolderField> needToInActive = new List<CCFolderField>();

            var objFields = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(fields).ToList();
            needtosave = CCFieldRepository.IsAvailableField(objFields, objFolderFields.FolderID);

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

            foreach (var field in objFields)
            {
                CCFolderField folderField = CCFieldRepository.CCFolderFields.Where(f => f.FieldCaption == field & f.AccountGUID == accountObj.AccountGUID & f.FolderID == objFolderFields.FolderID).FirstOrDefault();
                activeFields.Add(folderField);
            }

            var resp = CCFieldRepository.SaveFolderFieldsObj(savedobj);

            var fieldList = CCFieldRepository.CCFolderFields.Where(f => f.AccountGUID == accountObj.AccountGUID & f.FolderID == objFolderFields.FolderID).ToList();

            var toInActiveList = fieldList.Except(activeFields).ToList();

            int flag = 0;

            foreach (var f1 in fieldList)
            {
                flag = 0;
                foreach (var f2 in activeFields)
                {
                    try
                    {
                        if (f2.FieldID == f1.FieldID)
                        {
                            if (f1.isActive != false)
                            {
                                flag = 1;
                                break;
                            }
                            else
                            {
                                flag = 2;
                                break;
                            }

                        }
                    }
                    catch (Exception ex)
                    {  }

                }

                if (flag == 0)
                {
                    f1.isActive = false;
                    CCFieldRepository.SaveFolderFields(f1);
                }

                else if (flag == 2)
                {
                    f1.isActive = true;
                    CCFieldRepository.SaveFolderFields(f1);
                }
            }

            // add field into default group
            var defaultGrp = CCGroupRepository.CCGroups.Where(fid => fid.FolderID == objFolderFields.FolderID & fid.GroupName == "Default" & fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();

            if (defaultGrp != null)
            {
                foreach (var field in resp)
                {
                    long grpFieldOrder = 0;
                    // get group fileds last order
                    var groupFieldOrder = CCGroupFieldRepository.CCGroupsFields.Where(gid => gid.GroupID == defaultGrp.GroupID & gid.AccountGUID == accountObj.AccountGUID).OrderByDescending(grp => grp.FieldOrder).FirstOrDefault();
                    if (groupFieldOrder != null) { grpFieldOrder = groupFieldOrder.FieldOrder; }

                    CCGroupField objgrpfield = new CCGroupField();
                    objgrpfield.FieldID = field.FieldID;
                    objgrpfield.FieldOrder = grpFieldOrder + 1;
                    objgrpfield.FolderID = defaultGrp.FolderID;
                    objgrpfield.GroupID = defaultGrp.GroupID;
                    objgrpfield.AccountGUID = accountObj.AccountGUID;
                    var aa = CCGroupFieldRepository.SaveGroupField(objgrpfield);
                }
            }

            return RedirectToAction("ViewFields", "Folder", new { id = objFolderFields.FolderID });
        }
        public ActionResult SelectFolderFields(long id)
        {
            TempData["SelectedMenu"] = "Manage";
            TempData["SelectedSubMenu"] = "ManageFolders";
            Account accountObj = (Account)Session["account"];
            FolderFieldsSelectModel folderField = new FolderFieldsSelectModel();

            var type = CCFolderRepository.CCFolders.FirstOrDefault(fid => fid.FolderID == id & fid.AccountGUID == accountObj.AccountGUID).Type;

            var savedFields = CCFieldRepository.CCFolderFields.Where(fid => fid.FolderID == id & fid.AccountGUID == accountObj.AccountGUID & fid.isActive == true).Select(fname => fname.FieldCaption).ToList();
            if (type == 1)
            {
                folderField.SelectFolderFieldsSchemas = FieldsConfigHelper.GetFieldForPrimaryConfig();
            }
            else if (type == 2)
            {
                folderField.SelectFolderFieldsSchemas = FieldsConfigHelper.GetFieldForAppointmentConfig();
            }
            else
            { }

            folderField.FolderID = id;
            folderField.SavedFields = savedFields;
            folderField.Type = type;

            return View(folderField);
        }