using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ComboBox;

using SystemX.Product.ALIS.Interface;

using CpApplication;
using CpApplication.Manager;
using CpCommon;
using CpTesterPlatform.CpLogUtil;
using static PsCommon.PsCommon;

using PsKGaudi;
using PsKGaudi.Parser.PsCCSArea;
using PsKGaudi.Parser.PsCCSSTDFn;
using PsKGaudi.Parser.MacroModuleSkel;
using PsKGaudi.Parser.PsCCSSTDFn.Parameters;
using static PsKGaudi.Parser.PsCCS;

using static CpCommon.ExceptionHandler;
using SystemX.Common;
using static SystemX.Product.ALIS.UI.View.InfoList.UcTestListView;
using SystemX.Common.Archive;
using SystemX.Product.PTS.UI.View.Test_List.Infos;
using DevExpress.XtraWizard;

namespace SystemX.Product.ALIS.UI.View.InfoList
{
    public partial class TestListVariantCopyWizard : Form
    {
        public eSelectMode CurrentWizardMode { internal set; get; }

        private CVariantInformation CopyTestListVariant;

        private List<CVariantInformation> lstCopyInsertSetTestListVariant;

        private CVariantInformation MakeInformation;

        private DataTable dtCurrentDataTable;

        //private CpTestlistLoad loadTestListProc;

        //private bool bModifyFirstLoad;

        private int nSelectedTestListFileNo;

        private string[] strSelectedTestListFileVariantList;

        public int GetInsertSize()
        {
            return lstCopyInsertSetTestListVariant.Count;
        }

        public IEnumerable<CVariantInformation> GetInsertList()
        {
            foreach (CVariantInformation info in lstCopyInsertSetTestListVariant)
            {
                yield return info;
            }
        }

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

        private string strTitle = "PROD TestList-Variant [Copy And Paste]";
        private string strWizardControl = "PROD TestList-Variant";
        private string strWelcomeWizard = "Welcome to the PROD TestList-Variant [Copy And Paste] wizard!";
        private string strWizardPage = "PROD TestList Information";

        private int nReadyViewSelectRow { set; get; } = -1;

        private bool GroupCheckResult { set; get; } = false;

        private int nNowSelectGroupNo { set; get; } = -1;

        public void WizardCancelEvent(object sender, EventArgs e)
        {
            if (((bool)sender) == false)
                this.DialogResult = DialogResult.Cancel;
        }

        private void RefreshReadyGridView()
        {
            dataGridViewReady.DataSource = dtCurrentDataTable;

            dataGridViewReady.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewReady.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            dataGridViewReady.Refresh();

            dataGridViewReady.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

            this.ActiveControl = textBoxItem1;

            textBoxItem1.Focus();

            if (string.IsNullOrEmpty(textBoxItem1.Text) == false)
            {
                textBoxItem1.SelectionStart = textBoxItem1.Text.Length;
                textBoxItem1.SelectionLength = 0;
            }
        }

        private void RefreshResultGridView()
        {
            dataGridViewResult.DataSource = dtCurrentDataTable;

            dataGridViewResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            dataGridViewResult.Refresh();

            dataGridViewResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
        }

        private void AddResultList(DataRow dr)
        {
            CVariantInformation InformationTemp = new CVariantInformation();

            InformationTemp.ProdNo_P = dr.Field<string>("ProdNo_P");
            InformationTemp.TestType = dr.Field<string>("TestType");
            InformationTemp.Version = dr.Field<string>("Version");
            InformationTemp.ProdCode = dr.Field<string>("ProdCode");
            InformationTemp.FileName = dr.Field<string>("FileName");

            InformationTemp.TestListFileNo = nSelectedTestListFileNo;

            InformationTemp.RegDT = DateTime.Now;
            InformationTemp.RegUser = ctrlDB.GetLoginInfo().UserID;
            InformationTemp.UpdateDT = DateTime.Now;
            InformationTemp.UpdateUser = ctrlDB.GetLoginInfo().UserID;
            InformationTemp.GroupNo = dr.Field<int>("GroupNo");
            InformationTemp.Comment = dr.Field<string>("Comment");
            InformationTemp.Description = dr.Field<string>("Description");

            lstCopyInsertSetTestListVariant.Add(InformationTemp);
        }

