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 System.Runtime.InteropServices;
using Bt.ScanLib;
using Bt;
using CsvReadWrite;
using System.Data.SqlClient;

namespace HT_SKT_Stock
{
    public partial class FrmStockCheck : Form
    {
        public static FrmStockCheck _Form_StockCheckInstance;
        public String AppPath = "";
        public List<List<string>> tableList;
        List<DisplayTable> lstTable = new List<DisplayTable>();
        private BtScanLib btScanLib;


        public FrmStockCheck()
        {
            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.FrmStockCheck_KeyDown);

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

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

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

            dataGrid1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmStockCheck_KeyDown);
        }

        void btScanLib_OnScan(string result)
        {
            string[] kanban = result.Split('|');
            string kanbanCode = "";
            string kanbanClass = "";
            string remainderClass = "";
            string partNoScan = "";
            string quantity = "0";
            string kanbanSerialNo = "";
            Int32 intCheck1 = 0;

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

                try
                {
                    intCheck1 = CheckStockScan(partNoScan);

                    if (intCheck1 == 2)
                    {
                        WarningErr1 err = new WarningErr1("Stock Not Found");
                        err.ShowDialog();

                        dataGrid1.Focus();

                        return;

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

                        dataGrid1.Focus();

                        return;
                    }

                }
                catch (Exception)
                {
                    
                    
                }

            }
        }

        private Int32 CheckStockScan(string partNo)
        {
            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;

                strSQL = "EXEC sp_get_Stock_Check_table @partNo = '" + partNo + "'";

                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)
                {
                    lstTable.Clear();

                    for (int i = 0; i <= dt.Rows.Count - 1; i++)
                    {
                        string strRow = dt.Rows[i]["DRow"].ToString();
                        string strLocationId = dt.Rows[i]["location_id"].ToString();
                        string strQty = dt.Rows[i]["sumqty"].ToString();
                        lblQtyAll.Text = dt.Rows[i]["allqty"].ToString();
                        
                        //strQty = strQty.Substring(0, strQty.Length - 3);
                        //lblQtyAll.Text = lblQtyAll.Text.Substring(0, lblQtyAll.Text.Length - 3);
                                               
                        lstTable.Add(new DisplayTable
                        {                            
                            DRow = strRow,                            
                            locationId = strLocationId,
                            QtySum = strQty,
                        });

                    }

                    var blist = new BindingList<DisplayTable>(lstTable);
                    dataGrid1.DataSource = blist;

                    InitialDataGrid();

                    txtPartsNo.Text = partNo;

                    dataGrid1.Focus();


                    return 1;
                }
                else
                {
                    return 2;
                }




            }
            catch (Exception)
            {

                return 3;

            }


        }

        private void InitialDataGrid()
        {
            int grid_width = 445;
            // hidden column and row header
            dataGrid1.ColumnHeadersVisible = true;
            dataGrid1.RowHeadersVisible = false;

            // set style
            DataGridTableStyle tableStyle = new DataGridTableStyle();
            tableStyle.MappingName = (new BindingList<DisplayTable>()).GetType().Name;

            // Column 0
            DataGridTextBoxColumn tbc0 = new DataGridTextBoxColumn();
            tbc0.Width = 30;
            tbc0.MappingName = "DRow";
            tbc0.HeaderText = "#";
            tableStyle.GridColumnStyles.Add(tbc0);

            // Column 1
            DataGridTextBoxColumn tbc1 = new DataGridTextBoxColumn();
            tbc1.Width = 105;
            tbc1.MappingName = "locationId";
            tbc1.HeaderText = "Location";
            tableStyle.GridColumnStyles.Add(tbc1);

            // Column 2
            DataGridTextBoxColumn tbc2 = new DataGridTextBoxColumn();
            tbc2.Width = 70;
            tbc2.MappingName = "QtySum";
            tbc2.HeaderText = "Qty";
            tableStyle.GridColumnStyles.Add(tbc2);
                       
            dataGrid1.TableStyles.Clear();
            dataGrid1.TableStyles.Add(tableStyle);


        }

        private void FrmStockCheck_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;

            _Form_StockCheckInstance = this;


            String path = this.GetType().Assembly.GetModules()[0].FullyQualifiedName;
            Int32 en = path.LastIndexOf("\\");
            AppPath = path.Substring(0, en);

            lblUser.Text = "User : " + FrmLogin._Form_LoginInstance.userName;

        }

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

            FrmStockMenu._Form_StockMenuInstance.Show();
            //Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }

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

                case "F1":
                    btnBack_Click(sender, e);
                    break;

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

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

            FrmMainMenu._Form_MainMenuInstance.Show();
            //Bt.ScanLib.Control.btScanDisable();
            //this.Hide();
            this.Close();
        }
    }
}