private void dataTeamsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.Match.MatchId != 0)
            {
                FrmAddTeam dataAddTeam = new FrmAddTeam(this.HomeClub.ClubId, this.GuestClub.ClubId);
                dataAddTeam.ShowDialog();

                if (dataAddTeam.TeamsSelected)
                {
                    Thread loadingThread = new Thread(new ThreadStart(showLoadingForm));
                    loadingThread.Start();

                    this.HomeClub = dataAddTeam.HomeTeam;
                    this.GuestClub = dataAddTeam.GuestTeam;

                    this.HomeTeam = new Team(this.Match.MatchId, this.HomeClub.ClubId);
                    this.GuestTeam = new Team(this.Match.MatchId, this.GuestClub.ClubId);
                    this.HomeTeam.JerseyColor = this.HomeClub.JerseyColorHome;
                    this.GuestTeam.JerseyColor = this.GuestClub.JerseyColorGuest;

                    this.lblTeamA.Text = HomeClub.Name;
                    this.lblTeamB.Text = GuestClub.Name;

                    if (dataAddTeam.TeamSelectionChanged)
                    {
                        this.HomePlayers = new BindingList<Player>();
                        this.GuestPlayers = new BindingList<Player>();

                        this.HomeTeamPlayers = new BindingList<TeamPlayer>();
                        this.GuestTeamPlayers = new BindingList<TeamPlayer>();

                        dgvHomeTeam.DataSource = HomeTeamPlayers;
                        dgvHomeTeam.Refresh();
                        dgvGuestTeam.DataSource = GuestTeamPlayers;
                        dgvGuestTeam.Refresh();

                        this.HomePlays = new BindingList<Play>();
                        this.GuestPlays = new BindingList<Play>();

                        this.HomeClubOfficials = new BindingList<ClubOfficial>();
                        this.GuestClubOfficials = new BindingList<ClubOfficial>();

                        this.HomeTeamOfficials = new BindingList<TeamOfficial>();
                        this.GuestTeamOfficials = new BindingList<TeamOfficial>();

                        dgvHomeOfficials.DataSource = this.HomeTeamOfficials;
                        dgvHomeOfficials.Refresh();
                        dgvGuestOfficials.DataSource = this.GuestTeamOfficials;
                        dgvGuestOfficials.Refresh();

                        this.HomeManages = new BindingList<Manage>();
                        this.GuestManages = new BindingList<Manage>();

                        using (var db = new MatchReporterEntities())
                        {
                            List<Team> previousTeams = new List<Team>(db.Team.Where<Team>(t => t.MatchId == this.Match.MatchId).ToList<Team>());
                            foreach (Team team in previousTeams)
                            {
                                db.Team.Remove(team);
                            }
                            db.SaveChanges();
                        }
                    }

                    using (var db = new MatchReporterEntities())
                    {
                        db.Entry(this.HomeTeam).State = dataAddTeam.TeamSelectionChanged == true ? EntityState.Added : EntityState.Modified;
                        db.Entry(this.GuestTeam).State = dataAddTeam.TeamSelectionChanged == true ? EntityState.Added : EntityState.Modified;
                        db.SaveChanges();

                        this.SavedTeams = true;
                    }

                    btnTeamATTO1.Enabled = true;
                    btnTeamATTO2.Enabled = true;
                    btnTeamATTO3.Enabled = true;

                    btnTeamBTTO1.Enabled = true;
                    btnTeamBTTO2.Enabled = true;
                    btnTeamBTTO3.Enabled = true;

                    loadingThread.Abort();
                }
                //dataAddTeam.Dispose();
            }
            else
            {
                MessageBox.Show(this, "Da biste dodali momčadi, prvo je potrebno unijeti podatke o utakmici.", "Greška",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void dataPlayersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.HomeClub.ClubId != 0 && this.GuestClub.ClubId != 0)
            {
                FrmAddPlayers dataAddPlayers = new FrmAddPlayers(this.HomeClub.ClubId, this.GuestClub.ClubId, this.HomePlayers, this.GuestPlayers);
                dataAddPlayers.ShowDialog();

                if (dataAddPlayers.PlayersAddSuccess)
                {
                    Thread loadingThread = new Thread(new ThreadStart(showLoadingForm));
                    loadingThread.Start();

                    this.HomePlays = new BindingList<Play>();
                    this.GuestPlays = new BindingList<Play>();

                    this.HomePlayers = dataAddPlayers.HomePlayersPlay;
                    this.GuestPlayers = dataAddPlayers.GuestPlayersPlay;

                    this.HomeTeamPlayers.Clear();
                    this.GuestTeamPlayers.Clear();

                    using (var db = new MatchReporterEntities())
                    {
                        List<Play> previousPlays = new List<Play>(db.Play.Where<Play>(p => p.MatchId == this.Match.MatchId).ToList<Play>());
                        foreach(Play play in previousPlays)
                        {
                            db.Play.Remove(play);
                        }
                        db.SaveChanges();
                    }

                    foreach (Player player in this.HomePlayers)
                    {
                        TeamPlayer teamPlayer = new TeamPlayer(player.PlayerId, player.FirstName, player.LastName, player.Number);
                        this.HomeTeamPlayers.Add(teamPlayer);

                        Play play = new Play(this.Match.MatchId, player.PlayerId);
                        this.HomePlays.Add(play);
                    }

                    foreach (Player player in this.GuestPlayers)
                    {
                        TeamPlayer teamPlayer = new TeamPlayer(player.PlayerId, player.FirstName, player.LastName, player.Number);
                        this.GuestTeamPlayers.Add(teamPlayer);

                        Play play = new Play(this.Match.MatchId, player.PlayerId);
                        this.GuestPlays.Add(play);
                    }

                    using (var db = new MatchReporterEntities())
                    {
                        foreach(Play play in this.HomePlays)
                        {
                            db.Play.Add(play);
                        }

                        foreach(Play play in this.GuestPlays)
                        {
                            db.Play.Add(play);
                        }
                        db.SaveChanges();

                        this.SavedPlays = true;
                    }

                    dgvHomeTeam.DataSource = this.HomeTeamPlayers;
                    dgvGuestTeam.DataSource = this.GuestTeamPlayers;
                    dgvHomeTeam.Refresh();
                    dgvGuestTeam.Refresh();

                    btnHomeGoal.Enabled = true;
                    btnGuestGoal.Enabled = true;
                    btnHome7m.Enabled = true;
                    btnGuest7m.Enabled = true;
                    btnHomeWarning.Enabled = true;
                    btnGuestWarning.Enabled = true;
                    btnHomeSuspension.Enabled = true;
                    btnGuestSuspension.Enabled = true;
                    btnHomeDisqualification.Enabled = true;
                    btnGuestDisqualification.Enabled = true;
                    btnHomeDisqualificationReport.Enabled = true;
                    btnGuestDisqualificationReport.Enabled = true;
                    btnHomeUndo.Enabled = true;
                    btnGuestUndo.Enabled = true;

                    loadingThread.Abort();
                }
                //dataAddPlayers.Dispose();
            }
            else
            {
                MessageBox.Show(this, "Da biste dodali igrače, prvo je potrebno dodati momčadi.", "Greška",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void dataOfficialsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.HomeClub.ClubId != 0 && this.GuestClub.ClubId != 0)
            {
                FrmAddOfficials dataAddOfficials = new FrmAddOfficials(this.HomeClub.ClubId, this.GuestClub.ClubId, this.HomeClubOfficials, this.GuestClubOfficials);
                dataAddOfficials.ShowDialog();

                if (dataAddOfficials.OfficialsAddSuccess)
                {
                    Thread loadingThread = new Thread(new ThreadStart(showLoadingForm));
                    loadingThread.Start();

                    this.HomeManages = new BindingList<Manage>();
                    this.GuestManages = new BindingList<Manage>();

                    this.HomeClubOfficials = dataAddOfficials.HomeOfficialsManage;
                    this.GuestClubOfficials = dataAddOfficials.GuestOfficialsManage;

                    this.HomeTeamOfficials.Clear();
                    this.GuestTeamOfficials.Clear();

                    using (var db = new MatchReporterEntities())
                    {
                        List<Manage> previousManages = new List<Manage>(db.Manage.Where<Manage>(m => m.MatchId == this.Match.MatchId).ToList<Manage>());
                        foreach (Manage manage in previousManages)
                        {
                            db.Manage.Remove(manage);
                        }
                        db.SaveChanges();
                    }

                    foreach (ClubOfficial official in this.HomeClubOfficials)
                    {
                        TeamOfficial teamOfficial = new TeamOfficial(official.ClubOfficialId, official.FirstName, official.LastName);
                        this.HomeTeamOfficials.Add(teamOfficial);

                        Manage manage = new Manage(this.Match.MatchId, official.ClubOfficialId);
                        this.HomeManages.Add(manage);
                    }

                    foreach (ClubOfficial official in this.GuestClubOfficials)
                    {
                        TeamOfficial teamOfficial = new TeamOfficial(official.ClubOfficialId, official.FirstName, official.LastName);
                        this.GuestTeamOfficials.Add(teamOfficial);

                        Manage manage = new Manage(this.Match.MatchId, official.ClubOfficialId);
                        this.GuestManages.Add(manage);
                    }

                    using (var db = new MatchReporterEntities())
                    {
                        foreach (Manage manage in this.HomeManages)
                        {
                            db.Manage.Add(manage);
                        }

                        foreach (Manage manage in this.GuestManages)
                        {
                            db.Manage.Add(manage);
                        }
                        db.SaveChanges();

                        this.SavedManages = true;
                    }

                    dgvHomeOfficials.DataSource = this.HomeTeamOfficials;
                    dgvGuestOfficials.DataSource = this.GuestTeamOfficials;
                    dgvHomeOfficials.Refresh();
                    dgvGuestOfficials.Refresh();

                    btnHomeOfficialWarning.Enabled = true;
                    btnGuestOfficialWarning.Enabled = true;
                    btnHomeOfficialSuspension.Enabled = true;
                    btnGuestOfficialSuspension.Enabled = true;
                    btnHomeOfficialDisqualification.Enabled = true;
                    btnGuestOfficialDisqualification.Enabled = true;
                    btnHomeOfficialUndo.Enabled = true;
                    btnGuestOfficialUndo.Enabled = true;

                    loadingThread.Abort();
                }
                //dataAddOfficials.Dispose();
            }
            else
            {
                MessageBox.Show(this, "Da biste dodali službene osobe, prvo je potrebno dodati momčadi.", "Greška",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void dataMatchDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAddMatchDetails dataAddMatchDetails = new FrmAddMatchDetails(this.Match);
            dataAddMatchDetails.ShowDialog();

            if (dataAddMatchDetails.MatchDetailsAddSuccess)
            {
                this.Match.LeagueId = dataAddMatchDetails.LeagueId;
                this.Match.Round = dataAddMatchDetails.Round;
                this.Match.Date = dataAddMatchDetails.Date;
                this.Match.Time = dataAddMatchDetails.Time;
                this.Match.HallId = dataAddMatchDetails.HallId;
                this.Match.Spectators = dataAddMatchDetails.Spectators;
                this.Match.RefereePairId = dataAddMatchDetails.RefereePairId;
                this.Match.DelegateId = dataAddMatchDetails.DelegateId;
                this.Match.TimeKeeper = dataAddMatchDetails.TimeKeeper;
                this.Match.Scorer = dataAddMatchDetails.Scorer;
                this.RefereePairName = dataAddMatchDetails.RefereePairName;

                using (var db = new MatchReporterEntities())
                {
                    db.Entry(this.Match).State = this.Match.MatchId == 0 ? EntityState.Added : EntityState.Modified;
                    db.SaveChanges();
                }
            }
            //dataAddMatchDetails.Dispose();
        }
        private void matchSaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread savingDataThread = new Thread(new ThreadStart(showSavingForm));
            savingDataThread.Start();

            try
            {
                // HomeTeamPlayers > HomePlays, GuestTeamPlayers > GuestPlays

                foreach(TeamPlayer teamPlayer in HomeTeamPlayers)
                {
                    Play play = HomePlays
                        .Where(p => p.MatchId == this.Match.MatchId)
                        .Where(p => p.PlayerId == teamPlayer.TeamPlayerId)
                        .FirstOrDefault();

                    if(play != null)
                    {
                        play.Goals = teamPlayer.Goals;
                        play.Goals7m = teamPlayer.Goals7m;
                        play.Warning = teamPlayer.Warning;
                        play.SuspensionFirst = teamPlayer.Suspension1st;
                        play.SuspensionSecond = teamPlayer.Suspension2nd;
                        play.SuspensionThird = teamPlayer.Suspension3rd;
                        play.Disqualification = teamPlayer.Disqualification;
                        play.DisqualificationAdnReport = teamPlayer.DisqualificationReport;
                    }
                }

                foreach (TeamPlayer teamPlayer in GuestTeamPlayers)
                {
                    Play play = GuestPlays
                        .Where(p => p.MatchId == this.Match.MatchId)
                        .Where(p => p.PlayerId == teamPlayer.TeamPlayerId)
                        .FirstOrDefault();

                    if (play != null)
                    {
                        play.Goals = teamPlayer.Goals;
                        play.Goals7m = teamPlayer.Goals7m;
                        play.Warning = teamPlayer.Warning;
                        play.SuspensionFirst = teamPlayer.Suspension1st;
                        play.SuspensionSecond = teamPlayer.Suspension2nd;
                        play.SuspensionThird = teamPlayer.Suspension3rd;
                        play.Disqualification = teamPlayer.Disqualification;
                        play.DisqualificationAdnReport = teamPlayer.DisqualificationReport;
                    }
                }

                // HomeTeamOfficials > HomeManages, GuestTeamOfficials > GuestManages
                foreach(TeamOfficial teamOfficial in HomeTeamOfficials)
                {
                    Manage manage = HomeManages
                        .Where(m => m.MatchId == this.Match.MatchId)
                        .Where(m => m.ClubOfficialId == teamOfficial.TeamOfficialId)
                        .FirstOrDefault();

                    if(manage != null)
                    {
                        manage.Warning = teamOfficial.Warning;
                        manage.Suspension = teamOfficial.Suspension;
                        manage.Disqualification = teamOfficial.Disqualification;
                    }
                }

                foreach (TeamOfficial teamOfficial in GuestTeamOfficials)
                {
                    Manage manage = GuestManages
                        .Where(m => m.MatchId == this.Match.MatchId)
                        .Where(m => m.ClubOfficialId == teamOfficial.TeamOfficialId)
                        .FirstOrDefault();

                    if (manage != null)
                    {
                        manage.Warning = teamOfficial.Warning;
                        manage.Suspension = teamOfficial.Suspension;
                        manage.Disqualification = teamOfficial.Disqualification;
                    }
                }

                // Saving data
                using (var db = new MatchReporterEntities())
                {
                    db.Entry(this.Match).State = this.Match.MatchId == 0 ? EntityState.Added : EntityState.Modified;

                    db.Entry(this.HomeTeam).State = this.SavedTeams == false ? EntityState.Added : EntityState.Modified;
                    db.Entry(this.GuestTeam).State = this.SavedTeams == false ? EntityState.Added : EntityState.Modified;
                    this.SavedTeams = true;

                    foreach (Play play in HomePlays)
                    {
                        db.Entry(play).State = this.SavedPlays == false ? EntityState.Added : EntityState.Modified;
                    }
                    foreach (Play play in GuestPlays)
                    {
                        db.Entry(play).State = this.SavedPlays == false ? EntityState.Added : EntityState.Modified;
                    }
                    this.SavedPlays = true;

                    foreach (Manage manage in HomeManages)
                    {
                        db.Entry(manage).State = this.SavedManages == false ? EntityState.Added : EntityState.Modified;
                    }
                    foreach (Manage manage in GuestManages)
                    {
                        db.Entry(manage).State = this.SavedManages == false ? EntityState.Added : EntityState.Modified;
                    }
                    this.SavedManages = true;

                    db.SaveChanges();
                }
                savingDataThread.Abort();

                MessageBox.Show(this, "Podaci su uspješno spremljeni.", "Spremanje podataka",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                savingDataThread.Abort();
                MessageBox.Show(this, "Došlo je do pogreške.\nPodaci nisu spremljeni.", "Spremanje podataka",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void matchConcludeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(int.Parse(lblTimeMinutes.Text) == 60)
            {
                if (MessageBox.Show(this, "Jeste li sigurni da želite zaključiti utakmicu? \nViše neće biti moguće mijenjati podatke o utakmici.",
                "Zaključi utakmicu", MessageBoxButtons.YesNo, MessageBoxIcon.Question).Equals(DialogResult.Yes))
                {
                    try
                    {
                        this.Match.Concluded = 1;

                        panelMain.Enabled = false;

                        this.dataMatchDetailsToolStripMenuItem.Enabled = false;
                        this.dataTeamsToolStripMenuItem.Enabled = false;
                        this.dataPlayersToolStripMenuItem.Enabled = false;
                        this.dataOfficialsToolStripMenuItem.Enabled = false;

                        this.matchSaveToolStripMenuItem.Enabled = false;
                        this.matchConcludeToolStripMenuItem.Enabled = false;

                        using (var db = new MatchReporterEntities())
                        {
                            db.Entry(this.Match).State = this.Match.MatchId == 0 ? EntityState.Added : EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                    catch
                    {
                        MessageBox.Show(this, "Došlo je do greške. \nUtakmica nije zaključena.", "Greška",
                            MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show(this, "Ne možete zaključiti utakmicu jer \nutakmica nije završena.", "Greška",
                    MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }