-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
328 lines (308 loc) · 9.45 KB
/
MainWindow.xaml.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using Microsoft.Win32;
using System;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace CardFilePBX
{
public partial class MainWindow : Window
{
private DataListApplication dl;
private AbonentInfo InfoWindow;
public MainWindow()
{
InitializeComponent();
dl = new DataListApplication();
this.DataContext = dl;
// Подключение БД и инициализация таблицы
dl.UpdateTable();
ConnectionDB(this, new RoutedEventArgs());
}
private void AddAbonent(object sender, RoutedEventArgs e)
{
TabMenu.SelectedIndex = 0;
Random r = new Random();
bool valid = true;
var firstName = NameAddBox.Text;
var lastName = LastNameAddBox.Text;
var patronymic = PatronymicAddBox.Text;
var phoneNumber = PhoneNumberAddBox.Text;
var tariff = TariffAddBox.SelectedIndex;
// Восстановление стиля
NameAddBox.Style = (Style)Application.Current.Resources["TextBox"];
LastNameAddBox.Style = (Style)Application.Current.Resources["TextBox"]; ;
PhoneNumberAddBox.Style = (Style)Application.Current.Resources["TextBox"];
TariffAddBox.Style = (Style)Application.Current.Resources["ComboBox"];
// Валидация ввода
if (string.IsNullOrEmpty(firstName))
{
NameAddBox.Style = (Style)Application.Current.Resources["RedTextBox"];
valid = false;
}
if (string.IsNullOrEmpty(lastName))
{
LastNameAddBox.Style = (Style)Application.Current.Resources["RedTextBox"];
valid = false;
}
if (string.IsNullOrEmpty(patronymic))
{
patronymic = "-";
}
Regex ex = new Regex(@"^(\+\d)(\(\d{3}\)\d{3})(\-)(\d{2}\-\d{2})$");
if (!ex.IsMatch(phoneNumber))
{
PhoneNumberAddBox.Style = (Style)Application.Current.Resources["RedTextBox"];
valid = false;
}
if (tariff < 0)
{
TariffAddBox.Style = (Style)Application.Current.Resources["RedComboBox"];
valid = false;
}
if (valid)
{
dl.AddAbonent(firstName, lastName, patronymic, phoneNumber, tariff.ToString(), DataListApplication.GetTotalCallTime(r), DataListApplication.GetTotalCallTime(r));
dl.UpdateTable();
NameAddBox.Text = string.Empty;
LastNameAddBox.Text = string.Empty;
PatronymicAddBox.Text = string.Empty;
PhoneNumberAddBox.Text = string.Empty;
TariffAddBox.SelectedIndex = -1;
}
}
private void QuestionMark_Click(object sender, RoutedEventArgs e)
{
var about = new AboutWindow();
about.Owner = this;
about.Show();
}
private void RefreshBtn_Click(object sender, RoutedEventArgs e)
{
dl.UpdateTable();
}
// Наименование и ширина столбцов в таблице
private void AbonentsDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var col = e.Column.Header.ToString();
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
switch (col)
{
case "Id":
e.Column.Header = "ИН";
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
break;
case "Name":
e.Column.Header = "Имя";
break;
case "LastName":
e.Column.Header = "Фамилия";
break;
case "Patronymic":
e.Column.Header = "Отчество";
break;
case "PhoneNumber":
e.Column.Header = "Номер телефона";
break;
case "Tariff":
e.Column.Header = "Тариф";
break;
default:
e.Column.Visibility = Visibility.Collapsed;
break;
}
}
private void AbonentViewChanged(object sender, EventArgs e)
{
var data = AbonentsDataGrid.CurrentCell.Item as DataRowView;
if (data != null)
{
var curr = dl.SelectById((int)data.Row[0]);
dl.AbonentView = curr;
AbonentCard.IsSelected = true;
}
}
private void ConnectionDB(object sender, RoutedEventArgs args)
{
// Проверка доступности БД
if (dl.CheckDB())
{
string dbName = Path.GetFileNameWithoutExtension(dl.connectionString);
dl.State = ConnectionState.Open;
if (sender != this)
{
MessageBox.Show($"Успешное подключение к базе данных {dbName}.", "Подключение установлено", MessageBoxButton.OK);
}
return;
}
else dl.State = ConnectionState.Broken;
if(!dl.FirstStart)
MessageBox.Show("Невозможно подключиться к базе данных", "Подключение не установлено", MessageBoxButton.OK);
}
private void SaveDB(object sender, RoutedEventArgs e)
{
dl.WriteDatabase();
}
private void TextPreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex(@"[0-9]");
if (regex.Match(e.Text).Success)
{
e.Handled = true;
}
}
private void PhoneNumberBox_GotFocus(object sender, RoutedEventArgs e)
{
((Xceed.Wpf.Toolkit.MaskedTextBox)sender).CaretIndex = 3;
}
private async void EditButton_Click(object sender, RoutedEventArgs e)
{
TabMenu.SelectedIndex = 2;
if (dl.AbonentView is null)
{
await Task.Run(() =>
{
NullSelectionPopup.Dispatcher.Invoke(() => NullSelectionPopup.IsOpen = true);
while (dl.AbonentView is null)
{
}
NullSelectionPopup.Dispatcher.Invoke(() => NullSelectionPopup.ClearValue(Popup.IsOpenProperty));
dl.IsNoEditing = false;
});
}
else dl.IsNoEditing = false;
}
private void ConfirmEditing(object sender, RoutedEventArgs e)
{
dl.AbonentView.Tariff = dl.TariffConverter(TariffChangeComboBox.SelectedIndex.ToString());
var dialogResult = MessageBox.Show("Вы действительно изменить данные?", "Изменение данных абонента", MessageBoxButton.YesNo);
if (dialogResult == MessageBoxResult.Yes)
{
dl.EditAbonentData();
}
dl.IsNoEditing = true;
dl.AbonentView = null;
dl.UpdateTable();
}
private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
TabMenu.SelectedIndex = 2;
if (!dl.IsNoEditing) {
dl.IsNoEditing = true;
dl.AbonentView = null;
dl.UpdateTable();
return;
}
if (dl.AbonentView is null)
{
await Task.Run(() =>
{
NullSelectionPopup.Dispatcher.Invoke(() => NullSelectionPopup.IsOpen = true);
while (dl.AbonentView is null)
{
}
NullSelectionPopup.Dispatcher.Invoke(() => NullSelectionPopup.ClearValue(Popup.IsOpenProperty));
});
}
var dialogResult = MessageBox.Show("Вы действительно хотите удалить абонента из базы данных?",
"Удаление из базы данных", MessageBoxButton.YesNo);
if (dialogResult == MessageBoxResult.Yes)
{
dl.DeleteAbonent(dl.AbonentView.Id);
dl.AbonentView = null;
}
dl.UpdateTable();
}
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
dl.UpdateTable();
string query = "";
if (SearchNameBox.Text != "")
{
query = "Name LIKE '%" + SearchNameBox.Text.Trim() + "%' AND ";
}
if (SearchLastNameBox.Text != "")
{
query += "LastName LIKE '%" + SearchLastNameBox.Text.Trim() + "%' AND ";
}
if (SearchPatronymicBox.Text != "")
{
query += "Patronymic LIKE '%" + SearchPatronymicBox.Text.Trim() + "%' AND ";
}
if (SearchPhoneNumberBox.IsMaskFull)
{
query += "PhoneNumber LIKE '%" + SearchPhoneNumberBox.Text.Trim() + "%'";
}
if (query.EndsWith("AND "))
{
query = query.Remove(query.Length - 4);
}
dl.SearchAbonent(query);
}
private void SetConnectionDB(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() != true)
return;
dl.SetConnectionString(openFileDialog1.FileName);
dl.UpdateTable();
}
private void CreateDB(object sender, RoutedEventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = "*.db",
AddExtension = true,
Filter = "Data Base File(*.db)|*.db",
InitialDirectory = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\database")),
};
if (dialog.ShowDialog() == true)
{
dl.CreateDbFile(dialog.FileName);
}
}
private void GetAbonentInfo(object sender, RoutedEventArgs e)
{
if (InfoWindow is null)
{
InfoWindow = new AbonentInfo();
InfoWindow.SetAbonent(dl.AbonentView);
InfoWindow.Closed += (o, args) => InfoWindow = null;
InfoWindow.Owner = this;
InfoWindow.Show();
}
else
{
InfoWindow.AddInfoCard(dl.AbonentView);
}
}
private void ExitProgram(object sender, RoutedEventArgs e)
{
this.Close();
}
private void AllInfo(object sender, RoutedEventArgs e)
{
var list = dl.GetAbonentList();
if (list is null || list.Count == 0)
{
MessageBox.Show("В базе данных отсутсвует информация об абонентах.", "Информация отсутствует", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
InfoWindow = new AbonentInfo();
InfoWindow.SetAbonent(list);
InfoWindow.Closed += (o, args) => InfoWindow = null;
InfoWindow.Owner = this;
InfoWindow.Show();
}
private void ChangePopupLocation(object sender, MouseEventArgs e)
{
var mousePosition = e.GetPosition(this.window);
this.NullSelectionPopup.HorizontalOffset = mousePosition.X + 15;
this.NullSelectionPopup.VerticalOffset = mousePosition.Y + 15;
}
}
}