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 RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }
        private static string connstring = "Data Source=.;Initial Catalog=MyQQ;Integrated Security=SSPI";
        public static SqlConnection connection = new SqlConnection(connstring);
        private void btnRegister_Click(object sender, EventArgs e)
        {
            int BloodType = 0;
            int Star = 0; 
            string sex = null;            
            if (rdoBoy.Checked == true)
            {
                sex = rdoBoy.Text;
            }
            else if (rdoGirl.Checked == true)
            {
                sex = rdoGirl.Text;
            }
            try
            {               
                if (txtPassword.Text.Trim() == txtReplyPassword.Text.Trim() && txtPassword.Text.Trim() != null)
                {                  
                    string sql = string.Format("select Id from Star where Star='{0}'", cboStar.Text.Trim());
                    SqlCommand command = new SqlCommand(sql, connection);
                    connection.Open();
                    SqlDataReader dataReader = command.ExecuteReader();
                    if (dataReader.Read())
                    {
                        Star = (int)dataReader["Id"];
                    }
                    dataReader.Close();
                    sql = string.Format("select Id from BloodType where BloodType='{0}'", cboBlood.Text.Trim());
                    command.CommandText = sql;
                    SqlDataReader Reader = command.ExecuteReader();
                    if (Reader.Read())
                    {
                        BloodType = (int)Reader["Id"];
                    }
                    Reader.Close();
                    int age = 0;
                    if (Convert.ToInt32(txtAge.Text) < 0 || Convert.ToInt32(txtAge.Text) > 100)
                    {
                        MessageBox.Show("年龄不符合规范！", "提示");
                        txtAge.Focus();
                    }
                    else
                    {
                        age = Convert.ToInt32(txtAge.Text);

                        //当姓名、血型、星座不为空时的Sql语句
                        if (!(txtName.Text.Trim().Equals("")) && !(cboBlood.Text.Trim().Equals("")) && !(cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (NickName,UserState,Age,Sex,LoginPwd,Name,StarId,BloodTypeId,FriendshipPolocyId,skinId) values ('{0}',{1},{2},'{3}','{4}','{5}',{6},{7},{8},{9})",
                            txtName.Text.Trim(), 2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), Star, BloodType, 1, 0);
                        }
                        //当姓名为空而血型、星座不为空时的Sql语句
                        else if ((txtName.Text.Trim().Equals("")) && !(cboBlood.Text.Trim().Equals("")) && !(cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (UserState,Age,Sex,LoginPwd,Name,StarId,BloodTypeId,FriendshipPolocyId,skinId) values ({0},{1},'{2}','{3}','{4}',{5},{6},{7},{8})",
                            2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), Star, BloodType, 1, 0);
                        }
                        //当姓名、血型为空而星座不为空时Sql语句
                        else if ((txtName.Text.Trim().Equals("")) && (cboBlood.Text.Trim().Equals("")) && !(cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (UserState,Age,Sex,LoginPwd,Name,StarId,FriendshipPolocyId,skinId) values ({0},{1},'{2}','{3}','{4}',{5},{6},{7})",
                            2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), Star, 1, 0);
                        }
                        //当姓名、血型、星座都为空时的Sql语句
                        else if ((txtName.Text.Trim().Equals("")) && (cboBlood.Text.Trim().Equals("")) && (cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (UserState,Age,Sex,LoginPwd,Name,FriendshipPolocyId,skinId) values ({0},{1},'{2}','{3}','{4}',{5},{6})",
                            2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), 1, 0);
                        }
                        //当姓名、星座不为空而血型为空时的Sql语句
                        else if (!(txtName.Text.Trim().Equals("")) && (cboBlood.Text.Trim().Equals("")) && !(cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (NickName,UserState,Age,Sex,LoginPwd,Name,StarId,FriendshipPolocyId,skinId) values ('{0}',{1},{2},'{3}','{4}','{5}',{6},{7},{8})",
                            txtName.Text.Trim(), 2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), Star, 1, 0);
                        }
                        //当姓名、血型不为空时而星座为空时的Sql语句
                        else if (!(txtName.Text.Trim().Equals("")) && !(cboBlood.Text.Trim().Equals("")) && (cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (NickName,UserState,Age,Sex,LoginPwd,Name,BloodTypeId,FriendshipPolocyId,skinId) values ('{0}',{1},{2},'{3}','{4}','{5}',{6},{7},{8})",
                            txtName.Text.Trim(), 2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), BloodType, 1, 0);
                        }
                        //当姓名不为空而星座、血型为空时的Sql语句
                        else if (!(txtName.Text.Trim().Equals("")) && (cboBlood.Text.Trim().Equals("")) && (cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (NickName,UserState,Age,Sex,LoginPwd,Name,FriendshipPolocyId,skinId) values ('{0}',{1},{2},'{3}','{4}','{5}',{6},{7})",
                            txtName.Text.Trim(), 2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), 1, 0);
                        }
                        //当姓名、星座为空而血型不为空时的Sql语句
                        else if ((txtName.Text.Trim().Equals("")) && !(cboBlood.Text.Trim().Equals("")) && (cboStar.Text.Trim().Equals("")))
                        {
                            //把填写的信息插入数据库中的Sql语句
                            sql = string.Format("insert into Users (UserState,Age,Sex,LoginPwd,Name,BloodTypeId,FriendshipPolocyId,skinId) values ({0},{1},'{2}','{3}','{4}',{5},{6},{7})",
                            2, age, sex, txtPassword.Text.Trim(), txtTureName.Text.Trim(), BloodType, 1, 0);
                        }
                        command.CommandText = sql;

                        int result = command.ExecuteNonQuery();

                        if (result == 1)
                        {
                            sql = "select @@Identity from Users";
                            command.CommandText = sql;
                            int MyQQNum = Convert.ToInt32(command.ExecuteScalar());
                            string message = string.Format("注册成功！你的MyQQ号码是{0}", MyQQNum);
                            MessageBox.Show(message, "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtName.Text = "";
                            txtTureName.Text = "";
                            txtAge.Text = "";
                            txtPassword.Text = "";
                            txtReplyPassword.Text = "";
                            cboBlood.Text = "";
                            cboStar.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("抱歉！注册失败", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("密码和重复密码不相同！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtPassword.Text = "";
                    txtReplyPassword.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }

        //注册界面的加载事件
        private void RegisterForm_Load(object sender, EventArgs e)
        {
            string sql = string.Format("select Star from Star");
            try
            {
                SqlCommand command = new SqlCommand(sql, connection);
                connection.Open();
                SqlDataReader dataReader = command.ExecuteReader();
                while (dataReader.Read())
                {
                    cboStar.Items.Add((string)dataReader["Star"]);
                }
                dataReader.Close();
                sql = string.Format("select BloodType from BloodType");
                command.CommandText = sql;
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    cboBlood.Items.Add((string)reader["BloodType"]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }
        //关闭当前页面
        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}