using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Runtime.InteropServices;
using MESClassLibrary.BLL.BasicInfo;
using MESClassLibrary.Model;
using MESClassLibrary.BLL.Log;

namespace InjectionPC
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Program.station = ConfigurationManager.AppSettings["Station"].ToString();
            Program.IP = ConfigurationManager.AppSettings["IP"].ToString();
            Program.RemoteIP = ConfigurationManager.AppSettings["RemoteIP"].ToString();
            Program.PicturePath = ConfigurationManager.AppSettings["PicturePath"].ToString();

            Program.WeightFolder = ConfigurationManager.AppSettings["WeightFolder"].ToString();
            Program.WeightFile = ConfigurationManager.AppSettings["WeightFile"].ToString();
            Program.WeightUser = ConfigurationManager.AppSettings["WeightUser"].ToString();
            Program.WeightPsw = ConfigurationManager.AppSettings["WeightPsw"].ToString();

            Program.interVal = Convert.ToInt32(ConfigurationManager.AppSettings["InterVal"].ToString());
            if (DateTime.Now.Hour >= 8 && DateTime.Now.Hour < 20)
            {
                Program.Shift = "A班";
                Program.ProductDate = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {
                Program.Shift = "B班";
                if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 8)
                {
                    Program.ProductDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                }
                Program.ProductDate = DateTime.Now.ToString("yyyy-MM-dd");
            }

            OpenDb();
            string stationID="";
            #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)
            {
                this.Visible = false;
                Form fr=new FrmBarCode();
                fr.Show();
                
            }
            else
            {

            }
            dt.Dispose();
        }

        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 button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            ShowInputPanel();
        }

        private const Int32 WM_SYSCOMMAND = 274;

        private const UInt32 SC_CLOSE = 61536;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        private static extern int RegisterWindowMessage(string lpString);



        //显示屏幕键盘

        public static int ShowInputPanel()
        {

            try
            {

                dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";

                if (!System.IO.File.Exists(file))

                    return -1;

                Process.Start(file);

                //return SetUnDock(); //不知SetUnDock()是什么,所以直接注释返回1

                return 1;

            }

            catch (Exception)
            {

                return 255;

            }

        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            ShowInputPanel();
        }

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

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}