public static async Task<bool> IsBlockedAsync(this form f)
 {
     using (var repo = new Repository())
     {
         var formIsBlocked = false;
         switch ((FormType) f.form_type_id)
         {
             case FormType.Edu:
             case FormType.OtherEdu:
                 {
                     var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                     var form = await repo.GetForm(f.form_id);
                     if (await form.TimeRemainingAsync() == TimeSpan.Zero)
                     {
                         formIsBlocked = form.blocked_edus.Contains(edu);
                     }
                     break;
                 }
             case FormType.Municipality:
             case FormType.OtherMunicipality:
                 {
                     var munit = await repo.GetMunicipality(Authentication.Credentials.MunitId);
                     var form = await repo.GetForm(f.form_id);
                     if (await form.TimeRemainingAsync() == TimeSpan.Zero)
                     {
                         formIsBlocked = form.blocked_municipalities.Contains(munit);
                     }
                     break;
                 }
         }
         return formIsBlocked;
     }
 }
 public static async void NotifyAboutForms(Credentials user)
 {
     using (var repo = new Repository())
     {
         var formList = new List<form>();
         if (user.IsEdu)
         {
             var kind = (await repo.GetEdu(user.EduId)).edu_kind;
             formList = kind.forms.ToList();
         } 
         else if (user.IsMunicipality)
         {
             formList = await repo.GetMunicipalityForms();
         }
         else if (user.IsRegion)
         {
             formList = await repo.GetRegionForms();
         }
         var now = await repo.GetNowAsync();
         foreach (var form in formList)
         {
             IForm uploadedForm = null;
             if (user.IsEdu)
                 uploadedForm = await repo.FindEduFormData(form.form_id, user.EduId, now.Year);
             else if (user.IsMunicipality)
                 uploadedForm = await repo.FindMunicipalityFormData(form.form_id, user.MunitId, now.Year);
             else if (user.IsRegion)
                 uploadedForm = await repo.FindRegionFormData(form.form_id, user.RegionId, now.Year);
             if (uploadedForm != null)
                 continue;
             var timeRemaining = await form.TimeRemainingAsync();
             if (timeRemaining == TimeSpan.Zero || timeRemaining.Days >= 30) 
                 continue;
             var warn = new Notifications.ExpireWarn()
             {
                 Now = now,
                 TimeRemaining = timeRemaining,
                 FormName = form.name,
                 FormId = form.form_id
             };
             Notifications.Add(warn);
         }
     }
 }
        private async void windowsUIViewMain_QueryControl(object sender, QueryControlEventArgs e)
        {
            try
            {
                var queryReportRegex = new Regex(@"QueryReport(?<idx>\d+)");
                var queryReportRegex2 = new Regex(@"QueryReport-2(?<idx>\d+)");
                var queryReportMatch = queryReportRegex.Match(e.Document.ControlName);
                var queryReportMatch2 = queryReportRegex2.Match(e.Document.ControlName);
                if (queryReportMatch.Success)
                {
                    try
                    {
                        var idx = int.Parse(queryReportMatch.Groups["idx"].Value);
                        e.Control = new XtraReportViewControl(windowsUIViewMain) {Report = ReportFactory.Reports[idx]};
                        return;
                    }
                    catch
                    {
                        throw new KeyNotFoundException("Отчет не найден");
                    }
                }
                if (queryReportMatch2.Success)
                {
                    try
                    {
                        var idx = int.Parse(queryReportMatch2.Groups["idx"].Value);
                        e.Control = new XtraReportViewControl2(windowsUIViewMain) { ReportData = ReportFactory2.Reports[idx] };
                        return;
                    }
                    catch
                    {
                        throw new KeyNotFoundException("Отчет не найден");
                    }
                }
                switch (e.Document.ControlName)
                {
                    case "Questionary":
                        {
                            //XtraDictionaryQuestionaryControl questionaryControl = new XtraDictionaryQuestionaryControl(this.windowsUIViewMain);
                            //int? questionaryId = TagHelper.GetQuestionaryId(e.Document.Tag.ToString());
                            //if (questionaryId.HasValue)
                            //    questionaryControl.CurrentQuestionary = RepoHelper.Repo.GetById<Questionary>(questionaryId.Value);
                            //e.Control = questionaryControl;
                            break;
                        }
                    case "QueriesEdu":
                        {
                            var repo = new Repository();
                            var queryControl = new XtraQueryEduControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetFormsHaveQueries(FormType.Edu, FormType.OtherEdu),
                                YearDataSource = await repo.GetAvailableYears(),
                                QueryPartHeadDataSource = await repo.GetQueriesHeadParts()
                            };
                            e.Control = queryControl;
                            break;
                        }
                    case "QueriesMunicipality":
                        {
                            var repo = new Repository();
                            var queryControl = new XtraQueryMunicipalityControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetFormsHaveQueries(FormType.Municipality, FormType.OtherMunicipality),
                                YearDataSource = await repo.GetAvailableYears(),
                                QueryPartHeadDataSource = await repo.GetQueriesHeadParts()
                            };
                            e.Control = queryControl;
                            break;
                        }
                    case "QueriesRegion":
                        {
                            var repo = new Repository();
                            var queryControl = new XtraQueryRegionControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetFormsHaveQueries(FormType.Region, FormType.OtherRegion),
                                YearDataSource = await repo.GetAvailableYears(),
                                QueryPartHeadDataSource = await repo.GetQueriesHeadParts()
                            };
                            e.Control = queryControl;
                            break;
                        }
                    case "AlignData":
                        {
                            var repo = new Repository();
                            var formDataId = (int) RuntimeSettings.Current["FormDataId"];
                            var formType = (FormType) RuntimeSettings.Current["FormTypeId"];
                            var currentFormData = await repo.GetFormDataById(formDataId, formType);
                            var currentForm = currentFormData.form;
                            var previousData = await repo.FindYesteryearFormData(currentFormData);

                            var alignDataControl = new XtraAlignDataControl(windowsUIViewMain, repo)
                            {
                                PreviousFormData = previousData,
                                CurrentFormData = currentFormData
                            };
                            e.Control = alignDataControl;
                            await alignDataControl.Process(currentForm);
                            break;
                        }
                    case "DictAlignData":
                        {
                            var repo = new Repository();
                            var alignDataControl = new XtraDictionaryAlignDataFormulaControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetForms()
                            };
                            e.Control = alignDataControl;
                            break;
                        }
                    case "DictCreateQuery":
                        {
                            var repo = new Repository();
                            var createQueryControl = new XtraCreateQueryControl(windowsUIViewMain, repo)
                            {
                                QueryAutocomletePartSource = await repo.GetQueriesAutocompleteParts(),
                                QueryHeadPartSource = await repo.GetQueriesHeadParts()
                            };
                            if (Authentication.Credentials.IsRegion)
                            {
                                createQueryControl.FormsWithTemplatesDataSource = 
                                    await repo.GetFormsWithTemplates();
                            }
                            else if (Authentication.Credentials.IsMunicipality)
                            {
                                createQueryControl.FormsWithTemplatesDataSource =
                                    await repo.GetFormsWithTemplatesForMunicipality(Authentication.Credentials.MunitId);
                            }
                            else if (Authentication.Credentials.IsEdu)
                            {
                                createQueryControl.FormsWithTemplatesDataSource =
                                    await repo.GetFormsWithTemplatesForEdu(Authentication.Credentials.EduId);
                            }
                            e.Control = createQueryControl;
                            break;
                        }
                    case "DictCredentials":
                    {
                        var repo = new Repository();
                        var dictCredControl = new XtraDictCredentials(windowsUIViewMain, repo)
                        {
                            CredentialsDataSource = await repo.GetCredential(),
                            EduDataSource = await repo.GetEdus(),
                            MunicipalityDataSource = await repo.GetMunicipalities(),
                            RegionDataSource = await repo.GetRegions()
                        };
                        e.Control = dictCredControl;
                        break;
                    }
                    case "DictEdu":
                        {
                            var repo = new Repository();
                            var dictionaryEduControl =
                                new XtraDictionaryEduControl(windowsUIViewMain, repo)
                                {
                                    EduDataSource = await repo.GetEdus(),
                                    MunicipalityDataSource = await repo.GetMunicipalities(),
                                    DataSourceEduKind = await repo.GetEduKinds()
                                };

                            e.Control = dictionaryEduControl;
                            break;
                        }
                    case "DictMunicipality":
                        {
                            var repo = new Repository();
                            var dictionaryMunitControl =
                                new XtraDictionaryMunitControl(windowsUIViewMain, repo)
                                {
                                    MunicipalityDataSource = await repo.GetMunicipalities()
                                };
                            e.Control = dictionaryMunitControl;
                            break;
                        }
                    case "DictTemplate":
                        {
                            var repo = new Repository();
                            var templateControl = new XtraDictionaryTemplateControl(windowsUIViewMain, repo)
                            {
                                FormsWithTemplatesDataSource = await repo.GetFormsWithTemplates(),
                                FormsWithoutTemplatesDataSource = await repo.GetFormsWithoutTemplates()
                            };
                            e.Control = templateControl;
                            break;
                        }
                    case "DictForms":
                        {
                            var repo = new Repository();
                            var dictionaryFormControl = new XtraDictionaryFormsControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetForms(),
                                DataSourceEduKind = await repo.GetEduKinds(),
                                FormTypeDataSource = await repo.GetFormTypes(),
                                MunicipalityDataSource = await repo.GetMunicipalities()
                            };

                            e.Control = dictionaryFormControl;
                            break;
                        }
                    case "DictMunicipalityFormula":
                        {
                            var repo = new Repository();
                            var dictionaryMunicipalityFormulaControl = new XtraDictionarySummaryFormulaControl(windowsUIViewMain, repo);
                            e.Control = dictionaryMunicipalityFormulaControl;
                            dictionaryMunicipalityFormulaControl.RegularFormDataSource = await repo.GetForms();
                            dictionaryMunicipalityFormulaControl.SummaryFormDataSource = await repo.GetForms();
                            break;
                        }
                    case "ArchiveFormData6":
                    case "FormData6": //допформа
                    case "ArchiveFormData5":
                    case "FormData5": //форма региона
                        {
                            var dataId = TagHelper.GetFormDataId(e.Document.Tag.ToString());
                            if (dataId.HasValue)
                            {
                                var repo = new Repository();
                                var fd = await repo.GetRegionFormDataById(dataId.Value);
                                var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                                {
                                    FormDataSource = await repo.GetRegionForms(),
                                    FormData = fd,
                                    FormStatus = (Status)fd.status,
                                    Source = XtraSpreadsheet.FormSource.File,
                                    Mode = XtraSpreadsheet.ControlMode.Edit,
                                    ActiveForm = fd.form
                                };
                                sheetControl.LoadDocument();

                                e.Control = sheetControl;
                            }
                            else
                            {
                                e.Control = new Control();
                            }
                            break;
                        }
                    case "ArchiveFormData4":
                    case "FormData4": //допформа
                    case "ArchiveFormData3":
                    case "FormData3": //форма муниципалитета
                        {
                            var dataId = TagHelper.GetFormDataId(e.Document.Tag.ToString());
                            if (dataId.HasValue)
                            {
                                var repo = new Repository();
                                var fd = await repo.GetMunitFormDataById(dataId.Value);
                                var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                                {
                                    FormDataSource = await repo.GetMunicipalityForms(),
                                    FormData = fd,
                                    FormStatus = (Status)fd.status,
                                    Source = XtraSpreadsheet.FormSource.File,
                                    Mode = await fd.form.IsBlockedAsync() ? XtraSpreadsheet.ControlMode.Disabled : XtraSpreadsheet.ControlMode.Edit,
                                    ActiveForm = fd.form
                                };
                                sheetControl.LoadDocument();

                                e.Control = sheetControl;
                            }
                            else
                            {
                                e.Control = new Control();
                            }
                            break;
                        }
                    case "ArchiveFormData1":
                    case "ArchiveFormData2":
                    case "FormData2": //допформа
                    case "FormData1": //форма организации
                        {
                            var dataId = TagHelper.GetFormDataId(e.Document.Tag.ToString());
                            if (dataId.HasValue)
                            {
                                var repo = new Repository();
                                var fd = await repo.GetEduFormDataById(dataId.Value);
                                var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                                {
                                    FormData = fd,
                                    FormStatus = (Status)fd.status,
                                    Source = XtraSpreadsheet.FormSource.File,
                                    Mode = await fd.form.IsBlockedAsync() ? XtraSpreadsheet.ControlMode.Disabled : XtraSpreadsheet.ControlMode.Edit
                                };
                                if (Authentication.Credentials.IsEdu)
                                {
                                    var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                                    if (edu.edu_kind_id.HasValue)
                                        sheetControl.FormDataSource = await repo.GetFormsByEduKind(edu.edu_kind_id.Value);
                                }
                                else
                                {
                                    sheetControl.FormDataSource = await repo.GetForms();
                                }
                                sheetControl.ActiveForm = fd.form;
                                sheetControl.LoadDocument();

                                e.Control = sheetControl;
                            }
                            else
                            {
                                e.Control = new Control();
                            }
                            break;
                        }
                    case "XtraProgress":
                        e.Control = new XtraProgress();
                        break;
                    case "Authentication":
                        e.Control = _authForm;
                        break;
                    case "Passport":
                        {
                            var repo = new Repository();
                            if (Authentication.Credentials.IsEdu)
                            {
                                var passportControl = new XtraDictionaryEduPassportControl(windowsUIViewMain, repo)
                                {
                                    MunicipalityDataSource = await repo.GetMunicipalities(),
                                    ActivityTypeDataSource = await repo.GetActivityTypes(),
                                    EduStatusDataSource = await repo.GetEduStatuses(),
                                    EduTypeDataSource = await repo.GetEduTypes(),
                                    DataSourceEduKind = await repo.GetEduKinds(),
                                    ManagementAgencyDataSource = await repo.GetManagementAgencies(),
                                    ManagementAgencyActivityDataSource = await repo.GetManagementAgencyActivities(),
                                    OwnershipTypeDataSource = await repo.GetOwnershipTypes(),
                                    Edu = await repo.GetEdu(Authentication.Credentials.EduId)
                                };
                                e.Control = passportControl;
                            }
                            else if(Authentication.Credentials.IsMunicipality)
                            {
                                var passportControl = new XtraDictionaryMunicipalityPassportControl(windowsUIViewMain, repo)
                                {
                                    Municipality = await repo.GetMunicipality(Authentication.Credentials.MunitId)
                                };
                                e.Control = passportControl;
                            }
                            break;
                        }
                    case "UploadSheetFromFile":
                        {
                            var repo = new Repository();
                            var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                            {
                                Source = XtraSpreadsheet.FormSource.File,
                                Mode = XtraSpreadsheet.ControlMode.New
                            };
                            var forms = new List<form>();
                            if (Authentication.Credentials.IsEdu)
                            {
                                var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                                forms = await repo.GetFormsByEduKind(edu.edu_kind.edu_kind_id);
                            }
                            else if (Authentication.Credentials.IsMunicipality)
                            {
                                forms = await repo.GetMunicipalityForms();
                            }
                            else if (Authentication.Credentials.IsRegion)
                            {
                                forms = await repo.GetForms();
                            }
                            sheetControl.FormDataSource = forms;
                            sheetControl.ActiveForm = forms.FirstOrDefault();
                            e.Control = sheetControl;
                            break;
                        }
                    case "UploadSheetFromTemplate":
                        {
                            var repo = new Repository();
                            var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                            {
                                Source = XtraSpreadsheet.FormSource.Template,
                                Mode = XtraSpreadsheet.ControlMode.New
                            };
                            var forms = new List<form>();
                            if (Authentication.Credentials.IsEdu)
                            {
                                var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                                forms = await repo.GetFormsByEduKind(edu.edu_kind.edu_kind_id);
                            }
                            else if (Authentication.Credentials.IsMunicipality)
                            {
                                forms = await repo.GetFormsByType(false, FormType.Municipality, FormType.OtherMunicipality, FormType.Edu, FormType.OtherEdu);
                            }
                            else if (Authentication.Credentials.IsRegion)
                            {
                                forms = await repo.GetForms();
                            }
                            sheetControl.FormDataSource = forms;
                            sheetControl.ActiveForm = forms.FirstOrDefault();
                            e.Control = sheetControl;
                            break;
                        }
                    case "RequestQuery":
                        {
                            var repo = new Repository();
                            var messageControl = new XtraMessageControl(windowsUIViewMain, repo)
                            {
                                MessageType = XtraMessageControl.MessageTypeEnum.RequestForQuery,
                                FormDataSource = await repo.GetForms()
                            };

                            e.Control = messageControl;
                            break;
                        }
                    case "BugReport":
                        {
                            var repo = new Repository();
                            var messageControl = new XtraMessageControl(windowsUIViewMain, repo)
                            {
                                MessageType = XtraMessageControl.MessageTypeEnum.BugReport,
                                FormDataSource = await repo.GetForms()
                            }; e.Control = messageControl;
                            break;
                        }
                    case "MessageList":
                        {
                            var repo = new Repository();
                            var messageListControl = new XtraMessageListControl(windowsUIViewMain, repo)
                            {
                                DataSourceMessage = await repo.GetMessages()
                            };
                            e.Control = messageListControl;
                            break;
                        }
                    case "FormDetailedStatisticsReport":
                        {
                            var doc = windowsUIViewMain.Documents.FindFirst(t => t.ControlName == "FormDetailedStatistics");
                            var formStatData = new List<t_detailed_form_statistics>();
                            if (doc != null)
                            {
                                formStatData = (List<t_detailed_form_statistics>)doc.Tag;
                            }
                            var formStatisticReport = new XtraDetailedFormStatisticReport();
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain);
                            formStatisticReport.FormStatisticDataSource = formStatData;
                            formStatisticReportControl.Report = formStatisticReport;
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "FormDetailedStatistics":
                        {
                            var repo = new Repository();
                            var detailedFormStatisticsReport = new XtraFormDetailedStatisticControl(windowsUIViewMain, repo);
                            e.Control = detailedFormStatisticsReport;
                            detailedFormStatisticsReport.FormStatisticDataSource =
                                Authentication.Credentials.IsMunicipality
                                    ? await repo.GetDetailedFormStatistics(Authentication.Credentials.MunitId, DateTime.Now.Year)
                                    : await repo.GetDetailedFormStatistics(DateTime.Now.Year);
                            detailedFormStatisticsReport.YearsDataSource = await repo.GetAvailableYears();
                            break;
                        }
                    case "FormStatistics":
                        using (var repo = new Repository())
                        {
                            var formStatisticReport = new XtraFormStatisticReport
                            {
                                FormStatisticDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetFormStatistics().Result
                            };
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain)
                            {
                                Report = formStatisticReport
                            };
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "MunicipalityFormStatistics":
                        using (var repo = new Repository())
                        {
                            var formStatisticReport = new XtraMunicipalityFormStatisticReport //не ясно почему вылетает deferredloadexception, если использовать await
                            {
                                FormStatisticDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetMunicipalityFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetMunicipalityFormStatistics().Result
                            };
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain)
                            {
                                Report = formStatisticReport
                            };
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "MunicipalityFormDetailedStatisticsReport":
                        {
                            var doc = windowsUIViewMain.Documents.FindFirst(t => t.ControlName == "MunicipalityFormDetailedStatistics");
                            var formStatData = new List<t_detailed_municipality_form_statistics>();
                            if (doc != null)
                            {
                                formStatData = (List<t_detailed_municipality_form_statistics>)doc.Tag;
                            }
                            var formStatisticReport = new XtraDetailedMunicipalityFormStatisticReport();
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain);

                            formStatisticReport.FormStatisticDataSource = formStatData;
                            formStatisticReportControl.Report = formStatisticReport;
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "MunicipalityFormDetailedStatistics":
                        {
                            var repo = new Repository();
                            var detailedFormStatisticsReport = new XtraMunicipalityFormDetailedStatisticControl(windowsUIViewMain, repo)
                            {
                                FormStatisticDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetDetailedMunicipalityFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetDetailedMunicipalityFormStatistics().Result,
                                YearsDataSource = await repo.GetAvailableYears()
                            };

                            e.Control = detailedFormStatisticsReport;
                            break;
                        }
                    case "FormStatisticsSummary":
                        using (var repo = new Repository())
                        {
                            var formStatisticReport = new XtraFormStatisticSummaryReport
                            {
                                FormStatisticSummaryDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetSummaryFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetSummaryFormStatistics().Result
                            };
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain)
                            {
                                Report = formStatisticReport
                            };
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "SignInOutLog":
                        {
                            var repo = new Repository();
                            var signInOutReport = new XtraSignInOutReport {Repo = repo};
                            var signInOutReportControl = new XtraReportViewControl(windowsUIViewMain);

                            var startDate = DateTime.Now.AddDays(-30);
                            var endDate = DateTime.Now;

                            var logs = await repo.GetLoginLogs(startDate, endDate);
                            signInOutReport.SignInOutLogDataSource = logs;
                            signInOutReportControl.Report = signInOutReport;
                            signInOutReport.CreateDocument();

                            e.Control = signInOutReportControl;
                            break;
                        }
                    case "ExportReport":
                        {
                            var exportControl = new XtraExportReportControl();
                            e.Control = exportControl;
                            break;
                        }
                    default:
                        e.Control = new Control();
                        break;
                }
            }
            catch (Exception ex)
            {
                GuiUtility.ShowFlyoutMessage(windowsUIViewMain, flyoutMessage, "Ошибка", ex.Message + "\n" + ex.InnerException ?? ex.InnerException.Message, FlyoutCommand.OK);
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                e.Control = new Control();
            }
        }
        private async void TriggerControlsWithPrivilegies(Credentials user)
        {
            using (var repo = new Repository())
            {
                tilePassport.Tag = user.IsEdu ? user.EduId : (user.IsMunicipality ? user.MunitId : 0);

                var syncCtx = SynchronizationContext.Current;
                var view = windowsUIViewMain;
                if (user.IsRegion || user.IsMinistry)
                {
                    tileForms.Click += (sender, args) =>
                        Actions.LoadMunicipalityLayerAction.Invoke(sender, args, view, false, FormType.Edu, syncCtx);
                    tileArchiveForms.Click += (sender, args) =>
                        Actions.LoadMunicipalityLayerAction.Invoke(sender, args, view, true, FormType.Edu, syncCtx);
                    tileAdditionalForms.Click += (sender, args) => 
                        Actions.LoadMunicipalityLayerAction.Invoke(sender, args, view, false, FormType.OtherEdu, syncCtx);
                    tileArchiveAdditionalForms.Click += (sender, args) => 
                        Actions.LoadMunicipalityLayerAction.Invoke(sender, args, view, true, FormType.OtherEdu, syncCtx);
                    tileMunicipalityForms.Click += (sender, args) => 
                        Actions.LoadMunicipalityMunicipalityLayerAction.Invoke(sender, args, view, false, FormType.Municipality, syncCtx);
                    tileMunicipalityArchiveForms.Click += (sender, args) => 
                        Actions.LoadMunicipalityMunicipalityLayerAction.Invoke(sender, args, view, true, FormType.Municipality, syncCtx);
                    tileMunicipalityAdditionalForms.Click += (sender, args) => 
                        Actions.LoadMunicipalityMunicipalityLayerAction.Invoke(sender, args, view, false, FormType.OtherMunicipality, syncCtx);
                    tileMunicipalityArchiveAdditionalForms.Click += (sender, args) => 
                        Actions.LoadMunicipalityMunicipalityLayerAction.Invoke(sender, args, view, true, FormType.OtherMunicipality, syncCtx);

                    var regionAction = Actions.LoadRegionsLayerAction;
                    if (user.IsRegion)
                    {
                        regionAction = Actions.LoadRegionFormsLayerAction;
                    }
                    tileRegionForms.Click += (sender, args) => 
                        regionAction.Invoke(sender, args, view, false, FormType.Region, syncCtx);
                    tileRegionAdditionalForms.Click += (sender, args) => 
                        regionAction.Invoke(sender, args, view, false, FormType.OtherRegion, syncCtx);
                    tileRegionArchiveForms.Click += (sender, args) => 
                        regionAction.Invoke(sender, args, view, true, FormType.Region, syncCtx);
                    tileRegionArchiveAdditionalForms.Click += (sender, args) => 
                        regionAction.Invoke(sender, args, view, true, FormType.OtherRegion, syncCtx);
                }
                else if (user.IsMunicipality)
                {
                    var munit = await repo.GetMunicipality(user.MunitId);
                    tileMunicipalityForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit);
                    tileMunicipalityForms.Click +=
                        (sender, args) =>
                            Actions.LoadMunicipalityFormsLayerAction.Invoke(sender, args, view, false,
                                FormType.Municipality, syncCtx);
                    tileMunicipalityArchiveForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit, "archive");
                    tileMunicipalityArchiveForms.Click +=
                        (sender, args) =>
                            Actions.LoadMunicipalityFormsLayerAction.Invoke(sender, args, view, true,
                                FormType.Municipality, syncCtx);

                    tileMunicipalityAdditionalForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit);
                    tileMunicipalityAdditionalForms.Click +=
                        (sender, args) =>
                            Actions.LoadMunicipalityFormsLayerAction.Invoke(sender, args, view, false,
                                FormType.OtherMunicipality, syncCtx);
                    tileMunicipalityArchiveAdditionalForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit, "archive");
                    tileMunicipalityArchiveAdditionalForms.Click +=
                        (sender, args) =>
                            Actions.LoadMunicipalityFormsLayerAction.Invoke(sender, args, view, true,
                                FormType.OtherMunicipality, syncCtx);

                    tileForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit);
                    tileForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduKindLayerAction.Invoke(sender, args, view, false, FormType.Edu,
                                syncCtx);
                    tileArchiveForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit, "archive");
                    tileArchiveForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduKindLayerAction.Invoke(sender, args, view, true, FormType.Edu,
                                syncCtx);

                    tileAdditionalForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit);
                    tileAdditionalForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduKindLayerAction.Invoke(sender, args, view, false, FormType.OtherEdu,
                                syncCtx);
                    tileArchiveAdditionalForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, munit,
                        "archive");
                    tileArchiveAdditionalForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduKindLayerAction.Invoke(sender, args, view, true, FormType.OtherEdu,
                                syncCtx);

                    pageGroupQueries.Items.Clear();
                    pageGroupQueries.Items.Add(documentQueriesEdu);

                    tabbedGroupStatistics.Items.Remove(documentMunicipalityFormStatistics);
                    tabbedGroupStatistics.Items.Remove(documentMunicipalityFormDetailedStatistics);
                }
                else if (user.IsEdu)
                {
                    var edu = await repo.GetEdu(user.EduId);
                    tileForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, edu);
                    tileForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduFormsLayerAction.Invoke(sender, args, view, false, FormType.Edu,
                                syncCtx);
                    tileArchiveForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, edu, "archive");
                    tileArchiveForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduFormsLayerAction.Invoke(sender, args, view, true, FormType.Edu,
                                syncCtx);

                    tileAdditionalForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, edu);
                    tileAdditionalForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduFormsLayerAction.Invoke(sender, args, view, false, FormType.OtherEdu,
                                syncCtx);
                    tileArchiveAdditionalForms.Tag = TagHelper.GetTag(TagHelper.TagType.Tile, edu, "archive");
                    tileArchiveAdditionalForms.Click +=
                        (sender, args) =>
                            Actions.LoadEduFormsLayerAction.Invoke(sender, args, view, true, FormType.OtherEdu,
                                syncCtx);
                    pageGroupQueries.Items.Clear();
                }

                if (user.AccessRights.HasFlag(AccessRights.EduForms))
                {
                    tileContainerMain.Items.Add(tileForms);
                    tileContainerMain.Items.Add(tileArchiveForms);
                    tileContainerMain.Items.Add(tileAdditionalForms);
                    tileContainerMain.Items.Add(tileArchiveAdditionalForms);
                }
                if (user.AccessRights.HasFlag(AccessRights.MunitForms))
                {
                    tileContainerMain.Items.Add(tileMunicipalityForms);
                    tileContainerMain.Items.Add(tileMunicipalityArchiveForms);
                    tileContainerMain.Items.Add(tileMunicipalityAdditionalForms);
                    tileContainerMain.Items.Add(tileMunicipalityArchiveAdditionalForms);
                }
                if (user.AccessRights.HasFlag(AccessRights.RegionForms))
                {
                    tileContainerMain.Items.Add(tileRegionForms);
                    tileContainerMain.Items.Add(tileRegionArchiveForms);
                    tileContainerMain.Items.Add(tileRegionAdditionalForms);
                    tileContainerMain.Items.Add(tileRegionArchiveAdditionalForms);
                }
                if (user.AccessRights.HasFlag(AccessRights.Statistics))
                {
                    tileContainerMain.Items.Add(tileFormStatistics);
                }
                if (user.AccessRights.HasFlag(AccessRights.UploadForm))
                {
                    tileContainerMain.Items.Add(tileSpreadsheet);
                }
                if (user.AccessRights.HasFlag(AccessRights.Dictionaries))
                {
                    tileContainerMain.Items.Add(tileDictionaries);
                    tileContainerMain.Items.Add(tileMessageList);
                }
                if (user.AccessRights.HasFlag(AccessRights.Queries))
                {
                    tileContainerMain.Items.Add(tileQueries);
                }
                if (user.AccessRights.HasFlag(AccessRights.CreateQueries))
                {
                    tileContainerMain.Items.Add(tileCreateQuery);
                }
                if (user.AccessRights.HasFlag(AccessRights.Passport))
                {
                    tileContainerMain.Items.Add(tilePassport);
                }
                if (user.AccessRights.HasFlag(AccessRights.SendMessage))
                {
                    tileContainerMain.Items.Add(tileMessage);
                }
                
                if (user.IsEdu)
                {
                    tileForms.Tag = string.Format("Forms{0}", (int) FormType.Edu);
                    tileAdditionalForms.Tag = string.Format("Forms{0}", (int) FormType.OtherEdu);
                }
                var items = new BaseTile[tileContainerMain.Items.Count];
                tileContainerMain.Items.CopyTo(items, 0);
                tileContainerMain.Items.Clear();
                tileContainerMain.Items.AddRange(items.OrderBy(t => t.Group));
                
                labelLoggedUser.Text = user.Name;
                labelLoggedUser.Cursor = Cursors.Hand;
                if (user.IsEdu)
                {
                    labelLoggedUser.Click += (s, e) =>
                    {
                        windowsUIViewMain.ActivateContainer(pagePassport);
                    };
                }
                GuiUtility.NotifyAboutForms(user);
                if (Notifications.List.Count > 0)
                    flyoutPanelNotifications.ShowPopup();
                labelLoggedUser.Text = Authentication.Credentials.Name;

                ((HostForm)MdiParent).BarVisible = Authentication.Credentials.IsRegion;
            }
        }