        public TestListVariantCopyWizard(IDataController ctrlDB, eSelectMode setMode, CVariantInformation getSelTestListVariantInfo)
        {
            InitializeComponent();

            this.Text = strTitle;

            wizardControl.Text = strWizardControl;
            welcomeWizardPage.Text = strWelcomeWizard;
            wizardPage.Text = strWizardPage;

            CurrentWizardMode = setMode;

            CopyTestListVariant = getSelTestListVariantInfo.Clone() as CVariantInformation;

            lstCopyInsertSetTestListVariant = new List<CVariantInformation>();

            this.ctrlDB = ctrlDB;

            switch (CurrentWizardMode)
            {
                case eSelectMode.Insert:
                    welcomeWizardPage.IntroductionText = "This is the procedure to copy and paste into the " + strWizardControl + " table.";
                    wizardControl.Text = strTitle + " - Information";
                    this.Text = strTitle + " - Information";
                    break;
                case eSelectMode.Modify:
                    break;
            }

            GetGroupList();

            CheckGroupInfo("SELECT * FROM PROD_Group WHERE No = " + CopyTestListVariant.GroupNo + " ORDER BY No ASC;");

            MakeInformation = new CVariantInformation();

            dtCurrentDataTable = MakeDataTable();

            /*DataRow[] setMakeDataRows = new DataRow[1]; //DataRow[CopyTestListVariant.Count];
            dtCurrentDataTable = MakeDataTable();

            for (int i = 0; i < 1; *//*CopyTestListVariant.Count;*//* i++)
            {
                setMakeDataRows[i] = dtCurrentDataTable.NewRow();
                setMakeDataRows[i]["No"] = i + 1;
                setMakeDataRows[i]["ProdNo_P"] = CopyTestListVariant.ProdNo_P;
                setMakeDataRows[i]["TestType"] = CopyTestListVariant.TestType;
                setMakeDataRows[i]["Version"] = CopyTestListVariant.Version;
                setMakeDataRows[i]["ProdCode"] = CopyTestListVariant.ProdCode;
                setMakeDataRows[i]["FileName"] = CopyTestListVariant.FileName;
                setMakeDataRows[i]["RegDT"] = DateTime.Now;
                setMakeDataRows[i]["RegUser"] = ctrlDB.GetLoginInfo().UserID;
                setMakeDataRows[i]["UpdateDT"] = DateTime.Now;
                setMakeDataRows[i]["UpdateUser"] = ctrlDB.GetLoginInfo().UserID;
                setMakeDataRows[i]["GroupNo"] = CopyTestListVariant.GroupNo;
                setMakeDataRows[i]["GroupName"] = textBoxDispGroupName.Text;
                setMakeDataRows[i]["ModelName"] = textBoxDispModelName.Text;
                setMakeDataRows[i]["Comment"] = CopyTestListVariant.Comment;
                setMakeDataRows[i]["Description"] = CopyTestListVariant.Description;

                dtCurrentDataTable.Rows.Add(setMakeDataRows[i]);
            }*/

            textBoxModify1.Text = CopyTestListVariant.ProdNo_P;
            textBoxModify2.Text = CopyTestListVariant.TestType;
            textBoxModify3.Text = CopyTestListVariant.Version;
            textBoxModify4.Text = CopyTestListVariant.ProdCode;
            textBoxModify5.Text = CopyTestListVariant.FileName;
            textBoxModify6.Text = textBoxDispGroupName.Text;
            textBoxModify7.Text = textBoxDispModelName.Text;
            textBoxModify8.Text = CopyTestListVariant.Comment;
            textBoxModify9.Text = CopyTestListVariant.Description;

            //bModifyFirstLoad = false;

            string strGetQueryText = $"SELECT No FROM [PROD_Variant] WHERE ProdNo_P = '" + CopyTestListVariant.ProdNo_P + "' AND " +
                                     $"TestListFileNo = " + CopyTestListVariant.TestListFileNo + " AND " +
                                     $"GroupNo = " + CopyTestListVariant.GroupNo + ";";

            DataTable dtResult = ctrlDB.GetTable(strGetQueryText);

            if (XCommons.isHasRow(dtResult))
            {
                strGetQueryText = "SELECT * FROM [HIST_TestListFileVariantList] AS X WHERE TestListFileNo = " + CopyTestListVariant.TestListFileNo + ";";

                DataTable dtVariantList = ctrlDB.GetTable(strGetQueryText);

                if (XCommons.isHasRow(dtVariantList))
                {
                    nSelectedTestListFileNo = CopyTestListVariant.TestListFileNo;

                    comboBoxVariant.BeginUpdate();
                    comboBoxVariant.Items.Clear();
                    comboBoxVariant.Refresh();
                    comboBoxVariant.Items.Add("Select Variant ...");

                    strSelectedTestListFileVariantList = dtVariantList.Rows[0]["VariantList"].ToString().Split(';');
                    foreach (string strVariant in strSelectedTestListFileVariantList)
                        comboBoxVariant.Items.Add(strVariant);

                    comboBoxVariant.SelectedIndex = 0;
                    comboBoxVariant.EndUpdate();

                    labelDispLoadResult.Text = "Test-list data verification completed.";
                    labelDispLoadResult.BackColor = Color.LimeGreen;

                    comboBoxVariant.Visible = true;
                }
            }
        }

