using MesWork;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Configuration.ConfigurationManager;

namespace LogRecord
{
    public partial class LogRecord : Form
    {
        public LogRecord()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        public LogRecord(int _Width,int _Height)
        {
            InitializeComponent();
            this.Width = _Width;
            this.Height = _Height;
        }

        private void LogRecord_Load(object sender, EventArgs e)
        {
            Refresh_Log();
        }

        private void Refresh_Log()
        {
            var dt = Select_Log();
            dgv_Log.DataSource = dt;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                var 类型 = (Type_Log)Enum.Parse(typeof(Type_Log),dt.Rows[i]["类型"].ToString());
                switch (类型)
                {
                    case Type_Log.Alarm:
                        RowsColor(i, Color.Red, dgv_Log);
                        break;
                    case Type_Log.Login:
                        RowsColor(i, Color.Green, dgv_Log);
                        break;
                    case Type_Log.Tip:
                        RowsColor(i, Color.Yellow, dgv_Log);
                        break;
                    default:
                        RowsColor(i, Color.Gray, dgv_Log);
                        break;
                }
            }
            dgv_Log.ClearSelection();
            //dgv_Log.CurrentCell = null;
            //dgv_Log.Rows[0].Selected = false;
        }
        private void RowsColor(int row, Color color, DataGridView dgv)
        {
            var dt = (System.Data.DataTable)dgv.DataSource;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dgv.Rows[row].Cells[i].Style.BackColor = color;
            }
        }

        public static bool Insert_Log(Type_Log LogType, string Log_Content)
        {
            /*
SELECT [IDG],[Log_Station],[Log_Type],[Log_Time],[Log_Content] FROM [记录_日志_运行日志]
             */
            bool success = false;
            var sql =
$" INSERT INTO [记录_日志_运行日志]([Log_Station],[Log_Type],[Log_Time],[Log_Content])" +
$" VALUES('{MesWorkForm.StationName}','{LogType.ToString()}',getdate(),'{Log_Content}')";
            success = DataLinkMesWork.SQLCommon.ExecuteSql(sql, MesWorkForm.ConnectionString, out string err2);

            return success;

        }
        public static DataTable Select_Log()
        {
            /*
SELECT [IDG],[Log_Station],[Log_Type],[Log_Time],[Log_Content] FROM [记录_日志_运行日志]
             */
            var sql =
$" (SELECT " +
$" row_number() over(order by [Log_Time]) AS 序号" +
$" ,[Log_Station] AS 工位号" +
$" ,[Log_Type] AS 类型" +
$" ,[Log_Time] AS 时间" +
$" ,[Log_Content] AS 内容" +

$" FROM [记录_日志_运行日志]" +
$" WHERE [Log_Station] = '{MesWorkForm.StationName}')" +
$" ORDER BY 序号 DESC";
            DataLinkMesWork.SQLCommon.ExecuteDataTable(sql, MesWorkForm.ConnectionString,out DataTable dt, out string err2);
            return dt;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Insert_Log(Type_Log.Alarm, "dfghjkjfdfghjk法国红酒看官方一股好久哦i反对牛v军方肯定没得查看你的孙女i的女哦i的妇女哦看v哦对妇女的发");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Insert_Log(Type_Log.Login, "废物");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Insert_Log(Type_Log.Tip, "滚");
        }

        public enum Type_Log
        {
            /// <summary>
            /// 报警
            /// </summary>
            Alarm = 1,
            /// <summary>
            /// 登录
            /// </summary>
            Login = 2,
            /// <summary>
            /// 提示
            /// </summary>
            Tip = 3
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            Refresh_Log();
        }
    }
}
