using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static DataLinkMesWork.SQLCommon;
using System.Windows.Forms;
using SlMesDbIterface;
using MesToMs;
using System.Reflection;
using ConverterBytesAndStruct;
using MesWork;
using DC_A95;
using NPOI.SS.Formula.Functions;
using System.Security.Cryptography;

namespace ExternalDataSync
{
    /// <summary>
    /// 
    /// </summary>
    public partial class F_StationManage : Form
    {
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_Station_AID = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_工位代码 = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_StationName = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_工位名称 = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_工序号 = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_工序名称 = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_PLC = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_区域代码 = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_机床类型 = "";
        /// <summary>
        /// 
        /// </summary>
        private static string Curr_是否启用MES = "";
        /// <summary>
        /// 
        /// </summary>
        public F_StationManage()
        {
            InitializeComponent();

            var sql_area = $" SELECT [区域代码] FROM [MES_计划BOM_工位与名称]  group by [区域代码]";
            SqlOperation.ExecuteSql(sql_area, out DataTable dt_area, out string error);
            DataRow emptyRow = dt_area.NewRow();
            emptyRow[0] = "";
            dt_area.Rows.InsertAt(emptyRow, 0);
            this.cmb_Area.ValueMember = "区域代码";
            this.cmb_Area.DisplayMember = "区域代码";
            this.cmb_Area.DataSource = dt_area;

            Refresh_Station();
        }

        private void Form_MoveSqlTable_Load(object sender, EventArgs e)
        {
        }
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (MessageBox.Show("确认要退出吗？", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            //{
            //}
            //else
            //{
            //    e.Cancel = true;
            //}
        }

        #region 工位管理

        private void btn_Station_Add_Click(object sender, EventArgs e)
        {

             var dlg = new D_Station(From_Opera.Insert, Curr_StationName, Curr_工位名称, Curr_工序号, Curr_工序名称, Curr_PLC
                  , Curr_区域代码, Curr_机床类型, Curr_是否启用MES
            );
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                var D_StationName = dlg.D_StationName;
                var D_工位名称 = dlg.D_工位名称;
                var D_工序号 = dlg.D_工序号;
                var D_工序名称 = dlg.D_工序名称;
                var D_PLC = dlg.D_PLC;
                var D_区域代码 = dlg.D_区域代码;
                var D_机床类型 = dlg.D_机床类型;
                var D_是否启用MES = dlg.D_是否启用MES;

                var name = "Event_工位_增加";
                var sqlParameter = new SqlParameter[] {
                     new SqlParameter("@工位号", D_StationName),
                     new SqlParameter("@工位名称", D_工位名称),
                     new SqlParameter("@工序号", D_工序号),
                     new SqlParameter("@工序名称", D_工序名称),
                     new SqlParameter("@PLC", D_PLC),
                     new SqlParameter("@区域代码", D_区域代码),
                     new SqlParameter("@机床类型", D_机床类型),
                     new SqlParameter("@是否启用MES", D_是否启用MES)
                 };
                SqlOperation.ExecuteStoredProcedure(name, sqlParameter, out string error);

                Refresh_Station();
            }
        }

        private void btn_Station_Update_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Curr_StationName))
            {
                MessageBox.Show("请选择一个工位！");
                return;
            }
            var dlg = new D_Station(From_Opera.Update, Curr_StationName, Curr_工位名称, Curr_工序号, Curr_工序名称, Curr_PLC
         , Curr_区域代码, Curr_机床类型, Curr_是否启用MES);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                var D_StationName = dlg.D_StationName;
                var D_工位名称 = dlg.D_工位名称;
                var D_工序号 = dlg.D_工序号;
                var D_工序名称 = dlg.D_工序名称;
                var D_PLC = dlg.D_PLC;
                var D_区域代码 = dlg.D_区域代码;
                var D_机床类型 = dlg.D_机床类型;
                var D_是否启用MES = dlg.D_是否启用MES;

                var name = "Event_工位_修改";
                var sqlParameter = new SqlParameter[] {
                     new SqlParameter("@工位号", D_StationName),
                     new SqlParameter("@工位名称", D_工位名称),
                     new SqlParameter("@工序号", D_工序号),
                     new SqlParameter("@工序名称", D_工序名称),
                     new SqlParameter("@PLC", D_PLC),
                     new SqlParameter("@区域代码", D_区域代码),
                     new SqlParameter("@机床类型", D_机床类型),
                     new SqlParameter("@是否启用MES", D_是否启用MES)
                 };
                SqlOperation.ExecuteStoredProcedure(name, sqlParameter, out string error);

                Refresh_Station();
            }

        }

        private void btn_Station_Delete_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Curr_StationName))
            {
                MessageBox.Show("请选择一个工位！");
                return;
            }
            if (MessageBox.Show($"确认删除工位？工位号：{Curr_StationName}", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (MessageBox.Show($"再次 确认删除工位？工位号：{Curr_StationName}", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //SELECT[AID],[产品名称],[机型号],[机型名称],[操作时间],[备注] FROM[配方_大柴_机型]
                    //SELECT[AID],[机型号],[区域代码],[工位号],[配方号],[操作时间],[备注] FROM[配方_大柴_机型配方]
                    var name = "Event_工位_删除";
                    var sqlParameter = new SqlParameter[] {
                         new SqlParameter("@工位号", Curr_StationName)
                    };
                    SqlOperation.ExecuteStoredProcedure(name, sqlParameter, out string error);
                    Refresh_Station();
                }
            }
        }


        #endregion
     
        private void Refresh_Station()
        {
            var sql = $@" SELECT [ID],[工位代码],[工位号],[工位名称],[工序号],[工序名称],[是否启用MES],[PLC],[区域代码],[机床类型]  ";
            sql += "FROM [MES_计划BOM_工位与名称] ";
            sql += cmb_Area.Text!=""? $"Where 区域代码='{cmb_Area.Text}' ":"";
            sql += " ORDER BY [工位号]";
            SqlOperation.ExecuteSql(sql, out DataTable dt, out string error);
            dgv_Station.DataSource = dt;
        }

       
        private void dgv_Station_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                Curr_Station_AID = dgv_Station.CurrentRow.Cells["ID"].Value.ToString();
                Curr_工位代码 = dgv_Station.CurrentRow.Cells["工位代码"].Value.ToString();
                Curr_StationName = dgv_Station.CurrentRow.Cells["工位号"].Value.ToString();
                Curr_工位名称 = dgv_Station.CurrentRow.Cells["工位名称"].Value.ToString();
                Curr_工序号 = dgv_Station.CurrentRow.Cells["工序号"].Value.ToString();
                Curr_工序名称 = dgv_Station.CurrentRow.Cells["工序名称"].Value.ToString();
                Curr_PLC = dgv_Station.CurrentRow.Cells["PLC"].Value.ToString();
                Curr_区域代码 = dgv_Station.CurrentRow.Cells["区域代码"].Value.ToString();
                Curr_机床类型 = dgv_Station.CurrentRow.Cells["机床类型"].Value.ToString();
                Curr_是否启用MES = dgv_Station.CurrentRow.Cells["是否启用MES"].Value.ToString();

            }
        }

        private void cmb_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            StationList_Binding(cmb_Area.SelectedValue.ToString());

        }
        private void StationList_Binding(string areaname)
        {
            Refresh_Station();
        }

    }


}