        private void TestListCopyWizard_Load(object sender, EventArgs e)
        {
            RefreshReadyGridView();

            textBoxItem6_Picker.Value = DateTime.Now;

            textBoxItem8_Picker.Value = DateTime.Now;

            textBoxItem1.Text = CopyTestListVariant.ProdNo_P;
            textBoxItem2.Text = CopyTestListVariant.TestType;
            textBoxItem3.Text = CopyTestListVariant.Version;
            textBoxItem4.Text = CopyTestListVariant.ProdCode;
            textBoxItem5.Text = CopyTestListVariant.FileName;

            textBoxItem11.Text = CopyTestListVariant.Comment;
            textBoxItem12.Text = CopyTestListVariant.Description;

            textBoxItem7.Text = ctrlDB.GetLoginInfo().UserID;
            textBoxItem9.Text = ctrlDB.GetLoginInfo().UserID;
        }

        /*
        public void CompleteTestListLoad(object sender, EventArgs e)
        {
            bool bLoadResult = (bool)sender;

            if (bLoadResult)
            {
                Invoke((MethodInvoker)delegate ()
                {
                    textBoxItem2.Text = loadTestListProc.psGaudiInfo.psTestListInfo.Type;
                    textBoxItem3.Text = loadTestListProc.psGaudiInfo.psFileInfo.FileVersion;
                    textBoxItem4.Text = loadTestListProc.psGaudiInfo.psFileInfo.FileType;
                    textBoxItem5.Text = CopyTestList.FileName; //loadTestListProc.psGaudiInfo.psFileInfo.FileName;

                    comboBoxVariant.BeginUpdate();
                    comboBoxVariant.Items.Clear();
                    comboBoxVariant.Refresh();
                    comboBoxVariant.Items.Add("Select Variant ...");

                    foreach (KeyValuePair<string, string> kvp in loadTestListProc.psGaudiInfo.psVariantTable.GetStrVariantTable)
                        comboBoxVariant.Items.Add(kvp.Key);

                    comboBoxVariant.SelectedIndex = 0;
                    comboBoxVariant.EndUpdate();

                    labelDispLoadResult.Text = "Test-list data verification completed.";
                    labelDispLoadResult.BackColor = Color.LimeGreen;

                    textBoxItem11.Text = CopyTestList.Comment;
                    textBoxItem12.Text = loadTestListProc.GetTestListDescription();

                    comboBoxVariant.Visible = true;
                });
            }
            else
            {
                Invoke((MethodInvoker)delegate ()
                {
                    labelDispLoadResult.Text = "Failed to check test-list data.";
                    labelDispLoadResult.BackColor = Color.Red;
                });
            }
        }
        */

