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>
    /// TeamManagePage.xaml 的交互逻辑
    /// </summary>
    public partial class TeamManagePage : Page
    {
        private readonly ITeam teamManager;

        private string keywork;
        public TeamManagePage()
        {
            InitializeComponent();
            teamManager = new TeamManager();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            GetListData();
        }
        private void GetListData(int pageIndex = 1)
        {
            keywork = tbkeyword.Text;
            PageData<Team> pa = teamManager.GetAll(keywork, pageIndex, pagination.PageDataCount);
            lstTeam.ItemsSource = pa.Data;
            pagination.TotalDataCount = pa.TotalCount;
            pagination.CurrentPageNumber = pageIndex;
        }
        private void search_Click(object sender, RoutedEventArgs e)
        {
            GetListData();
        }

        private void btAddTeam_Click(object sender, RoutedEventArgs e)
        {
            TeamAddWindow teamAddWindow = new TeamAddWindow();
            teamAddWindow.ShowDialog();
            GetListData();
        }

        private void btEditTeam_Click(object sender, RoutedEventArgs e)
        {
            if (lstTeam.SelectedItems.Count == 0)
            {
                Msg.Info("请选择一个班组！");
                return;
            }
           
            var t_Team = lstTeam.SelectedItem as Team;
            TeamEditWindow userAddWindow = new TeamEditWindow(t_Team);
            userAddWindow.ShowDialog();
            GetListData();
        }

        private async void btDeleteTeam_Click(object sender, RoutedEventArgs e)
        {
            if (lstTeam.SelectedItems.Count == 0)
            {
                Msg.Info("请选择一个班组！");
                return;
            }
            var t_Team = lstTeam.SelectedItem as Team;

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

                }

            }
        }
    }
}
