using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Domain.View.History;
using MWS.Library.Common;
using MWS.Library.Common.Module;
using MWS.Model;
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 MWS.Domain.View.SystemManage
{
    /// <summary>
    /// LoginManagePage.xaml 的交互逻辑
    /// </summary>
    public partial class LoginManagePage : Page
    {
        private readonly IUserLog userLogManager;
        private string keyword;
        private DateTime startTime;
        private DateTime endTime;
        public LoginManagePage()
        {
            InitializeComponent();
            userLogManager = new UserLogManager();
            startTime = DateTime.Now.AddHours(-24);
            endTime = DateTime.Now;
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            btDataSelect.Content = startTime.ToString() + "-" + endTime.ToString();
        }
        private void Pagination_CurrentPageNumberChanging(object sender, RoutedEventArgs e)
        {
            GetListData(pagination.CurrentPageNumber);
        }
        private void GetListData(int pageIndex = 1)
        {
            keyword = tbkeyword.Text.Trim();
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();
            PageData<UserLog> pa = userLogManager.GetAll(keyword, startTime, endTime, pageIndex, pagination.PageDataCount);

            lstUserLog.ItemsSource = pa.Data;

            pagination.TotalDataCount = pa.TotalCount;
            pagination.CurrentPageNumber = pageIndex;
        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            GetListData();
        }

        private void btDataSelect_Click(object sender, RoutedEventArgs e)
        {
            var dateSelect = btDataSelect.Content.ToString().Split('-');
            startTime = dateSelect[0].ToDateTime();
            endTime = dateSelect[1].ToDateTime();

            DateTimeSelectWindow dateTimeSelectWindow = new DateTimeSelectWindow(startTime, endTime);
            dateTimeSelectWindow.ShowDialog();
            if ((bool)dateTimeSelectWindow.DialogResult)
            {
                this.startTime = dateTimeSelectWindow.BeginTime;
                this.endTime = dateTimeSelectWindow.EndTime;

                btDataSelect.Content = dateTimeSelectWindow.BeginTime.ToString() + "-" + dateTimeSelectWindow.EndTime.ToString();
            };
        }
    }
}