        private DataTable MakeDataTable()
        {
            // Create a new DataTable titled 'Names.'
            DataTable logTable = new DataTable("TempTable");

            // Add three column objects to the table.
            DataColumn Column0 = new DataColumn();
            Column0.DataType = System.Type.GetType("System.Int64");
            Column0.ColumnName = "No";
            Column0.AutoIncrement = true;
            Column0.AllowDBNull = false;
            logTable.Columns.Add(Column0);

            DataColumn Column1 = new DataColumn();
            Column1.DataType = System.Type.GetType("System.String");
            Column1.ColumnName = "ProdNo_P";
            Column1.AutoIncrement = false;
            Column1.AllowDBNull = false;
            logTable.Columns.Add(Column1);

            DataColumn Column2 = new DataColumn();
            Column2.DataType = System.Type.GetType("System.String");
            Column2.ColumnName = "TestType";
            Column2.AutoIncrement = false;
            Column2.AllowDBNull = false;
            logTable.Columns.Add(Column2);

            DataColumn Column00 = new DataColumn();
            Column00.DataType = System.Type.GetType("System.String");
            Column00.ColumnName = "Version";
            Column00.AutoIncrement = false;
            Column00.AllowDBNull = false;
            logTable.Columns.Add(Column00);

            DataColumn Column3 = new DataColumn();
            Column3.DataType = System.Type.GetType("System.String");
            Column3.ColumnName = "ProdCode";
            Column3.AutoIncrement = false;
            Column3.AllowDBNull = false;
            logTable.Columns.Add(Column3);

            DataColumn Column01 = new DataColumn();
            Column01.DataType = System.Type.GetType("System.String");
            Column01.ColumnName = "FileName";
            Column01.AutoIncrement = false;
            Column01.AllowDBNull = false;
            logTable.Columns.Add(Column01);

            DataColumn Column02 = new DataColumn();
            Column02.DataType = System.Type.GetType("System.DateTime");
            Column02.ColumnName = "RegDT";
            Column02.AutoIncrement = false;
            Column02.AllowDBNull = true;
            Column02.DefaultValue = DateTime.Now;
            logTable.Columns.Add(Column02);

            DataColumn Column03 = new DataColumn();
            Column03.DataType = System.Type.GetType("System.String");
            Column03.ColumnName = "RegUser";
            Column03.AutoIncrement = false;
            Column03.AllowDBNull = true;
            logTable.Columns.Add(Column03);

            DataColumn Column04 = new DataColumn();
            Column04.DataType = System.Type.GetType("System.DateTime");
            Column04.ColumnName = "UpdateDT";
            Column04.AutoIncrement = false;
            Column04.AllowDBNull = true;
            Column04.DefaultValue = DateTime.Now;
            logTable.Columns.Add(Column04);

            DataColumn Column4 = new DataColumn();
            Column4.DataType = System.Type.GetType("System.String");
            Column4.ColumnName = "UpdateUser";
            Column4.AutoIncrement = false;
            Column4.AllowDBNull = false;
            logTable.Columns.Add(Column4);

            DataColumn Column5 = new DataColumn();
            Column5.DataType = System.Type.GetType("System.Int32");
            Column5.ColumnName = "GroupNo";
            Column5.AutoIncrement = false;
            Column5.AllowDBNull = true;
            logTable.Columns.Add(Column5);

            DataColumn Column6 = new DataColumn();
            Column6.DataType = System.Type.GetType("System.String");
            Column6.ColumnName = "GroupName";
            Column6.AutoIncrement = false;
            Column6.AllowDBNull = true;
            logTable.Columns.Add(Column6);

            DataColumn Column7 = new DataColumn();
            Column7.DataType = System.Type.GetType("System.String");
            Column7.ColumnName = "ModelName";
            Column7.AutoIncrement = false;
            Column7.AllowDBNull = true;
            logTable.Columns.Add(Column7);

            DataColumn Column8 = new DataColumn();
            Column8.DataType = System.Type.GetType("System.String");
            Column8.ColumnName = "Comment";
            Column8.AutoIncrement = false;
            Column8.AllowDBNull = true;
            logTable.Columns.Add(Column8);

            DataColumn Column9 = new DataColumn();
            Column9.DataType = System.Type.GetType("System.String");
            Column9.ColumnName = "Description";
            Column9.AutoIncrement = false;
            Column9.AllowDBNull = true;
            logTable.Columns.Add(Column9);

            // Create an array for DataColumn objects.
            DataColumn[] keys = new DataColumn[1];
            keys[0] = Column0;
            logTable.PrimaryKey = keys;

            // Return the new DataTable.
            return logTable;
        }

