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;

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

        private List<CReleaseInformation> lstCopyTestListRelease;

        private List<CReleaseInformation> lstCopyInsertSetTestListRelease;

        private CReleaseInformation MakeInformation;

        private DataTable dtCurrentDataTable;

        private string strCopyProductNoInput;

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

        public IEnumerable<CReleaseInformation> GetInsertList()
        {
            foreach (CReleaseInformation info in lstCopyInsertSetTestListRelease)
            {
                yield return info;
            }
        }

        private IDataController ctrlDB;

        private static object objInfoControlNextLock = new object();

        public enum eSelectMode
        {
            Insert = 0,
            Modify
        }

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

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

        private bool TestCodeCheckResult { set; get; }
        private int nNowTestCodeNo { set; get; } = -1;

        private bool TestListCheckResult { set; get; }

        private int nNowTestListNo { 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;
        }

        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)
        {
            CReleaseInformation InformationTemp = new CReleaseInformation();

            InformationTemp.ProdNo_C = dr.Field<string>("ProdNo_C");
            InformationTemp.TestCodeNo = dr.Field<Int32>("TestCodeNo");
            InformationTemp.TestCode = dr.Field<string>("TestCode");
            InformationTemp.VariantNo = dr.Field<Int32>("VariantNo");
            InformationTemp.ProdNo_P = dr.Field<string>("ProdNo_P");
            InformationTemp.TestType = dr.Field<string>("TestType");
            InformationTemp.FileVersion = dr.Field<string>("FileVersion");
            InformationTemp.ProdCode = dr.Field<string>("ProdCode");
            InformationTemp.Config = dr.Field<string>("Config");
            InformationTemp.RegDT = DateTime.Now;                       //dr.Field<DateTime>("RegDT", DateTime.Now);
            InformationTemp.RegUser = ctrlDB.GetLoginInfo().UserID;     //dr.Field<string>("RegUser");
            InformationTemp.RegUserComment = dr.Field<string>("RegUserComment");

            lstCopyInsertSetTestListRelease.Add(InformationTemp);
        }

        public TestListReleaseCopyWizard(IDataController ctrlDB, eSelectMode setMode, List<CReleaseInformation> getSelTestListRelInfo, string strGetCopyPorductNo = "")
        {
            InitializeComponent();

            strCopyProductNoInput = strGetCopyPorductNo;

            this.Text = strTitle;

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

            CurrentWizardMode = setMode;

            lstCopyTestListRelease = getSelTestListRelInfo.ToList();

            lstCopyInsertSetTestListRelease = new List<CReleaseInformation>();

            this.ctrlDB = ctrlDB;

            GetTestCodeList();

            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;
            }

            MakeInformation = new CReleaseInformation();

            DataRow[] setMakeDataRows = new DataRow[lstCopyTestListRelease.Count];
            dtCurrentDataTable = MakeDataTable();

            for (int i = 0; i < lstCopyTestListRelease.Count; i++)
            {
                setMakeDataRows[i] = dtCurrentDataTable.NewRow();
                setMakeDataRows[i]["No"] = i + 1;

                if (strCopyProductNoInput.Length > 0)
                    setMakeDataRows[i]["ProdNo_C"] = strCopyProductNoInput;
                else
                    setMakeDataRows[i]["ProdNo_C"] = lstCopyTestListRelease[i].ProdNo_C;

                setMakeDataRows[i]["TestCodeNo"] = lstCopyTestListRelease[i].TestCodeNo;
                setMakeDataRows[i]["TestCode"] = lstCopyTestListRelease[i].TestCode;
                setMakeDataRows[i]["VariantNo"] = lstCopyTestListRelease[i].VariantNo;
                setMakeDataRows[i]["ProdNo_P"] = lstCopyTestListRelease[i].ProdNo_P;
                setMakeDataRows[i]["TestType"] = lstCopyTestListRelease[i].TestType;
                setMakeDataRows[i]["FileVersion"] = lstCopyTestListRelease[i].FileVersion;
                setMakeDataRows[i]["ProdCode"] = lstCopyTestListRelease[i].ProdCode;
                setMakeDataRows[i]["Config"] = lstCopyTestListRelease[i].Config;
                setMakeDataRows[i]["RegDT"] = DateTime.Now;
                setMakeDataRows[i]["RegUser"] = ctrlDB.GetLoginInfo().UserID;
                setMakeDataRows[i]["RegUserComment"] = lstCopyTestListRelease[i].RegUserComment;

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

            DataTable dtTestCode = ctrlDB.GetTable("SELECT * FROM STAT_TestCode WHERE No = " + lstCopyTestListRelease[0].TestCodeNo + " ORDER BY No ASC;");

            CheckTestCodeInfo(dtTestCode);

            /*
            DataTable dtTestList = ctrlDB.GetTable("SELECT No, ProdNo_P, TestType, Version, ProdCode, FileName, Comment, Description FROM [PROD_Variant] WHERE No = " + lstCopyTestListRelease[0].VariantNo + " ORDER BY No ASC;");

            CheckTestListInfo(dtTestList);
            */

            DataTable dtTestList = ctrlDB.GetTable($"SELECT X.No, X.ProdNo_P, Y.TestType, Y.Version, Y.ProdCode, Y.FileName, X.Comment, X.Description FROM [PROD_Variant] AS X WITH(NOLOCK) " +
                        $"INNER JOIN [STOR_TestListFile] AS Y WITH(NOLOCK) ON X.TestListFileNo = Y.No " +
                        $"WHERE X.No = " + lstCopyTestListRelease[0].VariantNo + " ORDER BY No ASC;");

            CheckTestListInfo(dtTestList);
        }

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

            textBoxItemRegDT_Picker.Value = DateTime.Now;

            textBoxItemProdC.Text = lstCopyTestListRelease[0].ProdNo_C;

            textBoxItemRegUser.Text = ctrlDB.GetLoginInfo().UserID;
        }

        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_C";
            Column1.AutoIncrement = false;
            Column1.AllowDBNull = false;
            logTable.Columns.Add(Column1);

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

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

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

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

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

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

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

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

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

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

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

            // 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 GetTestCodeList()
        {
            SqlDataReader reader;

            ctrlDB.GetConnSqlCmd().CommandText = "SELECT * FROM STAT_TestCode 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();

            comboBoxTestCodeList.Items.Clear();

            if (XCommons.isHasRow(ds))
            {
                comboBoxTestCodeList.Items.Add("Select Test Code ...");
                comboBoxTestCodeList.SelectedIndex = 0;

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

        private bool CheckTestCodeInfo(DataTable dt)
        {
            if (XCommons.isHasRow(dt) == false)
            {
                textBoxTextCodeInput.BackColor = Color.Red;

                nNowTestCodeNo = -1;

                return false;
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            textBoxDispTestCodeNo.Text = dt.Rows[0][0].ToString();
            textBoxDispTestCode.Text = dt.Rows[0][1].ToString();
            textBoxDispGate1.Text = dt.Rows[0][2].ToString();
            textBoxDispGate2.Text = dt.Rows[0][3].ToString();
            textBoxDispComment.Text = dt.Rows[0][4].ToString();

            textBoxTextCodeInput.Text = dt.Rows[0][1].ToString();

            nNowTestCodeNo = Convert.ToInt32(textBoxDispTestCodeNo.Text);

            TestCodeCheckResult = true;

            textBoxTextCodeInput.BackColor = Color.LimeGreen;

            return true;
        }

        private bool CheckTestListInfo(DataTable dt)
        {
            if (XCommons.isHasRow(dt) == false)
            {
                textBoxPProdNoInput.BackColor = Color.Red;
                textBoxDispInTestType.BackColor = Color.Red;
                textBoxDispInVersion.BackColor = Color.Red;
                textBoxDispInProdCode.BackColor = Color.Red;
                textBoxDispInFileName.BackColor = Color.Red;

                textBoxDispInTestType.ReadOnly = false;
                textBoxDispInVersion.ReadOnly = false;
                textBoxDispInProdCode.ReadOnly = false;
                textBoxDispInFileName.ReadOnly = false;

                nNowTestListNo = -1;

                return false;
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            textBoxDispTestListVariantNo.Text = dt.Rows[0][0].ToString();
            textBoxPProdNoInput.Text = dt.Rows[0][1].ToString();
            textBoxDispInTestType.Text = dt.Rows[0][2].ToString();
            textBoxDispInVersion.Text = dt.Rows[0][3].ToString();
            textBoxDispInProdCode.Text = dt.Rows[0][4].ToString();
            textBoxDispInFileName.Text = dt.Rows[0][5].ToString();
            textBoxDispTestListComment.Text = dt.Rows[0][6].ToString();
            textBoxDispTestListDescription.Text = dt.Rows[0][7].ToString();

            textBoxPProdNoInput.Text = dt.Rows[0][1].ToString();

            nNowTestListNo = Convert.ToInt32(textBoxDispTestListVariantNo.Text);

            TestListCheckResult = true;

            textBoxPProdNoInput.BackColor = Color.LimeGreen;
            textBoxDispInTestType.BackColor = Color.LimeGreen;
            textBoxDispInVersion.BackColor = Color.LimeGreen;
            textBoxDispInProdCode.BackColor = Color.LimeGreen;
            textBoxDispInFileName.BackColor = Color.LimeGreen;

            textBoxDispInTestType.ReadOnly = true;
            textBoxDispInVersion.ReadOnly = true;
            textBoxDispInProdCode.ReadOnly = true;
            textBoxDispInFileName.ReadOnly = true;

            return true;
        }

        private void wizardControl_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            if (wizardControl.SelectedPageIndex == 1)
            {
                lock (objInfoControlNextLock)
                {
                    richTextBoxErr.Clear();

                    bool bCheckInformationResult = true;

                    StringBuilder sb = new StringBuilder();

                    lstCopyInsertSetTestListRelease = new List<CReleaseInformation>();
                    lstCopyInsertSetTestListRelease.Clear();

                    try
                    {
                        /*
                        string strItemName = "";

                        if (TestCodeCheckResult == false) { strItemName = "TestCode No"; throw new Exception($"The value must be select and check. [" + strItemName + "]"); }
                        if (TestListCheckResult == false) { strItemName = "TestList No"; throw new Exception($"The value must be select and check. [" + strItemName + "]"); }
                        */

                        int nRowPos = 0;

                        foreach (DataRow dr in dtCurrentDataTable.Rows)
                        {
                            string strGetProdC = dr.Field<string>("ProdNo_C");
                            int nGetTestCodeNo = dr.Field<int>("TestCodeNo");

                            string strGetTestCode = dr.Field<string>("TestCode");

                            string strGetTestType = dr.Field<string>("TestType");
                            string strGetVersion = dr.Field<string>("FileVersion");
                            string strGetProdCode = dr.Field<string>("ProdCode");

                            int nGetTestListNo = dr.Field<int>("VariantNo");

                            //"SELECT * FROM [PROD_Release] WHERE No IN(SELECT No FROM [PROD_Release] WHERE [ProdNo_C] = '" + strGetProdC + "' AND [TestCodeNo] = "+ strGetTestCodeNo + " AND [VariantNo] = "+ strGetTestListNo + " ) ORDER BY No ASC;";
                            // 'C_ProdNo' // 'TestCodeNo' // 'VariantNo' // 'P_ProdNo' // 'FileVersion'
                            string strGetQueryText =
                            $"SELECT X.No, " +
                            $"X.ProdNo_C AS 'C_ProdNo', " +
                            $"Y.No AS 'TestCodeNo', " +
                            $"Y.TestCode, " +
                            $"Z.No AS 'VariantNo', " +
                            $"Z.ProdNo_P AS 'P_ProdNo', " +
                            $"J.TestType, " +
                            $"J.Version AS 'FileVersion', " +
                            $"J.ProdCode, " +
                            $"X.Config, " +
                            $"X.RegDT, " +
                            $"X.RegUser, " +
                            $"X.RegUserComment " +
                            $"FROM [PROD_Release] AS X " +
                            $"INNER JOIN [STAT_TestCode] AS Y WITH(NOLOCK) ON X.TestCodeNo = Y.No " +
                            $"INNER JOIN [PROD_Variant] AS Z WITH(NOLOCK) ON X.VariantNo = Z.No " +
                            $"INNER JOIN [STOR_TestListFile] AS J WITH(NOLOCK) ON Z.TestListFileNo = J.No " +
                            $"WHERE X.ProdNo_C = '" + strGetProdC + "' AND X.TestCodeNo = " + nGetTestCodeNo + " " +
                            $"ORDER BY No ASC;";

                            DataTable dtCheck = ctrlDB.GetTable(strGetQueryText);

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

                                string strTableProdNoC = dtCheck.Rows[0]["C_ProdNo"].ToString();
                                string strTableTestCode = dtCheck.Rows[0]["TestCode"].ToString();
                                string strTableTestType = dtCheck.Rows[0]["TestType"].ToString();
                                string strTableVersion = dtCheck.Rows[0]["FileVersion"].ToString();
                                string strTableProductionCode = dtCheck.Rows[0]["ProdCode"].ToString();

                                if (strGetProdC.CompareTo(strTableProdNoC) == 0 &&
                                   strGetTestCode.CompareTo(strTableTestCode) == 0 &&
                                   strGetTestType.CompareTo(strTableTestType) == 0 &&
                                   strGetVersion.CompareTo(strTableVersion) == 0 &&
                                   strGetProdCode.CompareTo(strTableProductionCode) == 0)
                                    bExistException = true;

                                if (bExistException)
                                {
                                    sb.AppendLine($"<Row : {nRowPos + 1}> [ProdNo_C]-[TestCode]-[TestType]-[Version]-[ProductionCode] Combine Key is information that already exists. : " +
                                        $"{strGetProdC}" + " " + $"{strGetTestCode}" + " " + $"{strGetTestType}" + " " + $"{strGetVersion}" + " " + $"{strGetProdCode}");

                                    bCheckInformationResult = false;
                                }
                            }
                            else
                            {
                                string strScanInfo = strGetProdC + strGetTestCode + strGetTestType + strGetVersion + strGetProdCode;

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

                                    string CompareProdNoC = dtCurrentDataTable.Rows[i].Field<string>("ProdNo_C");
                                    string CompareTestCode = dtCurrentDataTable.Rows[i].Field<string>("TestCode");
                                    string CompareTestType = dtCurrentDataTable.Rows[i].Field<string>("TestType");
                                    string CompareFileVersion = dtCurrentDataTable.Rows[i].Field<string>("FileVersion");
                                    string CompareProdCode = dtCurrentDataTable.Rows[i].Field<string>("ProdCode");

                                    string strCompareInfo = CompareProdNoC + CompareTestCode + CompareTestType + CompareFileVersion + CompareProdCode;

                                    if (strScanInfo.CompareTo(strCompareInfo) == 0)
                                    {
                                        sb.AppendLine($"<Row : {nRowPos + 1}> [ProdNo_C]-[TestCode]-[TestType]-[Version]-[ProductionCode] Combine Key is information that already exists. in registration waiting list. : " +
                                            $"{strGetProdC}" + " " + $"{strGetTestCode}" + " " + $"{strGetTestType}" + " " + $"{strGetVersion}" + " " + $"{strGetProdCode}");

                                        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 dataGridViewReady_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int n = e.RowIndex;

            nReadyViewSelectRow = e.RowIndex;

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

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

        private void dataGridViewReady_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (CheckRowSelectInfo(nReadyViewSelectRow))
            {
                MakeInformation.ProdNo_C = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["ProdNo_C"].Value.ToString();
                MakeInformation.TestCodeNo = Convert.ToInt32(dataGridViewReady.Rows[nReadyViewSelectRow].Cells["TestCodeNo"].Value);
                MakeInformation.VariantNo = Convert.ToInt32(dataGridViewReady.Rows[nReadyViewSelectRow].Cells["VariantNo"].Value);
                MakeInformation.Config = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["Config"].Value.ToString();
                MakeInformation.RegDT = Convert.ToDateTime(dataGridViewReady.Rows[nReadyViewSelectRow].Cells["RegDT"].Value);
                MakeInformation.RegUser = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["RegUser"].Value.ToString();
                MakeInformation.RegUserComment = dataGridViewReady.Rows[nReadyViewSelectRow].Cells["RegUserComment"].Value.ToString();

                DataTable dtTestCode = ctrlDB.GetTable("SELECT * FROM STAT_TestCode WHERE No = " + MakeInformation.TestCodeNo + " ORDER BY No ASC;");

                CheckTestCodeInfo(dtTestCode);

                /*
                DataTable dtTestList = ctrlDB.GetTable("SELECT No, ProdNo_P, TestType, Version, ProdCode, FileName, Comment, Description FROM [PROD_Variant] WHERE No = " + MakeInformation.VariantNo + " ORDER BY No ASC;");

                CheckTestListInfo(dtTestList);
                */

                DataTable dtTestList = ctrlDB.GetTable($"SELECT X.No, X.ProdNo_P, Y.TestType, Y.Version, Y.ProdCode, Y.FileName, X.Comment, X.Description FROM [PROD_Variant] AS X WITH(NOLOCK) " +
                       $"INNER JOIN [STOR_TestListFile] AS Y WITH(NOLOCK) ON X.TestListFileNo = Y.No " +
                       $"WHERE X.No = " + lstCopyTestListRelease[0].VariantNo + " ORDER BY No ASC;");

                CheckTestListInfo(dtTestList);

                textBoxItemProdC.Text = MakeInformation.ProdNo_C;
                textBoxItemConfig.Text = MakeInformation.Config;
                textBoxItemRegDT_Picker.Value = DateTime.Now;
                textBoxItemDescription.Text = MakeInformation.RegUserComment;
            }
        }

        private void buttonTestCodeChk_Click(object sender, EventArgs e)
        {
            TestCodeCheckResult = false;

            textBoxDispTestCodeNo.Text = "";
            textBoxDispTestCode.Text = "";
            textBoxDispGate1.Text = "";
            textBoxDispGate2.Text = "";
            textBoxDispComment.Text = "";

            DataTable dtTestCode = ctrlDB.GetTable("SELECT * FROM STAT_TestCode WHERE TestCode = '" + textBoxTextCodeInput.Text + "' ORDER BY No ASC;");

            CheckTestCodeInfo(dtTestCode);
        }

        private void buttonTestListChk_Click(object sender, EventArgs e)
        {
            TestListCheckResult = false;

            textBoxDispTestListVariantNo.Text = "";
            textBoxDispTestListComment.Text = "";
            textBoxDispTestListDescription.Text = "";

            /*
            string strSetQueryText = "SELECT No, ProdNo_P, TestType, Version, ProdCode, FileName, Comment, Description FROM [PROD_Variant] " +
                "WHERE ProdNo_P = '" + textBoxPProdNoInput.Text + "' ";
            */

            string strSetQueryText = $"SELECT X.No, X.ProdNo_P, Y.TestType, Y.Version, Y.ProdCode, Y.FileName, X.Comment, X.Description FROM [PROD_Variant] AS X WITH(NOLOCK) " +
                    $"INNER JOIN [STOR_TestListFile] AS Y WITH(NOLOCK) ON X.TestListFileNo = Y.No " +
                    $"WHERE X.ProdNo_P = '" + textBoxPProdNoInput.Text + "' ";

            string strSetQueryEndText = "ORDER BY No ASC;";

            if (textBoxDispInTestType.Text.Length > 0)
            {
                strSetQueryText += "AND ";
                strSetQueryText += "Y.TestType = '" + textBoxDispInTestType.Text + "' ";
            }
            if (textBoxDispInVersion.Text.Length > 0)
            {
                strSetQueryText += "AND ";
                strSetQueryText += "Y.Version = '" + textBoxDispInVersion.Text + "' ";
            }
            if (textBoxDispInProdCode.Text.Length > 0)
            {
                strSetQueryText += "AND ";
                strSetQueryText += "Y.ProdCode = '" + textBoxDispInProdCode.Text + "' ";
            }
            if (textBoxDispInFileName.Text.Length > 0)
            {
                strSetQueryText += "AND ";
                strSetQueryText += "Y.FileName = '" + textBoxDispInFileName.Text + "' ";
            }

            strSetQueryText += strSetQueryEndText;

            DataTable dtTestList = ctrlDB.GetTable(strSetQueryText);

            if (CheckTestListInfo(dtTestList))
            {
                wizardPage.ActiveControl = buttonAdd;

                buttonAdd.Focus();
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            int nTag = Convert.ToInt32(((Control)sender).Tag);

            switch (nTag)
            {
                case 0:
                    if (textBoxDispInTestType.ReadOnly) textBoxDispInTestType.ReadOnly = false;
                    else if (textBoxDispInTestType.ReadOnly == false) textBoxDispInTestType.ReadOnly = true;
                    break;
                case 1:
                    if (textBoxDispInVersion.ReadOnly) textBoxDispInVersion.ReadOnly = false;
                    else if (textBoxDispInVersion.ReadOnly == false) textBoxDispInVersion.ReadOnly = true;
                    break;
                case 2:
                    if (textBoxDispInProdCode.ReadOnly) textBoxDispInProdCode.ReadOnly = false;
                    else if (textBoxDispInProdCode.ReadOnly == false) textBoxDispInProdCode.ReadOnly = true;
                    break;
                case 3:
                    if (textBoxDispInFileName.ReadOnly) textBoxDispInFileName.ReadOnly = false;
                    else if (textBoxDispInFileName.ReadOnly == false) textBoxDispInFileName.ReadOnly = true;
                    break;
            }
        }

        private void textBoxDispIn_ReadOnlyChanged(object sender, EventArgs e)
        {
            int nTag = Convert.ToInt32(((Control)sender).Tag);

            bool bReadOnly = ((TextBox)sender).ReadOnly;

            switch (nTag)
            {
                case 0:
                    if (bReadOnly) button1.BackColor = Color.Transparent;
                    else button1.BackColor = Color.Yellow; textBoxDispInTestType.Focus();
                    break;
                case 1:
                    if (bReadOnly) button2.BackColor = Color.Transparent;
                    else button2.BackColor = Color.Yellow; textBoxDispInVersion.Focus();
                    break;
                case 2:
                    if (bReadOnly) button3.BackColor = Color.Transparent;
                    else button3.BackColor = Color.Yellow; textBoxDispInProdCode.Focus();
                    break;
                case 3:
                    if (bReadOnly) button4.BackColor = Color.Transparent;
                    else button4.BackColor = Color.Yellow; textBoxDispInFileName.Focus();
                    break;
            }
        }

        private void textBoxDispIn_TextChanged(object sender, EventArgs e)
        {
            TestListCheckResult = false;

            textBoxPProdNoInput.BackColor = Color.Yellow;
        }
        private void checkBoxAutoComplete_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxAutoComplete.Checked == false)
            {
                textBoxPProdNoInput.AutoCompleteCustomSource = null;
                textBoxPProdNoInput.AutoCompleteMode = AutoCompleteMode.None;
                textBoxPProdNoInput.AutoCompleteSource = AutoCompleteSource.None;
            }
            else
            {
                /*if (checkBoxAutoComplete.Checked != true || textBoxPProdNoInput.Text.Length <= 0)
                    return;*/

                //WHERE ProdNo_P LIKE '%" + textBoxPProdNoInput.Text + "%'

                DataTable dtProdNoP = ctrlDB.GetTable("SELECT No, ProdNo_P FROM [PROD_Variant] ORDER BY No ASC;");

                if (dtProdNoP == null)
                    return;

                string[] postSource = dtProdNoP
                    .AsEnumerable()
                    .Select<System.Data.DataRow, String>(x => x.Field<string>("ProdNo_P"))
                    .ToArray();

                var source = new AutoCompleteStringCollection();

                source.AddRange(postSource);

                textBoxPProdNoInput.AutoCompleteCustomSource = source;

                textBoxPProdNoInput.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBoxPProdNoInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
        }

        private void textBoxPProdNoInput_TextChanged(object sender, EventArgs e)
        {
            TestListCheckResult = false;

            textBoxPProdNoInput.BackColor = Color.Yellow;

            textBoxDispInTestType.Text = "";
            textBoxDispInVersion.Text = "";
            textBoxDispInProdCode.Text = "";
            textBoxDispInFileName.Text = "";
        }

        private void textBoxTextCodeInput_TextChanged(object sender, EventArgs e)
        {
            TestCodeCheckResult = false;

            textBoxTextCodeInput.BackColor = Color.Yellow;
        }

        private void comboBoxTestCodeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxTestCodeList.SelectedIndex > 0)
            {
                textBoxTextCodeInput.Text = comboBoxTestCodeList.Items[comboBoxTestCodeList.SelectedIndex].ToString();

                TestCodeCheckResult = false;

                textBoxTextCodeInput.BackColor = Color.Yellow;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (TestCodeCheckResult == false || TestListCheckResult == false)
                return;

            DataRow setMakeDataRows = null;

            setMakeDataRows = dtCurrentDataTable.NewRow();

            setMakeDataRows["No"] = dtCurrentDataTable.Rows.Count + 1;
            setMakeDataRows["ProdNo_C"] = textBoxItemProdC.Text;
            setMakeDataRows["TestCodeNo"] = nNowTestCodeNo;
            setMakeDataRows["TestCode"] = textBoxDispTestCode.Text;
            setMakeDataRows["VariantNo"] = nNowTestListNo;
            setMakeDataRows["ProdNo_P"] = textBoxPProdNoInput.Text;
            setMakeDataRows["TestType"] = textBoxDispInTestType.Text;
            setMakeDataRows["FileVersion"] = textBoxDispInVersion.Text;
            setMakeDataRows["ProdCode"] = textBoxDispInProdCode.Text;
            setMakeDataRows["Config"] = textBoxItemConfig.Text;
            setMakeDataRows["RegDT"] = DateTime.Now;
            setMakeDataRows["RegUser"] = textBoxItemRegUser.Text;
            setMakeDataRows["RegUserComment"] = textBoxItemDescription.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 (nReadyViewSelectRow == -1 || dtCurrentDataTable.Rows.Count <= nReadyViewSelectRow)
                return;

            if (TestCodeCheckResult == false || TestListCheckResult == false)
                return;

            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("ProdNo_C", textBoxItemProdC.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<Int32>("TestCodeNo", nNowTestCodeNo);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("TestCode", textBoxDispTestCode.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<Int32>("VariantNo", nNowTestListNo);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("ProdNo_P", textBoxPProdNoInput.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("TestType", textBoxDispInTestType.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("FileVersion", textBoxDispInVersion.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("ProdCode", textBoxDispInProdCode.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("Config", textBoxItemConfig.Text);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<DateTime>("RegDT", DateTime.Now);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("RegUser", ctrlDB.GetLoginInfo().UserID);
            dtCurrentDataTable.Rows[nReadyViewSelectRow].SetField<string>("RegUserComment", textBoxItemDescription.Text);

            RefreshReadyGridView();
        }

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

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

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