using HandyControl.Controls;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Media;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TouchSocket.Core;
using WCS.Model.ApiModel.MatInventoryDetail;
using WCS.Model.ApiModel.MXBackgroundThread;
using 智能仓储WCS管理系统.ViewModel;

namespace 智能仓储WCS管理系统
{
    public partial class MXOutOrderView : HandyControl.Controls.Window
    {
        public MXOutOrderViewModel viewModel = new MXOutOrderViewModel();
        public MXOutOrderView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

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

        int index = 0;

        private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            System.Windows.Controls.DataGrid datagrid = sender as System.Windows.Controls.DataGrid;
            index = datagrid.SelectedIndex;
            if (index >= 0)
            {
                if (viewModel.DataGridItemSource != null && viewModel.DataGridItemSource.Count() > index)
                {
                    var data = viewModel.DataGridItemSource.ElementAt(index);
                    try
                    {
                        var current = viewModel.DataGridItemSource.Where(t => t.IsSelected).First();
                        current.IsSelected = false;
                    }
                    catch { }

                    data.IsSelected = true;
                    viewModel.DataGridItemSource = viewModel.DataGridItemSource.ToList();
                }
            }
            //datagrid.UnselectAllCells();
        }

        private void txtMatQty1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //要求可以输入英文字符
            if (!Regex.IsMatch(e.Text, @"^[0-9A-Za-z]+$"))
            {
                e.Handled = true;
            }
        }

        public PickOrder SelectedOrder { get; set; }

        public List<MatInventoryDetailModel> inventoryDetails { get; set; }



        private void comfirmClick(object sender, RoutedEventArgs e)
        {
            var isFirstSend = 0;
            try
            {
                var button = sender as System.Windows.Controls.Button;
                if (button != null)
                {
                    if (button.Content.Equals("首盘发料"))
                        isFirstSend = 1;
                    else if (button.Content.Equals("全部发料"))
                        isFirstSend = 0;
                    else
                        isFirstSend = 2;
                }
                else
                {
                    Logs.Write("button获取失败了！！！");
                }
            }
            catch (Exception ex)
            {

            }


            if (viewModel.DataGridItemSource == null)
            {
                Growl.Warning("请搜索并选择领料单后进行确认！");
                return;
            }

            SelectedOrder = viewModel.DataGridItemSource.Where(t => t.IsSelected).FirstOrDefault();
            if (SelectedOrder == null)
            {
                Growl.Warning("请选择领料单后进行确认！");
                return;
            }

            if (SelectedOrder.pickBillNumber.Contains("SCLL"))
            {
                if (string.IsNullOrEmpty(viewModel.WarehouseCode))
                {
                    Growl.Warning("请输入仓库代码！");
                    txtWarehouseCode.Focus();
                    return;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(viewModel.WarehouseCode))
                {

                    Growl.Warning("非生产领料无需输入仓库代码！");
                    txtWarehouseCode.Focus();
                    return;
                }
                viewModel.WarehouseCode = string.Empty;
            }

            this.Hide();
            var detailWindow = new MXOutOrderDetailView(SelectedOrder.pickBillNumber, viewModel.WarehouseCode
                , SelectedOrder.orderProdNumbers, SelectedOrder.orderWorkNumbers, isFirstSend, reportSideCbx.Text
                );
            System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(detailWindow);
            mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;

            detailWindow.ShowDialog();
            if (detailWindow.DialogResult == true && detailWindow.DataGridItemSource != null && detailWindow.DataGridItemSource.Count > 0)
            {
                inventoryDetails = detailWindow.DataGridItemSource;
                //LocalStatic.CurrentPickBillNumber = SelectedOrder.pickBillNumber;
                this.Close();
            }
            else
                this.ShowDialog();
        }
    }

}
