using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bt.ScanLib;
using Bt;
using CsvReadWrite;
using System.Threading;
using Bt.FileLib;
using Bt.SysLib;
using System.IO;
using System.Runtime.InteropServices;
using System.Data.SqlClient;


namespace HT_SKT_Stock
{
    public partial class Form_Loading_FG : Form
    {
        public static Form_Loading_FG _FormInstance;		// フォーム
        public String AppPath = "";						// アプリケーション実行パス

        //public String dbFilePath = "\\FlashDisk\\BT_FILES\\drv1\\HT_SKT.DB";
        public String dbFilePath = "\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\HT_SKT.DB";
        public int dbIdx = 0;
        public int recordCount = 0;
        string[] resultsSKAN = null;
        private BtScanLib btScanLib;
        private String sysKanbanSerialNo = "";
        private String customerName = "";
        private String sysKanbanCode = "";
        private String sysKanbanClass = "";
        private String sysRemainderClass = "";

        public Form_Loading_FG()
        {
            InitializeComponent();

            // Enable BTScanLib
            btScanLib = BtScanLib.getInstance();
            btScanLib.OnScan += new ScanEventHandler(btScanLib_OnScan);
            btScanLib.Debug = false;
            btScanLib.Enable();

            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            button2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            btnConfirm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            btnCancel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            txtPDS.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            txtCKAN.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            txtCQTY.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            txtSKAN.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

            txtSQTY.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_Loading_FG_KeyDown);

        }

        private void Form_Loading_FG_Load(object sender, EventArgs e)
        {
            // 画面サイズ調整
            if (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width == 240)
            {
                resolution.ScreenSize.VGAtoQVGA(this);
            }

            // フォームの最大化・最小化ボタン非表示
            this.MaximizeBox = !this.MaximizeBox;
            this.MinimizeBox = !this.MinimizeBox;

            _FormInstance = this;


            // 実行パスを取得
            String path = this.GetType().Assembly.GetModules()[0].FullyQualifiedName;
            Int32 en = path.LastIndexOf("\\");
            AppPath = path.Substring(0, en);

            //StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\NoOCR.ini");
            StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\NoOCR.ini");
            int ret = Setting.btScanLoadOCRConfig(filenameSet);
            SetScanStatus(0, 0, true);

        }

        void btScanLib_OnScan(string result)
        {
            try
            {
                if (txtPDS.Text == string.Empty)
                {
                    txtPDS.Text = result.Substring(6, 9);
                    txtCKAN.Text = result.Substring(23, 10);
                    int CQTY = int.Parse(result.Substring(result.Length - 5, 2));

                    if (result.Length == 70)
                    {
                        CQTY = int.Parse(result.Substring(result.Length - 6, 3));
                    }

                    txtCQTY.Text = CQTY.ToString();
                    customerName = result.Substring(0, 6).Trim();

                    btnCancel.Focus();
                }
                else
                {
                    string[] kanban = result.Split('|');
                    string kanbanCode = "";
                    string kanbanClass = "";
                    string remainderClass = "";
                    string partNoScan = "";
                    string quantity = "0";
                    string kanbanSerialNo = "";


                    if (kanban.Length == 6)
                    {
                        kanbanCode = kanban[0];
                        kanbanClass = kanban[1];
                        remainderClass = kanban[2];
                        partNoScan = kanban[3];
                        quantity = kanban[4];
                        kanbanSerialNo = kanban[5];

                        txtSKAN.Text = partNoScan;
                        txtSQTY.Text = quantity;
                        sysKanbanSerialNo = kanbanSerialNo;

                        sysKanbanCode = kanbanCode;
                        sysKanbanClass = kanbanClass;
                        sysRemainderClass = remainderClass;

                        //btnConfirm.Enabled = true;

                        Confirm();
                    }
                    else
                    {
                        WarningErr1 err = new WarningErr1("Kanban Format Incorrect");
                        err.ShowDialog();
                    }
                }
            }
            catch (Exception)
            {

                //call error form here
                ErrorForm err = new ErrorForm();
                err.ShowDialog();

                //******* disable BTScanLib
                btScanLib.OnScan -= btScanLib_OnScan;
                btScanLib.Disable();

                FrmPOKAYOKEMatching._Form_POKAYOKEMatchingInstance.Show();
                Bt.ScanLib.Control.btScanDisable();
                //this.Hide();
                this.Close();
            }



        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //******* disable BTScanLib
            btScanLib.OnScan -= btScanLib_OnScan;
            btScanLib.Disable();

            FrmPOKAYOKEMatching._Form_POKAYOKEMatchingInstance.Show();
            Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }

        private void Confirm()
        {
            bool KOK = false;
            bool QOK = false;

            if (txtSKAN.Text == txtCKAN.Text)
                KOK = true;

            int CQTY = int.Parse(txtCQTY.Text);

            if (txtSQTY.Text != "")
            {
                int SQTY = int.Parse(txtSQTY.Text);

                if (SQTY == CQTY)
                    QOK = true;
            }


            if (KOK && QOK)
            {
                insertRecordKB();

                Int32 intCheck = InsertKB();

                if (intCheck == 3)
                {

                    WarningErr1 err2 = new WarningErr1("Error Database Connection ");
                    err2.ShowDialog();

                    btnCancel.Focus();

                    return;
                }

                if ((sysKanbanCode == "8") || (sysKanbanCode == "3"))
                {
                    Int32 intCheck22 = CheckStatusScan(sysKanbanCode, sysKanbanClass, txtSKAN.Text, sysKanbanSerialNo, sysRemainderClass);

                    if ((intCheck22 == 4) || (intCheck22 == 2))
                    {
                        WarningErr1 err1 = new WarningErr1("Kanban status doesn't match ");
                        err1.ShowDialog();

                        btnCancel.Focus();

                        return;

                    }
                    else if (intCheck22 == 3)
                    {
                        WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                        err1.ShowDialog();

                        btnCancel.Focus();

                        return;
                    }

                    Int32 intCheck11 = CheckInboundScan(txtSKAN.Text, sysKanbanSerialNo, Convert.ToDecimal(txtSQTY.Text), sysKanbanCode, sysKanbanClass, sysRemainderClass);

                    if (intCheck11 == 2)
                    {
                        WarningErr1 err1 = new WarningErr1("Over Stock ");
                        err1.ShowDialog();

                        btnCancel.Focus();

                        return;

                    }
                    else if (intCheck11 == 3)
                    {
                        WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                        err1.ShowDialog();

                        btnCancel.Focus();

                        return;

                    }


                    Int32 intCheck1 = InsertOutbound(txtSKAN.Text, sysKanbanSerialNo, txtSQTY.Text, sysKanbanCode, sysKanbanClass, sysRemainderClass);


                    if (intCheck1 == 3)
                    {

                        WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                        err1.ShowDialog();

                        btnCancel.Focus();

                        return;
                    }

                    Int32 intCheck2 = UpdateStatusKanban(sysKanbanCode,
                            sysKanbanClass,
                            txtSKAN.Text,
                            sysKanbanSerialNo, 0, 
                            sysRemainderClass);

                        if (intCheck2 == 3)
                        {

                            WarningErr1 err1 = new WarningErr1("Error Database Connection ");
                            err1.ShowDialog();

                            btnCancel.Focus();

                            return;
                        }

                }

                Form frmD = new Success();

                frmD.ShowDialog();
                frmD.Dispose();
                txtSKAN.Text = "";
                txtSQTY.Text = "";
                txtPDS.Text = "";
                txtCQTY.Text = "";
                txtCKAN.Text = "";
                customerName = "";
                sysKanbanCode = "";
                sysKanbanSerialNo = "";
                //StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\NoOCR.ini");
                StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\NoOCR.ini");
                int ret = Setting.btScanLoadOCRConfig(filenameSet);
                SetScanStatus(0, 0, true);
                btnConfirm.Enabled = false;
                btnCancel.Focus();

            }
            else
            {
                Form frmD = new NGSCAN(KOK, QOK, txtPDS.Text, txtCKAN.Text, txtCQTY.Text, txtSKAN.Text,
                    txtSQTY.Text, sysKanbanSerialNo, customerName);

                frmD.ShowDialog();
                frmD.Dispose();
                txtSKAN.Text = "";
                txtSQTY.Text = "";
                txtPDS.Text = "";
                txtCQTY.Text = "";
                txtCKAN.Text = "";
                customerName = "";
                sysKanbanCode = "";
                sysKanbanSerialNo = "";
                //StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\NoOCR.ini");
                StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\NoOCR.ini");
                int ret = Setting.btScanLoadOCRConfig(filenameSet);
                SetScanStatus(0, 0, true);
                btnConfirm.Enabled = false;
                btnCancel.Focus();
            }
        }

