using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.Model;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.BLL.Injection;

namespace InjectionPC
{
    public partial class FrmDown : Form
    {
        private static string  reason = "",downType="";
        public FrmDown()
        {
            InitializeComponent();
        }

        private void label22_Click(object sender, EventArgs e)
        {
            label21.BackColor = System.Drawing.Color.Gray;
            label22.BackColor = System.Drawing.Color.Chartreuse;
            label11.BackColor = System.Drawing.Color.Gray;
        }

        void dLbRoom_Click(object sender, EventArgs e)
        {
            Label a = (Label)sender;

            if (a.BackColor == Color.Transparent)
            {
                a.BackColor = Color.FromArgb(150, Color.Chartreuse);
                reason += a.Text + ";";
            }
            else
            {
                reason = reason.Replace(a.Text + ";", "");
                a.BackColor = Color.Transparent;
            }
        }

        private void FrmDown_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            label6.Text = Program.station;
            label8.Text = Program.OperatorName;
            comboBox1.Text = Program.Shift;
            label13.BackColor = System.Drawing.Color.Gray;
            label14.BackColor = System.Drawing.Color.Gray;
            label12.BackColor = System.Drawing.Color.Gray;
            label22.BackColor = System.Drawing.Color.Gray;
            Thread t = new Thread(new ThreadStart(TimeGo));
            t.Start();
        }

        private void TimeGo()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Thread.Sleep(500);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string stationID="";
            InjectionDownRecordBLL bll=new InjectionDownRecordBLL();
            InjectionDownRecordModel md=new InjectionDownRecordModel();

            #region 获取工位编号

            StationBLL sbll=new StationBLL();
            DataTable sdt = sbll.SearchInfoByNo(Program.station);
            if (sdt != null && sdt.Rows.Count > 0)
            {
                stationID = sdt.Rows[0]["StationID"].ToString();
                sdt.Dispose();
            }
            
            #endregion

