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_MesReadOver(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            string funID = ht_OpnameFun[OpName].ToString();

            switch (funID)
            {
                case "1":
                    break;
                case "2":
                    fun_MesReadOver2( e);
                    break;
                case "3":
                    fun_MesReadOver3(e);

                    break;
            }

        }
        private void fun_MesReadOver2(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            SetChekcBox(tagValue, checkBox_MesReadOver2);

        }
        private void fun_MesReadOver3(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            SetChekcBox(tagValue, checkBox_MesReadOver3);

        }
        private void fun_MesRead(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            string funID = ht_OpnameFun[OpName].ToString();

            switch (funID)
            {
                case "1":
                    break;
                case "2":
                    fun_MesRead2(e);
                    break;
                case "3":
                    fun_MesRead3(e);

                    break;
            }
        }
        private void fun_MesRead2(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_MesRead2);

            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="";
                //从库房拿 库位号
                //判断是否有库位 如果有

                DataTable dt;
                BaseDataSystemMES.GetUnLoadWarehouseNo(OPName02, out dt);
                if (dt.Rows.Count >= 1)
                {
                    kuweiNum = Convert.ToByte(dt.Rows[0]["库位号"]);
                    DtWebApi.ReadWritePLC.WritePLC(warehouseLocation.ToString(), OpName, kuweiNum);
                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);

                }
                else
                {
                    //button_ManualPartLoad.Invoke(new Action(() =>
                    //{
                    //    button_ManualPartLoad.Enabled = true;
                    //}));
                    MessageBox.Show("虚拟库存无位置");
                    return;
                }
                //engineTypeID = Convert.ToInt32(dt.Rows[0]["机型代码"]);
                //engineID = dt.Rows[0]["发动机号"].ToString();
                //engineType = dt.Rows[0]["发动机型号"].ToString();
                //kuweiNum = Convert.ToByte(dt.Rows[0]["库位号"]);



                //kuweiNum =DtWebApi.ReadWritePLC.ReadPLC(warehouseLocation.ToString(), OpName);

//                BaseDataSystemMES.GetUpLoadBarCode_Update(OpName, kuweiNum.ToString(), "", "",
//EngineID, EngineType, EngineTypeID);
//                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);
            }

        }
        private void fun_EngineTypeGetOver_PLC2(object e)
        {
            DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
            string OpName = Convert.ToString(msgEvent.OpName);
            int tagValue = Convert.ToInt32(msgEvent.TagValue);

            SetChekcBox(tagValue, checkBox_PlcEngineTypeGetOver2);
            try
            {
                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);
                    SetTextBox(EngineType, textBox_PlcType2);
                    SetTextBox(EngineID, textBox_PlcEngineID2);
                    DataTable dt;
                    BaseDataSystemMES.GetUnLoadWarehouseNo(OpName, out  dt);
                    if(dt.Rows.Count==0)
                    {
                        MessageBox.Show("下线库位已满");
                        button_ManualPartLoad2.Invoke(new Action(() => {
                            button_ManualPartLoad2.Enabled = true;
                        }));
                    }
                    else
                    {
                        byte kuweiNum = Convert.ToByte(dt.Rows[0]["库位号"]);
                        DtWebApi.ReadWritePLC.WritePLC(warehouseLocation.ToString(), OpName, kuweiNum);


                        MIS_BASE.Write_FixAllow(OpName, true);


                    }
                    //EngineTypeGetOver_PLC_Work(OpName, EngineTypeID, EngineType, EngineID, PartPallet_Code);
                }

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