Imports Mediatheek.data
Imports Mediatheek.DataAcces

Public Class frmOntleningToevoegen
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents cboKiesOntlener As System.Windows.Forms.ComboBox
    Friend WithEvents groupbox2 As System.Windows.Forms.GroupBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents lvwBeschikbaar As System.Windows.Forms.ListView
    Friend WithEvents lvwVoorOntlener As System.Windows.Forms.ListView
    Friend WithEvents btnVoegOntleningToeVoegToe As System.Windows.Forms.Button
    Friend WithEvents btnVoegAlbumToe As System.Windows.Forms.Button
    Friend WithEvents btnVoegAlbumTerug As System.Windows.Forms.Button
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents txtToevoegId As System.Windows.Forms.TextBox
    Friend WithEvents dtpTijd As System.Windows.Forms.DateTimePicker
    Friend WithEvents clhAlbumNaam As System.Windows.Forms.ColumnHeader
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmOntleningToevoegen))
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.cboKiesOntlener = New System.Windows.Forms.ComboBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.groupbox2 = New System.Windows.Forms.GroupBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.dtpTijd = New System.Windows.Forms.DateTimePicker
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.btnVoegAlbumTerug = New System.Windows.Forms.Button
        Me.btnVoegAlbumToe = New System.Windows.Forms.Button
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.lvwVoorOntlener = New System.Windows.Forms.ListView
        Me.lvwBeschikbaar = New System.Windows.Forms.ListView
        Me.btnVoegOntleningToeVoegToe = New System.Windows.Forms.Button
        Me.Label7 = New System.Windows.Forms.Label
        Me.txtToevoegId = New System.Windows.Forms.TextBox
        Me.clhAlbumNaam = New System.Windows.Forms.ColumnHeader
        Me.GroupBox1.SuspendLayout()
        Me.groupbox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(-8, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(608, 72)
        Me.PictureBox1.TabIndex = 1
        Me.PictureBox1.TabStop = False
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.cboKiesOntlener)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Location = New System.Drawing.Point(24, 88)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(544, 56)
        Me.GroupBox1.TabIndex = 2
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Kies ontlener"
        '
        'cboKiesOntlener
        '
        Me.cboKiesOntlener.Location = New System.Drawing.Point(160, 24)
        Me.cboKiesOntlener.Name = "cboKiesOntlener"
        Me.cboKiesOntlener.Size = New System.Drawing.Size(320, 21)
        Me.cboKiesOntlener.TabIndex = 1
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Ontlener :"
        '
        'groupbox2
        '
        Me.groupbox2.Controls.Add(Me.Label2)
        Me.groupbox2.Controls.Add(Me.dtpTijd)
        Me.groupbox2.Location = New System.Drawing.Point(24, 168)
        Me.groupbox2.Name = "groupbox2"
        Me.groupbox2.Size = New System.Drawing.Size(544, 56)
        Me.groupbox2.TabIndex = 3
        Me.groupbox2.TabStop = False
        Me.groupbox2.Text = "Kies uitlening"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 24)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(96, 24)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "Datum uitlening :"
        '
        'dtpTijd
        '
        Me.dtpTijd.Format = System.Windows.Forms.DateTimePickerFormat.Short
        Me.dtpTijd.Location = New System.Drawing.Point(160, 24)
        Me.dtpTijd.Name = "dtpTijd"
        Me.dtpTijd.TabIndex = 6
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.btnVoegAlbumTerug)
        Me.GroupBox3.Controls.Add(Me.btnVoegAlbumToe)
        Me.GroupBox3.Controls.Add(Me.Label6)
        Me.GroupBox3.Controls.Add(Me.Label5)
        Me.GroupBox3.Controls.Add(Me.lvwVoorOntlener)
        Me.GroupBox3.Controls.Add(Me.lvwBeschikbaar)
        Me.GroupBox3.Location = New System.Drawing.Point(24, 240)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(544, 200)
        Me.GroupBox3.TabIndex = 4
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Kies album"
        '
        'btnVoegAlbumTerug
        '
        Me.btnVoegAlbumTerug.Location = New System.Drawing.Point(248, 112)
        Me.btnVoegAlbumTerug.Name = "btnVoegAlbumTerug"
        Me.btnVoegAlbumTerug.Size = New System.Drawing.Size(32, 32)
        Me.btnVoegAlbumTerug.TabIndex = 5
        Me.btnVoegAlbumTerug.Text = "<"
        '
        'btnVoegAlbumToe
        '
        Me.btnVoegAlbumToe.Location = New System.Drawing.Point(248, 64)
        Me.btnVoegAlbumToe.Name = "btnVoegAlbumToe"
        Me.btnVoegAlbumToe.Size = New System.Drawing.Size(32, 32)
        Me.btnVoegAlbumToe.TabIndex = 4
        Me.btnVoegAlbumToe.Text = ">"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(320, 32)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(160, 24)
        Me.Label6.TabIndex = 3
        Me.Label6.Text = "Album voor de ontlener"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(24, 32)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(160, 24)
        Me.Label5.TabIndex = 2
        Me.Label5.Text = "Beschikbare albums"
        '
        'lvwVoorOntlener
        '
        Me.lvwVoorOntlener.FullRowSelect = True
        Me.lvwVoorOntlener.Location = New System.Drawing.Point(320, 56)
        Me.lvwVoorOntlener.MultiSelect = False
        Me.lvwVoorOntlener.Name = "lvwVoorOntlener"
        Me.lvwVoorOntlener.Size = New System.Drawing.Size(184, 120)
        Me.lvwVoorOntlener.TabIndex = 1
        Me.lvwVoorOntlener.View = System.Windows.Forms.View.List
        '
        'lvwBeschikbaar
        '
        Me.lvwBeschikbaar.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.clhAlbumNaam})
        Me.lvwBeschikbaar.FullRowSelect = True
        Me.lvwBeschikbaar.Location = New System.Drawing.Point(24, 56)
        Me.lvwBeschikbaar.MultiSelect = False
        Me.lvwBeschikbaar.Name = "lvwBeschikbaar"
        Me.lvwBeschikbaar.Size = New System.Drawing.Size(184, 120)
        Me.lvwBeschikbaar.Sorting = System.Windows.Forms.SortOrder.Ascending
        Me.lvwBeschikbaar.TabIndex = 0
        Me.lvwBeschikbaar.View = System.Windows.Forms.View.Details
        '
        'btnVoegOntleningToeVoegToe
        '
        Me.btnVoegOntleningToeVoegToe.Location = New System.Drawing.Point(432, 456)
        Me.btnVoegOntleningToeVoegToe.Name = "btnVoegOntleningToeVoegToe"
        Me.btnVoegOntleningToeVoegToe.Size = New System.Drawing.Size(112, 24)
        Me.btnVoegOntleningToeVoegToe.TabIndex = 5
        Me.btnVoegOntleningToeVoegToe.Text = "Voeg ontlening toe"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(40, 456)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(16, 16)
        Me.Label7.TabIndex = 6
        Me.Label7.Text = "ID:"
        '
        'txtToevoegId
        '
        Me.txtToevoegId.Location = New System.Drawing.Point(80, 456)
        Me.txtToevoegId.Name = "txtToevoegId"
        Me.txtToevoegId.ReadOnly = True
        Me.txtToevoegId.Size = New System.Drawing.Size(40, 20)
        Me.txtToevoegId.TabIndex = 7
        Me.txtToevoegId.Text = ""
        '
        'clhAlbumNaam
        '
        Me.clhAlbumNaam.Text = "Album Naam"
        Me.clhAlbumNaam.Width = 160
        '
        'frmOntleningToevoegen
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(600, 494)
        Me.Controls.Add(Me.txtToevoegId)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.btnVoegOntleningToeVoegToe)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.groupbox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.PictureBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.Name = "frmOntleningToevoegen"
        Me.Text = "Ontlening toevoegen"
        Me.GroupBox1.ResumeLayout(False)
        Me.groupbox2.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Event OntleningToegevoegd()


