using HandyControl.Controls;
using HandyControl.Tools.Extension;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WCS.BLL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.MatBaseInfo;
using 智慧物流软件系统.Api;
using 智慧物流软件系统.Tool;
using 智慧物流软件系统.ViewModel;
using 智慧物流软件系统.Views.Controls;

namespace 智慧物流软件系统
{
    public partial class MatBaseInoGenarateMatInfoView : System.Windows.Window
    {

        public MatBaseInfoModel matBaseInfo = null;


        public MatBaseInoGenarateMatInfoView(MatBaseInfoModel _matBaseInfo = null)
        {
            InitializeComponent();
            if (_matBaseInfo != null)
            {
                matBaseInfo = _matBaseInfo;
                //绑定数据
                txtMatCode.Text = matBaseInfo.MatCode;
                txtMatName.Text = matBaseInfo.MatName;
                txtMatSpec.Text = matBaseInfo.MatSpec;
                txtMatCustomer.Text = matBaseInfo.MatCustomer;
            }
        }


        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            #region 数据校验
            if (string.IsNullOrEmpty(txtMatBatch.Text.Trim()))
            {
                Growl.Warning("请输入物料批次后进行操作！");
                txtMatBatch.Focus();
                return;
            }
            var matBatch = txtMatBatch.Text.Trim();
            if (!IsInputNumberAllowed(matBatch))
            {
                Growl.Warning("【物料批次】请输入数字！");
                txtMatBatch.Focus();
                return;
            }

            var matQty = txtMatQty.Text.Trim();
            if (!IsInputNumberAllowed(matQty))
            {
                Growl.Warning("【单盘数量】请输入数字！");
                txtMatQty.Focus();
                return;
            }

            var totalCount = txtTotalCount.Text.Trim();
            if (!IsInputNumberAllowed(totalCount))
            {
                Growl.Warning("【物料盘数】请输入数字！");
                txtTotalCount.Focus();
                return;
            }
            #endregion

            #region 调用接口生成条码
            try
            {
                var body = new GenerateMatInfoRequest()
                {
                    MatBaseInfo = matBaseInfo,
                    MatBatch = matBatch,
                    TotalCount = Convert.ToInt32(txtTotalCount.Text.Trim()),
                    MatQty = Convert.ToInt32(txtMatQty.Text.Trim()),
                    UserName = LocalStatic.CurrentUser,
                    DeviceType = LocalFile.Config.DeviceType,
                };
                var Result = ApiHelp.GetDataFromHttp<ResponseCommon<List<MatInfoModel>>>(LocalFile.Config.ApiIpHost + "matBaseInfo/generateMatInfo", body, "POST");
                if (Result != null && Result.Data != null && Result.Data.Count > 0)
                {
                    if (button != null && button.Content.ToString().Contains("打印"))
                    //批量打印
                    {
                        Task.Run(() =>
                    {
                        ProcessDialog process = null;
                        Dialog dia = null;
                        var totalCount = Result.Data.Count;
                        int currentCount = 0;

                        App.Current.Dispatcher.Invoke(() =>
                        {
                            process = new ProcessDialog();
                            dia = Dialog.Show(process);
                        });
                        Result.Data.ForEach(t =>
                        {
                            PrintTender.PrintTag(new PrintClass()
                            {
                                MatSn = t.MatSN,
                                MatName = t.MatName,
                                MatCode = t.MatCode,
                                MatBatch = t.MatBatch,
                                MatQty = t.MatQty.ToString(),
                                MatSpec = t.MatSpec,
                            });
                            currentCount++;
                            if (process != null)
                                process.viewModel.ProcessValue = Convert.ToInt32(((decimal)currentCount / totalCount) * 100);
                        });
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            dia.Close();
                            dia.Collapse();
                        });

                        #region 回传后端打印成功
                        try
                        {
                            var body = new PrintedMatInfoRequest()
                            {
                                PrintedMatInfoIds = Result.Data.Select(t => t.Id).ToList(),
                                UserName = LocalStatic.CurrentUser,
                                DeviceType = LocalFile.Config.DeviceType,
                            };
                            var Result11 = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "matBaseInfo/printedMatInfo", body, "POST");
                            if (Result11 != null && Result11.Code == 200)
                            {
                                //回传成功
                            }
                            else
                            {
                                //回传失败
                                Logs.Write("回传“打印成功”失败");
                            }
                        }
                        catch (Exception ex)
                        {
                            Logs.Write("回传“打印成功”失败：" + ex.Message);
                        }
                        #endregion
                    });
                    }
                    Growl.Success("生成成功！");
                }
            }
            catch (Exception ex)
            {
                Growl.Error("加载数据失败：" + ex.Message);
                return;
            }
            #endregion

            this.DialogResult = true;
            this.Close();
        }

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

        private void txtMatBatch_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = !IsInputNumberAllowed(e.Text);
        }

        private bool IsInputNumberAllowed(string text)
        {
            Regex regex = new Regex("^[0-9]+$"); // 只允许数字  
            return regex.IsMatch(text);
        }
    }
}
