using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace yinLouSys
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if ("" == txtUser.Text)
                {
                    MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtUser.Focus();
                    return;
                }

                if ("" == txtPwd.Text)
                {
                    MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtPwd.Focus();
                    return;
                }

                string sql = "select password from tab_user where userName=@user";
                SqlParameter[] parm ={ new SqlParameter("@user", txtUser.Text) };

                object obj=code.SQLHelp.ExecuteScalar(sql, parm);

                if (obj == null || !obj.Equals(code.Base.StringToMD5Hash(txtPwd.Text)))
                {
                    MessageBox.Show("您输入的用户名或密码不正确", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                string hostname;
                System.Net.IPHostEntry localhost;
                System.Net.IPAddress localaddr;

                hostname = System.Net.Dns.GetHostName();
                localhost = System.Net.Dns.GetHostByName(hostname);
                localaddr = localhost.AddressList[0]; //localaddr中就是本机ip地址 

                code.LogInfor bean = new yinLouSys.code.LogInfor();
                bean.UserName = txtUser.Text;
                bean.Ip = localaddr.ToString();
                bean.OperType = "用户登录";
                bean.Discrible = "计算机名“" + hostname + "”  IP地址“" + localaddr.ToString() + "”";

                code.LogMan.AddLog(bean);

                code.SQLHelp.ExecuteNoResult("update tab_user set ip='" + localaddr.ToString() + "',state='1'  where userName='" + txtUser.Text + "'");

                frmMain frm = new frmMain();
                frm.UserName = txtUser.Text;
                frm.Show();

                this.Hide();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}