using MesWorkMqtt;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;

using bizFacade;
using MesWorkMqtt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MQTTnet;
using System.Threading;
using DoWhatPlc;
using MesServerWork;

namespace SyUpLoad
{
    public partial class PartLoad
    {
        private void fun_EngineTypeGetOver_PLC3(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            SetChekcBox(tagValue, checkBox_PlcEngineTypeGetOver3);
            try
            {
                if (tagValue == 1)
                {
                    int EngineTypeID;//机型代码
                    int PartPallet_Code;//托盘编号
                    string EngineType;//机型
                    string EngineID;//工件编号
                                    //GetUpLoadWarehouseNoOpName02(string opName, string EngineIDType, out DataTable dt)
                    

                    MIS_BASE.GetEngineTypeFromPLC(OPName02, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code);
                   
                    
                    
                    SetTextBox(EngineType, textBox_PlcType3);
                    SetTextBox(EngineID, textBox_PlcEngineID3);

                    DataTable dt;
                    BaseDataSystemMES.GetUpLoadWarehouseNoOpName02(OPName02, EngineType, out dt);
                    if (dt.Rows.Count < 1)
                    {

                        button_ManualPartLoad.Invoke(new Action(() => {
                            button_ManualPartLoad.Enabled = true;
                        }));
                        MessageBox.Show("下线库存无匹配的工件！");
                        return;
                    }
                    int engineTypeID_MES = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                    string engineID_MES = dt.Rows[0]["发动机号"].ToString();
                    string engineType_MES = dt.Rows[0]["发动机型号"].ToString();
                    byte kuweiNum_MES = Convert.ToByte(dt.Rows[0]["库位号"]);
                    DtWebApi.ReadWritePLC.WritePLC(warehouseLocation.ToString(), OpName, kuweiNum_MES);


                    MIS_BASE.Write_FixAllow(OpName, true);

                    // EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code);
                }
                else
                {
                    MIS_BASE.Write_FixAllow(OpName, false);

                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function03:EngineTypeGetOver_PLC");
            }
        }

        private void fun_MesRead3(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);
            //var cb = button_ManualPartLoad;
            //cb.Invoke(new Action(() => {
            //    if (tagValue == 1)
            //    {
            //        cb.Enabled = false;
            //    }
            //    else
            //    {
            //        cb.Enabled = true;
            //    }
            //}));
            SetChekcBox(tagValue, checkBox_MesRead3);

            if (tagValue == 1)
            {

                //保存库数据
                int EngineTypeID;//机型代码
                int PartPallet_Code;//托盘编号
                string EngineType;//机型
                string EngineID;//工件编号

                MIS_BASE.GetEngineTypeFromPLC(OpName, out EngineTypeID, out EngineType, out EngineID, out PartPallet_Code);
                object kuweiNum;
                kuweiNum = DtWebApi.ReadWritePLC.ReadPLC(warehouseLocation.ToString(), OpName);

                //1转移数据到总线

                //2转移数据到总线
                BaseDataSystemMES.GetUpLoadBarCode_0utUpdate(OPName02, kuweiNum.ToString());
                BaseDataSystemMES.GetUpLoadBarCode(OPName02, out DataTable dt_OPName02);


                dataGridView_OPA1002.Invoke(new Action(() => {
                    dataGridView_OPA1002.DataSource = dt_OPName02;
                }));

                MIS_BASE.Write_MesReadOver(OpName, true);
            }
            else
            {
                MIS_BASE.Write_MesReadOver(OpName, false);
            }

        }
    }
}
