using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Library.Common.Module;
using MWS.Model;
using MWSDomain.Common.CoreLib;
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>
    /// UserManagePage.xaml 的交互逻辑
    /// </summary>
    public partial class UserManagePage : Page
    {
        private readonly IUser user;
        private string keywork;

        public UserManagePage()
        {
            InitializeComponent();
            user = new UserManager();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            pagination.CurrentPageNumberChanging += Pagination_CurrentPageNumberChanging;
            GetListData();
        }
        private void GetListData(int pageIndex = 1)
        {
            keywork = tbkeyword.Text;
            PageData<User> pa = user.GetAll(keywork, pageIndex, pagination.PageDataCount);
            dglist.ItemsSource = pa.Data;
            pagination.TotalDataCount = pa.TotalCount;
            pagination.CurrentPageNumber = pageIndex;
        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            GetListData();
        }
        private void Pagination_CurrentPageNumberChanging(object sender, RoutedEventArgs e)
        {
            GetListData(pagination.CurrentPageNumber);
        }

        private void btAddUser_Click(object sender, RoutedEventArgs e)
        {
            UserAddWindow userAddWindow = new UserAddWindow();
            userAddWindow.ShowDialog();
            GetListData();
        }

        private void btEditUser_Click(object sender, RoutedEventArgs e)
        {
            if (dglist.SelectedItems.Count == 0)
            {
                Msg.Info("请选择至少一个用户！");
                return;
            }
            else if (dglist.SelectedItems.Count > 1)
            {

                Msg.Info("最多选择1个用户！");
                return;
            }
            var t_user = dglist.SelectedItem as User;
            UserEditWindow userAddWindow = new UserEditWindow(t_user);
            userAddWindow.ShowDialog();
            GetListData();

        }

        private async void btDeleteUser_Click(object sender, RoutedEventArgs e)
        {
            if (dglist.SelectedItems.Count == 0)
            {
                Msg.Info("请选择至少一个用户！");
                return;
            }           
            var t_user = dglist.SelectedItem as User;

            if (await Msg.Question("确定要删除吗？"))
            {
                if (user.Delete(t_user))
                {
                    GetListData();
                    Msg.Info("删除成功！");
                }
                else
                {
                    Msg.Info("删除失败！");

                }
                
            }
        }
    }
}
