using Chloe;

using DNSD_DB;
using DNSD_DB.Entity;

using NewLife.Data;
using NewLife.Reflection;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NDSD_TouchSocket;
using NewLife;
using TouchSocket.Core;

namespace NDSD_Screwdriver
{
    public partial class FrmSetting : Form
    {
        

        public FrmSetting()
        {
            InitializeComponent();
            Init();
        }
        
        public void Init()
        {
            dataGridView1.AutoGenerateColumns = false;
            var ctx = SqlLiteTool.GetDb(AppTool.GetDb());
            var list = ctx.Query<RFIDConfigEntity>().Where(x => x.IsEnable == true).ToList();

            //  dataGridView1.DataSource = list;//DataGridView的行可以添加删除（只有允许添加行、删除行）
            dataGridView1.DataSource = new BindingList<RFIDConfigEntity>(list);//DataGridView的行可以添加删除（只有允许添加行、删除行）
            dataGridView1.AllowUserToAddRows = true;

            dataGridView1.AllowUserToDeleteRows = true;
        }


        private DataTable GetTable()
        {

            DataTable table = new DataTable();
            table.Columns.Add("DisplayMember", typeof(string));
            table.Columns.Add("ValueMember", typeof(string));

            var dr = table.NewRow();
            dr[0] = "绿色";
            dr[1] = "1";

            table.Rows.Add(dr);
            dr = table.NewRow();
            dr[0] = "黄色";
            dr[1] = "2";
            table.Rows.Add(dr);

            dr = table.NewRow();
            dr[0] = "红色";
            dr[1] = "3";
            table.Rows.Add(dr);

            return table;

        }

        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells[0].Value = "0";
            e.Row.Cells["D1"].Value = "0";
            e.Row.Cells["D2"].Value = "0";
            e.Row.Cells["D3"].Value = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            List<RFIDConfigEntity> list = new List<RFIDConfigEntity>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                // 检查行的类型，确保不是新行或者标题行
                if (!row.IsNewRow && row.Cells[0].Value != null)
                {
                    RFIDConfigEntity RFIDConfigEntity = new RFIDConfigEntity();

                    // 获取当前行的值，并添加到列表中
                    string rfid = row.Cells[0].Value.ToString();
                    string green = row.Cells[1].Value.ToString();
                    string yellow = row.Cells[2].Value.ToString();
                    string red = row.Cells[3].Value.ToString();

                    if (string.IsNullOrEmpty(rfid))
                    {
                        MessageBox.Show("请输入rfid标签");
                        return;
                    }

                    RFIDConfigEntity.IsEnable = true;
                    RFIDConfigEntity.CreateDateTime = DateTime.Now;
                    RFIDConfigEntity.RfidNo = rfid;

                    if (string.IsNullOrEmpty(green))
                    {
                        MessageBox.Show("请输入绿灯");

                        return;
                    }
                    else
                    {
                        if (!IsInRange(green.ToInt()))
                        {
                            MessageBox.Show("绿灯不在有效的范围");

                            return;
                        }
                    }

                    RFIDConfigEntity.Green = green;



                    if (string.IsNullOrEmpty(yellow))
                    {
                        MessageBox.Show("请输入黄灯");

                        return;
                    }
                    else
                    {
                        if (!IsInRange(yellow.ToInt()))
                        {
                            MessageBox.Show("绿灯不在有效的范围");

                            return;
                        }
                    }

                    RFIDConfigEntity.Yellow = yellow;

                    if (string.IsNullOrEmpty(red))
                    {
                        MessageBox.Show("请输入红灯");

                        return;
                    }
                    else
                    {
                        if (!IsInRange(red.ToInt()))
                        {
                            MessageBox.Show("绿灯不在有效的范围");

                            return;
                        }
                    }

                    RFIDConfigEntity.Red = red;
                    list.Add(RFIDConfigEntity);
                }
            }


            if (list.Count > 0)
            {
                var ctx = SqlLiteTool.GetDb(AppTool.GetDb());
                ctx.Update<RFIDConfigEntity>(a => a.IsEnable == true, a => new RFIDConfigEntity()
                {
                    IsEnable = false
                });

                ctx.InsertRange(list);
            }

            MessageBox.Show("保存成功");

        }


        bool IsInRange(int x)
        {
            int a = 1;
            int b = 16;
            return x >= a && x <= b;
        }

        private void SetServerIPButton_Click(object sender, EventArgs e)
        {
            MemorySetting.ServerIP = ServerIPTextbox.Text;
            MemorySetting.ServerPort = ServerPortTextbox.Text;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void SetAlarmTimeValueButton_Click(object sender, EventArgs e)
        {
            MemorySetting.AlarmTimeValue = Convert.ToInt32(AlarmTimeValueTextBox.Text);
        }
    }

}
