//removes a patient request from the db and refreshes ItemsSource of the DataGrid
        private async void btnDeleteRequest_Click(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("Вы уверены, что хотите удалить данное обращение?", "Внимание!",
                                         MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                var currentRequest = dgRequests.SelectedItem as Request;
                if (currentRequest != null)
                {
                    try
                    {
                        ClinicDataRepository clinicRepo = new ClinicDataRepository();
                        clinicRepo.DeleteRequest(currentRequest.RequestId);
                        dgRequests.ItemsSource = await clinicRepo.GetPatientRequests(currentRequest.Patient.Id);
                    }
                    catch (SqlException)
                    {
                        var err = ConfigurationSettings.AppSettings["dbError"].ToString();
                        MessageBox.Show(err, "Ошибка");
                        throw;
                    }
                    catch
                    {
                        MessageBox.Show("Что то пошло не так, приложение будет закрыто", "Ошибка");
                        throw;
                    }
                }
            }
        }
        //gets all requests from the current patient and loads it to dgRequests.ItemSource
        private async void btnPatientRequests_Click(object sender, RoutedEventArgs e)
        {
            var currentCard = dgPatients.SelectedItem as PatientCard;

            if (currentCard != null)
            {
                //in order to see the requests we are switching DataGrids here
                ToggleGridViews();
                ClinicDataRepository clinicRepo = new ClinicDataRepository();
                try
                {
                    dgRequests.ItemsSource = await clinicRepo.GetPatientRequests(currentCard.Id);

                    lblPatientName.Content = currentCard.Name;
                }
                catch (SqlException)
                {
                    var err = ConfigurationSettings.AppSettings["dbError"].ToString();
                    MessageBox.Show(err, "Ошибка");
                    throw;
                }
                catch
                {
                    MessageBox.Show("Что то пошло не так, приложение будет закрыто", "Ошибка");
                    throw;
                }
            }
        }
        //loads DataGrids asynchronously.
        private async void InitializeDataGrids()
        {
            //the cts is cancelling async animation task after we are done here
            CancellationTokenSource cts = new CancellationTokenSource();

            if (isInitializingFirstTime)
            {
                dgPatients.Visibility = Visibility.Collapsed;
                lblLoading.Visibility = Visibility.Visible;
            }
            try
            {
                Animation(cts.Token);
                ClinicDataRepository clinicRepo = new ClinicDataRepository();
                dgPatients.ItemsSource = await clinicRepo.GetPatientCards();

                //after adding a new request or after modifying one,
                //we should refresh the DataGrid to see the changes
                if (tempCard != null)
                {
                    dgRequests.ItemsSource = await clinicRepo.GetPatientRequests(tempCard.Id);
                }
            }
            catch (SqlException)
            {
                var err = ConfigurationSettings.AppSettings["dbError"].ToString();
                MessageBox.Show(err, "Ошибка");
                throw;
            }
            catch
            {
                MessageBox.Show("Что то пошло не так, приложение будет закрыто", "Ошибка");
                throw;
            }

            if (isInitializingFirstTime)
            {
                cts.Cancel();
                lblLoading.Content    = "";
                lblLoading.Visibility = Visibility.Collapsed;
                dgPatients.Visibility = Visibility.Visible;
                //don't invoke the loading loop next time
                isInitializingFirstTime = false;
            }
        }