#Region "Opvulling"

    Private Sub frmOntleningToevoegen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        vulcboontlenersop()

        beschikbarealbumsopvullen()
        toevoegidmeegeven()
        'gewoon dat et minder GUI afh is kan je ook bij clsOntleningDA getVolgendOntleningID public zettn
        ' ipv die van txtToevoegID te halen ma maakt ni uit voor ik
    End Sub

  

    Private Sub vulcboontlenersop()
        'de cbo met ontleners worden opgevuld
        Dim arlOntleners As ArrayList
        arlOntleners = clsOntlenerDA.GetOntleners

        cboKiesOntlener.DataSource = arlOntleners

    End Sub



    Private Sub beschikbarealbumsopvullen()
        lvwBeschikbaar.Items.Clear()
        Dim arlBeschikbaarId As New ArrayList
        Dim arlOntleningen As ArrayList
        arlOntleningen = clsOntleningDA.GetOntleningen
        Dim iAlbumIdInBezit As Integer

        For Each oOntlening As clsOntlening In arlOntleningen
            If oOntlening.InBezit = "Neen" Then
                iAlbumIdInBezit = oOntlening.AlbumId
                arlBeschikbaarId.Add(iAlbumIdInBezit)
            End If
        Next

        Dim arlAlbums As New ArrayList
        arlAlbums = clsAlbumDA.GetAlbums

        Dim iInArr As Integer = 0
        For Each oAlbum As clsAlbum In arlAlbums

            For i As Integer = 0 To arlBeschikbaarId.Count - 1
                Dim iID As Integer = arlBeschikbaarId(i)

                If oAlbum.AlbumId = iID Then

                    iInArr = 1
                End If

            Next
            If iInArr = 0 Then
                Dim olitm As ListViewItem
                olitm = New ListViewItem
                olitm.Text = oAlbum.AlbumNaam
                olitm.Tag = oAlbum
                lvwBeschikbaar.Items.Add(olitm)
            End If
            iInArr = 0

        Next

    End Sub


    Private Sub toevoegidmeegeven()
        Dim nieuweid As Integer = 0

        Dim arlOntleningen As ArrayList
        arlOntleningen = clsOntleningDA.GetOntleningen

        For Each oOntlening As clsOntlening In arlOntleningen
            nieuweid += 1
        Next
        nieuweid += 1
        txtToevoegId.Text = Convert.ToString(nieuweid)
    End Sub

