using SlMesDbIterface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesWork
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Temp { }
    /// <summary>
    /// 延时刷新
    /// </summary>
    public partial class MesWorkForm
    {
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_OnLine_Show_Tick(object sender, EventArgs e)
        {
            timer_OnLine_Show.Enabled = false;

            Show_OnLineState();
            label_RunSort.Text = $"{RunSort}";

            timer_OnLine_Show.Enabled = true;
        }
        /// <summary>
        /// 获取数据库连接状态
        /// </summary>
        private void CommunicationCheckOnLine()
        {
            //源数据库
            new Thread(new ThreadStart(delegate ()
            {
                while (true)
                {
                    try
                    {
                        MesWorkForm.OnLine_Sql = SqlOperation.ExecuteSql("select getdate()", out DataTable dt,out string error);
                    }
                    catch (Exception) { }
                    Thread.Sleep(5000);
                }
            }))
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 显示通讯状态
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="onLine"></param>
        private void OnLineState_C(PictureBox pb, bool onLine)
        {
            pb.Image = onLine ? global::CentralControl.Properties.Resources.连接状态
                    : global::CentralControl.Properties.Resources.断开状态;
        }
        /// <summary>
        /// 显示通讯状态
        /// </summary>
        private void Show_OnLineState()
        {
            //状态显示
            OnLineState_C(pb_OnLine_Sql, MesWorkForm.OnLine_Sql);
        }



        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_ShowStation_Tick(object sender, EventArgs e)
        {
            timer_ShowStation.Enabled = false;

            RefreshShowStation();

            timer_ShowStation.Enabled = true;

        }
        /// <summary>
        /// 显示工位状态
        /// </summary>
        public static void RefreshShowStation()
        {
            try
            {
                foreach (DictionaryEntry item in ht_Station_Content)
                {
                    var stationName = item.Key.ToString();
                    var control = (Panel)item.Value;

                    var auto = ht_Signal__Station_Auto[stationName].ToString().ToLower();
                    var running = ht_Signal__Station_Running[stationName].ToString().ToLower();
                    var manual = ht_Signal__Station_Manual[stationName].ToString().ToLower();
                    var alarm = ht_Signal__Station_Alarm[stationName].ToString().ToLower();
                    var offLine = ht_Signal__Station_OffLine[stationName].ToString().ToLower();

                    if (ValueT(offLine))
                    {
                        if (ValueT(auto))
                        {
                            control.BackColor = color_Yellow;
                        }
                        if (ValueT(running))
                        {
                            control.BackColor = color_Green;
                        }
                        if (ValueT(manual))
                        {
                            control.BackColor = color_Faw;
                        }
                        if (ValueT(alarm))
                        {
                            control.BackColor = color_Red;
                        }
                    }
                    else
                    {
                        control.BackColor = color_Bad;
                    }

                }

            }
            catch (Exception err)
            {
            }
        }
        /// <summary>
        /// 值处理函数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ValueT(string value)
        {
            bool success = false;
            if (value == "true" | value == "1")
            {
                success = true;
            }
            return success;
        }
        /// <summary>
        /// 显示报警信息
        /// 临时！！！！
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_ShowAlarm_Tick(object sender, EventArgs e)
        {
            timer_ShowAlarm.Enabled = false;
            //try
            //{
            //    var sql =
            //   $" SELECT [StationName],[AlarmCode],[AlarmTime],[SureTime] FROM [SP_Curr_Alarm]" +
            //   $" ORDER BY [ID] DESC";
            //    SqlOperation.ExecuteSql(sql, out DataTable dt_Alarm, out string error);
            //    dgv_Alarm.DataSource = dt_Alarm;
            //}
            //catch (Exception err) { }
            //try
            //{
            //    var sql =
            //   $" SELECT [StationName],[AlarmCode],[AlarmTime],[SureTime] FROM [SP_Curr_Alarm_Andon]" +
            //   $" ORDER BY [ID] DESC";
            //    SqlOperation.ExecuteSql(sql, out DataTable dt_Alarm_Andon, out string error);
            //    dgv_Alarm_Andon.DataSource = dt_Alarm_Andon;
            //}
            //catch (Exception err) { }

            timer_ShowAlarm.Enabled = true;
        }







        /// <summary>
        /// 布局按钮显示
        /// </summary>
        /// <param name="visible"></param>
        public void Visible_SaveLyoutBtn(bool visible)
        {
            btn_SaveLyout.Visible = visible;
        }








    }
}
