' 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/>.
Public Class Messagerie_interne_affiche
    Public message_row As PharmaDataSet.Messagerie_interneRow

    Private Sub Messagerie_interne_affiche_Load(sender As Object, e As EventArgs) Handles Me.Load
        If message_row.Etat <> "non lu" Then
            Plus_Tard_Button.Visible = False
        End If
    End Sub
    Private Sub Plus_Tard_Button_Click(sender As Object, e As EventArgs) Handles Plus_Tard_Button.Click
        Dim First_read As New Date
        If message_row.First_read = message_row.Date_From Then
            First_read = Now
        Else
            First_read = message_row.First_read
        End If
        BDD.Messagerie_interneTableAdapter.Update_Reader(First_read, "PLUS TARD", Now, "PLUS TARD", "non lu", DateAdd(DateInterval.Hour, CDbl(Rappel_Heures_TextBox.Text), Now), message_row.Num)
        Me.Close()
    End Sub

    
    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        Dim First_read As New Date
        Dim First_Action As String
        If message_row.First_read = message_row.Date_From Then
            First_read = Now
            First_Action = "OK"
        Else
            First_read = message_row.First_read
            First_Action = message_row.First_Action
        End If
        BDD.Messagerie_interneTableAdapter.Update_Reader(First_read, First_Action, Now, "OK", "lu", Now, message_row.Num)
        If message_row.Type_Message = "Commande" Then
            If message_row.Prod_PharmaML = True Then
                Commandes_PharmaML.Recherche_TextBox.Text = message_row.Prod_CodeProd
                Commandes_PharmaML.Visible = True
                Commandes_PharmaML.TopMost = True
                Commandes_PharmaML.Focus()
                Commandes_PharmaML.Recherche_TextBox.Focus()
                Commandes_PharmaML.SelectNextControl(Commandes_PharmaML.Recherche_TextBox, True, True, True, True)
                If Commandes_PharmaML.Recherche_DataGridView.Rows.Count > 0 Then
                    Commandes_PharmaML.Recherche_DataGridView.Rows(0).Selected = True
                    Commandes_PharmaML.Recherche_DataGridView_CellClick(Commandes_PharmaML.Recherche_DataGridView, New DataGridViewCellEventArgs(0, 0))
                    Commandes_PharmaML.Produits_DataGridView.Item(Commandes_PharmaML.Quantité.Index, Commandes_PharmaML.Produits_DataGridView.Rows.Count - 1).Value = message_row.Prod_Quantité
                    Commandes_PharmaML.Produits_DataGridView_CellValidated(Commandes_PharmaML.Produits_DataGridView, New DataGridViewCellEventArgs(Commandes_PharmaML.Quantité.Index, Commandes_PharmaML.Produits_DataGridView.Rows.Count - 1))
                    If Val(Commandes_PharmaML.Produits_DataGridView.Item(Commandes_PharmaML.en_commande.Index, Commandes_PharmaML.Produits_DataGridView.Rows.Count - 1).Value) > 0 Then
                        MyMsg_Box.Presentation("Officine !", "Le produit est déja en commande." & vbCrLf & "Verifier les quantités commandées.", False, "", False, "", True, "OK")
                        MyMsg_Box.ShowDialog()
                    End If
                End If
            End If

            'commandes_.BringToFront()
            Me.Close()
        End If
        Me.Close()
    End Sub

    Public Sub Check_UNREAD_messages(ByVal User As String)
        Dim messages = BDD.Messagerie_interneTableAdapter.GetDataBy_To_Etat_DateAction(User, "non lu", Now)
        For r = 0 To messages.Rows.Count - 1
            affiche_message(messages(r).Num)
        Next
    End Sub

    Public Sub affiche_message(ByVal num As Integer)
        Dim message_row = BDD.Messagerie_interneTableAdapter.GetDataBy_Num(num).Item(0)
        Dim message As New Messagerie_interne_affiche
        message.message_row = message_row
        Dim texte As String = _
            "Message pour " & message_row.To_ & ", envoyé par " & message_row.From_ & " créé le " & message_row.Date_From.ToLongDateString & vbCrLf

        Select Case message_row.Type_Message
            Case "Info"
                texte = texte & "INFORMATION" & vbCrLf
        End Select

        texte = texte & message_row.Text & vbCrLf

        If message_row.Prod_CodeProd <> "" Or message_row.Prod_Désignation <> "" Then
            texte = texte & "Produit concerné par ce message:" & vbCrLf & message_row.Prod_Désignation & " " & message_row.Prod_CodeProd & vbCrLf & "Quantité " & message_row.Prod_Quantité.ToString & vbCrLf
        End If
        message.RichTextBox1.Text = texte
        message.ShowDialog()

    End Sub

    Private Sub Rappel_Heures_TextBox_TextChanged(sender As Object, e As EventArgs) Handles Rappel_Heures_TextBox.TextChanged
        If Rappel_Heures_TextBox.Text = "" Then Exit Sub
        Try
            CInt(Rappel_Heures_TextBox.Text).ToString()
        Catch ex As Exception
            Rappel_Heures_TextBox.Text = ""
        End Try
    End Sub

    Private Sub Rappel_Heures_TextBox_Validated(sender As Object, e As EventArgs) Handles Rappel_Heures_TextBox.Validated
        If Rappel_Heures_TextBox.Text = "" Then
            Rappel_Heures_TextBox.Text = "1"
        End If
    End Sub
End Class