using CommunityToolkit.Mvvm.Input;
using GalaSoft.MvvmLight;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.WPF.Page;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SlnMesnac.WPF.ViewModel
{
    public partial class TaskHistoryViewModel : ViewModelBase
    {
        private ISqlSugarClient sqlSugarClient;

        public TaskHistoryViewModel()
        {
            sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>()!;
            GetTaskListAsync();
        }

        #region 界面属性定义

        /// <summary>
        /// 任务列表
        /// </summary>
        private ObservableCollection<WcsTaskLog> wcsTaskLogItems = new ObservableCollection<WcsTaskLog>();

        public ObservableCollection<WcsTaskLog> WcsTaskLogItems
        {
            get { return wcsTaskLogItems; }
            set
            {
                wcsTaskLogItems = value;
                RaisePropertyChanged(() => WcsTaskLogItems);
            }
        }

        #endregion 界面属性定义

        [RelayCommand]
        private async Task RefreshData()
        {
            await GetTaskListAsync();
        }

        [RelayCommand]
        private void CreateMove()
        {
            CreateMoveWindow window = new CreateMoveWindow();
            window.ShowDialog();
        }

        [RelayCommand]
        private void CreateResignBox()
        {
            MannulResignBoxWindow window = new MannulResignBoxWindow();
            window.ShowDialog();
        }

        /// <summary>
        /// 获取任务列表
        /// </summary>
        /// <returns></returns>
        private async Task GetTaskListAsync()
        {
            List<WcsTaskLog> tasks = await sqlSugarClient.Queryable<WcsTaskLog>().OrderByDescending(x => x.CreatedTime).Take(100).ToListAsync();
            WcsTaskLogItems.Clear();
            int i = 1;
            if (tasks != null && tasks.Count > 0)
            {
                foreach (var task in tasks)
                {
                    task.No = i++;
                    WcsTaskLogItems.Add(task);
                }
            }
        }
    }
}