        private void wizardControl_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            lock (objInfoControlNextLock)
            {
                if (wizardControl.SelectedPageIndex == 0)
                {
                    /*
                    if (bModifyFirstLoad == false)
                    {
                        labelDispLoadResult.Text = "Loading ...";
                        labelDispLoadResult.BackColor = Color.Yellow;

                        loadTestListProc = new CpTestlistLoad();
                        loadTestListProc.TestListLoadResultEvent += CompleteTestListLoad;
                        loadTestListProc.ExcuteTestListLoad(CopyTestList);
                        
                        bModifyFirstLoad = true;
                    }
                    */
                }
                if (wizardControl.SelectedPageIndex == 1)
                {

                    richTextBoxErr.Clear();

                    bool bCheckInformationResult = true;

                    StringBuilder sb = new StringBuilder();

                    lstCopyInsertSetTestListVariant = new List<CVariantInformation>();
                    lstCopyInsertSetTestListVariant.Clear();

                    string strItemName = string.Empty;

                    try
                    {
                        /*
                        if (loadTestListProc.GetTestListLoadState() == false)
                            throw new Exception("The test list is loading or there is no information on the selected test list.");
                        */

                        int nRowPos = 0;

                        foreach (DataRow dr in dtCurrentDataTable.Rows)
                        {
                            string strGetProdP = dr.Field<string>("ProdNo_P");
                            string strGetTestType = dr.Field<string>("TestType");
                            string strGetFileVersion = dr.Field<string>("Version");
                            string strGetProdCode = dr.Field<string>("ProdCode");
                            string strGetFileName = dr.Field<string>("FileName");

                            bool bFindVariant = false;

                            foreach (string strVariant in strSelectedTestListFileVariantList)
                            {
                                if (strVariant.CompareTo(strGetProdP) == 0)
                                {
                                    bFindVariant = true;

                                    break;
                                }
                            }

                            if (bFindVariant == false)
                            {
                                sb.AppendLine($"<Row : {nRowPos + 1}> P_ProdNo that does not exist in the test-list data(Variant). : {strGetProdP}");

                                bCheckInformationResult = false;
                            }

                            /*
                            string strGetQueryText = "SELECT * FROM [PROD_Variant] WHERE No IN(SELECT No FROM [PROD_Variant] WHERE [ProdNo_P] = '" + strGetProdP + "' " +
                                "AND [TestType] = '" + strGetTestType + "' " +
                                "AND [Version] = '" + strGetFileVersion + "' " +
                                "AND [FileName] = '" + strGetFileName + "' " +
                                ") ORDER BY No ASC;";
                            */

                            //Variant Duplicate Check
                            string strGetQueryText = "SELECT * FROM [PROD_Variant] WHERE No IN(SELECT No FROM [PROD_Variant] WHERE [ProdNo_P] = '" + strGetProdP + "' " +
                                "AND [TestListFileNo] = " + nSelectedTestListFileNo +
                                ") ORDER BY No ASC;";

                            DataTable dtVariantCheck = ctrlDB.GetTable(strGetQueryText);

                            strGetQueryText = "SELECT No, Name, TestType, Version, ProdCode, FileName FROM [STOR_TestListFile] WHERE No = " + nSelectedTestListFileNo + ";";

                            DataTable dtTestListFIleCheck = ctrlDB.GetTable(strGetQueryText);

                            if (XCommons.isHasRow(dtVariantCheck))
                            {
                                bool bExistException = false;

                                string strGetTableProdP = dtVariantCheck.Rows[0]["ProdNo_P"].ToString();
                                string strGetTableTestType = dtTestListFIleCheck.Rows[0]["TestType"].ToString();
                                string strGetTableFileVersion = dtTestListFIleCheck.Rows[0]["Version"].ToString();
                                string strGetTableProdCode = dtTestListFIleCheck.Rows[0]["ProdCode"].ToString();
                                string strGetTableFileName = dtTestListFIleCheck.Rows[0]["FileName"].ToString();

                                if (strGetProdP.CompareTo(strGetTableProdP) == 0 &&
                                   strGetTestType.CompareTo(strGetTableTestType) == 0 &&
                                   strGetFileVersion.CompareTo(strGetTableFileVersion) == 0 &&
                                   strGetFileName.CompareTo(strGetTableFileName) == 0)
                                    bExistException = true;

                                if (bExistException)
                                {
                                    sb.AppendLine($"<Row : {nRowPos + 1}> [ProdNo_P]-[TestType]-[FileVersion]-[FileName] Combine Key is information that already exists. : " +
                                        $"{strGetProdP}" + " " + $"{strGetTestType}" + " " + $"{strGetFileVersion}" + " " + $"{strGetFileName}");

                                    bCheckInformationResult = false;
                                }
                            }
                            else
                            {
                                string strScanInfo = strGetProdP + strGetTestType + strGetFileVersion + strGetFileName;

                                for (int i = 0; i < dtCurrentDataTable.Rows.Count; i++)
                                {
                                    if (i == nRowPos)
                                        continue;

                                    string CompareProdNoP = dtCurrentDataTable.Rows[i].Field<string>("ProdNo_P");
                                    string CompareTestType = dtCurrentDataTable.Rows[i].Field<string>("TestType");
                                    string CompareTestFileVer = dtCurrentDataTable.Rows[i].Field<string>("Version");
                                    string CompareFileName = dtCurrentDataTable.Rows[i].Field<string>("FileName");

                                    string strCompareInfo = CompareProdNoP + CompareTestType + CompareTestFileVer + CompareFileName;

                                    if (strScanInfo.CompareTo(strCompareInfo) == 0)
                                    {
                                        sb.AppendLine($"<Row : {nRowPos + 1}> [ProdNo_P]-[TestType]-[FileVersion]-[FileName] Combine Key is information that already exists. in registration waiting list. : " +
                                       $"{strGetProdP}" + " " + $"{strGetTestType}" + " " + $"{strGetFileVersion}" + " " + $"{strGetFileName}");

                                        bCheckInformationResult = false;

                                        throw new Exception("A problem occurred as a result of verification. After checking the details, correct the information and try again.");
                                    }
                                }
                            }

                            AddResultList(dr);

                            nRowPos++;
                        }

                        if (bCheckInformationResult == false)
                            throw new Exception("A problem occurred as a result of verification. After checking the details, correct the information and try again.");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        richTextBoxErr.AppendText(sb.ToString());

                        e.Handled = true;

                        return;
                    }

                    switch (CurrentWizardMode)
                    {
                        case eSelectMode.Insert:
                            break;
                        case eSelectMode.Modify:
                            break;
                    }

                    RefreshResultGridView();
                }
            }
        }
        private bool CheckRowSelectInfo(int nIndex)
        {
            object objHandle = null;

            bool bCheckResult = true;

            if ((dataGridViewReady.Rows.Count - 1) == nIndex)
                return false;

            try
            {
                objHandle = dataGridViewReady.Rows[nIndex].Cells["No"].Value.ToString();
            }
            catch
            {
                objHandle = null;

                bCheckResult = false;
            }
            finally
            {
                ;//
            }

            return bCheckResult;
        }

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

