//public ActionResult Index(SystemAdminDashboardViewModel obj)
        //{
        //    return View(obj);
        //}
        public ActionResult AccountInfo()
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];

            TempData["SelectedMenu"] = "accInfo";

            var model = new SystemAdminDashboardViewModel();

            Session["ErrorSourceFilterList"] = model.ErrorSourceFilterList;
            Session["ErrorTypeFilterList"] = model.ErrorTypeFilterList;

            if (Session["SystemAdminDashboardViewModel"] != null)
            {
                model = (SystemAdminDashboardViewModel)Session["SystemAdminDashboardViewModel"];
                model.ExistingAccounts = CCAccRepository.Accounts.ToList();

            }
            else
            {
                model.ExistingAccounts = CCAccRepository.Accounts.ToList();
            }

            var assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            ViewBag.version = assemblyVersion;
            Session["version"] = assemblyVersion;

            //model.SelectedAccountID = 0;
            return View(model);
        }
        public ActionResult viewMasterErrorLog()
        {
            var model = new SystemAdminDashboardViewModel();

            if (Session["SystemAdminDashboardViewModel"] != null)
            {
                model = (SystemAdminDashboardViewModel)Session["SystemAdminDashboardViewModel"];
                model.ErrorLogList = CCErrorLogRepository.CCErrorLogs.OrderByDescending(e => e.DateTime).Take(200).ToList();
            }
            else
            {
                model.ErrorLogList = CCErrorLogRepository.CCErrorLogs.OrderByDescending(e => e.DateTime).Take(200).ToList();
            }
            model.ErrorLogList = FilteredErrorLogList(model.ErrorLogList, model.ErrorSourceFilterList, model.ErrorTypeFilterList);

            Session["SystemAdminDashboardViewModel"] = model;

            return View(model);
        }
        public ActionResult ViewConnectionDetails(long ConnectionID, long AccID)
        {
            var model = new SystemAdminDashboardViewModel();

            model.SelectedAccountID = AccID;
            var selectedAccount = CCAccRepository.Accounts.Where(acc => acc.ID == model.SelectedAccountID);

            foreach (var acc in selectedAccount)
            {
                model.SelectedConnectionID = ConnectionID;
                model.SelectedConnection = new SelectedConnectionDetails();
                model.SelectedConnection.Connection = CCConnectionRepository.CCSubscriptions.Where(conn => conn.AccountGUID == acc.AccountGUID && conn.ConnectionID == model.SelectedConnectionID).FirstOrDefault();
                model.SelectedConnection.Folder = CCFolderRepository.CCFolders.Where(fold => fold.FolderID == model.SelectedConnection.Connection.FolderID && fold.AccountGUID == acc.AccountGUID).FirstOrDefault();

                model.SelectedConnectionCheckBoxes = new ConnectionCheckBoxToggles();

                if (model.SelectedConnection.Connection.AllowAdditions == true)
                    model.SelectedConnectionCheckBoxes.allowAdditions = "checked";
                if (model.SelectedConnection.Connection.IgnoreExisting == true)
                    model.SelectedConnectionCheckBoxes.ignoreExsisting = "checked";
                if (model.SelectedConnection.Connection.CategoryFilterUsed == true)
                    model.SelectedConnectionCheckBoxes.categoryFilter = "checked";
                if (model.SelectedConnection.Connection.CopyPhotos == true)
                    model.SelectedConnectionCheckBoxes.copyPhotos = "checked";
                if (model.SelectedConnection.Connection.TurnOffReminders == true)
                    model.SelectedConnectionCheckBoxes.turnOffReminders = "checked";
                if (model.SelectedConnection.Connection.tagSubject == true)
                    model.SelectedConnectionCheckBoxes.tagSubject = "checked";
                if (model.SelectedConnection.Connection.IsRunning == true)
                    model.SelectedConnectionCheckBoxes.isRunning = "checked";
                if (model.SelectedConnection.Connection.IsPaused == true)
                    model.SelectedConnectionCheckBoxes.isPaused = "checked";

            }
            return View(model);
        }
        public ActionResult Index()
        {
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];

            var model = new SystemAdminDashboardViewModel();

            model.FolderCount = CCFolderRepository.CCFolders.Count();
            model.ConnectionCount = CCConnectionRepository.CCSubscriptions.Count();
            model.UserCount = CCUserRepository.Users.Count();
            model.AccountCount = CCAccRepository.Accounts.Count();
            model.ItemCount = CCItemRepository.CCContacts.Count();

            DateTime DateStart = DateTime.Now.Date.AddDays(-3);
            DateTime DateEnd = DateTime.Now.Date;

            model.ErrorLogHistoryLimited = new List<CCErrorLog>();
            var query = CCErrorLogRepository.CCErrorLogs.OrderByDescending(x => x.DateTime).Take(100).ToList();
            model.ErrorLogHistoryLimited = CCErrorLogRepository.CCErrorLogs.OrderByDescending(x=>x.DateTime).Take(100).ToList();

            var assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            ViewBag.version = assemblyVersion;
            Session["version"] = assemblyVersion;

            //model.SelectedAccountID = 0;
            return View(model);
        }
        public ActionResult GenerateAccountDetails(SystemAdminDashboardViewModel obj)
        {
            var model = new SystemAdminDashboardViewModel();
            model.SelectedAccountID = obj.SelectedAccountID;
            model.SelectedInformationType = obj.SelectedInformationType;
            var selectedAccount = CCAccRepository.Accounts.Where(acc => acc.ID == model.SelectedAccountID);

            foreach (var acc in selectedAccount)
            {

                if (model.SelectedInformationType == 2) //Error List
                {
                    model.ErrorLogList = CCErrorLogRepository.CCErrorLogs.Where(error => error.AccountGUID == acc.AccountGUID).Take(100).ToList();
                    model.ErrorLogList = FilteredErrorLogList(model.ErrorLogList, model.ErrorSourceFilterList, model.ErrorTypeFilterList);
                    //model.ErrorSourceFilterList = (List<ErrorFilters>)Session["ErrorSourceFilterList"];
                    //model.ErrorTypeFilterList = (List<ErrorFilters>)Session["ErrorTypeFilterList"];
                }
                else if (model.SelectedInformationType == 1) //Folder List
                {
                    model.Folders = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == acc.AccountGUID).ToList();
                    model.FolderList = new List<FolderDetails>();
                    foreach (var folder in model.Folders)
                    {
                        FolderDetails FD = new FolderDetails();
                        FD.FolderName = folder.Name;
                        FD.ItemCount = CCItemRepository.CCContacts.Where(items => items.FolderID == folder.FolderID).Count();
                        model.FolderList.Add(FD);
                    }
                }
                else if (model.SelectedInformationType == 3) //Connection List
                {
                    model.Connections = CCConnectionRepository.CCSubscriptions.Where(conn => conn.AccountGUID == acc.AccountGUID).ToList();
                    model.ConnectionList = new List<ConnectionDetails>();
                    foreach (var conn in model.Connections)
                    {
                        ConnectionDetails CD = new ConnectionDetails();
                        CD.Connection = conn;
                        CD.Folders = CCFolderRepository.CCFolders.Where(fold => fold.FolderID == conn.FolderID && fold.AccountGUID == acc.AccountGUID).FirstOrDefault();
                        model.ConnectionList.Add(CD);
                    }
                }
                else if (model.SelectedInformationType == 4) //Database Usage
                {
                    double TotalTableUsage = 0;
                    double TotalTableUsageForAccount = 0;

                    model.TableUsageList = new List<DBTableUsageDetails>();

                    //Connection Table
                    TotalTableUsage = CCConnectionRepository.CCSubscriptions.Count();
                    TotalTableUsageForAccount = CCConnectionRepository.CCSubscriptions.Where(conn => conn.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblConnectionObj = new DBTableUsageDetails();
                    tblConnectionObj.TableName = "Connections";
                    tblConnectionObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblConnectionObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblConnectionObj);

                    //Credentials Table
                    TotalTableUsage = CCCredentialRepository.Credentials.Count();
                    TotalTableUsageForAccount = CCCredentialRepository.Credentials.Where(cred => cred.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblCredentialsObj = new DBTableUsageDetails();
                    tblCredentialsObj.TableName = "Credentials";
                    tblCredentialsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblCredentialsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblCredentialsObj);

                    //Field Mappings Table
                    TotalTableUsage = CCFieldMappingsRepository.CCFieldsMapping.Count();
                    TotalTableUsageForAccount = CCFieldMappingsRepository.CCFieldsMapping.Where(fieldMappings => fieldMappings.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblFieldMappingsObj = new DBTableUsageDetails();
                    tblFieldMappingsObj.TableName = "FieldMappings";
                    tblFieldMappingsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblFieldMappingsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblFieldMappingsObj);

                    //Field Values Table
                    TotalTableUsage = CCFieldValueRepository.CCFieldValues.Count();
                    TotalTableUsageForAccount = CCFieldValueRepository.CCFieldValues.Where(fieldValues => fieldValues.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblFieldValuesObj = new DBTableUsageDetails();
                    tblFieldValuesObj.TableName = "FieldValues";
                    tblFieldValuesObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblFieldValuesObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblFieldValuesObj);

                    //Folder Fields Table
                    TotalTableUsage = CCFieldValueRepository.CCFieldValues.Count();
                    TotalTableUsageForAccount = CCFieldValueRepository.CCFieldValues.Where(folderFields => folderFields.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblFolderFieldsObj = new DBTableUsageDetails();
                    tblFolderFieldsObj.TableName = "Folder Fields";
                    tblFolderFieldsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblFolderFieldsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblFolderFieldsObj);

                    //Folders Table
                    TotalTableUsage = CCFolderRepository.CCFolders.Count();
                    TotalTableUsageForAccount = CCFolderRepository.CCFolders.Where(folders => folders.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblFoldersObj = new DBTableUsageDetails();
                    tblFoldersObj.TableName = "Folders";
                    tblFoldersObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblFoldersObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblFoldersObj);

                    //Group Fields Table
                    TotalTableUsage = CCGroupFieldRepository.CCGroupsFields.Count();
                    TotalTableUsageForAccount = CCGroupFieldRepository.CCGroupsFields.Where(groupFields => groupFields.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblGroupFieldsObj = new DBTableUsageDetails();
                    tblGroupFieldsObj.TableName = "Group Fields";
                    tblGroupFieldsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblGroupFieldsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblGroupFieldsObj);

                    //Groups Table
                    TotalTableUsage = CCGroupRepository.CCGroups.Count();
                    TotalTableUsageForAccount = CCGroupRepository.CCGroups.Where(groups => groups.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblGroupsObj = new DBTableUsageDetails();
                    tblGroupsObj.TableName = "Groups";
                    tblGroupsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblGroupsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblGroupsObj);

                    //Items Table
                    TotalTableUsage = CCItemRepository.CCContacts.Count();
                    TotalTableUsageForAccount = CCItemRepository.CCContacts.Where(items => items.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblItemsObj = new DBTableUsageDetails();
                    tblItemsObj.TableName = "Items";
                    tblItemsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblItemsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblItemsObj);

                    //Layout Groups Table
                    TotalTableUsage = CCLayoutGroupRepository.CCLayoutGroups.Count();
                    TotalTableUsageForAccount = CCLayoutGroupRepository.CCLayoutGroups.Where(layoutGroup => layoutGroup.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblLayoutGroupsObj = new DBTableUsageDetails();
                    tblLayoutGroupsObj.TableName = "Layout Groups";
                    tblLayoutGroupsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblLayoutGroupsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblLayoutGroupsObj);

                    //Layouts Table
                    TotalTableUsage = CCLayoutRepository.CCLayouts.Count();
                    TotalTableUsageForAccount = CCLayoutRepository.CCLayouts.Where(layouts => layouts.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblLayoutsObj = new DBTableUsageDetails();
                    tblLayoutsObj.TableName = "Layouts";
                    tblLayoutsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblLayoutsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblLayoutsObj);

                    //Notes Table
                    TotalTableUsage = CCNoteRepository.CCNotes.Count();
                    TotalTableUsageForAccount = CCNoteRepository.CCNotes.Where(notes => notes.AccountGUID == acc.AccountGUID).Count();
                    DBTableUsageDetails tblNotesObj = new DBTableUsageDetails();
                    tblNotesObj.TableName = "Notes";
                    tblNotesObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    tblNotesObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    model.TableUsageList.Add(tblNotesObj);

                    ////Sync Fields Table
                    //TotalTableUsage = CCSyncFieldsRepository.CCSyncFields.Count();
                    //TotalTableUsageForAccount = CCSyncFieldsRepository.CCSyncFields.Where(cred => cred.AccountGUID == acc.AccountGUID).Count();
                    //DBTableUsageDetails tblSyncFieldsObj = new DBTableUsageDetails();
                    //tblSyncFieldsObj.TableName = "SyncFields";
                    //tblSyncFieldsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    //tblSyncFieldsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    //model.TableUsageList.Add(tblSyncFieldsObj);

                    ////Sync Items Table
                    //TotalTableUsage = CCSyncItemsRepository.CCSyncItems.Count();
                    //TotalTableUsageForAccount = CCSyncItemsRepository.CCSyncItems.Where(cred => cred.AccountGUID == acc.AccountGUID).Count();
                    //DBTableUsageDetails tblSyncItemsObj = new DBTableUsageDetails();
                    //tblSyncItemsObj.TableName = "SyncItems";
                    //tblSyncItemsObj.TableUsage = Math.Round((TotalTableUsageForAccount / TotalTableUsage) * 100, 2);
                    //tblSyncItemsObj.RecordCount = TotalTableUsageForAccount + " of " + TotalTableUsage;
                    //model.TableUsageList.Add(tblSyncItemsObj);

                }
                Session["SystemAdminDashboardViewModel"] = model;
            }

            return RedirectToAction("AccountInfo", "CorporateContactsAdmin");
        }
        public ActionResult FilterErrorList(long AccountID, string ErrorFilter, long FilterType, string redirectMethod)
        {
            var model = new SystemAdminDashboardViewModel();

            if (Session["SystemAdminDashboardViewModel"] != null)
            {
                model = (SystemAdminDashboardViewModel)Session["SystemAdminDashboardViewModel"];
            }

            if (FilterType == 1)
            {
                //List<ErrorFilters> ErrorSourceFilterList = (List<ErrorFilters>)Session["ErrorSourceFilterList"];
                foreach (var err in model.ErrorSourceFilterList)
                {
                    if (ErrorFilter == err.Error)
                    {
                        if(err.ErrorState == "active")
                            err.ErrorState = "disabled";
                        else
                            err.ErrorState = "active";
                    }
                }
            }
            else if (FilterType == 2)
            {
                //List<ErrorFilters> ErrorTypeFilterList = (List<ErrorFilters>)Session["ErrorTypeFilterList"];
                foreach (var err in model.ErrorTypeFilterList)
                {
                    if (ErrorFilter == err.Error)
                    {
                        if (err.ErrorState == "active")
                            err.ErrorState = "disabled";
                        else
                            err.ErrorState = "active";
                    }
                }
            }

            model.SelectedAccountID = AccountID;
            model.SelectedInformationType = 2;

             var selectedAccount = CCAccRepository.Accounts.Where(acc => acc.ID == model.SelectedAccountID);

             foreach (var acc in selectedAccount)
             {
                 if (model.SelectedInformationType == 2) //Error List
                 {
                     model.ErrorLogList = CCErrorLogRepository.CCErrorLogs.Where(error => error.AccountGUID == acc.AccountGUID).Take(100).ToList();
                     model.ErrorLogList = FilteredErrorLogList(model.ErrorLogList, model.ErrorSourceFilterList, model.ErrorTypeFilterList);
                     //model.ErrorSourceFilterList = (List<ErrorFilters>)Session["ErrorSourceFilterList"];
                     //model.ErrorTypeFilterList = (List<ErrorFilters>)Session["ErrorTypeFilterList"];
                 }
             }

            Session["SystemAdminDashboardViewModel"] = model;

            if (redirectMethod == "index")
                return RedirectToAction("Index", "CorporateContactsAdmin");
            else
                return RedirectToAction("viewMasterErrorLog", "CorporateContactsAdmin");
        }