using HandyControl.Controls;
using Ping9719.WpfEx.Mvvm;
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.Input;
using WCS.Model;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.OutStore;
using WCS.Model.ApiModel.Stocktaking;
using 智慧物流软件系统.Api;

namespace 智慧物流软件系统.ViewModels
{
    public class StocktakingDocumentViewModel : BindableBase
    {
        public StocktakingDocumentViewModel()
        {
            BtnSearch();
        }

        #region Properties界面绑定的实体
        private ObservableCollection<StockTakingOrderModel> dataGridItemSource;
        public ObservableCollection<StockTakingOrderModel> DataGridItemSource
        {
            get { return dataGridItemSource; }
            set
            {
                SetProperty(ref dataGridItemSource, value);
            }
        }

        public StockTakingOrderModel selectedataGridItem;
        public StockTakingOrderModel SelectedataGridItem
        {
            get { return selectedataGridItem; }
            set
            {
                SetProperty(ref selectedataGridItem, value);
            }
        }

        private string orderNumber;
        public string OrderNumber
        {
            get { return orderNumber; }
            set
            {
                SetProperty(ref orderNumber, value);
            }
        }

        private string orderType;
        public string OrderType
        {
            get { return orderType; }
            set
            {
                SetProperty(ref orderType, value);
            }
        }

        private string orderSource;
        public string OrderSource
        {
            get { return orderSource; }
            set
            {
                SetProperty(ref orderSource, value);
            }
        }

        #endregion