#End Region

#Region "Actions"

    Private Sub btnVoegAlbumToe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoegAlbumToe.Click
        If lvwVoorOntlener.Items.Count() = 0 Then
            albumtoevoegen()
        Else
            MsgBox("Een ontlener mag maar 1 album tegelijk ontlenen." & vbCrLf & "Maak het ontleende album eerst terug beschikbaar", MsgBoxStyle.OKOnly, "Maximum 1 album.")
        End If
    End Sub
    Private Sub btnVoegAlbumTerug_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoegAlbumTerug.Click
        albumterugzetten()
    End Sub

    Private Sub albumtoevoegen()
        'album naar rechts verplaatsen( niet meer in bezit )
        Dim iId As Integer
        Dim olitm As ListViewItem
        Dim oAlbum As clsAlbum
        For Each oItem As ListViewItem In lvwBeschikbaar.SelectedItems
            oAlbum = oItem.Tag
            lvwBeschikbaar.Items.Remove(oItem)
            lvwVoorOntlener.Items.Add(oItem)
        Next
    End Sub

    Private Sub albumterugzetten()
        'album terug inbezig zetten  (naar links)
        Dim iId As Integer
        Dim olitm As ListViewItem
        Dim oAlbum As clsAlbum
        For Each oItem As ListViewItem In lvwVoorOntlener.SelectedItems
            oAlbum = oItem.Tag
            lvwVoorOntlener.Items.Remove(oItem)
            lvwBeschikbaar.Items.Add(oItem)
        Next
    End Sub


#End Region

