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 MyQQ
{
    public partial class LoginForm : Form
    {
        int i = 1;
        public LoginForm()
        {
            InitializeComponent();
        }
        DataBase mydatabase = new DataBase();
        //连接注册号码的事件
        private void lkblshenqianhaoma_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RegisterForm registerForm = new RegisterForm();
            registerForm.Show();
        }

        //连接找回密码的事件
        private void lkblPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            searchPwdForm searchPwd = new searchPwdForm();
            searchPwd.Show();
        }

        //提示输入完整的方法
        private bool ValidateInput()
        {
           if (txtQQNum.Text.Trim() == "")
            {
                MessageBox.Show("请输入QQ号码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtQQNum.Focus();

                return false;
            }
            else
                if (txtPwd.Text.Trim() == "")
            {
                MessageBox.Show("请输入QQ密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPwd.Focus();

                return false;
            }
            else
            {
                return true;
            }
        }

        //判断用户是否存在的方法
        private bool ValidateUser(string txtQQNum, string txtPwd, ref string message)
        {
            int count = 0;

            bool isValidUser = false;
            string sql = string.Format("select count(*) from Users where Id='{0}'and LoginPwd='{1}'", txtQQNum, txtPwd);
            try
            {
                count = (int)mydatabase.GetScalar(sql);
                if (count < 1)
                {
                    message = "用户密码不存在！";

                    isValidUser = false;
                }
                else
                {
                    isValidUser = true;
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;

                Console.WriteLine(ex.Message);
            }
           
            return isValidUser;
        }
        //关闭登录页面
        private void btlCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //登录按钮的处理事件
        private void btnLoin_Click_1(object sender, EventArgs e)
        {
            bool isValiDUser = false;

            string message = "";

            if (ValidateInput())
            {
                isValiDUser = ValidateUser(txtQQNum.Text, txtPwd.Text, ref message);

                if (isValiDUser)
                {
                    int userstate = 0;
                    string sql = string.Format("select UserState from Users where Id={0}", Convert.ToInt32(txtQQNum.Text.Trim()));

                    SqlDataReader dataReader = mydatabase.GetDataReader(sql);
                    if (dataReader.Read())
                    {
                        userstate = Convert.ToInt32(dataReader["UserState"]);
                    }

                    dataReader.Close();

                    if (userstate == 2)
                    {
                        string sql2 = string.Format("Update Users set UserState={0} where Id={1}",i,Convert.ToInt32(txtQQNum.Text.Trim()));

                        int result = mydatabase.ExecuteSQL(sql2);
                        string sql3 = string.Format("delete Messages where (MessageStation=0 and ToUserId={0} and MessageState=1) or MessageTypeId=3", Convert.ToInt32(txtQQNum.Text.Trim()));
                         int result1=mydatabase.ExecuteSQL(sql3);
                        UserHelper.Loginld = txtQQNum.Text;
                        MainForm mainForm = new MainForm();
                        mainForm.Show();
                        this.Visible = false;
                    }
                    else
                    {
                        string messge = string.Format("在其它地方{0}已登录，不能重复登录！",txtQQNum.Text.ToString().Trim());
                        MessageBox.Show(messge, "登录提示");
                    }

                }
                else
                {
                    MessageBox.Show(message, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        //初始皮肤
        private void LoginForm_Load(object sender, EventArgs e)
        {
            this.skinEngine1.SkinFile = "MP10.ssk";
        }

        //获得登录状态的事件
        private void tsmiStations_Click(object sender, EventArgs e)
        {
            i = Convert.ToInt32(((ToolStripMenuItem)sender).Tag);
        }
    }
}