using school.FolderClasses;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace school
{
///
/// Логика взаимодействия для winWriteService.xaml
///
public partial class winWriteService : Window
{
Service thisService;
public winWriteService(int idServise)
{
thisService = baseConnect.thisBase.Service.Where(tb => tb.ID == idServise).FirstOrDefault();
InitializeComponent();
init();
}
private void init()
{
List list = baseConnect.thisBase.ClientService.Where(tb => tb.ServiceID == thisService.ID).OrderBy(tb => tb.Client.LastName).ThenBy(tb => tb.Client.FirstName).ThenBy(tb => tb.Client.Patronymic).ToList();
if (list.Count > 0)
{
DGClientService.ItemsSource = list;
TBnoWrites.Visibility = Visibility.Collapsed;
}
else
{
DGClientService.Visibility = Visibility.Collapsed;
TBnoWrites.Visibility = Visibility.Visible;
}
TBName.Text = thisService.Title;
string line = (thisService.Description == null || thisService.Description == "") ? "Описание отсутствует" : thisService.Description.ToString();
TBDescription.Text = line;
TBTime.Text = (thisService.DurationInSeconds / 60).ToString() + " мин.";
TBOldPrice.Visibility = thisService.discountVisible;
TBOldPrice.Text = thisService.oldPrice;
TBPrice.Text = Convert.ToInt32(thisService.Cost) + " рублей";
if (dataAdminRoot.adminRoot == ("0000").GetHashCode())
{
TBHeader.Text = "Просмотр услуги";
BtnWrite.Content = "Проверить";
}
else
{
GInfo.Visibility = Visibility.Collapsed;
}
}
private bool check()
{
try
{
bool flag = true;
string mess = "";
Regex reg_time = new Regex("^[0-2][0-9]:[0-6][0-9]$");
if (!reg_time.IsMatch(TBTimeServ.Text))//проверка корректности времени
{
flag = false;
mess += "Формат введенного времени не совпадает\n";
}
if (DPDate.SelectedDate <= DateTime.Today)//проверка даты на корректность
{
flag = false;
mess += "Выбранная дата не подходит для записи, выберите дату, например, завтра или позднее";
}
if (flag)
{
List list = baseConnect.thisBase.ClientService.Where(tb => tb.ServiceID == thisService.ID).ToList();//все записи на услугу
DateTime thisTime = (DateTime)DPDate.SelectedDate;//начало услуги
thisTime = thisTime.AddHours(Convert.ToDouble(TBTimeServ.Text.Split(':')[0])).AddMinutes(Convert.ToDouble(TBTimeServ.Text.Split(':')[1]));
DateTime endThisTime = thisTime.AddHours(thisService.DurationInSeconds / 3600).AddMinutes(thisService.DurationInSeconds %3600);//конец услуги
foreach (ClientService service in list)//проверка на время
{
DateTime start = service.StartTime;
DateTime end = service.StartTime.AddHours(thisService.DurationInSeconds / 3600).AddMinutes(thisService.DurationInSeconds % 3600);
if (start == thisTime || thisTime == end || (thisTime < start && endThisTime > start) || (end > thisTime && start < thisTime))
flag = false;
}
if (!flag) mess += "Выбранное время недоступно";
}
if (mess != "") MessageBox.Show(mess);
return flag;
}
catch
{
MessageBox.Show("Возникли ошибки при проверке данных");
return false;
}
}
private void BtnWriteClick(object sender, RoutedEventArgs e)
{
if (check())
{
if (dataAdminRoot.adminRoot == ("0000").GetHashCode())//проверка работы
{
MessageBox.Show("Запись корректна");
}
else
{
try
{
DateTime thisTime = (DateTime)DPDate.SelectedDate;//начало услуги
thisTime = thisTime.AddHours(Convert.ToDouble(TBTimeServ.Text.Split(':')[0])).AddMinutes(Convert.ToDouble(TBTimeServ.Text.Split(':')[1]));
ClientService cs = new ClientService()
{
ClientID = dataUser.user.ID,
ServiceID = thisService.ID,
StartTime = thisTime
};
baseConnect.thisBase.ClientService.Add(cs);
baseConnect.thisBase.SaveChanges();
this.Close();
MessageBox.Show("Вы успешно добавлены на услугу");
}
catch
{
MessageBox.Show("Возникла ошибка");
}
}
}
}
private void TBTimeServTextChanged(object sender, TextChangedEventArgs e)
{
try
{
string line = TBTimeServ.Text;
int lenght = line.Length;
switch (lenght)
{
case 0:
{
break;
}
case 1:
{
Regex n = new Regex(@"^[0-2]$");
if (!n.IsMatch(line))
{
line = "";
}
break;
}
case 2:
{
Regex n = new Regex(@"^[0-2][0-9]$");
if (!n.IsMatch(line))
{
int num = Convert.ToInt32(line);
if (num > 23 || num < 8) line = line.Substring(0, line.Length - 1);
}
else line += ":";
break;
}
case 4:
{
Regex n = new Regex(@"^[0-2][0-9]:[0-6]$");
if (!n.IsMatch(line))
{
int num = Convert.ToInt32(line);
if (num > 23 || num < 8) line = line.Substring(0, line.Length - 1);
}
break;
}
case 5:
{
Regex n = new Regex(@"^[0-2][0-9]:[0-6][0-9]$");
if (!n.IsMatch(line))
{
int num = Convert.ToInt32(line.Split(':')[0]);
int num2 = Convert.ToInt32(line.Split(':')[1]);
if (num > 23 || num < 8 || num2 > 60) line = line.Substring(0, line.Length - 1);
}
else
{
DateTime thisTime = (DateTime)DPDate.SelectedDate;//начало услуги
thisTime = thisTime.AddHours(Convert.ToDouble(TBTimeServ.Text.Split(':')[0])).AddMinutes(Convert.ToDouble(TBTimeServ.Text.Split(':')[1]));
DateTime endThisTime = thisTime.AddHours(thisService.DurationInSeconds / 3600).AddMinutes(thisService.DurationInSeconds % 3600);//конец услуги
TBEndTime.Text = "Время окончания " + endThisTime.ToString("dd MMMM yyyy HH:mm");
}
break;
}
default:
{
line = line.Substring(0, line.Length - 1);
break;
}
}
if (lenght != 3) TBTimeServ.Text = line;
}
catch
{
MessageBox.Show("Ошибка ввода");
}
}
}
}