﻿Imports System.Data.OleDb
Imports RestelII.My.MySettings

Public Class frmGeneralDefinitions

    Dim NewRow, NewRoomType As Boolean

    Private Sub frmGeneral_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.RoomsTableAdapter.Fill(Me.RestelDataSet.Rooms)

        Me.RoomCodesTableAdapter.Fill(Me.RestelDataSet.RoomCodes)

        HideSelectionColours(RoomNamesDGV)

    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

        If RoomSetup.Focus Then
            Me.RoomsTableAdapter.Fill(Me.RestelDataSet.Rooms)
            Me.RoomCodesTableAdapter.Fill(Me.RestelDataSet.RoomCodes)
        End If

        If Accounts.Focus Then
            Me.BudgetTypesTableAdapter.Fill(Me.RestelDataSet.BudgetTypes)
        End If

        If RoomPricing.Focus Then
            RoomPricingTableAdapter.RoomSelect(Me.RestelDataSet.RoomPricing, txtRoomType.Text)
        End If

        If Marketing.Focus Then
            Me.MarketingTableAdapter.Fill(Me.RestelDataSet.Marketing)
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        
        Try
            Me.Validate()

            If RoomCodesDGV.Focus Then
                Me.RoomCodesBindingSource.EndEdit()
                Me.RoomCodesTableAdapter.Update(Me.RestelDataSet.RoomCodes)
            End If

            If RoomPricingDGV.Focus Then
                Me.RoomPricingBindingSource.EndEdit()
                Me.RoomPricingTableAdapter.Update(Me.RestelDataSet.RoomPricing)
            End If

            If Accounts.Focus Then
                Me.BudgetTypesBindingSource.EndEdit()
                Me.BudgetTypesTableAdapter.Update(Me.RestelDataSet.BudgetTypes)
            End If

            If Marketing.Focus Then
                Me.MarketingBindingSource.EndEdit()
                Me.MarketingTableAdapter.Update(Me.RestelDataSet.Marketing)
            End If

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles RoomCodesDGV.DataError

        MsgBox("Room Code Already Exists!     " & vbCr & vbCr & "Please Enter A Different One.            ", MsgBoxStyle.Information, "Room Code Definition")

    End Sub

    Private Sub RoomCodesDGV_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles RoomCodesDGV.GotFocus

        HideSelectionColours(RoomNamesDGV)
        ShowSelectionColours(RoomCodesDGV)

    End Sub

    Private Sub RoomCodesDGV_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RoomCodesDGV.KeyDown

        Select Case e.KeyCode
            Case Keys.Delete

                If MsgBox("Are You Sure That You Want To Delete This Line?     ", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Room Code") = MsgBoxResult.No Then

                    Me.RoomCodesTableAdapter.Fill(Me.RestelDataSet.RoomCodes)

                End If

        End Select
    End Sub

    Private Sub AccountsDGV_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles AccountsDGV.KeyDown

        Select Case e.KeyCode
            Case Keys.Delete

                If MsgBox("Are You Sure That You Want To Delete This Line?     ", MsgBoxStyle.Question & MsgBoxStyle.YesNo, "Delete Accounting Type") = MsgBoxResult.No Then

                    Me.BudgetTypesTableAdapter.Fill(Me.RestelDataSet.BudgetTypes)

                End If

        End Select
    End Sub

    Private Sub HideSelectionColours(ByVal DGV As DataGridView)

        ' Sets Cell Back Colour and Text Colour to Normal

        DGV.DefaultCellStyle.SelectionBackColor = Color.FromArgb(255, 255, 255, 192)
        DGV.DefaultCellStyle.SelectionForeColor = Color.Black

    End Sub

    Private Sub ShowSelectionColours(ByVal DGV As DataGridView)

        ' Sets Cell Back Colour and Text Colour to Highlight Colours

        DGV.DefaultCellStyle.SelectionBackColor = Color.RoyalBlue
        DGV.DefaultCellStyle.SelectionForeColor = Color.White

    End Sub

    Private Sub RoomNamesDGV_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles RoomNamesDGV.GotFocus

        HideSelectionColours(RoomCodesDGV)
        ShowSelectionColours(RoomNamesDGV)

    End Sub

    Private Sub txtRoomType_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRoomType.TextChanged

        RoomPricingTableAdapter.RoomSelect(Me.RestelDataSet.RoomPricing, txtRoomType.Text)

    End Sub

    Private Sub RoomPricingDGV_NewRowNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles RoomPricingDGV.NewRowNeeded

        NewRow = True

    End Sub

    Private Sub RoomPricingDGV_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles RoomPricingDGV.RowsAdded

        If NewRow = True Then
            RoomPricingDGV.CurrentRow.Cells("RoomType").Value = txtRoomType.Text
        End If

        NewRow = False

    End Sub

End Class