' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.IO
Imports System.Text
Public Class Regroupement
    Dim Action_index, catégorie_index, Spécialité_index, Num_Org_Complémentaire_index, libellé_organisme_complémentaire_index, Type_convention_index, libellé_Type_convention_index, critère_secondaire_index, Id_org_signataire_index, Libellé_org_signataire_index As Integer

    Private Sub Regroupement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Datagrid()
    End Sub

    Private Sub Load_Datagrid()
        DataGridView1.Rows.Clear()
        DataGridView1.Columns.Clear()
        Dim Conventions_Datatable As DataTable = Me.Convention_Table_RegroupementsTableAdapter.GetData
        'ajout des colonnes dans le datagridview-----------------------------------------
        For c = 0 To Conventions_Datatable.Columns.Count - 1
            DataGridView1.Columns.Add(Conventions_Datatable.Columns.Item(c).ColumnName, Conventions_Datatable.Columns.Item(c).ColumnName)
        Next
        '--------------------------------------------------------------------------------
        For r = 0 To Conventions_Datatable.Rows.Count - 1 'parcours des conventions 
            DataGridView1.Rows.Add()
            For c = 0 To Conventions_Datatable.Columns.Count - 1
                DataGridView1.Item(c, r).Value = Conventions_Datatable(r).Item(c).ToString
            Next
        Next
        For c = 0 To DataGridView1.Columns.Count - 1
            Select Case DataGridView1.Columns.Item(c).Name
                Case "Action"
                    Action_index = c
                Case "catégorie"
                    catégorie_index = c
                Case "Spécialité"
                    Spécialité_index = c
                Case "Num_Org_Complémentaire"
                    Num_Org_Complémentaire_index = c
                Case "libellé_organisme_complémentaire"
                    libellé_organisme_complémentaire_index = c
                Case "Type_convention"
                    Type_convention_index = c
                Case "libellé_Type_convention"
                    libellé_Type_convention_index = c
                Case "critère_secondaire"
                    critère_secondaire_index = c
                Case "Id_org_signataire"
                    Id_org_signataire_index = c
                Case "Libellé_org_signataire"
                    Libellé_org_signataire_index = c
            End Select
        Next

    End Sub
    
    Private Sub Button_Importer_Table_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Importer_Table.Click
        OpenFileDialog1.InitialDirectory = Config.POP3_RepBAL + "Conventions\"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = "" Then Exit Sub
        Using sr As New StreamReader(OpenFileDialog1.FileName, Encoding.Default)
            Dim line As String
            Dim tab_data() As String
            ReDim tab_data(10)
            line = sr.ReadLine
            Do

                For i = 0 To 8
                    tab_data(i) = Mid(line, 1, InStr(line, ";") - 1)
                    line = Mid(line, InStr(line, ";") + 1)
                Next
                tab_data(9) = line
                If (tab_data(1).ToString = "50" Or tab_data(1).ToString = "") And (tab_data(2).ToString = "50" Or tab_data(2).ToString = "") Then
                    If tab_data(0) = "C" Or tab_data(0) = "M" Or tab_data(0) = "I" Then 'CDC annexe3 §5.3
                        If Me.Convention_Table_RegroupementsTableAdapter.Select_Count_By_Composite(tab_data(3), tab_data(5), tab_data(7), tab_data(8)) = 0 Then
                            Me.Convention_Table_RegroupementsTableAdapter.Insert(tab_data(0), tab_data(1), tab_data(2), tab_data(3), tab_data(4), tab_data(5), tab_data(6), _
                                                                               tab_data(7), tab_data(8), tab_data(9))
                        Else
                            'If MsgBox("MAJ Regroupement: " + tab_data(3) + ";" + tab_data(5) + ";" + tab_data(7) + ";" + tab_data(8), 36) Then
                            Me.Convention_Table_RegroupementsTableAdapter.Delete_by_Composite(tab_data(3), tab_data(5), tab_data(7), tab_data(8))
                            Me.Convention_Table_RegroupementsTableAdapter.Insert(tab_data(0), tab_data(1), tab_data(2), tab_data(3), tab_data(4), tab_data(5), tab_data(6), _
                                                                        tab_data(7), tab_data(8), tab_data(9))

                        End If
                    ElseIf tab_data(0) = "S" Then
                        Me.Convention_Table_RegroupementsTableAdapter.Delete_by_Composite(tab_data(3), tab_data(5), tab_data(7), tab_data(8))
                    End If
                End If

                    line = sr.ReadLine
            Loop Until line Is Nothing
        End Using
        Load_Datagrid()
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
       
        Id_org_signataireTextBox.Enabled = False
        Type_conventionTextBox.Enabled = False
        Critère_secondaireTextBox.Enabled = False
        Num_Org_ComplémentaireTextBox.Enabled = False

        For c = 0 To DataGridView1.Columns.Count - 1
            Select Case DataGridView1.Columns.Item(c).Name
                Case "Action"
                    ActionTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "catégorie"
                    CatégorieTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "Spécialité"
                    SpécialitéTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "Num_Org_Complémentaire"
                    Num_Org_ComplémentaireTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "libellé_organisme_complémentaire"
                    Libellé_organisme_complémentaireTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "Type_convention"
                    Type_conventionTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "libellé_Type_convention"
                    Libellé_Type_conventionTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "critère_secondaire"
                    Critère_secondaireTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "Id_org_signataire"
                    Id_org_signataireTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
                Case "Libellé_org_signataire"
                    Libellé_org_signataireTextBox.Text = DataGridView1.Item(c, DataGridView1.CurrentRow.Index).Value
            End Select
        Next

    End Sub

    Private Sub Button_Modifier_Regroupement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Modifier_Regroupement.Click
        If ActionTextBox.Text = "C" Or ActionTextBox.Text = "M" Or ActionTextBox.Text = "I" Then 'CDC annexe3 §5.3
            Dim r As Integer = DataGridView1.CurrentRow.Index

            Me.Convention_Table_RegroupementsTableAdapter.Delete_ligne(DataGridView1.Item(Action_index, r).Value.ToString, DataGridView1.Item(catégorie_index, r).Value.ToString, _
            DataGridView1.Item(Spécialité_index, r).Value.ToString, DataGridView1.Item(Num_Org_Complémentaire_index, r).Value.ToString, _
            DataGridView1.Item(libellé_organisme_complémentaire_index, r).Value.ToString, DataGridView1.Item(Type_convention_index, r).Value.ToString, _
            DataGridView1.Item(libellé_Type_convention_index, r).Value.ToString, DataGridView1.Item(critère_secondaire_index, r).Value.ToString, _
            DataGridView1.Item(Id_org_signataire_index, r).Value.ToString, DataGridView1.Item(Libellé_org_signataire_index, r).Value.ToString)

            Me.Convention_Table_RegroupementsTableAdapter.Delete_ligne(ActionTextBox.Text, CatégorieTextBox.Text, SpécialitéTextBox.Text, Num_Org_ComplémentaireTextBox.Text, _
            Libellé_organisme_complémentaireTextBox.Text, Type_conventionTextBox.Text, Type_conventionTextBox.Text, Critère_secondaireTextBox.Text, _
            Id_org_signataireTextBox.Text, Libellé_org_signataireTextBox.Text)

            Me.Convention_Table_RegroupementsTableAdapter.Insert(ActionTextBox.Text, CatégorieTextBox.Text, SpécialitéTextBox.Text, Num_Org_ComplémentaireTextBox.Text, _
            Libellé_organisme_complémentaireTextBox.Text, Type_conventionTextBox.Text, Libellé_Type_conventionTextBox.Text, Critère_secondaireTextBox.Text, _
            Id_org_signataireTextBox.Text, Libellé_org_signataireTextBox.Text)
            'DataGridView1.Item(Action_index, r).Value = ActionTextBox.Text
            'DataGridView1.Item(catégorie_index, r).Value = CatégorieTextBox.Text
            'DataGridView1.Item(Spécialité_index, r).Value = SpécialitéTextBox.Text
            'DataGridView1.Item(Num_Org_Complémentaire_index, r).Value = Num_Org_ComplémentaireTextBox.Text
            'DataGridView1.Item(libellé_organisme_complémentaire_index, r).Value = Libellé_organisme_complémentaireTextBox.Text
            'DataGridView1.Item(Type_convention_index, r).Value = Type_conventionTextBox.Text
            'DataGridView1.Item(libellé_Type_convention_index, r).Value = Libellé_Type_conventionTextBox.Text
            'DataGridView1.Item(critère_secondaire_index, r).Value = Critère_secondaireTextBox.Text
            'DataGridView1.Item(Id_org_signataire_index, r).Value = Id_org_signataireTextBox.Text
            'DataGridView1.Item(Libellé_org_signataire_index, r).Value = Libellé_org_signataireTextBox.Text
            Load_Datagrid()
        ElseIf ActionTextBox.Text = "S" Then 'CDC annexe3 §5.3
            Me.Convention_Table_RegroupementsTableAdapter.Delete_ligne(ActionTextBox.Text, CatégorieTextBox.Text, SpécialitéTextBox.Text, Num_Org_ComplémentaireTextBox.Text, _
           Libellé_organisme_complémentaireTextBox.Text, Type_conventionTextBox.Text, Type_conventionTextBox.Text, Critère_secondaireTextBox.Text, _
           Id_org_signataireTextBox.Text, Libellé_org_signataireTextBox.Text)
            Load_Datagrid()
        End If
    End Sub

    Private Sub Button_Ajouter_Convention_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Ajouter_Regroupement.Click
        For Each truc In Me.Controls
            If truc.GetType.ToString = "System.Windows.Forms.TextBox" Then
                truc.Clear()
            End If
        Next
        Id_org_signataireTextBox.Enabled = True
        Type_conventionTextBox.Enabled = True
        Critère_secondaireTextBox.Enabled = True
        Num_Org_ComplémentaireTextBox.Enabled = True
        Button_Ajouter_Regroupement.Enabled = False
        Button_Modifier_Regroupement.Enabled = False
        Button_Supprimer_Regroupement.Enabled = False
        Button_Importer_Table.Enabled = False
        Button_Ajout_OK.Visible = True
        Button_Ajout_Annuller.Visible = True
    End Sub

    Private Sub Insert_Regroupement()
        If ActionTextBox.Text = "C" Or ActionTextBox.Text = "M" Or ActionTextBox.Text = "I" Then 'CDC annexe3 §5.3
            Me.Convention_Table_RegroupementsTableAdapter.Delete_ligne(ActionTextBox.Text, CatégorieTextBox.Text, SpécialitéTextBox.Text, Num_Org_ComplémentaireTextBox.Text, _
            Libellé_organisme_complémentaireTextBox.Text, Type_conventionTextBox.Text, Type_conventionTextBox.Text, Critère_secondaireTextBox.Text, _
            Id_org_signataireTextBox.Text, Libellé_org_signataireTextBox.Text)
            Me.Convention_Table_RegroupementsTableAdapter.Insert(ActionTextBox.Text, CatégorieTextBox.Text, SpécialitéTextBox.Text, Num_Org_ComplémentaireTextBox.Text, _
            Libellé_organisme_complémentaireTextBox.Text, Type_conventionTextBox.Text, Type_conventionTextBox.Text, Critère_secondaireTextBox.Text, _
            Id_org_signataireTextBox.Text, Libellé_org_signataireTextBox.Text)
            Load_Datagrid()
        ElseIf ActionTextBox.Text = "S" Then 'CDC annexe3 §5.3
            Me.Convention_Table_RegroupementsTableAdapter.Delete_ligne(ActionTextBox.Text, CatégorieTextBox.Text, SpécialitéTextBox.Text, Num_Org_ComplémentaireTextBox.Text, _
           Libellé_organisme_complémentaireTextBox.Text, Type_conventionTextBox.Text, Type_conventionTextBox.Text, Critère_secondaireTextBox.Text, _
           Id_org_signataireTextBox.Text, Libellé_org_signataireTextBox.Text)
            Load_Datagrid()
        End If

    End Sub

    Private Sub Button_Ajout_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Ajout_OK.Click
        Insert_Regroupement()
        Id_org_signataireTextBox.Enabled = False
        Type_conventionTextBox.Enabled = False
        Critère_secondaireTextBox.Enabled = False
        Num_Org_ComplémentaireTextBox.Enabled = False
        Button_Ajouter_Regroupement.Enabled = True
        Button_Modifier_Regroupement.Enabled = True
        Button_Supprimer_Regroupement.Enabled = True
        Button_Importer_Table.Enabled = True
        Button_Ajout_OK.Visible = False
        Button_Ajout_Annuller.Visible = False
        Load_Datagrid()
    End Sub

    Private Sub Button_Ajout_Annuller_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Ajout_Annuller.Click
        Id_org_signataireTextBox.Enabled = False
        Type_conventionTextBox.Enabled = False
        Critère_secondaireTextBox.Enabled = False
        Num_Org_ComplémentaireTextBox.Enabled = False
        Button_Ajouter_Regroupement.Enabled = True
        Button_Modifier_Regroupement.Enabled = True
        Button_Supprimer_Regroupement.Enabled = True
        Button_Importer_Table.Enabled = True
        Button_Ajout_OK.Visible = False
        Button_Ajout_Annuller.Visible = False

    End Sub

    Private Sub Button_Supprimer_Regroupement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Supprimer_Regroupement.Click
        If MsgBox("Attention, Suppression definitive," + Chr(10) + "continuer?", 17) = MsgBoxResult.Ok Then
            Me.Convention_Table_RegroupementsTableAdapter.Delete_by_Composite(Num_Org_ComplémentaireTextBox.Text, Type_conventionTextBox.Text, _
                                                                               Critère_secondaireTextBox.Text, Id_org_signataireTextBox.Text)
            Load_Datagrid()
        End If
    End Sub
End Class