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 FrmWarningLeader : Form
    {
        private BtScanLib btScanLib;
        public List<List<string>> tableList;

        public FrmWarningLeader(string strWarning1, string strWarning2, string strWarning3)
        {
            InitializeComponent();

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

            lblWarning1.Text = strWarning1;
            lblWarning2.Text = strWarning2;
            lblWarning3.Text = strWarning3;

            if (strWarning1 == "")
            {
                lblWarning1.Visible = false;
            }
            if (strWarning2 == "")
            {
                lblWarning2.Visible = false;
            }
            if (strWarning3 == "")
            {
                lblWarning3.Visible = false;
            }
        }

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

            lblError.Visible = false;
            lblError.Text = string.Empty;

        }

        void btScanLib_OnScan(string result)
        {
            lblError.Visible = false;
            string userLeader = result;
            Int32 intCheck = CheckLeaderScan(userLeader);

            if (intCheck == 1)
            {
                //******* disable BTScanLib
                btScanLib.OnScan -= btScanLib_OnScan;
                btScanLib.Disable();
                this.Close();
            }
            else if (intCheck == 2)
            {
                lblError.Visible = true;
                lblError.Text = "Scan Leader Incorrect";

            }
            else if (intCheck == 3)
            {
                lblError.Visible = true;
                lblError.Text = "Error Database Connection";

            }
            
        }

        private Int32 CheckLeaderScan(string userLeader)
        {
            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_User_Leader @userID = '" + userLeader
                    + "'";

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

                if (dt.Rows.Count > 0)
                {
                    return 1;
                }
                else
                {
                    return 2;
                }


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

            }
            catch (Exception)
            {
                return 3;

            }


        }


    }
}