using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Media;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WCS.Model;
using WCS.Model.ApiModel.MatInventoryDetail;
using 智能仓储WCS管理系统.Api;
using 智能仓储WCS管理系统.ViewModel;
using 智能仓储WCS管理系统.Views.Controls;

namespace 智能仓储WCS管理系统
{
    public partial class MXOutOrderDetailView : HandyControl.Controls.Window
    {
        public MXOutOrderDetailViewViewModel viewModel;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="billNumber"></param>
        /// <param name="warehouseCode"></param>
        /// <param name="orderProdNumbers"></param>
        /// <param name="orderWorkNumbers"></param>
        /// <param name="isFirstSend">是否是首盘发料</param>
        public MXOutOrderDetailView(string billNumber, string warehouseCode
                 , string orderProdNumbers, string orderWorkNumbers, int isFirstSend = 0, string reportSide = null
            )
        {
            InitializeComponent();
            viewModel = new MXOutOrderDetailViewViewModel(billNumber, warehouseCode, orderProdNumbers, orderWorkNumbers, isFirstSend, reportSide);
            this.DataContext = viewModel;
        }

        private void closeClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataGrid datagrid = sender as DataGrid;
            var index = datagrid.SelectedIndex;
            if (index >= 0)
            {
                if (viewModel.DataGridItemSource != null && viewModel.DataGridItemSource.Count() > index)
                {
                    var data = viewModel.DataGridItemSource.ElementAt(index);
                    data.IsSelected = !data.IsSelected;
                    viewModel.DataGridItemSource = viewModel.DataGridItemSource.ToList();
                }
            }
            datagrid.UnselectAllCells();
        }

        private void txtMatQty1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Regex.IsMatch(e.Text, "^[0-9]"))
            {
                e.Handled = true;
            }
        }

        public List<MatInventoryDetailModel> DataGridItemSource = new List<MatInventoryDetailModel>();

        private void comfirmClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
             if (viewModel.DataGridItemSource != null)
            {
                DataGridItemSource = viewModel.DataGridItemSource.Where(t => t.IsSelected).ToList();

                if (DataGridItemSource != null && DataGridItemSource.Count > 0)
                {
                    var dia = Dialog.Show(new TextDialog());
                    try
                    {
                        #region 调用接口保存出库单据
                        var body1 = new SysOutOrderByMatSnRequest()
                        {
                            OrderNumber = viewModel.BillNumber,
                            OrderType = "出库",
                            OrderSource = "WCS前端",
                            SnList = DataGridItemSource.Select(t => t.MatSN).ToList(),
                            DeviceType = LocalFile.Config.DeviceType,
                            UserName = LocalStatic.CurrentUser
                        };
                        var Result1 = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "outstore/sysOutOrderByMatSn", body1, "POST");
                        if (Result1 != null && Result1.Code == 200)
                        {
                            //出库单据保存成功 刷新左侧出库单列表
                            MXOutInventoryView.viewModel.RefreshOutOrderList(viewModel.BillNumber);
                            Growl.Success("出库单据保存成功，请点击【开始发料】进行发料！");
                        }
                        else if (Result1 != null)
                        {
                            Growl.Warning(Result1.Message);
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        Growl.Error("保存出库单据失败：" + ex.Message);
                    }
                    finally
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            dia.Close();
                            dia.Collapse();
                        });
                    }
                }
                this.Close();
            }
            else
                this.Close();
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            viewModel.RefreshCount();
        }
        private void allChecked_Checked(object sender, RoutedEventArgs e)
        {
            if (viewModel != null && viewModel.DataGridItemSource != null && viewModel.DataGridItemSource.Count() > 0)
            {
                foreach (var item in viewModel.DataGridItemSource)
                {
                    item.IsSelected = true;
                }
                viewModel.DataGridItemSource = viewModel.DataGridItemSource.ToList();
            }
        }

        private void allChecked_Unchecked(object sender, RoutedEventArgs e)
        {
            if (viewModel != null && viewModel.DataGridItemSource != null && viewModel.DataGridItemSource.Count() > 0)
            {
                foreach (var item in viewModel.DataGridItemSource)
                {
                    item.IsSelected = false;
                }
                viewModel.DataGridItemSource = viewModel.DataGridItemSource.ToList();
            }
        }
    }
}