            nReadyViewSelectRow = -1;
        }

        private void GetGroupList()
        {
            SqlDataReader reader;

            ctrlDB.GetConnSqlCmd().CommandText = "SELECT * FROM PROD_Group WITH(NOLOCK) ORDER BY No ASC;";

            reader = ctrlDB.GetConnSqlCmd().ExecuteReader();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Load(reader);
            ds.Tables.Add(dt);
            reader.Close();

            comboBoxGroupList.Items.Clear();

            if (XCommons.isHasRow(ds))
            {
                comboBoxGroupList.Items.Add("Select Model Name(Group) ...");
                comboBoxGroupList.SelectedIndex = 0;

                for (int i = 0; i < dt.Rows.Count; i++)
                    comboBoxGroupList.Items.Add(dt.Rows[i]["ModelName"].ToString());
            }
        }

        private bool CheckGroupInfo(string strQueryText = "")
        {
            SqlDataReader reader;

            if (strQueryText.Length > 0)
                ctrlDB.GetConnSqlCmd().CommandText = strQueryText;

            reader = ctrlDB.GetConnSqlCmd().ExecuteReader();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Load(reader);
            ds.Tables.Add(dt);
            reader.Close();

            if (XCommons.isHasRow(ds))
            {
                textBoxDispGroupNo.Text = dt.Rows[0][0].ToString();

                nNowSelectGroupNo = Convert.ToInt32(textBoxDispGroupNo.Text);

                textBoxDispGroupName.Text = dt.Rows[0][1].ToString();
                textBoxDispModelName.Text = dt.Rows[0][2].ToString();
                textBoxDispComment.Text = dt.Rows[0][3].ToString();

                textBoxInputModelName.Text = dt.Rows[0][2].ToString();

                GroupCheckResult = true;

                textBoxInputModelName.BackColor = Color.LimeGreen;

                return true;
            }
            else
            {
                nNowSelectGroupNo = -1;

                textBoxInputModelName.BackColor = Color.Red;

                return false;
            }
        }

        private void GridViewSelectedProc(int nRow)
        {
            nReadyViewSelectRow = nRow;

            labelReadyViewInfo.Text = "Select Row : " + (nReadyViewSelectRow + 1).ToString();

            if (CheckRowSelectInfo(nReadyViewSelectRow))
            {
                ;//
            }
            else
                ClearListSelect();
        }

        private void dataGridViewReady_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            GridViewSelectedProc(e.RowIndex);
        }

        private void dataGridViewReady_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            /*
            if (loadTestListProc.CheckTestListLoad() == false)
                return;
            */

            if (CheckRowSelectInfo(nReadyViewSelectRow))
            {
                MakeInformation.ProdNo_P = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["ProdNo_P"].Value.ToString();
                MakeInformation.TestType = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["TestType"].Value.ToString();
                MakeInformation.Version = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["Version"].Value.ToString();
                MakeInformation.ProdCode = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["ProdCode"].Value.ToString();
                MakeInformation.FileName = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["FileName"].Value.ToString();
                MakeInformation.RegDT = Convert.ToDateTime(dataGridViewReady.Rows[nReadyViewSelectRow].Cells["RegDT"].Value);
                MakeInformation.RegUser = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["RegUser"].Value.ToString();
                MakeInformation.UpdateDT = Convert.ToDateTime(dataGridViewReady.Rows[nReadyViewSelectRow].Cells["UpdateDT"].Value);
                MakeInformation.UpdateUser = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["UpdateUser"].Value.ToString();
                MakeInformation.GroupNo = Convert.ToInt32(dataGridViewReady.Rows[nReadyViewSelectRow].Cells["GroupNo"].Value);
                MakeInformation.Comment = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["Comment"].Value.ToString();
                MakeInformation.Description = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["Description"].Value.ToString();

                textBoxItem1.Text = MakeInformation.ProdNo_P;
                textBoxItem2.Text = MakeInformation.TestType;
                textBoxItem3.Text = MakeInformation.Version;
                textBoxItem4.Text = MakeInformation.ProdCode;

                textBoxItem11.Text = MakeInformation.Comment;

                CheckGroupInfo("SELECT * FROM PROD_Group WHERE No = " + MakeInformation.GroupNo + " ORDER BY No ASC;");
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            /*
            if (loadTestListProc.CheckTestListLoad() == false)
                return;
            */

            if (GroupCheckResult == false)
                return;

            DataRow setMakeDataRows = null;

            setMakeDataRows = dtCurrentDataTable.NewRow();

            setMakeDataRows["No"] = dtCurrentDataTable.Rows.Count + 1;
            setMakeDataRows["ProdNo_P"] = textBoxItem1.Text;
            setMakeDataRows["TestType"] = textBoxItem2.Text;
            setMakeDataRows["Version"] = textBoxItem3.Text;
            setMakeDataRows["ProdCode"] = textBoxItem4.Text;
            setMakeDataRows["FileName"] = textBoxItem5.Text;
            setMakeDataRows["RegDT"] = DateTime.Now;
            setMakeDataRows["RegUser"] = textBoxItem7.Text;
            setMakeDataRows["UpdateDT"] = DateTime.Now;
            setMakeDataRows["UpdateUser"] = textBoxItem9.Text;
            setMakeDataRows["GroupNo"] = nNowSelectGroupNo;
            setMakeDataRows["GroupName"] = textBoxDispGroupName.Text;
            setMakeDataRows["ModelName"] = textBoxDispModelName.Text;
            setMakeDataRows["Comment"] = textBoxItem11.Text;
            setMakeDataRows["Description"] = textBoxItem12.Text;

            dtCurrentDataTable.Rows.Add(setMakeDataRows);

            RefreshReadyGridView();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (nReadyViewSelectRow == -1 || dtCurrentDataTable.Rows.Count <= nReadyViewSelectRow)
                return;

            dtCurrentDataTable.Rows.RemoveAt(nReadyViewSelectRow);

            RefreshReadyGridView();
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            /*
            if (loadTestListProc.CheckTestListLoad() == false)
                return;
            */

            if (nReadyViewSelectRow == -1 || dtCurrentDataTable.Rows.Count <= nReadyViewSelectRow)
                return;

            if (GroupCheckResult == false)
                return;

            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("ProdNo_P", textBoxItem1.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("TestType", textBoxItem2.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("Version", textBoxItem3.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("ProdCode", textBoxItem4.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("FileName", textBoxItem5.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<DateTime>("RegDT", DateTime.Now);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("RegUser", textBoxItem7.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<DateTime>("UpdateDT", DateTime.Now);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("UpdateUser", textBoxItem9.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<int>("GroupNo", nNowSelectGroupNo);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("GroupName", textBoxDispGroupName.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("ModelName", textBoxDispModelName.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("Comment", textBoxItem11.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("Description", textBoxItem12.Text);

            RefreshReadyGridView();
        }

        private void TestListCopyWizard_FormClosing(object sender, FormClosingEventArgs e)
        {
            /*
            if (loadTestListProc != null)
            {
                if (loadTestListProc.CheckTestListLoad() == false)
                {
                    this.DialogResult = DialogResult.None;

                    e.Cancel = true;
                }
            }
            */
        }

        private void wizardControl_CancelClick(object sender, CancelEventArgs e)
        {
            /*
            if (loadTestListProc != null)
            {
                if (loadTestListProc.CheckTestListLoad() == false)
                {
                    this.DialogResult = DialogResult.None;

                    e.Cancel = true;
                }
            }
            */
        }

        private void buttonGroupChk_Click(object sender, EventArgs e)
        {
            /*
            if (loadTestListProc.CheckTestListLoad() == false)
                return;
            */

            GroupCheckResult = false;

            textBoxDispGroupNo.Text = "";
            textBoxDispGroupName.Text = "";
            textBoxDispModelName.Text = "";
            textBoxDispComment.Text = "";

            CheckGroupInfo("SELECT * FROM PROD_Group WHERE ModelName = '" + textBoxInputModelName.Text + "' ORDER BY No ASC;");
        }

        private void comboBoxVariant_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxVariant.SelectedIndex > 0)
                textBoxItem1.Text = comboBoxVariant.Items[comboBoxVariant.SelectedIndex].ToString();
        }

        private void textBoxInputModelName_TextChanged(object sender, EventArgs e)
        {
            GroupCheckResult = false;

            textBoxInputModelName.BackColor = Color.Yellow;
        }

        private void comboBoxGroupList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxGroupList.SelectedIndex > 0)
            {
                textBoxInputModelName.Text = comboBoxGroupList.Items[comboBoxGroupList.SelectedIndex].ToString();

                GroupCheckResult = false;

                textBoxInputModelName.BackColor = Color.Yellow;
            }
        }

        private void wizardControl_CustomizeCommandButtons(object sender, DevExpress.XtraWizard.CustomizeCommandButtonsEventArgs e)
        {
            if (e.Page == wizardPage)
            {
                this.ActiveControl = textBoxItem1;

                textBoxItem1.Focus();
            }
            else if (e.Page == welcomeWizardPage)
            {
                this.ActiveControl = e.NextButton.Button;

                e.NextButton.Button.Focus();
            }
        }

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

            switch (ucGetChar)
            {
                case 32:
                    if (dataGridViewReady.SelectedRows.Count > 0)
                    {
                        int nRow = dataGridViewReady.SelectedRows[0].Index;

                        GridViewSelectedProc(nRow);
                    }
                    break;
                case 27:
                    this.ActiveControl = buttonDelete;

                    buttonDelete.Focus();
                    break;
            }
        }
    }
}
