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
    {
        /// <summary>
        /// 申请上线
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void fun_PartLoad(object e)
        {
            try
            {
                DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                string OpName = Convert.ToString(msgEvent.OpName);
                int tagValue = Convert.ToInt32(msgEvent.TagValue);

                SetChekcBox(tagValue, checkBox_PartLoad);


                int engineTypeID;
                string engineID;
                string engineType;
                byte kuweiNum;
                if (tagValue == 1)
                {
                    // BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);
                    DataTable dt;
                    BaseDataSystemMES.GetUpLoadBarCodeEngineID(OpName, out dt);
                    if (dt.Rows.Count < 1)
                    {

                        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]["库位号"]);




                    ////将产品信息写入PLC
                    MIS_BASE.Write_EngineTypeID_MES(OpName, engineTypeID);
                    MIS_BASE.Write_EngineType_MES(OpName, engineType);
                    MIS_BASE.Write_EngineID_MES(OpName, engineID);
                    DtWebApi.ReadWritePLC.WritePLC(warehouseLocation.ToString(), OpName, kuweiNum);

                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);

                    ////将产品信息写入PLC
                    MIS_BASE.Read_EngineTypeID_MES(OpName, out engineTypeID);
                    MIS_BASE.Read_EngineType_MES(OpName, out engineType);
                    MIS_BASE.Read_EngineID_MES(OpName, out engineID);

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

                    SetTextBox(engineType, textBox_MesType);
                    SetTextBox(engineID, textBox_MesEngineID);
                    SetTextBox(kuweiNum.ToString(), textBox_MesPosition);

                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function03:PartLoad");
            }
        }
        /// <summary>
        /// 申请上线
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        private void fun_PartLoadManual(string OpName)
        {
            try
            {
                bool engineTypeGetOver_MES;
                bool partLoad;
                MIS_BASE.Read_EngineTypeGetOver_MES(OpName, out engineTypeGetOver_MES);

                MIS_BASE.Read_PartLoad(OpName, out partLoad);

                if(partLoad == false || engineTypeGetOver_MES==true)
                {
                    MessageBox.Show("检查申请上线信号");
                    return;
                }

                //DoWhatPlc.CustomeEvetnArgs msgEvent = (DoWhatPlc.CustomeEvetnArgs)e;
                //string OpName = OPName01;
                //int tagValue = Convert.ToInt32(msgEvent.TagValue);

                //SetChekcBox(tagValue, checkBox_PartLoad);


                int engineTypeID;
                string engineID;
                string engineType;
                byte kuweiNum;
                //if (tagValue == 1)
                {
                    // BaseDataSystemMES.MesServer_Process_EngineTypeOver(OpName);
                    DataTable dt;
                    BaseDataSystemMES.GetUpLoadBarCodeEngineID(OpName, out dt);
                    if (dt.Rows.Count < 1)
                    {

                        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]["库位号"]);




                    ////将产品信息写入PLC
                    MIS_BASE.Write_EngineTypeID_MES(OpName, engineTypeID);
                    MIS_BASE.Write_EngineType_MES(OpName, engineType);
                    MIS_BASE.Write_EngineID_MES(OpName, engineID);
                    DtWebApi.ReadWritePLC.WritePLC(warehouseLocation.ToString(), OpName, kuweiNum);

                    MIS_BASE.Write_EngineTypeGetOver_MES(OpName, true);

                    ////将产品信息写入PLC
                    MIS_BASE.Read_EngineTypeID_MES(OpName, out engineTypeID);
                    MIS_BASE.Read_EngineType_MES(OpName, out engineType);
                    MIS_BASE.Read_EngineID_MES(OpName, out engineID);

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

                    SetTextBox(engineType, textBox_MesType);
                    SetTextBox(engineID, textBox_MesEngineID);
                    SetTextBox(kuweiNum.ToString(), textBox_MesPosition);

                }

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

        private void fun_PartLoadManual2(string OpName)
        {
            try
            {
                bool engineTypeGetOver;
                bool partLoad;

                int engineTypeID;
                string engineID;
                string engineType;
                byte kuweiNum;

                MIS_BASE.Read_EngineGetOver_PLC(OpName, out engineTypeGetOver);


                if (engineTypeGetOver == false)
                {
                    MessageBox.Show("检查传递机型信号");
                    return;
                }

                DataTable dt;
                BaseDataSystemMES.GetUnLoadWarehouseNo(OpName, out dt);
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("下线库位已满");
                    button_ManualPartLoad2.Invoke(new Action(() => {
                        button_ManualPartLoad2.Enabled = true;
                    }));
                    return;
                }
                else
                {
                    kuweiNum = Convert.ToByte(dt.Rows[0]["库位号"]);
                    DtWebApi.ReadWritePLC.WritePLC(warehouseLocation.ToString(), OpName, kuweiNum);
                    MIS_BASE.Write_FixAllow(OpName, true);
                }
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "Function03:PartLoad");
            }
        }

    }
}
