Public Class frmWorkingHours1
    Private Sub btnSubmit_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs)

    End Sub
    Private Sub LoadData()
        Dim dataset1 As New DataGridView
        FillDataGrid("SELECT ShiftID, ShiftName, StartTime, EndTime FROM WorkingHours", dataset1)
        GridControl1.DataSource = dataset1.DataSource
        '  ExecuteSQLQuery("SELECT ShiftID, ShiftName, StartTime, EndTime FROM WorkingHours")
    End Sub

    Private Sub frmWorkingHours1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub



    Private Sub btnSubmit_Click_1(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.EditValue, dtpEndTime.editValue)

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

            LoadData()
            txtShiftID.Text = ""
            txtShiftName.Text = ""
            dtpStartTime.EditValue = Now
            dtpEndTime.editValue = Now
            btnSubmit.Text = "SAVE"
            MsgBox("Information has been saved successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub

    Private Sub btnClose_Click_1(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub GridControl1_Click(sender As Object, e As EventArgs) Handles GridControl1.Click

    End Sub

    Private Sub Edit1_Click(sender As Object, e As EventArgs) Handles Edit1.Click
        If MsgBox("Are you sure to edit: " & GridView1.GetFocusedRowCellValue("ShiftName").ToString(), MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
            ExecuteSQLQuery("SELECT *  FROM  WorkingHours  WHERE ShiftID=" & num_fltr(GridView1.GetFocusedRowCellValue("ShiftID").ToString()) & "")
            If sqlDT.Rows.Count > 0 Then
                txtShiftID.Text = num_fltr(GridView1.GetFocusedRowCellValue("ShiftID").ToString())
                txtShiftName.Text = sqlDT.Rows(0)("ShiftName")
                dtpStartTime.EditValue = sqlDT.Rows(0)("StartTime").ToString()
                dtpEndTime.editvalue = sqlDT.Rows(0)("EndTime").ToString()
            End If
            btnSubmit.Text = "UPDATE"
        End If
    End Sub

    Private Sub Delete1_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles Delete1.ButtonClick
        If MsgBox("Are you sure to delete: " & GridView1.GetFocusedRowCellValue("ShiftName").ToString(), MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
            ExecuteSQLQuery("DELETE FROM  WorkingHours  WHERE ShiftID=" & num_fltr(GridView1.GetFocusedRowCellValue("ShiftID").ToString()) & " ")
            LoadData()
            MsgBox("Data has been deleted successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub
End Class