using System.Collections.Generic;
using System;
using System.Drawing;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using System.Linq;
using AR;
using vmsnet;

namespace vmsnet
{
    public partial class Frm_Alam
    {
        public Frm_Alam()
        {
            InitializeComponent();
            this.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == Keys.Escape) this.Close();
            };
        }


        DataTable dt;
        public void Frm_Alam_Load(object sender, System.EventArgs e)
        {
            dt = this.DataSet1.Tables[0];
            dt.Rows.Clear();
            try
            {
                var Dtg = PUB.DS.GRP.Select("use=1", "idx") as DocumentElement.GRPRow[]; // DBC.GetTable("select * from GRP where USE=1 order by IDX")
                foreach (var drg in Dtg)
                {
                    DataRow dr = dt.NewRow();
                    dr["group"] = drg.TITLE;// ["TITLE"].ToString();
                    dr["type"] = drg.ALAMTYPE;// ["ALAMTYPE"].ToString();
                    dr["high"] = drg.ALAMH;// ["ALAMH"].ToString();
                    dr["low"] = drg.ALAML;// ["ALAML"].ToString();
                    dr["ahigh"] = drg.AUTOH;//"].ToString();
                    dr["alow"] = drg.AUTOL;//"].ToString();
                    dr["nbhh"] = drg.NBHH;//"].ToString();
                    dr["nbh"] = drg.NBH;//"].ToString();
                    dr["nbl"] = drg.NBL;//"].ToString();
                    dr["nbll"] = drg.NBLL;//"].ToString();
                    dr["SET"] = "SET";
                    dr["IDX"] = drg["IDX"];
                    dt.Rows.Add(dr);
                }
                dt.AcceptChanges();
            }
            catch (Exception)
            {
                UTIL.MsgE("알람 목록을 불러올 수 없습니다" + "\r\n" + "이 창을 닫고 다시 클릭하세요");
            }

        }

        public void Button1_Click(System.Object sender, System.EventArgs e)
        {
            this.DataGridView1.EndEdit();
            this.BindingSource1.EndEdit();
            DataTable CDT = dt.GetChanges();
            if (CDT == null)
            {
                //MsgBox("변경된 내용이 없습니다", MsgBoxStyle.Information, "확인")
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
            else
            {
                foreach (DataRow DR in CDT.Rows)
                {
                    string TYPE = DR["type"].ToString();
                    string title = DR["group"].ToString();
                    string HIG = DR["high"].ToString().Trim();
                    string LOW = DR["low"].ToString().Trim();
                    string AHG = DR["ahigh"].ToString().Trim();
                    string ALW = DR["alow"].ToString().Trim();
                    var idx = int.Parse(DR["IDX"].ToString());
                    string NBH = DR["NBH"].ToString().Trim();
                    string NBL = DR["NBL"].ToString().Trim();

                    if (HIG.IsNumeric() == false) HIG = "0";
                    if (LOW.IsNumeric() == false) LOW = "0";
                    if (AHG.IsNumeric() == false) AHG = "0";
                    if (ALW.IsNumeric() == false) ALW = "0";
                    if (NBH.IsNumeric() == false) NBH = "0";
                    if (NBL.IsNumeric() == false) NBL = "0";

                    ////그룹정보업데이트
                    PUBC.UpdateGroup(idx, TYPE, float.Parse(HIG), float.Parse(LOW), float.Parse(AHG), float.Parse(ALW), float.Parse(NBH), float.Parse(NBL));

                    ////설정바꾼다.
                    PUB.RaiseRemoteCommandEvent(rCommand.UpdateAlarmSetting,idx);

                    //각 창별로 window1.grp / window2.grp 의 파일에 그룹상태를 저장 합니다
                    PUB.RaiseRemoteCommandEvent(rCommand.SaveGroupClass);
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        public void DataGridView1_CellContentClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {

        }

        public void DataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        {
            if (this.DataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == "MANUAL")
            {
                this.DataGridView1.Rows[e.RowIndex].Cells[2].Style.BackColor = Color.White;
                this.DataGridView1.Rows[e.RowIndex].Cells[3].Style.BackColor = Color.White;
                this.DataGridView1.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.Gainsboro;
                this.DataGridView1.Rows[e.RowIndex].Cells[5].Style.BackColor = Color.Gainsboro;
            }
            else if (this.DataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == "AUTO")
            {
                this.DataGridView1.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.White;
                this.DataGridView1.Rows[e.RowIndex].Cells[5].Style.BackColor = Color.White;
                this.DataGridView1.Rows[e.RowIndex].Cells[2].Style.BackColor = Color.Gainsboro;
                this.DataGridView1.Rows[e.RowIndex].Cells[3].Style.BackColor = Color.Gainsboro;
            }
            else
            {
                this.DataGridView1.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.Gainsboro;
                this.DataGridView1.Rows[e.RowIndex].Cells[5].Style.BackColor = Color.Gainsboro;
                this.DataGridView1.Rows[e.RowIndex].Cells[2].Style.BackColor = Color.Gainsboro;
                this.DataGridView1.Rows[e.RowIndex].Cells[3].Style.BackColor = Color.Gainsboro;
            }
        }

        private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
    }
}
