using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using SystemX.Product.ALIS.UI.View.InfoList;
using System.Data.SqlClient;
using SystemX.Product.ALIS.Interface;
using static SystemX.Product.ALIS.UI.View.ViewCfg;
using static SystemX.Net.Platform.Common.Util.LogMessage;
using DevExpress.XtraGrid.Columns;
using static SystemX.Product.ALIS.UI.Commons;
using System.Diagnostics;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace SystemX.Product.ALIS.UI.View.InfoList
{
    public partial class UcInfoView : UserControl, IUserControlSubscribe
    {
        /// <summary>
        /// IUserControlSubscribe - SetLoginStateNotice
        /// </summary>
        /// <param name="CurrentAccessLevel"></param>
        /// <param name="bLoginState"></param>
        public void SetLoginStateNotice(LoginAccessLevel CurrentAccessLevel, bool bLoginState)
        {
            bool bSetState = ((CurrentAccessLevel == ThisAccessLevel) && (bLoginState == true)) ? true : false;

            ViewLoginState = bSetState;

            WizardCancelEvent?.Invoke(bSetState, null);

            this.Invoke((MethodInvoker)delegate ()
            {
                if (bSetState)
                {
                    layoutControlItemDataMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutControlItemModelMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                }
                else
                {
                    layoutControlItemDataMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutControlItemModelMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }
            });
        }

        /// <summary>
        /// IUserControlSubscribe - GoToFindTestListFileFocus
        /// </summary>
        /// <param name="nTestListFileNo"></param>
        public void GoToFindTestListFileFocus(int nTestListFileNo)
        {
            ;//
        }

        /// <summary>
        /// IUserControlSubscribe - GoToFindParentFocus
        /// </summary>
        /// <param name="nParentNo"></param>
        public void GoToFindVariantFocus(int nVariantNo)
        {
            ;//
        }

        /// <summary>
        /// IUserControlSubscribe
        /// </summary>
        public void RefreshDataView()
        {
            ViewList();
        }


        private void EventClear(object obj)
        {
            if (WizardCancelEvent != null)
            {
                foreach (Delegate d in WizardCancelEvent?.GetInvocationList())
                {
                    if (d.Target.GetType() == obj.GetType())
                        WizardCancelEvent -= (EventHandler)d;
                }
            }
        }

        public event EventHandler WizardCancelEvent;

        public int AbsoluteIndexRow { get; set; }

        public string AbsoluteUniqueNo { get; set; }

        public string AbsoluteHostName { get; set; }

        public string AbsoluteSubName { get; set; }

        //public int AbsoluteIndexCol { get; set; }

        IDataController ctrlDB;

        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();

        eSelectType SelectOpenType { get; set; }

        string strSelectTable;

        public LoginAccessLevel ThisAccessLevel { get; private set; }

        public bool ViewLoginState { private set; get; }

        private Stopwatch stClickTimer;

        private long lClickTimerValue;

        private int nPopupMenuHitRow;

        public UcInfoView(eSelectType SelType, IDataController ctrlDB, LoginAccessLevel MakeAccessLevel, bool bLoginState = false)
        {
            InitializeComponent();

            ThisAccessLevel = MakeAccessLevel;

            ViewLoginState = bLoginState;

            if (ViewLoginState)
            {
                layoutControlItemModelMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layoutControlItemDataMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }

            // This line of code is generated by Data Source Configuration Wizard
            // Fill a SqlDataSource
            this.ctrlDB = ctrlDB;

            SelectOpenType = SelType;

            layoutControlItem4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlItem3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlItem5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlItem6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;

            switch (SelectOpenType)
            {
                case eSelectType.HostList:
                    layoutControlItem4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    strSelectTable = "STAT_Host";
                    break;
                case eSelectType.UserList:
                    layoutControlItem3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    strSelectTable = "STAT_User";
                    break;
                case eSelectType.GroupList:
                    layoutControlItem5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    strSelectTable = "PROD_Group";
                    break;
                case eSelectType.TestCodeList:
                    layoutControlItem6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    strSelectTable = "STAT_TestCode";
                    break;
            }

            ViewList();

            AbsoluteIndexRow = -1;
            //AbsoluteIndexCol = -1;

            AbsoluteUniqueNo = "";

            simpleButtonRegister1.Text = "Register " + SelectOpenType.ToString() + "(Ctrl+N)";

            stClickTimer = new Stopwatch();
            stClickTimer.Start();
        }

        public bool CreateInfo(eSelectType SelType, long lChkTime = long.MaxValue)
        {
            bool bCreateInfoResult = true;

            try
            {
                switch (SelType)
                {
                    case eSelectType.HostList:
                        SelHostInfo.HostID = gridHistView.GetFocusedRowCellValue("HostID").ToString();
                        SelHostInfo.Section = gridHistView.GetFocusedRowCellValue("Section").ToString();
                        SelHostInfo.IP = gridHistView.GetFocusedRowCellValue("IP").ToString();
                        SelHostInfo.TestCode = gridHistView.GetFocusedRowCellValue("TestCode").ToString();
                        SelHostInfo.Comment = gridHistView.GetFocusedRowCellValue("Comment").ToString();

                        AbsoluteHostName = SelHostInfo.HostID;
                        AbsoluteSubName = SelHostInfo.Section;
                        break;
                    case eSelectType.UserList:
                        SelUserInfo.UserID = gridHistView.GetFocusedRowCellValue("UserID").ToString();
                        SelUserInfo.Name = gridHistView.GetFocusedRowCellValue("Name").ToString();
                        //SelUserInfo.Password = gridHistView.GetFocusedRowCellValue("Password").ToString();
                        SelUserInfo.Dept = gridHistView.GetFocusedRowCellValue("Dept").ToString();
                        SelUserInfo.Email = gridHistView.GetFocusedRowCellValue("Email").ToString();
                        SelUserInfo.Comment = gridHistView.GetFocusedRowCellValue("Comment").ToString();

                        AbsoluteHostName = SelUserInfo.UserID;
                        AbsoluteSubName = SelUserInfo.Name;
                        break;
                    case eSelectType.GroupList:
                        SelGroupInfo.GroupName = gridHistView.GetFocusedRowCellValue("GroupName").ToString();
                        SelGroupInfo.ModelName = gridHistView.GetFocusedRowCellValue("ModelName").ToString();
                        SelGroupInfo.Comment = gridHistView.GetFocusedRowCellValue("Comment").ToString();

                        AbsoluteHostName = SelGroupInfo.GroupName;
                        AbsoluteSubName = SelGroupInfo.ModelName;
                        break;
                    case eSelectType.TestCodeList:            
                        SelTestCodeInfo.TestCode = gridHistView.GetFocusedRowCellValue("TestCode").ToString();
                        SelTestCodeInfo.Gate1 = gridHistView.GetFocusedRowCellValue("Gate1").ToString();
                        SelTestCodeInfo.Gate2 = gridHistView.GetFocusedRowCellValue("Gate2").ToString();
                        SelTestCodeInfo.Comment = gridHistView.GetFocusedRowCellValue("Comment").ToString();

                        AbsoluteHostName = SelTestCodeInfo.TestCode;
                        AbsoluteSubName = SelTestCodeInfo.Gate1 + "/" + SelTestCodeInfo.Gate2;
                        break;
                }
            }
            catch { bCreateInfoResult = false; }
            finally
            {
                if (bCreateInfoResult)
                {
                    if (lChkTime <= 200)
                    {
                        ModifyItem();
                    }
                }
            }

            return bCreateInfoResult;
        }

        private void ViewList(string strSetTableName = "")
        {
            eDbTableList selTable = eDbTableList.NONE;

            string vTblName = string.Empty;

            if (strSetTableName.Length > 0)
                vTblName = strSetTableName;
            else
                vTblName = strSelectTable;

            if (Enum.TryParse<eDbTableList>(vTblName, out selTable))
            {
                ViewgridControl.BeginUpdate();

                ViewgridControl.DataSource = null;

                gridHistView.RefreshData();

                ViewgridControl.DataSource = ctrlDB.GetTable(selTable);
                //ViewgridControl.DataMember = vTblName;
                ViewgridControl.Views[0].PopulateColumns();

                gridHistView.BestFitColumns();

                ViewgridControl.EndUpdate();
            }

            PasswordItemOff();
        }

        private void PasswordItemOff()
        {
            IEnumerable<bool> getIndexResult = gridHistView.Columns.Select(x => x.FieldName == "Password");

            if (getIndexResult == null)
                return;

            if (getIndexResult.Any())
            {
                if (gridHistView.Columns.Any(x => x.FieldName == "Password"))
                {
                    GridColumn gc = gridHistView.Columns.First(x => x.FieldName == "Password");
                    int igetIdx = gridHistView.Columns.IndexOf(gc);
                    if (gridHistView.Columns[igetIdx].Visible)
                        gridHistView.Columns[igetIdx].Visible = false;
                }
            }
        }

        private void NewItem()
        {
            DialogResult getResult = new DialogResult();
            getResult = DialogResult.None;
            switch (SelectOpenType)
            {
                case eSelectType.HostList:
                    HostInfoEdit CtrlHostinfo = new HostInfoEdit(ctrlDB, HostInfoEdit.eSelectMode.Insert, SelHostInfo);
                    CtrlHostinfo.Location = Cursor.Position;

                    EventClear(CtrlHostinfo);
                    WizardCancelEvent += CtrlHostinfo.WizardCancelEvent;

                    CtrlHostinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlHostinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "INSERT INTO STAT_Host (HostID, Section, IP, TestCode, Comment) VALUES " +
                            "('" + SelHostInfo.HostID + "', '" + SelHostInfo.Section + "', '" + SelHostInfo.IP + "', '" + SelHostInfo.TestCode + "', '" + SelHostInfo.Comment + "');";
                    }
                    break;
                case eSelectType.UserList:
                    UserInfoEdit CtrlUserinfo = new UserInfoEdit(ctrlDB, UserInfoEdit.eSelectMode.Insert, SelUserInfo);
                    CtrlUserinfo.Location = Cursor.Position;

                    EventClear(CtrlUserinfo);
                    WizardCancelEvent += CtrlUserinfo.WizardCancelEvent;

                    CtrlUserinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlUserinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "INSERT INTO STAT_User (UserID, Name, Password, Dept, Email, Comment) VALUES " +
                            "('" + SelUserInfo.UserID + "', '" + SelUserInfo.Name + "', '" + SelUserInfo.Password + "', '" + SelUserInfo.Dept + "', '" + SelUserInfo.Email + "', '" + SelUserInfo.Comment + "');";
                    }
                    break;
                case eSelectType.GroupList:
                    GroupInfoEdit CtrlGroupinfo = new GroupInfoEdit(ctrlDB, GroupInfoEdit.eSelectMode.Insert, SelGroupInfo);
                    CtrlGroupinfo.Location = Cursor.Position;

                    EventClear(CtrlGroupinfo);
                    WizardCancelEvent += CtrlGroupinfo.WizardCancelEvent;

                    CtrlGroupinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlGroupinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "INSERT INTO PROD_Group (GroupName, ModelName, Comment) VALUES" +
                            "('" + SelGroupInfo.GroupName + "', '" + SelGroupInfo.ModelName + "', '" + SelGroupInfo.Comment + "');";
                    }
                    break;
                case eSelectType.TestCodeList:
                    TestCodeInfoEdit CtrlTestCodeinfo = new TestCodeInfoEdit(ctrlDB, TestCodeInfoEdit.eSelectMode.Insert, SelTestCodeInfo);
                    CtrlTestCodeinfo.Location = Cursor.Position;

                    EventClear(CtrlTestCodeinfo);
                    WizardCancelEvent += CtrlTestCodeinfo.WizardCancelEvent;

                    CtrlTestCodeinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlTestCodeinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "INSERT INTO STAT_TestCode (TestCode, Gate1, Gate2, Comment) VALUES" +
                            "('" + SelTestCodeInfo.TestCode + "', '" + SelTestCodeInfo.Gate1 + "', '" + SelTestCodeInfo.Gate2 + "', '" + SelTestCodeInfo.Comment + "');";
                    }
                    break;
                default:
                    break;
            }

            try
            {
                if (getResult == DialogResult.OK)
                {
                    int EffectRows = ctrlDB.GetConnSqlCmd().ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" " + SelectOpenType.ToString() + " Insert Error. " + ex.Message, ConsoleColor.Red, LogMessageLevel.FATAL);
            }

            ViewList();
        }

        private void simpleButtonRegister1_Click(object sender, EventArgs e)
        {
            NewItem();
        }

        private void ModifyItem()
        {
            if (ViewLoginState == false)
                return;

            if (AbsoluteIndexRow < 0 || AbsoluteUniqueNo.Length < 0)
            {
                MessageBox.Show("Click the [No] column of the row you want.", strSelectTable, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            DialogResult getResult = new DialogResult();
            getResult = DialogResult.None;
            switch (SelectOpenType)
            {
                case eSelectType.HostList:
                    HostInfoEdit CtrlHostinfo = new HostInfoEdit(ctrlDB, HostInfoEdit.eSelectMode.Modify, SelHostInfo);
                    CtrlHostinfo.Location = Cursor.Position;

                    EventClear(CtrlHostinfo);
                    WizardCancelEvent += CtrlHostinfo.WizardCancelEvent;

                    CtrlHostinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlHostinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "UPDATE STAT_Host SET HostID = '" + SelHostInfo.HostID +
                            "', Section= '" + SelHostInfo.Section +
                            "', IP = '" + SelHostInfo.IP +
                            "', TestCode = '" + SelHostInfo.TestCode +
                            "', Comment = '" + SelHostInfo.Comment +
                            "' WHERE No = " + AbsoluteUniqueNo + ";";
                    }
                    break;
                case eSelectType.UserList:
                    UserInfoEdit CtrlUserinfo = new UserInfoEdit(ctrlDB, UserInfoEdit.eSelectMode.Modify, SelUserInfo);
                    CtrlUserinfo.Location = Cursor.Position;

                    EventClear(CtrlUserinfo);
                    WizardCancelEvent += CtrlUserinfo.WizardCancelEvent;

                    CtrlUserinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlUserinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "UPDATE STAT_User SET UserID = '" + SelUserInfo.UserID + "'," +
                        "Name = '" + SelUserInfo.Name + "'," +
                        "Password = '" + SelUserInfo.Password + "'," +
                        "Dept = '" + SelUserInfo.Dept + "'," +
                        "Email = '" + SelUserInfo.Email + "'," +
                        "Comment = '" + SelUserInfo.Comment + "'" +
                        "WHERE No = " + AbsoluteUniqueNo + ";";
                    }
                    break;
                case eSelectType.GroupList:
                    GroupInfoEdit CtrlGroupinfo = new GroupInfoEdit(ctrlDB, GroupInfoEdit.eSelectMode.Modify, SelGroupInfo);
                    CtrlGroupinfo.Location = Cursor.Position;

                    EventClear(CtrlGroupinfo);
                    WizardCancelEvent += CtrlGroupinfo.WizardCancelEvent;

                    CtrlGroupinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlGroupinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "UPDATE PROD_Group SET GroupName = '" + SelGroupInfo.GroupName + "'," +
                        "ModelName = '" + SelGroupInfo.ModelName + "'," +
                        "Comment = '" + SelGroupInfo.Comment + "'" +
                        "WHERE No = " + AbsoluteUniqueNo + ";";
                    }
                    break;
                case eSelectType.TestCodeList:
                    TestCodeInfoEdit CtrlTestCodeinfo = new TestCodeInfoEdit(ctrlDB, TestCodeInfoEdit.eSelectMode.Modify, SelTestCodeInfo);
                    CtrlTestCodeinfo.Location = Cursor.Position;

                    EventClear(CtrlTestCodeinfo);
                    WizardCancelEvent += CtrlTestCodeinfo.WizardCancelEvent;

                    CtrlTestCodeinfo.StartPosition = FormStartPosition.CenterParent;

                    getResult = CtrlTestCodeinfo.ShowDialog();
                    if (getResult == DialogResult.OK)
                    {
                        ctrlDB.GetConnSqlCmd().CommandText = "UPDATE STAT_TestCode SET TestCode = '" + SelTestCodeInfo.TestCode + "'," +
                        "Gate1 = '" + SelTestCodeInfo.Gate1 + "'," +
                        "Gate2 = '" + SelTestCodeInfo.Gate2 + "'," +
                        "Comment = '" + SelTestCodeInfo.Comment + "'" +
                        "WHERE No = " + AbsoluteUniqueNo + ";";
                    }
                    break;
                default:
                    break;
            }

            try
            {
                if (getResult == DialogResult.OK)
                {
                    int EffectRows = ctrlDB.GetConnSqlCmd().ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" " + SelectOpenType.ToString() + " Update Error. " + ex.Message, ConsoleColor.Red, LogMessageLevel.FATAL);
            }

            ViewList();
        }

        private void simpleButtonModify1_Click(object sender, EventArgs e)
        {
            ModifyItem();
        }

        private void ClearListSelect()
        {
            simpleLabelItemSelectRow.Text = "-";

            AbsoluteIndexRow = -1;

            AbsoluteUniqueNo = "";
        }

        private void GridViewSelectedProc(object objHandle, int nRow)
        {
            lClickTimerValue = stClickTimer.ElapsedMilliseconds;

            AbsoluteIndexRow = nRow;

            simpleLabelItemSelectRow.Text = "Select Row : " + (nRow + 1).ToString();

            if (objHandle != null)
            {
                try
                {
                    string strNumber = gridHistView.GetFocusedRowCellValue("No").ToString();

                    AbsoluteUniqueNo = strNumber;

                    if (CreateInfo(SelectOpenType, lClickTimerValue))
                        stClickTimer.Restart();
                }
                catch
                {
                    ClearListSelect();
                }
            }
            else
            {
                ClearListSelect();
            }
        }

        private void gridHistView_RowClick(object sender, RowClickEventArgs e)
        {
            object objHandle = null;

            try
            {
                objHandle = gridHistView.GetFocusedRowCellValue("No");
            }
            catch
            {
                objHandle = null;
            }
            finally
            {
                GridViewSelectedProc(objHandle, e.RowHandle);
            }
        }

        private void gridHistView_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            /*if (e.Column.FieldName.Equals("No"))
            {
                simpleLabelItemSelectRow.Text = "Select Row : " + (e.RowHandle + 1).ToString();
                simpleLabelItemSelectCol.Text = "Select Col : " + (e.Column.AbsoluteIndex + 1).ToString();

                AbsoluteIndexRow = e.RowHandle;
                AbsoluteIndexCol = e.Column.AbsoluteIndex;

                string strNumber = gridHistView.GetFocusedRowCellValue("No").ToString();

                AbsoluteUniqueNo = strNumber;

                CreateInfo(SelectOpenType);
            }
            else
            {
                simpleLabelItemSelectRow.Text = "-";
                simpleLabelItemSelectCol.Text = "-";

                AbsoluteIndexRow = -1;
                AbsoluteIndexCol = -1;

                AbsoluteUniqueNo = "";
            }*/
        }

        private void simpleButtonViewList_Click(object sender, EventArgs e)
        {
            ViewList();
        }

        private void simpleButtonDataMod_Click(object sender, EventArgs e)
        {
            if (layoutControlItemModelMod.Visibility == DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                layoutControlItemModelMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
                layoutControlItemModelMod.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
        }

        private void UcInfoView_Enter(object sender, EventArgs e)
        {
            PasswordItemOff();

            gridHistView.Focus();
        }

        private bool LoginStateCheck()
        {
            return ViewLoginState;
        }

        private bool CheckRowSelectInfo()
        {
            object objHandle = null;

            bool bCheckResult = true;

            try
            {
                objHandle = gridHistView.GetFocusedRowCellValue("No");
            }
            catch
            {
                objHandle = null;

                bCheckResult = false;
            }
            finally
            {
                ;//
            }

            return bCheckResult;
        }

        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            if (LoginStateCheck() == false)
                e.Cancel = true;

            if (AbsoluteIndexRow < 0 || AbsoluteUniqueNo.Length < 0)
                e.Cancel = true;

            if (CheckRowSelectInfo())
            {
                try
                {
                    string strNumber = gridHistView.GetFocusedRowCellValue("No").ToString();

                    if (AbsoluteUniqueNo != strNumber)
                        e.Cancel = true;

                    GridHitInfo hInfo = gridHistView.CalcHitInfo(ViewgridControl.PointToClient(Cursor.Position));

                    if (hInfo.RowInfo != null)
                        nPopupMenuHitRow = hInfo.RowInfo.RowHandle;
                    else
                        e.Cancel = true;
                }
                catch
                {
                    e.Cancel = true;
                }
            }
        }

        private void DeleteItem()
        {
            string strTitle = SelectOpenType.ToString();

            if (AbsoluteIndexRow >= 0 && AbsoluteUniqueNo.Length > 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the " + strTitle + " [" + AbsoluteHostName + "] [" + AbsoluteSubName + "] information ?", strTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    switch (SelectOpenType)
                    {
                        case eSelectType.HostList:
                            ctrlDB.GetConnSqlCmd().CommandText = "DELETE STAT_Host WHERE No = '" + AbsoluteUniqueNo + "';";
                            break;
                        case eSelectType.UserList:
                            ctrlDB.GetConnSqlCmd().CommandText = "DELETE STAT_User WHERE No = '" + AbsoluteUniqueNo + "';";
                            break;
                        case eSelectType.GroupList:
                            ctrlDB.GetConnSqlCmd().CommandText = "DELETE PROD_Group WHERE No = '" + AbsoluteUniqueNo + "';";
                            break;
                        case eSelectType.TestCodeList:
                            ctrlDB.GetConnSqlCmd().CommandText = "DELETE STAT_TestCode WHERE No = '" + AbsoluteUniqueNo + "';";
                            break;
                    }

                    try
                    {
                        int EffectRows = ctrlDB.GetConnSqlCmd().ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" " + SelectOpenType.ToString() + " Delete Item Error. " + ex.Message, ConsoleColor.Red, LogMessageLevel.FATAL);
                    }

                    ViewList();
                }
            }
        }

        private void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripItem tsp = e.ClickedItem;

            int nTag = Convert.ToInt32(tsp.Tag);

            switch (nTag)
            {
                case 0:
                    NewItem();
                    break;
                case 1:
                    ModifyItem();
                    break;
                case 2:
                    DeleteItem();
                    break;
            }
        }

        private void timerUI_Tick(object sender, EventArgs e)
        {
            if (ViewgridControl.ClientRectangle.Contains(ViewgridControl.PointToClient(Control.MousePosition)))
            {
                GridHitInfo hInfo = gridHistView.CalcHitInfo(ViewgridControl.PointToClient(Cursor.Position));

                if (hInfo.RowInfo != null)
                {
                    if (contextMenuStrip.ClientRectangle.Contains(contextMenuStrip.PointToClient(Control.MousePosition)))
                        return;

                    if (nPopupMenuHitRow != hInfo.RowInfo.RowHandle)
                    {
                        if (contextMenuStrip.Visible)
                            contextMenuStrip.Close();
                    }
                }
            }
            else
            {
                if (contextMenuStrip.Visible)
                    contextMenuStrip.Close();
            }
        }

        private void gridHistView_KeyPress(object sender, KeyPressEventArgs e)
        {
            byte ucGetChar = Convert.ToByte(e.KeyChar);

            switch (ucGetChar)
            {
                case 13:
                    if (gridHistView.SelectedRowsCount > 0)
                    {
                        object objHandle = null;

                        try
                        {
                            objHandle = gridHistView.GetFocusedRowCellValue("No");
                        }
                        catch
                        {
                            objHandle = null;
                        }
                        finally
                        {
                            int[] nRows = gridHistView.GetSelectedRows();

                            GridViewSelectedProc(objHandle, nRows[0]);
                        }
                    }
                    break;
                case 27:
                    simpleButtonRegister1.Focus();
                    break;
            }
        }
    }
}