        private Int32 CheckInboundScan(string partNo, string kanbanSerialNo, Decimal qtyScan, string kanbanCode, string kanbanClass, string remainderClass)
        {
            try
            {


                //tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 iRow = 0;


                strSQL = "EXEC sp_get_Inbound_part @partNo = '" + partNo
                    + "' , @serialNo = '" + kanbanSerialNo + "' , @qtyScan = "
                    + qtyScan + " , @kanbanCode = '" + kanbanCode
                    + "' , @kanbanClass = '" + kanbanClass + "' , @remainderClass = '" + remainderClass
                    + "'";


                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                iRow = dt.Rows.Count;

                dtAdapter = null;
                objConn.Close();
                objConn = null;


                if (iRow > 0)
                {

                    return 1;

                }
                else
                {
                    return 2;
                }




            }
            catch (Exception)
            {

                return 3;

            }


        }

        private Int32 CheckStatusScan(string kanbanCode, string kanbanClass, string partNo, string kanbanSerialNo, string remainderClass)
        {
            try
            {


                //tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 qtyScan = 0;


                strSQL = "EXEC sp_get_kanban_status @kanbanCode = '" + kanbanCode
                    + "' , @kanbanClass = '" + kanbanClass + "' , @partNo = '" + partNo
                + "' , @serialNo = '" + kanbanSerialNo + "' , @remainderClass = '" + remainderClass
                + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds);
                dt = ds.Tables[0];

                dtAdapter = null;
                objConn.Close();
                objConn = null;

                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["status"].ToString() == "1")
                    {
                        return 1;
                    }
                    else
                    {
                        return 4;
                    }


                }
                else
                {
                    return 2;
                }




            }
            catch (Exception)
            {

                return 3;

            }


        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            Confirm();
        }

        private Int32 UpdateStatusKanban(string kanbanCode, string kanbanClass, string partNo, string kanbanSerialNo, Int32 status, string remainderClass)
        {
            try
            {


                //tableList = new List<List<string>>();

                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Int32 qtyScan = 0;


                strSQL = "EXEC sp_update_status_kanban_HT @kanbanCode = '" + kanbanCode
                    + "' , @kanbanClass = '" + kanbanClass + "' , @partNo = '" + partNo
                    + "' , @serialNo = '" + kanbanSerialNo + "' , @userId = '" + FrmLogin._Form_LoginInstance.userName
                    + "' , @status = " + status + " , @remainderClass = '" + remainderClass
                    + "'";


                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                objCmd.ExecuteNonQuery();

                objConn.Close();
                objConn = null;


                return 1;


            }
            catch (Exception)
            {

                return 3;

            }


        }

        private Int32 InsertOutbound(string partNo, string serial, string quantity, string kanbanCode, string kanbanClass, string remainderClass)
        {
            try
            {


                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                Decimal qtyScan = Convert.ToDecimal(quantity);


                //strSQL = "EXEC sp_insert_Stock_Tracking_Outbound @partNo = '" + partNo
                //    + "' , @serial = '" + serial
                //    + "' , @qtySerial = " + quantity + " , @userId = '" + FrmLogin._Form_LoginInstance.userName
                //    + "'";

                strSQL = "EXEC sp_insert_Outbound @partNo = '" + partNo
                    + "' , @serial = '" + serial
                    + "' , @qtySerial = " + quantity + " , @userId = '" + FrmLogin._Form_LoginInstance.userName
                    + "' , @kanbanCode = '" + kanbanCode + "', @kanbanClass = '" + kanbanClass
                    + "' , @remainderClass = '" + remainderClass + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                objCmd.ExecuteNonQuery();

                objConn.Close();
                objConn = null;

                              

                return 1;

            }
            catch (Exception)
            {
                return 3;
            }

        }


        private void label1_ParentChanged(object sender, EventArgs e)
        {

        }


        public void SetScanStatus(int scanMode, int scanStep, bool enn)
        {
            FrmLogin._Form_LoginInstance.ScanStep = scanStep;
            FrmLogin._Form_LoginInstance.ScanMode = scanMode;
            if (enn)
            {
                Bt.ScanLib.Control.btScanEnable();
            }


        }

        public int RcvMsg(string result)
        {
            StringBuilder filenameSet;//= new StringBuilder("\\RamDisk\\KANBAN.ini");
            int ret;//= Setting.btScanLoadOCRConfig(filenameSet);
            int ans = 0;

            try
            {

                switch (FrmLogin._Form_LoginInstance.ScanStep)
                {
                    case 0://QR Cus kanban

                        txtPDS.Text = result.Substring(6, 9);
                        txtCKAN.Text = result.Substring(23, 10);
                        int CQTY = int.Parse(result.Substring(result.Length - 5, 2));
                        txtCQTY.Text = CQTY.ToString();


                        FrmLogin._Form_LoginInstance.ScanStep = 1;
                        //filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\KANBAN.ini");
                        filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\KANBAN.ini");
                        ret = Setting.btScanLoadOCRConfig(filenameSet);
                        ans = 1;
                        break;

                    case 1://OCR KANBAN
                        //txtSKAN.Text = result;


                        //Form_Login._Form_LoginInstance.ScanStep = 2;
                        //filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\QTY.ini");
                        //ret = Setting.btScanLoadOCRConfig(filenameSet);
                        //ans = 1;

                        resultsSKAN = null;

                        //if (result.Contains("|"))
                        //{
                        //    string[] kanban = result.Split('|');
                        //    string kanbanCode = "";
                        //    string kanbanClass = "";
                        //    string remainderClass = "";
                        //    string partNoScan = "";
                        //    string quantity = "0";
                        //    string kanbanSerialNo = "";

                        //    if (kanban.Length == 6)
                        //    {
                        //        kanbanCode = kanban[0];
                        //        kanbanClass = kanban[1];
                        //        remainderClass = kanban[2];
                        //        partNoScan = kanban[3];
                        //        quantity = kanban[4];
                        //        kanbanSerialNo = kanban[5];

                        //        txtSKAN.Text = partNoScan;
                        //        txtSQTY.Text = quantity;

                        //        filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\QTY.ini");
                        //        ret = Setting.btScanLoadOCRConfig(filenameSet);
                        //        ans = 1;
                        //        btnConfirm.Enabled = true;

                        //    }
                        //    else
                        //    {
                        //        WarningErr1 err = new WarningErr1("Kanban Format Incorrect");
                        //        err.ShowDialog();

                        //        ans = 1;
                        //    }

                        //    return ans;

                        //}

                        resultsSKAN = result.Substring(1, 3).Split('*');
                        //MessageBox.Show(resultsSKAN[0]);

                        if (resultsSKAN[0].Contains("SKT"))
                        {
                            var tmpSKAN = result.Substring(5, 10);
                            int tmpSQTY = int.Parse(result.Substring(16, 4));
                            txtSKAN.Text = tmpSKAN;
                            txtSQTY.Text = tmpSQTY.ToString();

                            //MessageBox.Show(txtSKAN.Text`);
                            //Form_Login._Form_LoginInstance.ScanStep = 2;
                            //filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\QTY.ini");
                            filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\QTY.ini");
                            ret = Setting.btScanLoadOCRConfig(filenameSet);
                            ans = 1;
                            btnConfirm.Enabled = true;

                        }
                        else
                        {
                            txtSKAN.Text = result;


                            FrmLogin._Form_LoginInstance.ScanStep = 2;
                            //filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\QTY.ini");
                            filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\QTY.ini");
                            ret = Setting.btScanLoadOCRConfig(filenameSet);
                            ans = 1;
                        }

                        break;

                    case 2://OCR Qty
                        txtSQTY.Text = result;
                        ans = 0;
                        btnConfirm.Enabled = true;
                        break;
                }
            }
            catch (Exception ex)
            {
                //call error form here
                ErrorForm err = new ErrorForm();
                err.Show();

            }
            return ans;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtSQTY.Text = "";
            txtSKAN.Text = "";
            //StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\KANBAN.ini");
            StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\KANBAN.ini");
            int ret = Setting.btScanLoadOCRConfig(filenameSet);
            SetScanStatus(0, 1, true);
            btnConfirm.Enabled = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtPDS.Text = "";
            txtCQTY.Text = "";
            txtCKAN.Text = "";
            customerName = "";
            //StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT\\NoOCR.ini");
            StringBuilder filenameSet = new StringBuilder("\\FlashDisk\\BT_FILES\\drv1\\SKT_Stock\\NoOCR.ini");
            int ret = Setting.btScanLoadOCRConfig(filenameSet);
            SetScanStatus(0, 0, true);
            btnConfirm.Enabled = false;
        }

        private void Form_Loading_FG_Closed(object sender, EventArgs e)
        {

        }


        /////////////////////////////////////////////////////////////
        /// Open
        /////////////////////////////////////////////////////////////
        private int openDB()
        {
            if (dbIdx > 0) return -1;
            dbIdx = Bt.FileLib.SQLite.btSQLiteOpen(new StringBuilder(dbFilePath));
            if (dbIdx > 0)
            {
                return 0;
            }
            else
            {

                return -1;
            }
        }

        /////////////////////////////////////////////////////////////
        /// Close
        /////////////////////////////////////////////////////////////
        private int closeDB()
        {
            if (dbIdx <= 0) return -1;
            int ret = Bt.FileLib.SQLite.btSQLiteClose(dbIdx);
            dbIdx = 0;
            if (ret == 0)
            {

                return 0;
            }
            else
            {

                return -1;
            }
        }



        /////////////////////////////////////////////////////////////
        /// Insert
        /////////////////////////////////////////////////////////////
        private void insertRecordKB()
        {
            openDB();

            string tableName = "Record_KB";

            if (dbIdx <= 0) return;
            StringBuilder sql = new StringBuilder("INSERT INTO " + tableName + " VALUES('" + DateTime.Now.ToString("yyMMdd") + "', '" +
                                                                                                DateTime.Now.ToString("HHmmss") + "', '" +
                                                                                                FrmLogin._Form_LoginInstance.userName + "', '" +
                                                                                                FrmLogin._Form_LoginInstance.DevNO + "', '" +
                                                                                                txtPDS.Text + "', '" +
                                                                                                txtCKAN.Text + "', '" +
                                                                                                txtCQTY.Text + "', '" +
                                                                                                txtSKAN.Text + "', '" +
                                                                                                txtSQTY.Text + "', '" +
                                                                                                "OK" + "', '" +
                                                                                                "OK" + "', '" +

                                                                                                "-" + "');");
            int ret = Bt.FileLib.SQLite.btSQLiteExecute(dbIdx, sql);
            if (ret == 0)
            {

            }
            else
            {

            }

            closeDB();

            //InsertKB();


        }

        private Int32 InsertKB()
        {
            try
            {


                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt;
                String strSQL;

                strSQL = "EXEC sp_insert_KB @PDSNo = '" + txtPDS.Text
                     + "' , @HTID = '" + FrmLogin._Form_LoginInstance.DevNO + "' , @userName = '"
                     + FrmLogin._Form_LoginInstance.userName
                     + "' , @logDate = '" + DateTime.Now.ToString("yyMMdd") + "' , @logTime = '"
                     + DateTime.Now.ToString("HHmmss") + "' , @CustomerKB = '" + txtCKAN.Text
                     + "' , @CustomerQty = '" + txtCQTY.Text + "' , @SKTKB = '" + txtSKAN.Text
                     + "' , @SKTQTY = '" + txtSQTY.Text + "' , @ResultKB = '" + "OK"
                     + "' , @ResultQT = '" + "OK" + "' , @Approve = '" + "-"
                     + "' , @SKTKBSerial = '" + sysKanbanSerialNo
                     + "' , @customerName = '" + customerName + "'";

                objConn.ConnectionString = FrmLogin.strConnString;
                objConn.Open();
                objCmd.Connection = objConn;
                objCmd.CommandText = strSQL;
                objCmd.CommandType = CommandType.Text;


                objCmd.ExecuteNonQuery();

                objConn.Close();
                objConn = null;

                return 1;

            }
            catch (Exception)
            {
                return 3;
            }

        }


        /////////////////////////////////////////////////////////////
        /// CSV output
        /////////////////////////////////////////////////////////////
        private int outputCSVFile(string dbFileName, string outputPath)
        {
            int retcode = -1;
            int cIdx = 0;
            int cIdx2 = 0;
            int dbIdx = Bt.FileLib.SQLite.btSQLiteOpen(new StringBuilder(dbFilePath));
            if (dbIdx <= 0) return retcode;
            IntPtr data;
            data = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Char)) * (8192 + 1));
            string sep = ",";

            StringBuilder sql = new StringBuilder("SELECT name FROM sqlite_master WHERE type = 'table';");
            cIdx = Bt.FileLib.SQLite.btSQLiteCmdCreate(dbIdx);
            if (cIdx <= 0) goto FUNCEND;
            int ret = Bt.FileLib.SQLite.btSQLiteCmdSetCommandText(cIdx, sql);
            if (ret < 0) goto FUNCEND;
            ret = Bt.FileLib.SQLite.btSQLiteCmdExecuteReader(cIdx);
            if (ret < 0) goto FUNCEND;

            int read_ret = 0;
            do
            {
                read_ret = Bt.FileLib.SQLite.btSQLiteCmdRead(cIdx);
                if (read_ret < 0) goto FUNCEND;
                else if (read_ret == 1)
                {
                    ret = Bt.FileLib.SQLite.btSQLiteCmdGetValue(cIdx, 0, data, 8192);
                    if (ret != 0) goto FUNCEND;
                    string tableName = Marshal.PtrToStringUni(data);

                    sql = new StringBuilder("SELECT * FROM " + tableName + ";");
                    cIdx2 = Bt.FileLib.SQLite.btSQLiteCmdCreate(dbIdx);
                    if (cIdx2 <= 0) goto FUNCEND;
                    ret = Bt.FileLib.SQLite.btSQLiteCmdSetCommandText(cIdx2, sql);
                    if (ret < 0) goto FUNCEND;
                    ret = Bt.FileLib.SQLite.btSQLiteCmdExecuteReader(cIdx2);
                    if (ret < 0) goto FUNCEND;

                    System.IO.StreamWriter writer = null;
                    try
                    {
                        writer = new System.IO.StreamWriter(outputPath + tableName + ".csv", false, System.Text.Encoding.GetEncoding("Shift_JIS"));
                        do
                        {
                            ret = Bt.FileLib.SQLite.btSQLiteCmdRead(cIdx2);
                            if (ret < 0) goto FUNCEND;
                            else if (ret == 1)
                            {
                                StringBuilder line = new StringBuilder("");
                                int cnt = Bt.FileLib.SQLite.btSQLiteCmdGetValueCount(cIdx2);
                                for (int i = 0; i < cnt; i++)
                                {
                                    if (line.ToString() != "") line.Append(sep);
                                    Bt.FileLib.SQLite.btSQLiteCmdGetValue(cIdx2, i, data, 8192);
                                    line.Append("\"" + Marshal.PtrToStringUni(data) + "\"");
                                }
                                writer.WriteLine(line);
                            }
                        } while (ret == 1);
                    }
                    finally
                    {
                        if (writer != null) writer.Close();
                    }
                }
            } while (read_ret == 1);

            retcode = 0;

        FUNCEND:
            if (cIdx > 0) Bt.FileLib.SQLite.btSQLiteCmdDelete(dbIdx);
            if (cIdx2 > 0) Bt.FileLib.SQLite.btSQLiteCmdDelete(cIdx2);
            Marshal.FreeCoTaskMem(data);
            if (dbIdx > 0) Bt.FileLib.SQLite.btSQLiteClose(dbIdx);
            return retcode;
        }

        private void Form_Loading_FG_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode.ToString())
            {

                case "F1":
                    if (btnConfirm.Enabled == true)
                    {
                        btnConfirm_Click(sender, e);
                    }
                    
                    break;

                case "F2":
                    button1_Click(sender, e);                    
                    break;

                case "F3":
                    button2_Click(sender, e);
                    break;

                case "F4":
                    btnCancel_Click(sender, e);
                    break;
            }
        }




    }
}