' 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
Public Class Messagerie_Interne

    Public Current_ID_user As String

 

    Private Sub Nouveau_Button_Click(sender As Object, e As EventArgs) Handles Nouveau_Button.Click
        Nouveau_Panel.BringToFront()
        
    End Sub

    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        Dim date_default As New Date(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second, 0)
        Dim date_action As New Date(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, 0, 0, 0, 0)
        Dim type_message As String = ""
        If RadioButton1.Checked = True Then
            type_message = "Info"
        ElseIf RadioButton2.Checked = True Then
            type_message = "Commande"
        ElseIf RadioButton3.Checked = True Then
            type_message = "Tâche"
        End If
        If type_message = "" Then
            MyMsg_Box.Presentation("Officine !", "Renseignez TYPE MESSAGE", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        For Each item In To_CheckedListBox.CheckedItems
            Dim num_message As Integer = BDD.Messagerie_interneTableAdapter.Select_Max_Num + 1
            BDD.Messagerie_interneTableAdapter.Insert(num_message, Current_ID_user, date_default, item.ToString, date_default, "", date_default, "", _
                                                      Text_RichTextBox.Text, type_message, "non lu", Désignation_TextBox.Text, CInt(Quantité.Text), _
                                                      date_action, CodeProd_TextBox.Text, PharmaML_CheckBox.Checked)
        Next
         Bandeau_info_bas_show("Transmis!")
        System.Threading.Thread.Sleep(1000)
        Text_RichTextBox.Text = ""
        CodeProd_TextBox.Text = ""
        Désignation_TextBox.Text = ""
        Quantité.Text = ""
        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
    End Sub

   

   

    Private Sub Reçus_Button_Click(sender As Object, e As EventArgs) Handles Reçus_Button.Click
        Reçus_Panel.BringToFront()
    End Sub

    Private Sub affichage_Reçus()
        If Reçus_affiches_nbre_TextBox.Text = "" Or Reçus_De_ComboBox.Text = "" Or Reçus_Type_ComboBox.Text = "" Or Reçus_Etat_ComboBox.Text = "" Or Current_ID_user = "" Then Exit Sub

        Dim reçus = BDD.Messagerie_interneTableAdapter.GetData_AffichageBoites(CInt(Reçus_affiches_nbre_TextBox.Text), Current_ID_user, Reçus_De_ComboBox.Text, Reçus_Type_ComboBox.Text, Reçus_Etat_ComboBox.Text)
        Reçus_DataGridView.Rows.Clear()
        For r = 0 To reçus.Rows.Count - 1
            Reçus_DataGridView.Rows.Add()
        Next
        For r = 0 To reçus.Rows.Count - 1
            Reçus_DataGridView.Item(Reçus_Column_Num.Index, r).Value = reçus(r).Num
            Reçus_DataGridView.Item(Reçus_Column_Etat.Index, r).Value = reçus(r).Etat
            Reçus_DataGridView.Item(Reçus_Column_From.Index, r).Value = reçus(r).From_
            Reçus_DataGridView.Item(Reçus_Column_Date_from.Index, r).Value = reçus(r).Date_From.ToShortDateString
            Reçus_DataGridView.Item(Reçus_Column_Type_Message.Index, r).Value = reçus(r).Type_Message
            Reçus_DataGridView.Item(Reçus_Column_Date_Action.Index, r).Value = reçus(r).Date_Action.ToShortDateString
            If reçus(r).Text.Length > 20 Then
                Reçus_DataGridView.Item(Reçus_Column_Text.Index, r).Value = Mid(reçus(r).Text, 1, 20) & " ..."
            Else
                Reçus_DataGridView.Item(Reçus_Column_Text.Index, r).Value = reçus(r).Text
            End If
            Reçus_DataGridView.Item(Reçus_Column_Prod.Index, r).Value = reçus(r).Prod_Désignation & " " & reçus(r).Prod_CodeProd
        Next

    End Sub

    Private Sub Reçus_affiches_nbre_TextBox_TextChanged(sender As Object, e As EventArgs) Handles Reçus_affiches_nbre_TextBox.TextChanged
        Try
            CInt(Reçus_affiches_nbre_TextBox.Text).ToString()
        Catch ex As Exception
            Reçus_affiches_nbre_TextBox.Text = 20
        End Try
        affichage_Reçus()
    End Sub

    Private Sub Reçus_De_ComboBox_TextChanged(sender As Object, e As EventArgs) Handles Reçus_De_ComboBox.TextChanged
        affichage_Reçus()
    End Sub

    Private Sub Reçus_Type_ComboBox_TextChanged(sender As Object, e As EventArgs) Handles Reçus_Type_ComboBox.TextChanged
        affichage_Reçus()
    End Sub

    Private Sub Reçus_Etat_ComboBox_TextChanged(sender As Object, e As EventArgs) Handles Reçus_Etat_ComboBox.TextChanged
        affichage_Reçus()
    End Sub

    Private Sub Messagerie_Interne_Load(sender As Object, e As EventArgs) Handles Me.Load
        To_CheckedListBox.Items.Clear()
        Dim To_ = BDD.UsersTableAdapter.GetData
        For r = 0 To To_.Rows.Count - 1
            To_CheckedListBox.Items.Add(To_(r).login)
        Next
    End Sub

    Private Sub Messagerie_Interne_Shown(sender As Object, e As EventArgs) Handles Me.Shown
       Dim my_USERS_Select As New USERS_Select
        my_USERS_Select.Location = New Point(Me.Location.X + 100, Me.Location.Y + 100)
        my_USERS_Select.ShowDialog()
        If password_validated = True Then
            Current_ID_user = current_User.ID
            ID_User_Label.Text = Current_ID_user
            Dim users = BDD.UsersTableAdapter.GetData
            Reçus_De_ComboBox.Items.Clear()
            Reçus_De_ComboBox.Items.Add("tous")
            For r = 0 To users.Rows.Count - 1
                Reçus_De_ComboBox.Items.Add(users(r).login)
            Next
            affichage_Reçus()
        Else
            Me.Close()
        End If
    End Sub

    Private Sub Reçus_DataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Reçus_DataGridView.CellClick
        If e.RowIndex < 0 Then Exit Sub
        Select Case e.ColumnIndex
            Case Reçus_Column_Reset.Index
                BDD.Messagerie_interneTableAdapter.Update_Etat_By_Num("non lu", CInt(Reçus_DataGridView.Item(Reçus_Column_Num.Index, e.RowIndex).Value))
                affichage_Reçus()
            Case Else
                Messagerie_interne_affiche.affiche_message(Reçus_DataGridView.Item(Reçus_Column_Num.Index, e.RowIndex).Value)
                affichage_Reçus()
        End Select

    End Sub


    

    Private Sub Reçus_DataGridView_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles Reçus_DataGridView.CellPainting
        If e.RowIndex < 0 Then Exit Sub
        Select Case e.ColumnIndex
            Case Reçus_Column_Reset.Index
                e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentForeground)
                e.Graphics.DrawImage(officine.My.Resources.reset2, e.CellBounds)
                e.Handled = True
        End Select
    End Sub

    Private Sub Reçus_DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Reçus_DataGridView.CellContentClick

    End Sub
End Class