//Najdenie najblizsieho datumu kde studen chybal na cviceni
        private Nullable <DateTime> GetClosestAbsentDate(int studentId, AttendanceStud attend)
        {
            try
            {
                using (var con = new StudentDBDataContext(conn_str))
                {
                    var attendance = con.AttendanceStuds.Where(a => a.IDSkupina == StudentSkup.Id && a.IDStudent == studentId && a.Type == "Cvičenie" && a.Status == "Neprítomný" || a.Status == "Ospravedlnené");

                    var date = attendance.Select(x => x.Date).OrderByDescending(x => x.Date);
                    if (date.Count() > 0)
                    {
                        if (DateInsideOneWeek(date.FirstOrDefault(), attend.Date))
                        {
                            var closestDate = date.FirstOrDefault();
                            return(closestDate);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger newLog = new Logger();
                newLog.LogError(ex);
                MessageBox.Show("Nastala chyba, viac informácii nájdete v logoch.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
        }
        private void AddManualAttendanceBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if ((string)comboBox3.SelectedValue != string.Empty)
                {
                    using (var con = new StudentDBDataContext(conn_str))
                    {
                        var student = studentsManual.TryGetValue(comboBox3.Text, out int value);
                        var std     = con.GetTable <Student>().Where(x => x.Id == value).FirstOrDefault();
                        if (std == null)
                        {
                            MessageBox.Show("Nie je vybraný žiaden študent");
                            return;
                        }
                        var insert = new AttendanceStud
                        {
                            IDSkupina = this.StudentSkup.Id,
                            Type      = CheckType(),
                            Date      = GetDate(),
                            IDStudent = value,
                            IdGroup   = std.ID_Kruzok,
                            Comment   = string.Empty
                        };
                        var attend  = con.GetTable <AttendanceStud>();
                        var AttList = from AttendanceStud in attend
                                      where insert.IDSkupina == AttendanceStud.IDSkupina && AttendanceStud.Date == insert.Date && AttendanceStud.Type == insert.Type && AttendanceStud.IDStudent == insert.IDStudent
                                      select new { AttendanceStud.Type, AttendanceStud.Date };

                        if (AttList.Count() == 0)
                        {
                            con.AttendanceStuds.InsertOnSubmit(insert);
                            con.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                        Filter();
                        ResetLabels();
                    }
                }
            }
            catch (Exception ex)
            {
                Logger newLog = new Logger();
                newLog.LogError(ex);
                MessageBox.Show("Nastala chyba, viac informácii nájdete v logoch.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (var con = new StudentDBDataContext(conn_str))
                {
                    var type = CheckType();

                    if (attGrid.Rows.Count <= 0)
                    {
                        MessageBox.Show("Nie je vytvorený žiaden záznam");
                        return;
                    }

                    if (attGrid.SelectedRows.Count <= 0)
                    {
                        MessageBox.Show("Nie je vybraný žiaden záznam.");
                        return;
                    }
                    foreach (DataGridViewRow selected in attGrid.SelectedRows)
                    {
                        AttendanceStud attendance      = con.AttendanceStuds.Where(a => a.IDSkupina == StudentSkup.Id && a.IdAttendance == (int)selected.Cells[5].Value).FirstOrDefault();
                        var            totalAttendance = con.GetTable <TotalAttendance>().Where(x => x.IdStudent == (int)selected.Cells[7].Value).FirstOrDefault();
                        if (attendance == null)
                        {
                            MessageBox.Show("Nie je vybraný žiaden záznam.");
                            return;
                        }
                        if (totalAttendance == null)
                        {
                            MessageBox.Show("Nenašiel sa záznam o celkovej dochádzke, ak máte pridaných študentov, skúste reštartovať aplikáciu", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        attendance.Status = comboBox2.SelectedItem.ToString();
                        AttendanceStud attDate;
                        var            date = GetLatestDate((int)selected.Cells[7].Value);
                        switch (comboBox2.SelectedItem.ToString())
                        {
                        case "Neprítomný":

                            if (attendance.Type == "Cvičenie" && date == null)
                            {
                                attendance.IsReplacable = true;
                            }
                            else if (attendance.Type == "Cvičenie" && date != null)
                            {
                                attDate = con.AttendanceStuds.Where(a => a.IDSkupina == StudentSkup.Id && a.IDStudent == (int)selected.Cells[7].Value && a.Date == date && a.Type == "Cvičenie").FirstOrDefault();

                                if (attDate.Status == "Nahradené" && attDate.IsReplacable)
                                {
                                    attDate.IsReplacable    = false;
                                    attendance.IsReplacable = false;
                                }
                                else
                                {
                                    attendance.IsReplacable = true;
                                }
                            }
                            attendance.Comment = string.Empty;
                            break;

                        case "Nahradené":
                            //date = GetLatestDate((int)selected.Cells[7].Value);
                            date    = GetClosestAbsentDate((int)selected.Cells[7].Value, attendance);
                            attDate = con.AttendanceStuds.Where(a => a.IDSkupina == StudentSkup.Id && a.IDStudent == (int)selected.Cells[7].Value && a.Date == date && a.Type == "Cvičenie").FirstOrDefault();

                            if (attDate != null)
                            {
                                if (CheckType() == "Cvičenie" && attDate.IsReplacable == true && RWS(attDate.Status) == "Neprítomný" || attDate.Status == "Ospravedlnené")
                                {
                                    attDate.IsReplacable    = false;
                                    attendance.IsReplacable = false;
                                }
                                else
                                {
                                    attendance.IsReplacable = true;
                                }
                            }
                            else if (attendance.Type == "Cvičenie")
                            {
                                attendance.IsReplacable = false;
                            }
                            attendance.Comment = string.Empty;

                            con.SubmitChanges();
                            break;

                        case "Zrušené":
                            attendance.IsReplacable = false;
                            break;

                        case "Ospravedlnené":

                            if (attendance.Type == "Cvičenie" && date == null)
                            {
                                attendance.IsReplacable = true;
                            }
                            else if (attendance.Type == "Cvičenie" && date != null)
                            {
                                attDate = con.AttendanceStuds.Where(a => a.IDSkupina == StudentSkup.Id && a.IDStudent == (int)selected.Cells[7].Value && a.Date == date && a.Type == "Cvičenie").FirstOrDefault();

                                if (attDate.Status == "Nahradené" && attDate.IsReplacable)
                                {
                                    attDate.IsReplacable    = false;
                                    attendance.IsReplacable = false;
                                }
                                else
                                {
                                    attendance.IsReplacable = true;
                                }
                            }
                            attendance.Comment = string.Empty;
                            break;

                        default: break;
                        }
                        globalRowIndex = selected.Index;
                        con.SubmitChanges();
                        PresenceCounter((int)selected.Cells[7].Value);
                        ResetLabels();
                    }

                    if (comboBox5.Text != string.Empty)
                    {
                        var value = comboBox5.SelectedItem;
                        FilterBy();
                        comboBox5.SelectedItem = value;
                    }
                    else
                    {
                        Filter();
                    }

                    this.attGrid.ClearSelection();
                    this.attGrid.CurrentCell.Selected = false;

                    try
                    {
                        this.attGrid.Rows[globalRowIndex + 1].Selected = true;
                        this.attGrid.Focus();
                    }
                    catch (Exception)
                    {
                    }
                    if (comboBox2.Text == "Zrušené")
                    {
                        IQueryable <AttendanceStud> attCancelled;
                        var cancelledComment = Interaction.InputBox("Uveďte dôvod zrušenia výučby", "Dôvod zrušenia", "Zrušená hodina", -1, -1);



                        switch (comboBox1.SelectedIndex)
                        {
                        case 0:
                            attCancelled = con.AttendanceStuds.Where(a => a.IDSkupina == StudentSkup.Id && a.Type == CheckType() && a.Date == (DateTime)attGrid.CurrentRow.Cells[2].Value);
                            break;

                        default:
                            attCancelled = con.AttendanceStuds
                                           .Where(a => a.IDSkupina == StudentSkup.Id && a.Type == CheckType() && a.Date == (DateTime)attGrid.CurrentRow.Cells[2].Value && a.Student.ID_Kruzok == comboBox1.Text);
                            break;
                        }
                        foreach (var x in attCancelled)
                        {
                            x.Status  = comboBox2.Text;
                            x.Comment = cancelledComment;
                            con.SubmitChanges();
                        }
                        Filter();
                    }
                    ResetLabels();
                }
            }
            catch (Exception ex)
            {
                Logger newLog = new Logger();
                newLog.LogError(ex);
                MessageBox.Show("Nastala chyba, viac informácii nájdete v logoch.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void CreateButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (var con = new StudentDBDataContext(conn_str))
                {
                    IQueryable <Student> student;

                    Nullable <int> idStud;
                    if (comboBox1.SelectedIndex == 0)
                    {
                        var studentId = con.GetTable <Student>().Where(x => x.Forma == StudentSkup.Forma && x.ID_stud_skupina == StudentSkup.Id);
                        student = studentId;
                        if (student.Count() <= 0)
                        {
                            MessageBox.Show("Nenašiel sa žiaden študent");
                            return;
                        }
                    }
                    else
                    {
                        var studentId = con.GetTable <Student>().Where(x => x.Forma == StudentSkup.Forma && x.ID_stud_skupina == StudentSkup.Id && x.ID_Kruzok == (string)comboBox1.SelectedItem);
                        student = studentId;
                        if (student.Count() <= 0)
                        {
                            MessageBox.Show("Nenašiel sa žiaden študent");
                            return;
                        }
                    }

                    foreach (var x in student)
                    {
                        var insert = new AttendanceStud
                        {
                            IDSkupina = this.StudentSkup.Id,
                            Type      = CheckType(),
                            Date      = GetDate(),
                            IDStudent = x.Id,
                            IdGroup   = x.ID_Kruzok,
                            Comment   = string.Empty
                        };
                        var attend  = con.GetTable <AttendanceStud>();
                        var AttList = from AttendanceStud in attend
                                      where insert.IDSkupina == AttendanceStud.IDSkupina && AttendanceStud.Date == insert.Date && AttendanceStud.Type == insert.Type && AttendanceStud.IDStudent == insert.IDStudent
                                      select new { AttendanceStud.Type, AttendanceStud.Date };

                        if (AttList.Count() == 0)
                        {
                            con.AttendanceStuds.InsertOnSubmit(insert);
                            con.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        }
                        //GetAttendance();
                        Filter();
                        if (attGrid.Rows.Count > 0 && (int?)attGrid.CurrentRow.Cells[7].Value != null)
                        {
                            idStud = (int?)attGrid.CurrentRow.Cells[7].Value;
                            if (idStud != null)
                            {
                                PresenceCounter(idStud.GetValueOrDefault());
                            }
                        }
                    }
                    ResetLabels();
                }
            }
            catch (Exception ex)
            {
                Logger newLog = new Logger();
                newLog.LogError(ex);
                MessageBox.Show("Nastala chyba, viac informácii nájdete v logoch.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }