-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineCheckHistoryForm.xaml.cs
More file actions
95 lines (86 loc) · 3.35 KB
/
MachineCheckHistoryForm.xaml.cs
File metadata and controls
95 lines (86 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using MachineCheck.Models;
using MachineCheck.Services;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace MachineCheck
{
public partial class MachineCheckHistoryForm : Window
{
private readonly long _machineId;
private readonly DatabaseService _dbService;
public ObservableCollection<MachineCheckRecord> MachineChecks { get; set; } = new ObservableCollection<MachineCheckRecord>();
public MachineCheckHistoryForm(long machineId, DatabaseService dbService)
{
InitializeComponent();
_machineId = machineId;
_dbService = dbService;
dgHistory.ItemsSource = MachineChecks;
_ = LoadHistoryAsync();
}
private async Task LoadHistoryAsync()
{
try
{
var history = await _dbService.GetMachineCheckHistoryAsync(_machineId);
MachineChecks.Clear();
foreach (var check in history)
{
MachineChecks.Add(check);
}
}
catch (Exception ex)
{
MessageBox.Show($"Błąd ładowania historii: {ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async void DeleteCheckButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn && btn.DataContext is MachineCheckRecord check)
{
var result = MessageBox.Show("Czy na pewno chcesz usunąć ten przegląd?", "Potwierdzenie", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
try
{
await _dbService.DeleteMachineCheckAsync(check.CheckId);
var itemToRemove = MachineChecks.FirstOrDefault(c => c.CheckId == check.CheckId);
if (itemToRemove != null)
{
MachineChecks.Remove(itemToRemove);
}
}
catch (Exception ex)
{
MessageBox.Show($"Nie można usunąć przeglądu. {ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
private void OpenPdfButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn && btn.DataContext is MachineCheckRecord check)
{
try
{
string tempPath = Path.Combine(Path.GetTempPath(), $"Protocol_{check.CheckId}.pdf");
File.WriteAllBytes(tempPath, check.ProtocolPdf);
Process.Start(new ProcessStartInfo(tempPath) { UseShellExecute = true });
}
catch (Exception ex)
{
MessageBox.Show($"Nie można otworzyć protokołu PDF. {ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void BackButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}