Public Class myStaff


    Private Sub myStaff_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        fillGrid(dgv_staff, fileLoc_Staff)
        StartPosition = FormStartPosition.CenterScreen

        txt_staffID.BackColor = gray
        txt_forename.BackColor = gray
        txt_surname.BackColor = gray
        txt_password.BackColor = gray
        txt_accessLevel.BackColor = gray
        txt_phoneNumber.BackColor = gray
        txt_postcode.BackColor = gray
        txt_address.BackColor = gray
        DTP_DoB.BackColor = gray
    End Sub

    Private Sub fillText(ByVal rowIndex As Integer)
        txt_staffID.Text = dgv_staff.Rows(rowIndex).Cells(0).Value
        txt_forename.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(1).Value)
        txt_surname.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(2).Value)
        txt_password.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(3).Value)
        txt_accessLevel.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(4).Value)
        txt_phoneNumber.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(5).Value)
        txt_postcode.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(6).Value)
        txt_address.Text = Decryption(dgv_staff.Rows(rowIndex).Cells(7).Value)
        DTP_DoB.Value = dgv_staff.Rows(rowIndex).Cells(8).Value
    End Sub

    Private Sub clearText()
        txt_staffID.Text = ""
        txt_forename.Text = ""
        txt_surname.Text = ""
        txt_password.Text = ""
        txt_accessLevel.Text = ""
        txt_phoneNumber.Text = ""
        txt_postcode.Text = ""
        txt_address.Text = ""
        DTP_DoB.CustomFormat = " "
    End Sub

    'Navigation
    Private dgvNavigator As New DataGridViewNavigator()
    Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
        dgvNavigator.SelectFirst(dgv_staff)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
        dgvNavigator.SelectPrevious(dgv_staff)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
        dgvNavigator.SelectNext(dgv_staff)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
        dgvNavigator.SelectLast(dgv_staff)
        fillText(dgvNavigator.CurrentIndex)
    End Sub
    '----------------------------------------------------------------------------------------------'
    Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
        clearText()
        Dim newID As Integer = AddRecord(fileLoc_Staff)
        txt_staffID.Text = newID
    End Sub

    Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
        'Checks the data before attempting to save it to file
        If Check() = False Then
            Return
        End If


        Dim staffID As Integer = txt_staffID.Text
        Dim password As String = Encryption(txt_password.Text)

        If UniqueCheck(staffID, fileLoc_Staff) Then
            Dim newValues As String = txt_staffID.Text + "," + Encryption(txt_forename.Text) + "," + Encryption(txt_surname.Text) + "," + password + "," + Encryption(txt_accessLevel.Text) + "," + Encryption(txt_phoneNumber.Text) + "," + Encryption(txt_postcode.Text) + "," + Encryption(txt_address.Text) + "," + DTP_DoB.Value
            ' Adds the data from the text boxes, encrypting where possible
            SaveRecord(fileLoc_Staff, newValues)
            fillGrid(dgv_staff, fileLoc_Staff)
        Else
            MsgBox("ID is not unique.")
        End If



    End Sub

    Private Function Check()

        ' Presence check on staff ID
        If txt_staffID.Text = "" Then
            MsgBox("please enter a staff ID")
            Return False
        End If

        ' Integer check on staff ID
        If Not IntCheck(txt_staffID.Text) Then
            MsgBox("Non integer input in staff ID textbox")
            Return False
        End If

        If txt_forename.Text.Length < 3 Or txt_forename.Text.Length > 20 Then
            'Length Check on forename also acts as a presence check
            MsgBox("Forename entered invalid, must be between 3 and 20 characters")
            Return False
        End If

        If txt_surname.Text.Length < 3 Or txt_surname.Text.Length > 20 Then
            'Length Check on surname also acts as a presence check
            MsgBox("Surname entered invalid, must be between 3 and 20 characters")
            Return False
        End If

        'password
        'want to contain. capital,lower case, number at least 7 letters

        If txt_password.Text.Length < 7 Then
            MsgBox("password must be at least 7 characters")
            Return False
        End If

        Dim passwordArray() As Char = txt_password.Text.ToCharArray
        Dim upper As Boolean = False
        Dim lower As Boolean = False
        Dim number As Boolean = False
        ' Each of the conditions  are set to false, only satisfied when a character ticks one off, all must be true when the character array is finished being searched
        For Each c As Char In passwordArray
            If Char.IsUpper(c) Then
                upper = True
            End If
            If Char.IsLower(c) Then
                lower = True
            End If
            If Char.IsNumber(c) Then
                number = True
            End If
        Next

        If Not (upper = True AndAlso lower = True AndAlso number = True) Then
            MsgBox("invalid password, must contain, An upper case character, A lower case character and a number")
            Return False
        End If


        If Not IntCheck(txt_accessLevel.Text) Then
            Return False
        End If

        ' Checks the access level is within the bounds of the system
        If txt_accessLevel.Text < 0 Or txt_accessLevel.Text > 4 Then
            MsgBox("invalid access level, must be between 0 and 4")
            Return False
        End If

        ' checks the phone number is an integer
        If Not IntCheck(txt_phoneNumber.Text) Then
            MsgBox("invalid phone number, must only contain numbers")
            Return False
        End If

        ' After checking its an integer, it checks the length of the number
        If txt_phoneNumber.Text.Length <> 11 Then
            MsgBox("invalid phone number, must be 11 digits long")
            Return False
        End If


        'PostCode not checked

        ' Length check also acts as a presence check
        If txt_address.Text.Length < 2 Or txt_address.Text.Length > 255 Then
            MsgBox("Address entered invalid, must be between 3 and 255 characters")
            Return False
        End If

        Return True

        'Date of birth not checked
    End Function

    Private Sub btn_edit_Click(sender As Object, e As EventArgs) Handles btn_edit.Click
        ' Checks new data to ensure valid data only
        If Check() = False Then
            Return
        End If
        ' Compiles a string of the new data, and edits the existing record with the corresponding ID
        Dim ID As Integer = txt_staffID.Text
        Dim newValues As String = txt_staffID.Text & "," & Encryption(txt_forename.Text) & "," & Encryption(txt_surname.Text) & "," & Encryption(txt_password.Text) & "," & Encryption(txt_accessLevel.Text) & "," & Encryption(txt_phoneNumber.Text) & "," & Encryption(txt_postcode.Text) & "," & Encryption(txt_address.Text) & "," & DTP_DoB.Value.ToString()
        EditRecord(ID, newValues, fileLoc_Staff)
        fillGrid(dgv_staff, fileLoc_Staff)
    End Sub

    Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
        ' Presence checks staff ID textbox to avoid crash
        If txt_staffID.Text = "" Then
            MsgBox("No staff ID given, please try again")
            Return
        End If
        Dim staffID As Integer = txt_staffID.Text
        DeleteRecord(dgv_staff, fileLoc_Staff, staffID)
        fillGrid(dgv_staff, fileLoc_Staff)
    End Sub


    ' Takes user back to main menu, doesn't delete non saved inputs
    Private Sub btn_menu_Click(sender As Object, e As EventArgs) Handles btn_menu.Click
        Me.Visible = False
        myMenu.Visible = True
    End Sub

    ' Sorts
    ' Staff ID Ascending
    ' Staff ID Descending
    Private Sub btn_staffIdAsc_Click(sender As Object, e As EventArgs) Handles btn_staffIdAsc.Click
        AscSort(fileLoc_Staff, 0)
        fillGrid(dgv_staff, fileLoc_Staff)
    End Sub

    Private Sub btn_staffIdDesc_Click(sender As Object, e As EventArgs) Handles btn_staffIdDesc.Click
        DescSort(fileLoc_Staff, 0)
        fillGrid(dgv_staff, fileLoc_Staff)
    End Sub


End Class