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 WindowsApplication25
{
    public partial class set : Form
    {
        DataBase db = new DataBase();      
        private int friendshipPolicyId = 0; //身份验证
        private SqlCommand cmd;
        private SqlDataReader dr;
        private string sql = "";
        public set()
        {
            InitializeComponent();
        }
        //页面加载初始化事件，显示个人用户的基本信息
        private void set_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "MSN.ssk";//加载皮肤美化文件
            ShowInfo();      //把个人信息显示到窗体上方法，代码见下
            ShowBloodType(); //显示血型方法，代码见下
            ShowStar();      //显示星座方法，代码见下
        }
        //主界面“取消”按钮的鼠标单击事件，主要用于关闭窗体。
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
        //主界面“确定”按钮的鼠标单击事件，主要用于将用户填写的个人资料更新到数据库
        private void btnOk_Click(object sender, EventArgs e)
        {
            int starId = GetStar();//GetStar()方法获取星座Id编号，代码见下
            int bloodType = GetBloodType();//血型
            if (rdoAll.Checked == true)
                friendshipPolicyId = 1;
            else if (rdoVlidate.Checked == true)
                friendshipPolicyId = 2;
            else if (rdoNotAll.Checked == true)
                friendshipPolicyId = 3;
            //Validates()方法主要用于判别用户输入信息是否合法，代码见下。
            if (Validates())
            {
                sql = string.Format("UPDATE Users SET LoginPwd = '{0}',FriendshipPolicyId = {1},NickName = '{2}',FaceId={3}, Sex='{4}',Age={5},Name='{6}',StarId={7},BloodTypeId={8} WHERE Id={9}",
                txtPwd2.Text.Trim(), friendshipPolicyId, txtId.Text.Trim(),ID.faceId, cboSex.Text.Trim(), txtAge.Text.Trim(), txtName.Text.Trim(), starId, bloodType, ID.Id);
                cmd = new SqlCommand(sql, db.connection);
                try
                {
                    db.open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    int result = cmd.ExecuteNonQuery();
                    if (result < 1)
                        MessageBox.Show("更新个人信息失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                        MessageBox.Show("服务器已经接受你的请求！", "操作结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    db.close();
                }
            }
        }
        /// <summary>
        /// 获取星座Id
        /// </summary>
        /// <returns>返回整数类型：获取星座编号</returns>
        public int GetStar()
        {
            int starId = 0;
            sql = string.Format("select Id from Star where Star = '{0}'", cboStar.Text);
            try
            {   db.open();
                cmd = new SqlCommand(sql, db.connection);
               
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    starId = (int)dr["Id"];
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.close();
            }
            return starId;
        }
        /// <summary>
        /// 获取血型Id
        /// </summary>
        /// <returns>返回整数类型：获取血型Id编号</returns>
        public int GetBloodType()
        {
            int bloodTypeId = 0;
            sql = string.Format("select Id from BloodType where BloodType = '{0}'", cboBoodType.Text);
            try
            {   db.open();
                cmd = new SqlCommand(sql,db.connection);
                
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    bloodTypeId = (int)dr["Id"];
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.open();
            }
            return bloodTypeId;
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        /// <returns>返回布尔类型：验证用户输入信息的合法性</returns>
        private bool Validates()
        {
            if (txtId.Text.Trim() == "")
            {
                MessageBox.Show("请输入昵称,同学!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (cboSex.Text == "")
            {
                MessageBox.Show("不要忘了选性别哦,同学!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (txtAge.Text == "")
            {
                MessageBox.Show("你忘了写年龄啦,同学!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else if (txtPwd2.Text.Trim() != txtPwd3.Text.Trim())
            {
                MessageBox.Show("二次输入密码不一致,请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPwd2.Text = "";
                txtPwd3.Text = "";
                txtPwd2.Focus();
                return false;
            }
            else if (txtPwd1.Text != "" && txtPwd2.Text == "" && txtPwd3.Text == "")
            {
                txtPwd2.Text = txtPwd1.Text;
                return true;
            }
            return true;
        }
        /// <summary>
        /// 把个人信息显示到窗体上
        /// </summary>
        public void ShowInfo()
        {
            sql = string.Format("SELECT u.Id,u.NickName,u.FaceId,u.Sex,u.Age,u.Name,b.BloodType,s.Star,u.LoginPwd,u.FriendshipPolicyId FROM Users AS u INNER JOIN BloodType AS b ON(u.BloodTypeId=b.Id) INNER JOIN Star AS s ON(u.StarId=s.Id) WHERE u.Id={0}",
            ID.Id);
            db.open();
            cmd = new SqlCommand(sql, db.connection);
            try
            {
                
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtNumber.Text = dr[0].ToString();       //QQ号码
                    txtId.Text = dr[1].ToString();      //昵称
                    picPhoto.Image = ilPic.Images[(int)dr[2]];       //头像
                    cboSex.Text = dr[3].ToString();     //性别
                    txtAge.Text = dr[4].ToString();     //年龄
                    txtPwd1.Text = dr[8].ToString();
                    friendshipPolicyId = (int)dr[9];        //身份验证

                    if (!(dr[5] is DBNull))
                        txtName.Text = dr[5].ToString();        //真实姓名
                    if (!(dr[7] is DBNull))
                        cboStar.Text = dr[7].ToString();        //星座
                    if (!(dr[6] is DBNull))
                        cboBoodType.Text = dr[6].ToString();        //血型
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.close();
            }
            switch (friendshipPolicyId)
            {
                case 1:
                    rdoAll.Checked = true;
                    break;
                case 2:
                    rdoVlidate.Checked = true;
                    break;
                case 3:
                    rdoNotAll.Checked = true;
                    break;
            }
        }
        /// <summary>
        /// 显示血型方法
        /// </summary>
        private void ShowBloodType()
        {   db.open();
            sql = "SELECT BloodType FROM BloodType";
            cmd = new SqlCommand(sql, db.connection);
            
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cboBoodType.Items.Add(dr[0].ToString());
            }
            dr.Close();
        }
        /// <summary>
        /// 显示星座方法
        /// </summary>
        private void ShowStar()
        {   db.open();
            sql = "SELECT Star FROM Star";
            cmd = new SqlCommand(sql,db.connection);
            
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cboStar.Items.Add(dr[0].ToString());
            }
            dr.Close();
        }
        //鼠标左键单击按钮“个人资料”，触发单击事件
        private void button1_Click(object sender, EventArgs e)
        {
            pnlPerson.Visible = true;           //使个人资料可见
            pnlSecurity.Visible = false;        //使安全设置隐藏
        }
        //鼠标左键单击按钮“安全设置”，触发单击事件。
        private void button2_Click(object sender, EventArgs e)
        {
            pnlSecurity.Visible = true;           //使个人资料隐藏
            pnlPerson.Visible = false;            //使安全设置可见
        }
        //在pnlPerson面板中，鼠标左键单击点击“>>”按钮时候触发
        private void btnSelect_Click(object sender, EventArgs e)
        {   //打开头像窗体
            choosefaces frm = new choosefaces();
            frm.ShowDialog();
        }
        //每当窗体被激活时的事件
        private void set_Activated_1(object sender, EventArgs e)
        {   
            picPhoto.Image = ilPic.Images[ID.faceId];
        }
    }
}