#Region "Controle en uitvoering"

    Private Sub btnVoegOntleningToeVoegToe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoegOntleningToeVoegToe.Click
        If gebruikerGekozen() Then
            voercontroleuit()
        End If

    End Sub

    Private Sub voercontroleuit()
        'controle als alles ingevuld is
        If cboKiesOntlener.Text <> "" And dtpTijd.Text <> "" And lvwVoorOntlener.Items.Count <> 0 Then


            controlebestaand()
        Else
            MsgBox("Gelieve geldige waarden in te geven om een nieuwe ontlening toe te voegen")
        End If
    End Sub

    Private Function gebruikerGekozen() As Boolean
        'validatie in case geen ontlener selected
        If cboKiesOntlener.SelectedIndex = -1 Then
            MsgBox("Gelieve een gebruiker te kiezen", MsgBoxStyle.OKOnly, "Ongeldige invoer")
            cboKiesOntlener.Focus()
            Return False

        Else
            Return True
        End If
    End Function


    Private Sub controlebestaand()
        'controle als de ontlening nog niet bestaat + uitvoering
        Dim arlOntleners As ArrayList
        Dim arlOntleners2 As ArrayList
        Dim sOntlenerAchternaam As String
        Dim iIdOntlener As Integer
        Dim iIndexOntlener As Integer

        '  iIndexOntlener = cboKiesOntlener.SelectedIndex + 1

        Dim cboOntlener As clsOntlener
        cboOntlener = cboKiesOntlener.SelectedItem
        iIndexOntlener = cboOntlener.Id



        arlOntleners = clsOntlenerDA.GetOntlenersById(iIndexOntlener)

        For Each oOntlener As clsOntlener In arlOntleners
            sOntlenerAchternaam = oOntlener.AchterNaam
        Next



        arlOntleners2 = clsOntlenerDA.GetOntleners2(sOntlenerAchternaam)
        For Each oOntlener As clsOntlener In arlOntleners2
            iIdOntlener = oOntlener.Id
        Next



        Dim iAantal As Integer
        iAantal = lvwVoorOntlener.Items.Count

        Dim iId As Integer
        Do While iAantal <> 0



            Dim sGevondenNaam As String

            For Each olitm As ListViewItem In lvwVoorOntlener.Items
                sGevondenNaam = olitm.Text

                Dim arlAlbums As New ArrayList
                arlAlbums = clsAlbumDA.GetAlbumByNaam(sGevondenNaam)
                For Each oAlbum As clsAlbum In arlAlbums
                    iId = oAlbum.AlbumId

                Next
            Next

            Dim sVandaag As Date

            Dim oOntlening2 As New clsOntlening(txtToevoegId.Text, Convert.ToString(iId), Convert.ToString(iIdOntlener), Convert.ToDateTime(dtpTijd.Text), Convert.ToDateTime(sVandaag.Today), "")

            Dim arlOntleningen As ArrayList
            arlOntleningen = clsOntleningDA.GetOntleningen
            Dim uitvoeren As Integer = 0

            For Each oOntlening As clsOntlening In arlOntleningen
                If oOntlening.OntlenerId = oOntlening2.OntlenerId And oOntlening.AlbumId = oOntlening2.AlbumId And oOntlening.DatumUit = oOntlening2.DatumUit Then
                    MsgBox("De ontlening die u wilt toevoegen bestaat al!")
                    allesledigen()
                    uitvoeren = 1
                End If

            Next
            If uitvoeren = 0 Then
                Try

                    Dim oOntlening As New clsOntlening(txtToevoegId.Text, Convert.ToString(iId), _
                    Convert.ToString(iIdOntlener), Convert.ToDateTime(dtpTijd.Text), _
                    Convert.ToDateTime(sVandaag.Today), "Neen")
                

                    clsOntleningDA.VoegOntleningToe(oOntlening)

                    allesledigen()

                    RaiseEvent OntleningToegevoegd()

                Catch ex As Exception
                    MsgBox(ex.ToString, MsgBoxStyle.OKCancel, "Er trad een onverwachte fout op bij het toevoegen van de ontlening")
                End Try
            End If
            iAantal -= 1
        Loop

    End Sub



    Private Sub allesledigen()
        cboKiesOntlener.Text = ""
        lvwVoorOntlener.Items.Clear()
    End Sub

#End Region



End Class
