using HandyControl.Controls;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WCS.BLL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.MatInventoryDetail;
using 智慧物流软件系统.Api;

namespace 智慧物流软件系统.ViewModels
{
    public class OutInventoryAddDucumentViewModel : BindableBase
    {
        #region Property
        private ObservableCollection<MatInventorySummaryModel> dataGridItemSource;
        public ObservableCollection<MatInventorySummaryModel> DataGridItemSource
        {
            get { return dataGridItemSource; }
            set
            {
                SetProperty(ref dataGridItemSource, value);
            }
        }

        private MatInventorySummaryModel selectedItemSource;
        public MatInventorySummaryModel SelectedItemSource
        {
            get { return selectedItemSource; }
            set
            {
                SetProperty(ref selectedItemSource, value);
            }
        }

        private List<ShelfTypeModel> shelfTypeItems;
        public List<ShelfTypeModel> ShelfTypeItems
        {
            get { return shelfTypeItems; }
            set
            {
                SetProperty(ref shelfTypeItems, value);
            }
        }
        public void InitShelfTypeItems()
        {
            //调用接口更新！
            Task.Run(() =>
            {
                var body = new RequestBase()
                {
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };

                var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<ShelfTypeModel>>(LocalFile.Config.ApiIpHost + "home/getShelfTypes", body, "POST");
                if (Result != null && Result.Data != null && Result.Data.Lists.Count() > 0)
                {
                    ShelfTypeItems = Result.Data.Lists;
                    SelectedShelfTypeItem = Result.Data.Lists.First();
                }
            });
        }

        private ShelfTypeModel selectedShelfTypeItem;
        public ShelfTypeModel SelectedShelfTypeItem
        {
            get { return selectedShelfTypeItem; }
            set
            {
                SetProperty(ref selectedShelfTypeItem, value);
            }
        }

        private int selectedTypeCount = 0;
        public int SelectedTypeCount
        {
            get => selectedTypeCount;
            set
            {
                SetProperty(ref selectedTypeCount, value);
                if (selectedTypeCount == 0)
                {
                    ShelfTypeIsEnabled = true;
                }
                else
                {
                    ShelfTypeIsEnabled = false;
                }
            }
        }

        private bool shelfTypeIsEnabled = true;
        public bool ShelfTypeIsEnabled
        {
            get => shelfTypeIsEnabled; set { SetProperty(ref shelfTypeIsEnabled, value); }
        }


        #endregion

        #region Command
        /// <summary>
        /// 新增物料
        /// </summary>
        public ICommand BtnAddCommand { get => new DelegateCommand(BtnAdd); }
        public void BtnAdd()
        {
            if (SelectedShelfTypeItem == null)
            {
                HandyControl.Controls.MessageBox.Show("请选择货架类型！");
            }
            //var window = new OutInventoryAddMatView(SelectedRecordTypeItem.Id);
            //window.Owner = Application.Current.MainWindow;
            //window.Topmost = true;

            //var result = window.ShowDialog();
            //if (result == true)
            //{
            //    if (DataGridItemSource == null)
            //        DataGridItemSource = new ObservableCollection<MatInventorySummaryModel>();
            //    DataGridItemSource.Add(window.inventorySummary);
            //    SelectedTypeCount = DataGridItemSource.Count;
            //}
            //else
            //{
            //    return;
            //}
        }

        public ICommand DelCommand { get => new DelegateCommand<MatInventorySummaryModel>(Del); }
        public void Del(MatInventorySummaryModel obj)
        {
            try
            {

                DataGridItemSource.Remove(obj);
                SelectedTypeCount = DataGridItemSource.Count;
            }
            catch (Exception ex)
            {
                Growl.Info(ex.Message);
                Logs.Write($"删除物料时发生异常", LogsType.Err);
            }
        }

        // 定义一个事件，当需要关闭窗口时触发  
        public event Action TrueClose;
        public event Action FalseClose;
        // 一个方法，当满足某些条件时调用，以触发关闭窗口  
        protected virtual void OnRequestClose(bool IsTrue)
        {
            if (IsTrue)
                TrueClose?.Invoke();
            else
                FalseClose?.Invoke();
        }
        public ICommand GenerateOutOrderCommand { get => new DelegateCommand(GenerateOutOrder); }
        public void GenerateOutOrder()
        {
            //数据校验
            if (DataGridItemSource == null || DataGridItemSource.Count == 0)
            {
                HandyControl.Controls.MessageBox.Show("选择的物料为空无法生成出库单！");
                return;
            }
            foreach (var itemSource in DataGridItemSource)
            {
                if (itemSource.NeedQty == 0)
                {
                    Growl.Warning("需求数量未填！");
                    SelectedItemSource = itemSource;
                    return;
                }
                //问一下此处是否要做限制 TO DO 物料分类（Group By）的规则 分到哪一类
                //else if (itemSource.NeedQty > itemSource.TotalQty)
                //{
                //    Growl.Warning("需求数量大于库存数量！");
                //    SelectedItemSource = itemSource;
                //    return;
                //}
            }
            //#region 调用接口生成出库单据
            //try
            //{
            //    var body = new SysOutOrderByMatCodeRequest()
            //    {
            //        OrderType = "出库",
            //        OrderSource = "WCS前端",
            //        ShelfTypeId = SelectedRecordTypeItem.Id,
            //        ShelfTypeName = SelectedRecordTypeItem.ShelfTypeName,
            //        ItemList = DataGridItemSource.Select(t => new MatCodeItemList()
            //        {
            //            MatCode = t.MatCode,
            //            MatName = t.MatName,
            //            MatBatch = t.MatBatch,
            //            ReqQty = t.NeedQty,
            //        }).ToList(),
            //        DeviceType = LocalFile.Config.DeviceType,
            //        UserName = LocalStatic.CurrentUser
            //    };
            //    var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "outstore/sysOutOrderByMatCode", body, "POST");
            //    if (Result != null && Result.Code == 200)
            //    {
            //        Growl.Success(Result.Message);
            //        OnRequestClose(true);
            //    }
            //    else if (Result != null)
            //    {
            //        Growl.Warning(Result.Message);
            //        return;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Growl.Error("加载数据失败：" + ex.Message);
            //}
            //finally
            //{
            //}
            //#endregion
        }
        #endregion
    }
}
