# --- Début de votre script principal (exemple) ---
# Assurez-vous de définir le chemin de votre fichier au début du script
$script:journalFile = "C:\Users\walid\Desktop\123\fichierjournal.json"
# -------------------------------------------------
<#
$btnJournal.Add_Click({
if ($list.SelectedItems.Count -gt 0) {
[string]$selectedPolice = $list.SelectedItems.Item(0).Text
$selectedClient = $list.SelectedItems.Item(0).SubItems.Text
$currentUser = $script:currentUser # Assurez-vous que cette variable existe
# Appel de la fonction refactorisée avec le nom d'utilisateur
Show-Journal -police $selectedPolice -client $selectedClient -userName $currentUser
} else {
[System.Windows.Forms.MessageBox]::Show("Veuillez sélectionner une police d'assurance.", "Erreur de sélection", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
}
})
#>
# --- FONCTIONS DE GESTION DES DONNÉES ROBUSTES ---
Function Get-JournalData {
# Initialise un tableau de hachage vide par défaut
$data = @{}
# Vérifie si le fichier existe avant de tenter de le lire
if (Test-Path -Path $script:journalFile) {
try {
# Lit le contenu et le convertit de JSON en objet PowerShell
$data = Get-Content -Path $script:journalFile -Encoding Utf8 | ConvertFrom-Json
} catch {
Write-Error "Erreur lors de la lecture du fichier JSON: $_"
# Vous pourriez ajouter une notification UI ici si vous le souhaitez
}
}
# Renvoie soit les données lues (PSCustomObject), soit le tableau vide (@{})
return $data
}
Function Save-JournalData {
# Accepte n'importe quel objet, gérant ainsi le PSCustomObject de Get-JournalData
param($data)
try {
# Convertit l'objet en JSON et écrit dans le fichier
$data | ConvertTo-Json -Depth 10 | Out-File $script:journalFile -Encoding utf8
} catch {
Write-Error "Erreur lors de l'écriture dans le fichier JSON: $_"
}
}
# --- FONCTION PRINCIPALE REFACTORISÉE (avec la correction de syntaxe) ---
<#
Function Show-Journal {
param(
[string]$police,
[string]$client
# Nous n'avons plus besoin de passer $listView ici,
# car nous n'y faisons référence que dans le gestionnaire d'événements principal.
)
# Récupération des données via la fonction centralisée
# $journalData sera un PSCustomObject ou un Hashtable vide
$journalData = Get-JournalData
# --- CONFIGURATION FENÊTRE ---
$jouForm = New-Object System.Windows.Forms.Form
$jouForm.Text = "JOURNAL"
$jouForm.Size = New-Object System.Drawing.Size(650, 750)
$jouForm.BackColor = [System.Drawing.Color]::FromArgb(10, 10, 12)
$jouForm.StartPosition = "CenterParent"
$jouForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$jouForm.MaximizeBox = $false
$jouForm.MinimizeBox = $false
# ... (Code pour les panels, labels, listbox inchangé) ...
$panelHeader = New-Object System.Windows.Forms.Panel; $panelHeader.SetBounds(0, 0, 650, 100); $panelHeader.BackColor = [System.Drawing.Color]::FromArgb(15, 15, 25); $jouForm.Controls.Add($panelHeader)
$lblClient = New-Object System.Windows.Forms.Label; $lblClient.Text = "OPERATOR_TARGET: $($client.ToUpper())"; $lblClient.SetBounds(25, 20, 600, 25); $lblClient.ForeColor = [System.Drawing.Color]::Cyan; $lblClient.Font = New-Object System.Drawing.Font("Segoe UI", 12, [System.Drawing.FontStyle]::Bold); $panelHeader.Controls.Add($lblClient)
$lblPolice = New-Object System.Windows.Forms.Label; $lblPolice.Text = "REFERENCE_ID: $police"; $lblPolice.SetBounds(25, 45, 600, 20); $lblPolice.ForeColor = [System.Drawing.Color]::Gray; $lblPolice.Font = New-Object System.Drawing.Font("Consolas", 10); $panelHeader.Controls.Add($lblPolice)
$logBox = New-Object System.Windows.Forms.ListBox; $logBox.SetBounds(25, 120, 585, 380); $logBox.BackColor = [System.Drawing.Color]::FromArgb(10, 10, 12); $logBox.ForeColor = [System.Drawing.Color]::FromArgb(140, 198, 63); $logBox.Font = New-Object System.Drawing.Font("Consolas", 11); $logBox.BorderStyle = "None"; $jouForm.Controls.Add($logBox)
$lineSep = New-Object System.Windows.Forms.Panel; $lineSep.SetBounds(25, 510, 585, 2); $lineSep.BackColor = [System.Drawing.Color]::FromArgb(0, 255, 255); $jouForm.Controls.Add($lineSep)
# Ligne corrigée avec [System.Drawing.Color]::FromArgb
$txtNote = New-Object System.Windows.Forms.TextBox; $txtNote.Multiline = $true; $txtNote.SetBounds(25, 540, 585, 80); $txtNote.BackColor = [System.Drawing.Color]::FromArgb(25, 25, 30); $txtNote.ForeColor = [System.Drawing.Color]::White; $txtNote.Font = New-Object System.Drawing.Font("Segoe UI", 12); $txtNote.BorderStyle = "FixedSingle"; $jouForm.Controls.Add($txtNote)
$btnAddNote = New-Object System.Windows.Forms.Button; $btnAddNote.Text = "CONFIRMER L'ENREGISTREMENT"; $btnAddNote.SetBounds(25, 640, 585, 50); $btnAddNote.FlatStyle = "Flat"; $btnAddNote.FlatAppearance.BorderColor = [System.Drawing.Color]::Cyan; $btnAddNote.FlatAppearance.BorderSize = 2; $btnAddNote.ForeColor = [System.Drawing.Color]::Cyan; $btnAddNote.Font = New-Object System.Drawing.Font("Segoe UI", 11, [System.Drawing.FontStyle]::Bold); $jouForm.Controls.Add($btnAddNote)
# Chargement initial des notes
if ($journalData.PSObject.Properties[$police]) {
foreach ($n in $journalData."$police") { [void]$logBox.Items.Add($n) }
}
# Logique d'enregistrement
$btnAddNote.Add_Click({
if ($txtNote.Text.Trim() -ne "") {
$ts = Get-Date -Format "dd/MM/yyyy HH:mm"
$nouvelleNote = "[$ts] ⚡ $($txtNote.Text.Replace('"',"'"))"
$notesExistantes = @()
if ($journalData.PSObject.Properties[$police]) {
$notesExistantes = @($journalData."$police")
}
$nouveauTableau = @($nouvelleNote) + $notesExistantes
# Met à jour la variable locale et sauvegarde via la fonction centralisée
$journalData | Add-Member -MemberType NoteProperty -Name $police -Value $nouveauTableau -Force
Save-JournalData -data $journalData
[void]$logBox.Items.Insert(0, $nouvelleNote)
$txtNote.Clear()
}
})
$jouForm.ShowDialog()
}
#>
# --- N'OUBLIEZ PAS VOS FONCTIONS Get-JournalData et Save-JournalData intactes ---
# Function Get-JournalData { ... }
# Function Save-JournalData { ... }
# -------------------------------------------------------------------------------
Function Show-Journal {
param(
[string]$police,
[string]$client,
[string]$userName # Nouveau paramètre pour l'utilisateur
)
$journalData = Get-JournalData
# --- CONFIGURATION FENÊTRE (Thème Pro STAR) ---
$jouForm = New-Object System.Windows.Forms.Form
$jouForm.Text = "JOURNAL DE RECOUVREMENT"
$jouForm.Size = New-Object System.Drawing.Size(650, 750)
$jouForm.BackColor = [System.Drawing.Color]::FromArgb(20, 25, 35) # Fond sombre pro
$jouForm.StartPosition = "CenterParent"
$jouForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$jouForm.MaximizeBox = $false
$jouForm.MinimizeBox = $false
# --- BANDEAU SUPÉRIEUR AMÉLIORÉ ---
$panelHeader = New-Object System.Windows.Forms.Panel
$panelHeader.SetBounds(0, 0, 650, 100)
$panelHeader.BackColor = [System.Drawing.Color]::FromArgb(30, 40, 55)
$jouForm.Controls.Add($panelHeader)
# Label Titre (Centre Agence Star)
$lblTitle = New-Object System.Windows.Forms.Label
$lblTitle.Text = "AGENCE STAR - SUIVI DE DOSSIER"
$lblTitle.SetBounds(25, 15, 600, 25)
$lblTitle.ForeColor = [System.Drawing.Color]::FromArgb(0, 191, 255) # Bleu clair (Star color)
$lblTitle.Font = New-Object System.Drawing.Font("Segoe UI", 14, [System.Drawing.FontStyle]::Bold)
$panelHeader.Controls.Add($lblTitle)
# Label Client
$lblClient = New-Object System.Windows.Forms.Label
$lblClient.Text = "Client: $($client.ToUpper())"
$lblClient.SetBounds(25, 45, 600, 20)
$lblClient.ForeColor = [System.Drawing.Color]::WhiteSmoke
$lblClient.Font = New-Object System.Drawing.Font("Segoe UI", 10, [System.Drawing.FontStyle]::Regular)
$panelHeader.Controls.Add($lblClient)
# Label Police
$lblPolice = New-Object System.Windows.Forms.Label
$lblPolice.Text = "Police ID: $police"
$lblPolice.SetBounds(25, 65, 600, 20)
$lblPolice.ForeColor = [System.Drawing.Color]::Gray
$lblPolice.Font = New-Object System.Drawing.Font("Consolas", 10)
$panelHeader.Controls.Add($lblPolice)
# --- LISTBOX AMÉLIORÉE (Consolas pour un look log/pro) ---
$logBox = New-Object System.Windows.Forms.ListBox
$logBox.SetBounds(25, 120, 585, 380)
$logBox.BackColor = [System.Drawing.Color]::FromArgb(30, 40, 55)
$logBox.ForeColor = [System.Drawing.Color]::FromArgb(140, 198, 63) # Vert pour les notes OK
$logBox.Font = New-Object System.Drawing.Font("Consolas", 10)
$logBox.BorderStyle = "FixedSingle" # Bordure simple pour un look pro
$jouForm.Controls.Add($logBox)
# Séparateur Stylisé
$lineSep = New-Object System.Windows.Forms.Panel
$lineSep.SetBounds(25, 510, 585, 2)
$lineSep.BackColor = [System.Drawing.Color]::FromArgb(0, 191, 255) # Bleu Star
$jouForm.Controls.Add($lineSep)
# Chargement initial des notes
if ($journalData.PSObject.Properties[$police]) {
foreach ($n in $journalData."$police") { [void]$logBox.Items.Add($n) }
}
# --- INPUT ---
$txtNote = New-Object System.Windows.Forms.TextBox
$txtNote.Multiline = $true
$txtNote.SetBounds(25, 540, 585, 80)
$txtNote.BackColor = [System.Drawing.Color]::FromArgb(50, 60, 75)
$txtNote.ForeColor = [System.Drawing.Color]::White
$txtNote.Font = New-Object System.Drawing.Font("Segoe UI", 10)
$txtNote.BorderStyle = "FixedSingle"
$jouForm.Controls.Add($txtNote)
# Label "Agent" au-dessus du bouton
$lblAgent = New-Object System.Windows.Forms.Label
$lblAgent.Text = "Agent connecte: $($userName.ToUpper())"
$lblAgent.SetBounds(25, 625, 300, 15)
$lblAgent.ForeColor = [System.Drawing.Color]::FromArgb(0, 191, 255)
$lblAgent.Font = New-Object System.Drawing.Font("Segoe UI", 8)
$jouForm.Controls.Add($lblAgent)
# --- BOUTON STYLISÉ ---
$btnAddNote = New-Object System.Windows.Forms.Button
$btnAddNote.Text = "ENREGISTRER LA NOTE ET AUDITER"
$btnAddNote.SetBounds(25, 645, 585, 50)
$btnAddNote.FlatStyle = "Flat"
$btnAddNote.FlatAppearance.BorderColor = [System.Drawing.Color]::FromArgb(0, 191, 255)
$btnAddNote.FlatAppearance.BorderSize = 1
$btnAddNote.ForeColor = [System.Drawing.Color]::White
$btnAddNote.BackColor = [System.Drawing.Color]::FromArgb(30, 40, 55)
$btnAddNote.Font = New-Object System.Drawing.Font("Segoe UI", 11, [System.Drawing.FontStyle]::Bold)
$jouForm.Controls.Add($btnAddNote)
# Logique d'enregistrement (maintenant avec l'utilisateur)
$btnAddNote.Add_Click({
if ($txtNote.Text.Trim() -ne "") {
$ts = Get-Date -Format "dd/MM/yyyy HH:mm"
# Structure de note professionnelle: [Timestamp] [User] ⚡ Note
$nouvelleNote = "[$ts] [$userName] ⚡ $($txtNote.Text.Replace('"',"'"))"
$notesExistantes = @()
if ($journalData.PSObject.Properties[$police]) {
$notesExistantes = @($journalData."$police")
}
$nouveauTableau = @($nouvelleNote) + $notesExistantes
$journalData | Add-Member -MemberType NoteProperty -Name $police -Value $nouveauTableau -Force
Save-JournalData -data $journalData
[void]$logBox.Items.Insert(0, $nouvelleNote)
$txtNote.Clear()
}
})
$jouForm.ShowDialog()
}