using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
namespace ivf_tl_Operate.CustomUserControls
{
///
/// 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 currentYear;
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 int[] hours = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
private int[] mins = new int[] { 0, 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 };
public bool isNYR { get; set; }
///
/// 是否可以选择未来
///
public bool isSelectWeiLai { get; set; } = false;
public CustomerDateControl()
{
var currentDate = DateTime.Now;
InitializeComponent();
combH.ItemsSource = hours;
combH.SelectedIndex = 0;
combM.ItemsSource = mins;
combM.SelectedIndex = 0;
InitCurrentDate(currentDate);
Init(currentDate);
btnLeft.MouseLeftButtonDown += BtnLeft_MouseLeftButtonDown;
btnRight.MouseLeftButtonDown += BtnRight_MouseLeftButtonDown;
btnConfrim.MouseLeftButtonDown += BtnConfrim_MouseLeftButtonDown;
}
public void SetNYR(bool isnyr)
{
isNYR = isnyr;
if (isNYR)
{
c1.Visibility = Visibility.Hidden;
}
else
{
c1.Visibility = Visibility.Visible;
}
}
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(cuurentDate);
}
private void InitCurrentDate(DateTime currentDate)
{
currentDay = currentDate.Day;
currentMouth = currentDate.Month;
currentYear = currentDate.Year;
selectDay = currentDay + "";
}
private void CheckIsToday(DateTime date)
{
isToady = (date.Year == currentYear && date.Month == currentMouth && date.Day == currentDay);
}
private void BtnLeft_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cuurentDate = cuurentDate.AddMonths(-1);
Init(cuurentDate);
}
public void SetSelectTime(DateTime currentDate)
{
InitCurrentDate(currentDate);
Init(currentDate);
}
///
/// 可以选择未来
///
public void SetSelectWeiLai()
{
var currentDate = DateTime.Now;
isSelectWeiLai = true;
InitCurrentDate(currentDate);
Init(currentDate);
}
private void Init(DateTime now)
{
tbYear.Text = now.ToString("yyyy-MM");
CheckIsToday(now);
week.Children.Clear();
InitParameter(now);
InitGrid();
InitWeek();
}
private void ClearStyle()
{
foreach (var item in week.Children)
{
TextBlock tb = item as TextBlock;
if (tb != null)
{
if (DateTime.TryParse($"{this.tbYear.Text}-{tb.Text}", out DateTime itemDatetime))
{
if (!isSelectWeiLai)
{
if (itemDatetime > DateTime.Now)
{
}
else
{
ReSetSelected(tb);
tb = null;
}
}
else
{
ReSetSelected(tb);
tb = null;
}
}
}
}
}
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(DateTime now)
{
index = dataUtil.CurrentMonthFristDayOfWeek(now) - 1;
currentMouthDay = dataUtil.CurrentMonthCount(now);
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()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < defaultColumn; j++)
{
TextBlock d = new TextBlock();
d.Foreground = new SolidColorBrush(Colors.Black);
d.Width = week.Width / defaultColumn;
d.Height = week.Height / row;
if (!isSelectWeiLai)
{
if (DateTime.TryParse($"{this.tbYear.Text}-{CalcCount}", out DateTime itemDatetime))
{
if (itemDatetime > DateTime.Now)
{
d.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#B0B4B9"));
goto CC;
}
}
}
//d.MouseEnter += (o, e) => {
// SetSelected(d);
//};
//d.MouseLeave += (o, e) => {
// ReSetSelected(d);
//};
d.MouseLeftButtonDown += (o, e) =>
{
TextBlock tb = o as TextBlock;
if (o != null)
{
ClearStyle();
SetSelected(tb);
//if (isNYR)
//{
// Visibility = Visibility.Hidden;
// ReturnDate();
//}
}
};
CC:
d.FontSize = 60;
d.Text = CalcCount.ToString();
d.HorizontalAlignment = HorizontalAlignment.Center;
d.VerticalAlignment = VerticalAlignment.Center;
d.TextAlignment = TextAlignment.Center;
d.SetValue(Grid.RowProperty, i);
if (i != 0)
{
index = j;
d.SetValue(Grid.ColumnProperty, index);
}
else
{
d.SetValue(Grid.ColumnProperty, index);
if (index == 7)
break;
index++;
}
if (CalcCount == currentDay && isToady)//当天设置背景
SetSelected(d);
week.Children.Add(d);
if (CalcCount == currentMouthDay)
{
CalcCount = 1;
break;
}
CalcCount++;
}
}
}
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);
}
}
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;
}
}
}