using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MESClassLibrary;
using MESClassLibrary.BLL.Painting;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.DAL;

namespace PaintingView
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Timers.Timer timer2 = new System.Timers.Timer();

        private void Form1_Load(object sender, EventArgs e)
        {
            //webBrowser1.ScriptErrorsSuppressed = true;
            StockInBLL bll=new StockInBLL();

            try
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                Thread t = new Thread(new ThreadStart(TimeGo));
                t.Start();

                Thread t2 = new Thread(new ThreadStart(TimeLoad));
                t2.Start();

                label3.BackColor = System.Drawing.Color.FromArgb(40,153,244);
                label9.BackColor = System.Drawing.Color.FromArgb(40, 153, 244);

                label20.BackColor = System.Drawing.Color.FromArgb(40, 153, 244);
                label14.BackColor = System.Drawing.Color.FromArgb(40, 153, 244);

                label4.BackColor = System.Drawing.Color.FromArgb(00, 255, 00);
                label10.BackColor = System.Drawing.Color.FromArgb(00, 255, 00);

                label19.BackColor = System.Drawing.Color.FromArgb(00, 255, 00);
                label13.BackColor = System.Drawing.Color.FromArgb(00, 255, 00);

                #region 由07:30-19:30改成08:00-20:00

                if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 08:00:00") && GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 20:00:00"))
                {
                    DataTable dtA = bll.SearchInfoA();
                    if (dtA != null && dtA.Rows.Count > 0)
                    {
                        label6.Text = dtA.Rows[0]["c1"].ToString();
                        label7.Text = dtA.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label6.Text = "0";
                        label7.Text = "0";
                    }

                    DataTable dtBA = bll.SearchInfoBByA();
                    if (dtBA != null && dtBA.Rows.Count > 0)
                    {
                        label16.Text = dtBA.Rows[0]["c1"].ToString();
                        label17.Text = dtBA.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label16.Text = "0";
                        label17.Text = "0";
                    }

                }

                #endregion

                #region 由19:30-00:00改为20:00-00:00

                if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 20:00:00") && GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 23:59:59"))
                {
                    DataTable dtA2 = bll.SearchInfoA();
                    if (dtA2 != null && dtA2.Rows.Count > 0)
                    {
                        label6.Text = dtA2.Rows[0]["c1"].ToString();
                        label7.Text = dtA2.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label6.Text = "0";
                        label7.Text = "0";
                    }

                    DataTable dtB1 = bll.SearchInfoB();
                    if (dtB1 != null && dtB1.Rows.Count > 0)
                    {
                        label16.Text = dtB1.Rows[0]["c1"].ToString();
                        label17.Text = dtB1.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label16.Text = "0";
                        label17.Text = "0";
                    }
                }

                #endregion

                #region 由00:00-08:00

                if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 00:00:00") && GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 08:00:00"))
                {
                    DataTable dtA3 = bll.SearchInfoAByB();
                    if (dtA3 != null && dtA3.Rows.Count > 0)
                    {
                        label6.Text = dtA3.Rows[0]["c1"].ToString();
                        label7.Text = dtA3.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label6.Text = "0";
                        label7.Text = "0";
                    }

                    DataTable dtB1 = bll.SearchInfoB();
                    if (dtB1 != null && dtB1.Rows.Count > 0)
                    {
                        label16.Text = dtB1.Rows[0]["c1"].ToString();
                        label17.Text = dtB1.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label16.Text = "0";
                        label17.Text = "0";
                    }
                }

                #endregion
                
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
            }
        }

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

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

        private void TimeLoad()
        {
            //System.Timers.Timer timer = new System.Timers.Timer();
            timer2.Interval = 5000;
            timer2.Enabled = true;
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Tick);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
            try
            {
                timer2.Stop();

                StockInBLL bll = new StockInBLL();

                #region 由7:30-19:30改为08:00-20:00

                if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 08:00:00") && GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 20:00:00"))
                {
                    DataTable dtA = bll.SearchInfoA();
                    if (dtA != null && dtA.Rows.Count > 0)
                    {
                        label6.Text = dtA.Rows[0]["c1"].ToString();
                        label7.Text = dtA.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label6.Text = "0";
                        label7.Text = "0";
                    }

                    DataTable dtBA = bll.SearchInfoBByA();
                    if (dtBA != null && dtBA.Rows.Count > 0)
                    {
                        label16.Text = dtBA.Rows[0]["c1"].ToString();
                        label17.Text = dtBA.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label16.Text = "0";
                        label17.Text = "0";
                    }

                }

                #endregion

                #region 由19:30-00:00改为20:00-00:00

                if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 20:00:00") && GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 23:59:59"))
                {
                    DataTable dtA2 = bll.SearchInfoA();
                    if (dtA2 != null && dtA2.Rows.Count > 0)
                    {
                        label6.Text = dtA2.Rows[0]["c1"].ToString();
                        label7.Text = dtA2.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label6.Text = "0";
                        label7.Text = "0";
                    }

                    DataTable dtB1 = bll.SearchInfoB();
                    if (dtB1 != null && dtB1.Rows.Count > 0)
                    {
                        label16.Text = dtB1.Rows[0]["c1"].ToString();
                        label17.Text = dtB1.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label16.Text = "0";
                        label17.Text = "0";
                    }
                }

                #endregion

                #region 由00:00-08:00

                if (GetDateTime() > DateTime.Parse(GetDateTime().ToShortDateString() + " 00:00:00") && GetDateTime() < DateTime.Parse(GetDateTime().ToShortDateString() + " 08:00:00"))
                {
                    DataTable dtA3 = bll.SearchInfoAByB();
                    if (dtA3 != null && dtA3.Rows.Count > 0)
                    {
                        label6.Text = dtA3.Rows[0]["c1"].ToString();
                        label7.Text = dtA3.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label6.Text = "0";
                        label7.Text = "0";
                    }

                    DataTable dtB1 = bll.SearchInfoB();
                    if (dtB1 != null && dtB1.Rows.Count > 0)
                    {
                        label16.Text = dtB1.Rows[0]["c1"].ToString();
                        label17.Text = dtB1.Rows[0]["c2"].ToString();
                    }
                    else
                    {
                        label16.Text = "0";
                        label17.Text = "0";
                    }
                }

                #endregion

                timer2.Start();
            }
            catch (Exception ex)
            {
                timer2.Start();
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
            }

            Thread.Sleep(500);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Program.a = 1;
            FrmDetail fr=new FrmDetail();
            fr.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Program.a = 2;
            FrmDetail fr = new FrmDetail();
            fr.Show();
        }

        public DateTime GetDateTime()
        {
            string sql = "";
            DateTime time;
            DataTable dt;
            try
            {
                sql = @"select convert(char(23),getdate(),121) as time";

                dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    //time = Convert.ToDateTime(dt.Rows[0]["time"].ToString());
                    time = DateTime.ParseExact(dt.Rows[0]["time"].ToString(), "yyyy-MM-dd HH:mm:ss.fff",
                        System.Globalization.CultureInfo.CurrentCulture);

                }
                else
                {
                    time = DateTime.Now;
                }
                return time;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return DateTime.Now;
            }
        }

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

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (!IsHandleCreated)
            {
                this.Close();
            }
        }
    }
}