Public Class frmWorkingHours
    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub frmWorkingHours_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub
    Private Sub LoadData()
        FillDataGrid("SELECT ShiftID, ShiftName, StartTime, EndTime FROM WorkingHours", DataGridView1)
    End Sub
    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        If txtShiftName.Text = "" Then
            ErrorProvider1.SetError(txtShiftName, "Numeric input only!")
        Else
            ErrorProvider1.Clear()
            Dim WorkingMinutes As Double = DateDiff(DateInterval.Minute, dtpStartTime.Value, dtpEndTime.Value)

            If btnSubmit.Text = "SAVE" Then
                ExecuteSQLQuery(" INSERT INTO WorkingHours (ShiftName, StartTime, EndTime, WorkingMinutes) VALUES ('" + str_repl(txtShiftName.Text) + "', '" & Format(dtpStartTime.Value, "HH:mm") & "', '" & Format(dtpEndTime.Value, "HH:mm") & "', " & num_fltr(Math.Abs(WorkingMinutes)) & ") ")
            ElseIf btnSubmit.Text = "UPDATE" Then
                ExecuteSQLQuery("UPDATE  WorkingHours SET  ShiftName='" + str_repl(txtShiftName.Text) + "', StartTime='" & Format(dtpStartTime.Value, "HH:mm") & "', EndTime='" & Format(dtpEndTime.Value, "HH:mm") & "', WorkingMinutes=" & num_fltr(Math.Abs(WorkingMinutes)) & "  WHERE ShiftID=" & num_fltr(txtShiftID.Text) & " ")
            End If

            LoadData()
            txtShiftID.Text = ""
            txtShiftName.Text = ""
            dtpStartTime.Value = Now
            dtpEndTime.Value = Now
            btnSubmit.Text = "SAVE"
            MsgBox("Information has been saved successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 0 Then
            If MsgBox("Are you sure to edit: " & DataGridView1.CurrentRow.Cells(3).Value, MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                ExecuteSQLQuery("SELECT *  FROM  WorkingHours  WHERE ShiftID=" & num_fltr(DataGridView1.CurrentRow.Cells(2).Value) & "")
                If sqlDT.Rows.Count > 0 Then
                    txtShiftID.Text = num_fltr(DataGridView1.CurrentRow.Cells(2).Value)
                    txtShiftName.Text = sqlDT.Rows(0)("ShiftName")
                    dtpStartTime.Text = sqlDT.Rows(0)("StartTime")
                    dtpEndTime.Text = sqlDT.Rows(0)("EndTime")
                End If
                btnSubmit.Text = "UPDATE"
            End If
        ElseIf e.ColumnIndex = 1 Then
            If MsgBox("Are you sure to delete: " & DataGridView1.CurrentRow.Cells(3).Value, MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                ExecuteSQLQuery("DELETE FROM  WorkingHours  WHERE ShiftID=" & num_fltr(DataGridView1.CurrentRow.Cells(2).Value) & " ")
                LoadData()
                MsgBox("Data has been deleted successfully.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub
End Class