Imports System.ComponentModel
Imports System.IO
Imports MySql.Data.MySqlClient

Public Class frm_add_person

    Private Sub add_person()

        Dim person As New person(txt_fname.Text, txt_mname.Text, txt_lname.Text, date_birthday.Text, cbo_gender.Text, txt_weight.Text, txt_cpr.Text, cbo_country.SelectedValue, txt_mobile.Text, txt_email.Text, txt_address.Text, cbo_club.SelectedValue, pb_person_picture)

    End Sub

    Private Sub Load_Picture()

        'Creating A New Dialog
        Dim OpenPictureFile As New OpenFileDialog

        'Filtering The Types
        OpenPictureFile.Filter = "Image Files | *.jpg; *.jpeg; *.bmp; *.png; *.gif;"

        'Openning The File
        If OpenPictureFile.ShowDialog = System.Windows.Forms.DialogResult.OK Then

            Try
                ' Getting File Path
                Dim FilePath As String = OpenPictureFile.FileName()
                Dim FileName As String = System.IO.Path.GetFileName(OpenPictureFile.FileName)

                ' Picture File
                Dim Picture As New MemoryStream

                ' Define New Image Size
                Dim LogoSize As New Size(385, 500)

                ' Resizing The Image
                Dim ResizedImage = New Bitmap(Image.FromFile(FilePath), LogoSize)

                ' Display The Image
                pb_person_picture.Image = ResizedImage

                ' Saving The Image As PNG Format
                pb_person_picture.Image.Save(Picture, System.Drawing.Imaging.ImageFormat.Png)

                ' Read The Image As Bytes
                Dim arrImage() As Byte = Picture.GetBuffer

                ' Closing The Picture
                Picture.Close()

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End If

    End Sub

    Private Sub btn_add_person_Click(sender As Object, e As EventArgs) Handles btn_add_person.Click

        add_person()
        Me.Close()

    End Sub

    Private Sub frm_add_person_Load(sender As Object, e As EventArgs) Handles Me.Load

        Try
            ' List Down The Countries
            GetCountryList(cbo_country)
        Catch ex As Exception

        End Try

        Try
            ' List Down Clubs
            GetClubsList(cbo_club)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub cbo_country_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_country.SelectedIndexChanged

        Dim QRY As String

        Try
            QRY = "select flag from countries where id=" & cbo_country.SelectedValue
        Catch ex As Exception
            Exit Sub
        End Try

        Dim CON As New MySqlConnection(CON_STRING)
        Dim COM As New MySqlCommand(QRY, CON)
        Dim RDR As MySqlDataReader
        Dim LST As New List(Of dataset)

        Try
            CON.Open()
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then
                While RDR.Read
                    Try
                        Dim data As Byte() = DirectCast(RDR("flag"), Byte())
                        Dim flag As New System.IO.MemoryStream(data)
                        pb_flag.Image = Image.FromStream(flag)
                    Catch ex As Exception
                        pb_flag.Image = Nothing
                        Exit Sub
                    End Try
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Private Sub pb_person_picture_Click(sender As Object, e As EventArgs) Handles pb_person_picture.Click

        'Load_Picture()
        ResizePicture(pb_person_picture, 385, 500)

    End Sub

    Private Sub cbo_club_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_club.SelectedIndexChanged

        Try
            GetClubLogo(pb_club, cbo_club.SelectedValue)
        Catch ex As Exception

        End Try

    End Sub

End Class