using ivf_tl_Manage.Converts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ivf_tl_Manage.UserControls
{
///
/// CustomerDateControl.xaml 的交互逻辑
///
public partial class CustomerDateControl : UserControl
{
public event Action SelectDateAction;
public event Action CloseAction;
private string selectDay;
private bool isToady = false;
private int currentDay = 1;
private int currentMouth = 1;
private int upMonthday = 1;
private int currentYear;
private int currentHour;
private int currentMinute;
private DateUtil dataUtil = new DateUtil();
private int defaultColumn = 7;
private int row = 0;
private int index = 0;
private int currentMouthDay = 0;
private int CalcCount = 1;
private DateTime cuurentDate = DateTime.Now;
private string[] hours =new string[24];
private string [] mins = new string[60];
public bool isNYR { get; set; }
private List data = new List();
///
/// 是否可以选择未来
///
public bool isSelectWeiLai { get; set; } = false;
private DateTime today = DateTime.Now;
private bool CheckToday(DateTime time)
{
return today.Year == time.Year && today.Month == time.Month && today.Day == time.Day;
}
public CustomerDateControl()
{
for (int i = 0; i < 60; i++)
{
mins[i] = ":" + i.ToString("D2");
}
for (int i = 0; i < 24; i++)
{
hours[i] = i.ToString("D2");
}
var currentDate = DateTime.Now;
InitializeComponent();
lt_hour.ItemsSource = hours;
lt_min.ItemsSource = mins;
InitCurrentDate(currentDate);
Init();
//btnLeft.MouseLeftButtonDown += BtnLeft_MouseLeftButtonDown;
//btnRight.MouseLeftButtonDown += BtnRight_MouseLeftButtonDown;
}
private void BtnConfrim_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ReturnDate();
}
private void ReturnDate()
{
//string selectDate = "";
//if (!isNYR)
//{
// selectDate = string.Format("{0}-{1} {2}", tbYear.Text, selectDay.PadLeft(2, '0'), combH.SelectedValue.ToString().PadLeft(2, '0') + ":" + combM.SelectedValue.ToString().PadLeft(2, '0'));//+ ":00"
//}
//else
//{
// selectDate = string.Format("{0}-{1}", tbYear.Text, selectDay.PadLeft(2, '0'));
//}
//SelectDateAction?.Invoke(selectDate);
}
private void BtnRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cuurentDate = cuurentDate.AddMonths(1);
Init();
}
private void InitCurrentDate(DateTime currentDate)
{
currentDay = currentDate.Day;
currentMouth = currentDate.Month;
currentYear = currentDate.Year;
selectDay = currentDay + "";
}
///
/// 初始化数据
///
///
public void InitData(DateTime dateTime)
{
int totalCount = 0;
data.Clear();
int starDayNum = upMonthday - index + 1;
for (int i = 0; i < index; i++)
{
DateItemData dateItemData = new DateItemData();
dateItemData.IsCurrentMonth = false;
DateTime temp = dateTime.AddMonths(-1);
dateItemData.Time = new DateTime(temp.Year, temp.Month, starDayNum + i);
data.Add(dateItemData);
totalCount++;
}
for (int i = 0; i < currentMouthDay; i++)
{
DateItemData dateItemData = new DateItemData();
dateItemData.IsCurrentMonth = true;
dateItemData.Time = new DateTime(dateTime.Year, dateTime.Month, i + 1);
dateItemData.IsToday = CheckToday(dateItemData.Time);
data.Add(dateItemData);
totalCount++;
}
int j = 1;
while (totalCount < 42)
{
DateItemData dateItemData = new DateItemData();
dateItemData.IsCurrentMonth = false;
DateTime temp = dateTime.AddMonths(1);
dateItemData.Time = new DateTime(temp.Year, temp.Month, j);
dateItemData.IsToday = CheckToday(dateItemData.Time);
data.Add(dateItemData);
j++;
totalCount++;
}
}
private void CheckIsToday(DateTime date)
{
isToady = (date.Year == currentYear && date.Month == currentMouth && date.Day == currentDay);
}
public void SetSelectTime()
{
InitCurrentDate(cuurentDate);
Init();
}
private void Init()
{
//tb_Year.Text = cuurentDate.Year + "年";
//tb_month.Text = cuurentDate.Month + "月";
tb_Year.Text = cuurentDate.Year + KeyToStringConvert.GetLanguageStringByKey("0154");
tb_month.Text = cuurentDate.Month + KeyToStringConvert.GetLanguageStringByKey("0155");
CheckIsToday(cuurentDate);
week.Children.Clear();
InitParameter();
//InitGrid();
InitData(cuurentDate);
InitWeek();
currentHour = DateTime.Now.Hour;
currentMinute = DateTime.Now.Minute;
SetHourAndMin();
}
private void SetHourAndMin()
{
lt_hour.SelectedIndex = currentHour;
lt_min.SelectedIndex = currentMinute;
tb_time.Text = $"{lt_hour.SelectedValue}{lt_min.SelectedValue}";
SelectDateAction?.Invoke($"{currentYear}-{currentMouth.ToString("D2")}-{currentDay.ToString("D2")} {currentHour.ToString("D2")}:{currentMinute.ToString("D2")}");
}
private void InitRows(int rowCount)
{
while (rowCount-- > 0)
{
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength();
week.RowDefinitions.Add(rd);
}
}
private void InitColumns(int colCount)
{
while (colCount-- > 0)
{
ColumnDefinition rd = new ColumnDefinition();
rd.Width = new GridLength();
week.ColumnDefinitions.Add(rd);
}
}
private void InitParameter()
{
index = dataUtil.CurrentMonthFristDayOfWeek(cuurentDate) - 1;
currentMouthDay = dataUtil.CurrentMonthCount(cuurentDate);
upMonthday = dataUtil.CurrentMonthCount(cuurentDate.AddMonths(-1));
//int temp = currentMouthDay - (7 - index);
//row = 1 + temp / 7 + (temp % 7 == 0 ? 0 : 1);
}
private void InitGrid()
{
InitRows(row);
InitColumns(defaultColumn);
}
private void ReSetSelected(TextBlock d)
{
d.Background = new SolidColorBrush(Colors.White);
d.Foreground = new SolidColorBrush(Colors.Black);
}
private void SetSelected(TextBlock d)
{
d.Background = new SolidColorBrush(Colors.Black);
d.Foreground = new SolidColorBrush(Colors.White);
selectDay = d.Text;
}
private void InitWeek()
{
week.Children.Clear();
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
DateItemData item = data[7 * i + j];
DateItem dateItem = new DateItem();
dateItem.DataItemProperty = item;
dateItem.SetValue(Grid.RowProperty, i);
dateItem.SetValue(Grid.ColumnProperty, j);
dateItem.IsSelectProperty = CheckToday(item.Time);
dateItem.clickAction = () =>
{
ClearAllItmeBg();
dateItem.SetBg();
currentYear = item.Time.Year;
currentMouth = item.Time.Month;
currentDay = item.Time.Day;
SetHourAndMin();
};
week.Children.Add(dateItem);
}
}
}
public void ClearAllItmeBg()
{
var items = week.Children;
foreach (var item in items)
{
if (item is DateItem)
{
DateItem dateItem = (DateItem)item;
dateItem.bg.Fill = new SolidColorBrush(Colors.Transparent);
if (dateItem.DataItemProperty.IsCurrentMonth)
{
dateItem.tx_text.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D4D4D"));
}
else
{
dateItem.tx_text.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#9B9B9B"));
}
dateItem.SetTodayBg();
}
}
}
private void Cancel_MouseUp(object sender, MouseButtonEventArgs e)
{
CloseAction?.Invoke(sender);
}
private void OkButton_MouseUp(object sender, MouseButtonEventArgs e)
{
ReturnDate();
}
private void StartDish_Click(object sender, RoutedEventArgs e)
{
ReturnDate();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
CloseAction?.Invoke(sender);
}
private void img_YearLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cuurentDate = cuurentDate.AddYears(-1);
Init();
}
private void img_YearRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cuurentDate = cuurentDate.AddYears(1);
Init();
}
private void img_MonthLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cuurentDate = cuurentDate.AddMonths(-1);
Init();
}
private void img_MonthRight_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cuurentDate = cuurentDate.AddMonths(1);
Init();
}
private void img_YearLeft_MouseEnter(object sender, MouseEventArgs e)
{
img_YearLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_year_left.png", UriKind.Absolute));
}
private void img_YearLeft_MouseLeave(object sender, MouseEventArgs e)
{
img_YearLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_year_left.png", UriKind.Absolute));
}
private void img_YearRight_MouseEnter(object sender, MouseEventArgs e)
{
img_YearRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_year_right.png", UriKind.Absolute));
}
private void img_YearRight_MouseLeave(object sender, MouseEventArgs e)
{
img_YearRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_year_right.png", UriKind.Absolute));
}
private void img_MonthLeft_MouseEnter(object sender, MouseEventArgs e)
{
img_MonthLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_month_left.png", UriKind.Absolute));
}
private void img_MonthLeft_MouseLeave(object sender, MouseEventArgs e)
{//date_gray_month_left
img_MonthLeft.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_month_left.png", UriKind.Absolute));
}
private void img_MonthRight_MouseEnter(object sender, MouseEventArgs e)
{
img_MonthRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_blue_month_right.png", UriKind.Absolute));
}
private void img_MonthRight_MouseLeave(object sender, MouseEventArgs e)
{//date_gray_month_right
img_MonthRight.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/date_gray_month_right.png", UriKind.Absolute));
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
bd_today.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
tb_today.Foreground = new SolidColorBrush(Colors.White);
}
private void Border_MouseLeave(object sender, MouseEventArgs e)
{
bd_today.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#F6F6F6"));
tb_today.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#707070"));
}
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
ReSetDate();
}
public void ReSetDate()
{
cuurentDate = DateTime.Now;
Init();
SetHourAndMin();
}
private void bg_time_MouseEnter(object sender, MouseEventArgs e)
{
bg_time.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
}
private void bg_time_MouseLeave(object sender, MouseEventArgs e)
{
bg_time.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#C6C6C6"));
}
bool isOpen = false;
private void bg_time_MouseDown(object sender, MouseButtonEventArgs e)
{
isOpen = !isOpen;
pp_time.IsOpen= isOpen;
}
private void lt_hour_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
currentHour = lt_hour.SelectedIndex;
SetHourAndMin();
}
private void lt_min_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
currentMinute = lt_min.SelectedIndex;
SetHourAndMin();
}
private void pp_time_LostFocus(object sender, RoutedEventArgs e)
{
isOpen = false;
pp_time.IsOpen = isOpen;
}
private void Border_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (!pp_time.IsMouseOver)
{
isOpen = false;
pp_time.IsOpen = isOpen;
}
}
}
public class DateUtil
{
///
/// 当前月第一天是周几
///
/// 周几
public int CurrentMonthFristDayOfWeek()
{
DateTime now = DateTime.Now;
DateTime d1 = new DateTime(now.Year, now.Month, 1);
int day = (int)d1.DayOfWeek;
if (day == 0)
day = 7;
return day;
}
///
/// 当前月有几天
///
///
public int CurrentMonthCount()
{
DateTime now = DateTime.Now;
int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
return days;
}
///
/// 当前月有几天
///
/// 当前时间
///
public int CurrentMonthCount(DateTime now)
{
int days = DateTime.DaysInMonth(now.Year, now.Month);
return days;
}
///
/// 当前月第一天是周几
///
/// 周几
public int CurrentMonthFristDayOfWeek(DateTime now)
{
DateTime d1 = new DateTime(now.Year, now.Month, 1);
int day = (int)d1.DayOfWeek;
if (day == 0)
day = 7;
return day;
}
public static long ConvertDataTimeLong(DateTime dt)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan toNow = dt.Subtract(dtStart);
long timeStamp = toNow.Ticks;
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
return timeStamp;
}
}
}