using MesWork;
using System;
using System.Windows.Forms;

namespace DC_A95
{
    public partial class F_TestPage : Form
    {
        /// <summary>
        /// 
        /// </summary>
        static string stationName = "";
        /// <summary>
        /// 
        /// </summary>
        public F_TestPage()
        {
            InitializeComponent();
            foreach (var item in MesWorkForm.opNameList)
            {
                lb_Station.Items.Add(item);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lb_Station_SelectedIndexChanged(object sender, EventArgs e)
        {
            var sele = lb_Station.SelectedItem;
            if (sele != null)
            {
               stationName = sele.ToString();
            }
           
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Heartbeat_Click(object sender, EventArgs e)
        {
            var val = MesWorkForm.ReadPLC(14, stationName).ToString().ToLower().Replace("true", "1").Replace("false", "0");
            MesWorkForm.WritePLC_IF(14, stationName, val == "1" ? false : true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PartOn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(stationName))
            {
                MessageBox.Show("请先选择一个工位号！");
                return;
            }
          
           var engineNo = txt_EngineNo.Text;
           var sortNo = Convert.ToInt32(txt_SortNo.Text);
           var sortName = txt_SortName.Text;
           var orderNo = txt_OrderNo.Text;
           var palletNo = Convert.ToInt32(txt_PalletNo.Text);

           var printCode = txt_PrintCode.Text;
           var partCode = txt_PartCode.Text;

            /*
14	系统健康(PLC)
2	工件到位(PLC)
43	申请零件编号(PLC)
44	传递机型(PLC)
63	禁用MES(PLC)
9	机型标志（PLC）
10	变速箱总成编号（PLC）
11	产品型号（PLC）
80	托盘编号（PLC）
22	0空托盘，1合格件，2不合格件(PLC)
19	质量数据可以读数据（PLC）
21	质量数据工位合格标志(PLC)
23	0正常件，1返修件(PLC)
39	缸体打印码(PLC)
40	订单号(PLC)
120	上线缸体码(PLC)
*/
            MesWorkForm.WritePLC_IF(10, stationName, engineNo);
            MesWorkForm.WritePLC_IF(9, stationName, sortNo);
            MesWorkForm.WritePLC_IF(11, stationName, sortName);
            MesWorkForm.WritePLC_IF(40, stationName, orderNo);
            MesWorkForm.WritePLC_IF(80, stationName, palletNo);
            MesWorkForm.WritePLC_IF(39, stationName, printCode);
            MesWorkForm.WritePLC_IF(120, stationName, partCode);
            MesWorkForm.WritePLC_IF(2, stationName, true);
          
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Part_OnLine_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(stationName))
            {
                MessageBox.Show("请先选择一个工位号！");
                return;
            }

            MesWorkForm.WritePLC_IF(43, stationName, true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_TransferSort_Click(object sender, EventArgs e)
        {
            MesWorkForm.WritePLC_IF(44, stationName, true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_AllowSave_Click(object sender, EventArgs e)
        {
            MesWorkForm.WritePLC_IF(21, stationName, 1);
            MesWorkForm.WritePLC_IF(19, stationName, true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_PartOff_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(stationName))
            {
                MessageBox.Show("请先选择一个工位号！");
                return;
            }

            MesWorkForm.WritePLC_IF(10, stationName, 0);
            MesWorkForm.WritePLC_IF(9, stationName, 0);
            MesWorkForm.WritePLC_IF(11, stationName, 0);
            MesWorkForm.WritePLC_IF(40, stationName, 0);
            MesWorkForm.WritePLC_IF(80, stationName, 0);
            MesWorkForm.WritePLC_IF(39, stationName, 0);
            MesWorkForm.WritePLC_IF(120, stationName, 0);
            MesWorkForm.WritePLC_IF(2, stationName, false);

            MesWorkForm.WritePLC_IF(43, stationName, false);
            MesWorkForm.WritePLC_IF(44, stationName, false);
            MesWorkForm.WritePLC_IF(19, stationName, false);
        }
        
       


    }
}