            if (downType == "")
            {
                MessageBox.Show("请选择停线类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (reason == "")
            {
                MessageBox.Show("请选择停线原因!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (reason != "")
            {
                //md.ID = Guid.NewGuid().ToString();
                md.StationID = stationID;
                md.DownType = downType;
                md.DownReason = reason;
                md.Des = textBox1.Text;
                //md.StartTime = DateTime.Now;

                bool flag=bll.UpdateInfo(md);
                if (flag == true)
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                reason = "";
                downType = "";
            }
        }

        private void label12_Click(object sender, EventArgs e)
        {
            label11.BackColor = System.Drawing.Color.Gray;
            label12.BackColor = System.Drawing.Color.Chartreuse;
            label13.BackColor = System.Drawing.Color.Gray;
            label14.BackColor = System.Drawing.Color.Gray;
            downType = label12.Text;
            panel2.Visible = false;
        }

        private void label13_Click(object sender, EventArgs e)
        {
            label11.BackColor = System.Drawing.Color.Gray;
            label12.BackColor = System.Drawing.Color.Gray;
            label13.BackColor = System.Drawing.Color.Chartreuse;
            label14.BackColor = System.Drawing.Color.Gray;
            panel2.Visible = true;
            downType = label13.Text;

            DownReasonBLL bll = new DownReasonBLL();

            int drow = 0, dcol = 4;

            DataTable dt = bll.SearchReason();
            if (dt != null && dt.Rows.Count > 0)
            {
                drow = Convert.ToInt32(Math.Ceiling((double)dt.Rows.Count / dcol));
                Label[] dLb = new Label[7];
                string[] dstr = new string[dt.Rows.Count];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dstr[i] = dt.Rows[i]["Reason"].ToString();
                }

                for (int i = 0; i < dcol; i++) //列
                {
                    for (int j = 0; j < drow; j++) //行
                    {
                        dLb[i] = new Label();
                        if ((i + dcol * j) < dstr.Length)
                        {
                            dLb[i].Text = dstr[i + dcol * j].ToString();
                            dLb[i].Font = new Font(dLb[i].Font.FontFamily, 20, FontStyle.Bold);

                            dLb[i].Size = new Size(250, 50);
                            dLb[i].Location = new Point(20 + i * (dLb[i].Size.Width + 25),
                                5 + j * (dLb[i].Size.Height + 20));
                            dLb[i].BorderStyle = BorderStyle.FixedSingle;
                            dLb[i].BackColor = Color.Transparent;
                            dLb[i].TextAlign = ContentAlignment.MiddleCenter;
                            panel2.Controls.Add(dLb[i]);
                            dLb[i].Click += new EventHandler(dLbRoom_Click);
                        }
                    }
                }
                dt.Dispose();
            }
        }

        private void label14_Click(object sender, EventArgs e)
        {
            label11.BackColor = System.Drawing.Color.Gray;
            label12.BackColor = System.Drawing.Color.Gray;
            label13.BackColor = System.Drawing.Color.Gray;
            label14.BackColor = System.Drawing.Color.Chartreuse;
            downType = label14.Text;
            panel2.Visible = true;
            DownReasonBLL bll = new DownReasonBLL();

            int drow = 0, dcol = 4;

            DataTable dt = bll.SearchReason();
            if (dt != null && dt.Rows.Count > 0)
            {
                drow = Convert.ToInt32(Math.Ceiling((double)dt.Rows.Count / dcol));
                Label[] dLb = new Label[7];
                string[] dstr = new string[dt.Rows.Count];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dstr[i] = dt.Rows[i]["Reason"].ToString();
                }

                for (int i = 0; i < dcol; i++) //列
                {
                    for (int j = 0; j < drow; j++) //行
                    {
                        dLb[i] = new Label();
                        if ((i + dcol * j) < dstr.Length)
                        {
                            dLb[i].Text = dstr[i + dcol * j].ToString();
                            dLb[i].Font = new Font(dLb[i].Font.FontFamily, 20, FontStyle.Bold);

                            dLb[i].Size = new Size(250, 50);
                            dLb[i].Location = new Point(20 + i * (dLb[i].Size.Width + 25),
                                5 + j * (dLb[i].Size.Height + 20));
                            dLb[i].BorderStyle = BorderStyle.FixedSingle;
                            dLb[i].BackColor = Color.Transparent;
                            dLb[i].TextAlign = ContentAlignment.MiddleCenter;
                            panel2.Controls.Add(dLb[i]);
                            dLb[i].Click += new EventHandler(dLbRoom_Click);
                        }
                    }
                }
                dt.Dispose();
            }
        }

        private void label21_Click(object sender, EventArgs e)
        {
            label22.BackColor = System.Drawing.Color.Gray;
            label21.BackColor = System.Drawing.Color.Chartreuse;
            label11.BackColor = System.Drawing.Color.Chartreuse;
            label12.BackColor = System.Drawing.Color.Gray;
            label13.BackColor = System.Drawing.Color.Gray;
            label14.BackColor = System.Drawing.Color.Gray;
        }

        private void label11_Click(object sender, EventArgs e)
        {
            label11.BackColor = System.Drawing.Color.Chartreuse;
            label12.BackColor = System.Drawing.Color.Gray;
            label13.BackColor = System.Drawing.Color.Gray;
            label14.BackColor = System.Drawing.Color.Gray;
        }

        private void label9_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (!IsHandleCreated)
            {
                this.Close();
            }
        }
        private void textBox1_Click(object sender, EventArgs e)
        {
            ShowInputPanel();
        }

        private const Int32 WM_SYSCOMMAND = 274;

        private const UInt32 SC_CLOSE = 61536;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        private static extern int RegisterWindowMessage(string lpString);



        //显示屏幕键盘

        public static int ShowInputPanel()
        {

            try
            {

                dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";

                if (!System.IO.File.Exists(file))

                    return -1;

                Process.Start(file);

                //return SetUnDock(); //不知SetUnDock()是什么,所以直接注释返回1

                return 1;

            }

            catch (Exception)
            {

                return 255;

            }

        }

        private void label2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0); 
        }

    }
}