using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto.taskType;
using SlnMesnac.Plc;
using SlnMesnac.WPF.Library;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SlnMesnac.Business.@base;
using TouchSocket.Core;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;

namespace SlnMesnac.WPF.Page
{
    /// <summary>
    /// CreateMoveWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MannulResignBoxWindow : Window
    {
        private ISqlSugarClient? SqlSugarClient;
        public readonly PlcAbsractFactory workShop3Plc;

        public MannulResignBoxWindow()
        {
            InitializeComponent();
            SqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
            //BaseBusiness NewBase = new BaseBusiness(App.ServiceProvider);
            //workShop3Plc = NewBase.GetPlcByKey("workShop3Plc");
            LoadLocations();  // 初始化 ComboBox 选项
        }

        // 加载地点选项（3001-3024）
        private void LoadLocations()
        {
            for (int i = 301; i <= 312; i++)
            {
                this.StartComboBox.Items.Add(i.ToString() + "A");
                this.StartComboBox.Items.Add(i.ToString() + "B");
            }
            StartComboBox.Items.Add("异常库位3066");
        }

        // 确认按钮点击事件
        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            string startCode = StartComboBox.SelectedItem?.ToString();
            string BoxCode = BoxTextbox.Text .ToString();

            if(SqlSugarClient == null)
            {
                MessageBox.Show("SqlSugarClient为空！");
                return;
            }

            if (string.IsNullOrEmpty(startCode))
            {
                MessageBox.Show("请输入入库库位");
                return;
            }


            if(BoxCode !="" && BoxCode != null)
            {
                if (startCode == BoxCode || BoxCode.Substring(0,10) != "SHS0000000" || BoxCode.Length !=12)
                {
                    MessageBox.Show("请检查格式！");
                    return;
                }
            }
            

            if (startCode == "异常库位3066")
            {
                startCode = "3066";
            }

            WmsBaseLocation? startLocation = SqlSugarClient.Queryable<WmsBaseLocation>().Where(it => it.LocationCode == startCode).First();
            WmsPalletInfo? wmsPalletInfo = SqlSugarClient.Queryable<WmsPalletInfo>().InnerJoin<WmsBaseLocation>((x,y) => x.Amount >= 0 && y.LocationCode == startCode && y.ContainerCode.Length>1 ).First();

            if (wmsPalletInfo != null && ForceWrite_CheckBox.IsChecked == false) {
                MessageBox.Show("目标库位托盘未清空,无法入库！请检查！若需强制入库，请勾选右侧强制入库选项！");
                return;
            }  

            try
            {
                SqlSugarClient.AsTenant().BeginTran();                
                //锁定库位
                startLocation.LocationStatus = 0;
                startLocation.ContainerCode = BoxCode;
                SqlSugarClient.Updateable(startLocation).ExecuteCommand();
                SqlSugarClient.AsTenant().CommitTran();
                //workShop3Plc.writeBoolByAddress("DB100.DBX120.0", false);
                MessageBox.Show($"库位{startCode}，入库{BoxCode}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"生成入库任务提交事务异常:{ex.Message}");
                SqlSugarClient.AsTenant().RollbackTran();
            }
            this.Close();
        }
    }
}