using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Business;
using SlnMesnac.Model.dto;
using SlnMesnac.Model.enums;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：7b8b1dd3-c235-4b39-9df0-2c086246b206
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-10 10:31:47
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    public class ManualDeliveryViewModel : ViewModelBase
    {
        private readonly ProdMgmtBusiness? _prodMgmtBusiness;

        public ManualDeliveryViewModel()
        {
            _prodMgmtBusiness = App.ServiceProvider.GetService<ProdMgmtBusiness>();

            QueryStringCommand = new RelayCommand<string>(Query);

            this.Query(string.Empty);

            ConfirmCommand = new RelayCommand<object>(Confirm);
            CancelCommand = new RelayCommand<object>(Cancel);
        }

        #region 参数定义
        /// <summary>
        /// 计划列表DataGrid
        /// </summary>
        private ObservableCollection<MesProductPlanDto> planInfoDataGrid;

        public ObservableCollection<MesProductPlanDto> PlanInfoDataGrid
        {
            get { return planInfoDataGrid; }
            set { planInfoDataGrid = value; RaisePropertyChanged(() => PlanInfoDataGrid); }

        }

        /// <summary>
        /// 选中计划列表信息
        /// </summary>
        private MesProductPlanDto selectedCells;
        public MesProductPlanDto SelectedCells
        {
            get { return selectedCells; }
            set
            {
                selectedCells = value;
                RaisePropertyChanged(() => SelectedCells);
            }
        }
        #endregion

        #region 事件定义
        public RelayCommand<string> QueryStringCommand { get; set; }

        /// <summary>
        /// 确认事件
        /// </summary>
        public RelayCommand<object> ConfirmCommand { get; set; }

        /// <summary>
        /// 关闭事件
        /// </summary>
        public RelayCommand<object> CancelCommand { get; set; }

        #endregion

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="queryStr"></param>
        private void Query(string queryStr)
        {
            try
            {
                PlanInfoDataGrid = new ObservableCollection<MesProductPlanDto>();
                var list = _prodMgmtBusiness._mesProductPlanService.GetPlanDtos();
                if (list != null)
                {
                    list.OrderByDescending(x => x.PlanStatus);
                    list.ForEach(
                    arg =>
                    {
                        PlanInfoDataGrid.Add(arg);
                    });
                }
            }
            catch (Exception ex)
            {
                //_logger.LogError($"生产计划刷新异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="parameter"></param>
        private void Confirm(object parameter)
        {

            try
            {
                if (selectedCells != null)
                {
                    if (_prodMgmtBusiness.ApplyDeliveryHandle(selectedCells.MaterialId.ToString()))
                    {
                        _prodMgmtBusiness.RefreshMessage("手动叫料成功");
                    }
                    else
                    {
                        _prodMgmtBusiness.RefreshMessage("手动叫料失败");
                    }
                }
            }catch (Exception ex)
            {
                _prodMgmtBusiness.RefreshMessage($"手动叫料处理异常:{ex.Message}");
            }

            var window = parameter as Window;
            if (window != null)
            {
                window.Close();
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        private void Cancel(object parameter)
        {
            var window = parameter as Window;
            if (window != null)
            {
                window.Close();
            }
        }
    }
}
