using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using MESClassLibrary.BLL.BasicInfo;

namespace InjectionSearch
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void Login()
        {
            string stationID = "";
            Program.station = ConfigurationManager.AppSettings["Station"].ToString();
            Program.StationID = ConfigurationManager.AppSettings["StationID"].ToString();
            OpenDb();

            #region 判断输入合法性

            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
                return;
            }

            if (textBox2.Text.Trim() == "")
            {
                MessageBox.Show("密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
                return;
            }

            #endregion

            StationBLL sbll = new StationBLL();
            DataTable sdt = sbll.SearchInfoByNo(Program.station);
            if (sdt != null && sdt.Rows.Count > 0)
            {
                stationID = sdt.Rows[0]["StationID"].ToString();
            }

            sdt.Dispose();

            Program.OperatorName = textBox1.Text.Trim();

            OperatorBLL bll = new OperatorBLL();

            DataTable dt = bll.SearchInfoByNameAndPsw(textBox1.Text.Trim(), stationID, textBox2.Text.Trim());
            if (dt != null && dt.Rows.Count > 0)
            {
                Form fr = new FrmBarCodeSearch2408();
                fr.Show();
               this.Hide();
            }
            else
            {
            }
        }

        private bool OpenDb()
        {
            bool OpenDb = false;

            string value = ConfigurationManager.ConnectionStrings["SqlConnString"].ToString();
            Program.DBConn = new SqlConnection(value);
            if (Program.DBConn.State.ToString().ToUpper() == "OPEN") Program.DBConn.Close();
            try
            {
                Program.DBConn.Open();
            }
            catch (Exception Err)
            {
                if (Err != null)
                {
                    MessageBox.Show("数据库连接失败,请检查网络连接,并重新连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return OpenDb;
                }
            }

            OpenDb = true;
            return OpenDb;
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Login();
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            var serverUrl = "http://10.60.101.59:8013/";
            var updateXmlFileName = "InjectionSearchUpdate.xml";
            var updater = new AutoUpdater();
            if (updater.CheckUpdateLoad(serverUrl, updateXmlFileName))
            {
                Environment.Exit(0);
            }
        }
    }
}