        #region Command
        public ICommand BtnSearchCommand { get => new DelegateCommand(BtnSearchReset); }
        public void BtnSearchReset()
        {
            BtnSearch(true);
        }
        public void BtnSearch(bool IsPageReset = true)
        {
            if (CurrentPage == 0 || IsPageReset)
            {
                CurrentPage = 1;
                return;
            }

            #region 调用接口获取单据
            try
            {
                var body = new GetStockTakingOrdersRequest()
                {
                    StocktakingOrderNumber = OrderNumber,
                    StocktakingOrderType = OrderType,
                    StocktakingOrderSource = OrderSource,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                    PageNumber = CurrentPage,
                    PageSize = PageSize,
                };
                var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<StockTakingOrderModel>>(LocalFile.Config.ApiIpHost + "stockTaking/getStockTakingOrders", body, "POST");
                if (Result != null && Result.Data != null && Result.Data.Lists != null)
                {
                    DataGridItemSource = new ObservableCollection<StockTakingOrderModel>(Result.Data.Lists);
                    MaxPage = Result.Data.MaxPage;
                    TotalCount = Result.Data.TotalCount;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("加载单据失败：" + ex.Message);
            }
            finally
            {
            }
            #endregion
        }

        public ICommand BtnResetCommand { get => new DelegateCommand(BtnReset); }
        public void BtnReset()
        {
            OrderNumber = string.Empty;
            OrderType = string.Empty;
            OrderSource = string.Empty;
        }



        public ICommand BtnOrderDetailCommand { get => new DelegateCommand(BtnOrderDetail); }
        public void BtnOrderDetail()
        {
            try
            {
                if (DataGridItemSource == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }

                //判断是否勾选单据
                var selectetOrder = DataGridItemSource.Where(t => t.IsSelected)
                                                         .FirstOrDefault();
                if (selectetOrder == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }

                #region 调用接口获取单据

                var body = new GetStockTakingOrderMatDetailRequest()
                {
                    StockTakingOrderId = selectetOrder.Id,
                    StockTakingOrderNumber = selectetOrder.StocktakingOrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,

                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon<List<StockTakingOrderMatDetailModel>>>(LocalFile.Config.ApiIpHost + "stockTaking/getStockTakingOrderMatDetail", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    if (Result.Data.Count > 0)
                    {
                        //打开窗体
                        var window = new StocktakingDocumentDetailView(Result.Data);
                        window.Owner = Application.Current.MainWindow;
                        window.ShowDialog();
                    }
                    else
                    {
                        Growl.Warning("该盘点单据不存在单据明细！");
                    }
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }

        }

        public ICommand BtnOrderMatDetailCommand { get => new DelegateCommand(BtnOrderMatDetail); }
        public void BtnOrderMatDetail()
        {
            try
            {
                if (DataGridItemSource == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }

                //判断是否勾选单据
                var selectedOutOrder = DataGridItemSource.Where(t => t.IsSelected)
                                                         .FirstOrDefault();
                if (selectedOutOrder == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }
                #region 调用接口获取单据
                var body = new GetOutOrderDetailRequest()
                {
                    OrderId = selectedOutOrder.Id,
                    OrderNumber = selectedOutOrder.StocktakingOrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,

                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon<List<OutOrderMatDetailModel>>>(LocalFile.Config.ApiIpHost + "outStore/getOutOrderMatDetail", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    if (Result.Data.Count > 0)
                    {
                        //打开窗体
                        var window = new OutInventoryDocumentMatDetailView(Result.Data);
                        window.Owner = Application.Current.MainWindow;
                        window.ShowDialog();
                    }
                    else
                    {
                        Growl.Warning("该单据没有发料明细！");
                    }
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }
        }

        //开始入库 如果成功就直接跳转出库界面
        public ICommand BtnStartCommand { get => new DelegateCommand(BtnStart); }
        public void BtnStart()
        {
            try
            {
                //是否已搜索
                if (DataGridItemSource == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }
                //判断是否已勾选单据
                var selectetOrder = DataGridItemSource.Where(t => t.IsSelected)
                                                         .FirstOrDefault();
                if (selectetOrder == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }

                #region 调用接口开始盘点
                var body = new GetStockTakingOrderMatDetailRequest()
                {
                    StockTakingOrderId = selectetOrder.Id,
                    StockTakingOrderNumber = selectetOrder.StocktakingOrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "stockTaking/startStockTakingOrder", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    //成功后直接跳转
                    MainWindow1.viewModel.GoToStockTakingView = true;
                    //MainWindow2.viewModel.SelectedValue = "物料盘点";
                    Growl.Success("已跳转到物料盘点页面！");
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }
        }

        public ICommand BtnPauseCommand { get => new DelegateCommand(BtnPause); }
        public void BtnPause()
        {
            try
            {
                //是否已搜索
                if (DataGridItemSource == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }
                //判断是否已勾选单据
                var selectetOrder = DataGridItemSource.Where(t => t.IsSelected)
                                                         .FirstOrDefault();
                if (selectetOrder == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }

                #region 调用接口结束盘点
                var body = new GetStockTakingOrderMatDetailRequest()
                {
                    StockTakingOrderId = selectetOrder.Id,
                    StockTakingOrderNumber = selectetOrder.StocktakingOrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "stockTaking/endStockTakingOrder", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    Growl.Success("暂停盘点成功！");
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("暂停失败：调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }
        }

        public ICommand BtnCommitCommand { get => new DelegateCommand(BtnCommit); }
        public void BtnCommit()
        {
            try
            {
                //是否已搜索
                if (DataGridItemSource == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }
                //判断是否已勾选单据
                var selectetOrder = DataGridItemSource.Where(t => t.IsSelected)
                                                         .FirstOrDefault();
                if (selectetOrder == null)
                {
                    Growl.Warning("未勾选单据！");
                    return;
                }

                #region 调用接口提交盘点
                var body = new GetStockTakingOrderMatDetailRequest()
                {
                    StockTakingOrderId = selectetOrder.Id,
                    StockTakingOrderNumber = selectetOrder.StocktakingOrderNumber,
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "stockTaking/commitStockTakingOrder", body, "POST");
                if (Result != null && Result.Code == 200)
                {
                    Growl.Warning("单据已提交，库存数据已修改！");
                    BtnSearch();
                    StockTakingView.viewModel.RefreshStockTakingOrderList(StockTakingView.viewModel.SelectedOutOrderNumber);
                }
                else if (Result != null)
                {
                    Growl.Warning(Result.Message);
                }
                else
                {
                    Growl.Warning("调用接口失败！");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Growl.Error("出现异常：" + ex.Message);
            }
            finally
            {
            }
        }
        #endregion


        #region PageOperation 分页操作
        private int currentPage;
        public int CurrentPage
        {
            get { return currentPage; }
            set
            {
                SetProperty(ref currentPage, value);
                BtnSearch(false);
            }
        }

        private int maxPage;
        public int MaxPage
        {
            get { return maxPage; }
            set { SetProperty(ref maxPage, value); }
        }

        //总数量
        private int totalCount;
        public int TotalCount
        {
            get { return totalCount; }
            set { SetProperty(ref totalCount, value); }
        }

        private int pageSize = 10;
        public int PageSize
        {
            get => pageSize;
            set
            {
                SetProperty(ref pageSize, value);
                BtnSearch(true);
            }
        }


        public ICommand BtnFirstPageCommand { get => new DelegateCommand(BtnFirstPage); }
        public void BtnFirstPage()
        {
            CurrentPage = 1;
        }

        public ICommand BtnPrePageCommand { get => new DelegateCommand(BtnPrePage); }
        public void BtnPrePage()
        {
            if (CurrentPage > 1)
            {
                CurrentPage--;
            }
        }

        public ICommand BtnNextPageCommand { get => new DelegateCommand(BtnNextPage); }
        public void BtnNextPage()
        {
            if (CurrentPage < MaxPage)
            {
                CurrentPage++;
            }
        }

        public ICommand BtnLastPageCommand { get => new DelegateCommand(BtnLastPage); }
        public void BtnLastPage()
        {
            if (CurrentPage != MaxPage)
            {
                CurrentPage = MaxPage;
            }
        }
        #endregion
    }
}
