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 searchPwdForm : Form
    {
        public searchPwdForm()
        {
            InitializeComponent();
        }
        DataBase mydatabase = new DataBase();
        private void btnSearchPwd_Click(object sender, EventArgs e)
        {
            int starId = 0;
            int bloodId = 0;
            if (ValidateInput())
            {
                string sql = string.Format("select Id from Star where Star='{0}'", cboStar.Text.Trim());
                SqlDataReader dataReader = mydatabase.GetDataReader(sql);
                if (dataReader.Read())
                {
                    starId = Convert.ToInt32(dataReader["Id"]);
                }
                dataReader.Close();
                string sql1 = string.Format("select Id from BloodType where BloodType='{0}'", cboBlood.Text.Trim());
                SqlDataReader dataReader1 = mydatabase.GetDataReader(sql1);
                if (dataReader1.Read())
                {
                    bloodId = Convert.ToInt32(dataReader1["Id"]);
                }
                dataReader1.Close();
                string sql2 = string.Format("select LoginPwd from Users where BloodTypeId={0} and StarId={1} and Name='{2}' and Age={3} and Id={4}", bloodId, starId, txtName.Text.Trim(), txtAge.Text.Trim(),Convert.ToInt32(txtNum.Text.Trim()));
                SqlDataReader dataReader2 = mydatabase.GetDataReader(sql2);
                if (dataReader2.HasRows)
                {
                    if (dataReader2.Read())
                    {
                        string messgae = string.Format("你的密码是：{0}", dataReader2["LoginPwd"].ToString());
                        MessageBox.Show(messgae, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    dataReader2.Close();
                }
                else
                {
                    MessageBox.Show("你输入的信息不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                txtNum.Text = "";
                txtAge.Text = "";
                txtName.Text = "";
                cboStar.Text = "";
                cboBlood.Text = "";
            }
        }

        //编写信息的方法
        private bool ValidateInput()
        {
            if (txtNum.Text.Trim() == "") 
            {
                MessageBox.Show("请输入MyQQ号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            else if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("请输入姓名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            else if (txtAge.Text.Trim() == "")
            {
                MessageBox.Show("请输入年龄！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtAge.Focus();
                return false;
            }
            else if (cboStar.Text.Trim() == "")
            {
                MessageBox.Show("请输入星座！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cboStar.Focus();
                return false;
            }
            else if (cboBlood.Text.Trim() == "")
            {
                MessageBox.Show("请输入血型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cboBlood.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        //关闭找回密码窗体
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //窗体的加载事件
        private void searchPwdForm_Load(object sender, EventArgs e)
        {
            string sql = string.Format("select Id,Star from Star");
            try
            {
                SqlDataReader dataReader = mydatabase.GetDataReader(sql);
                while (dataReader.Read())
                {
                    cboStar.Items.Add((string)dataReader["Star"]);
                }
                dataReader.Close();
                sql = string.Format("select Id,BloodType from BloodType");
                SqlDataReader reader = mydatabase.GetDataReader(sql);
                while (reader.Read())
                {
                    cboBlood.Items.Add((string)reader["BloodType"]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}