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 PersonalInForm : Form
    {
        public PersonalInForm()
        {
            InitializeComponent();
        }
        DataBase mydatabase = new DataBase();
        private void btnPerson_Click(object sender, EventArgs e)
        {
            pnlPerson.Visible = true;
            pnlanquan.Visible = false;
        }
        private void btnAnquan_Click(object sender, EventArgs e)
        {
            //显示安全设置面板
            pnlanquan.Visible = true;
            //显示个人信息面板
            pnlPerson.Visible = false;
            //个人确定按钮隐藏
            btnOk.Visible = false;
            //安全确定按钮显示
            btnOK1.Visible = true;
            //安全确定按钮占用个人确定按钮的位置
            btnOK1.Location = btnOk.Location;
        }

        //显示皮肤的方法
        public void ShowSkin()
        {
            int i = 0;
            string sql = string.Format("select skinId from Users where Id={0}", UserHelper.Loginld);
            try
            {
                SqlDataReader dataReader = mydatabase.GetDataReader(sql);
                if (dataReader.Read())
                {
                    i = Convert.ToInt32(dataReader["skinId"]);
                }
                dataReader.Close();
                if (i == 1)
                {
                    this.skinEngine.SkinFile = "Wave.ssk";
                }
                else if (i == 2)
                {
                    this.skinEngine.SkinFile = "WaveColor1.ssk";
                }
                else if (i == 3)
                {
                    this.skinEngine.SkinFile = "WaveColor1.ssk";
                }
                else if (i == 4)
                {
                    this.skinEngine.SkinFile = "SportsBlack.ssk";
                }
                else if (i == 5)
                {
                    this.skinEngine.SkinFile = "SportsBlue.ssk";
                }
                else if (i == 6)
                {
                    this.skinEngine.SkinFile = "SportsCyan.ssk";
                }
                else if (i == 7)
                {
                    this.skinEngine.SkinFile = "SportsGreen.ssk";
                }
                else
                {
                    this.skinEngine.SkinFile = "MP10.ssk";
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void PersonalInForm_Load(object sender, EventArgs e)
        {
            ShowSkin();
            btnOk.Visible = true;
            Person();
            anquan();
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            string sql = null;

            int count = 0;

            int Blood = 0;　　

            int star = 0;   

            if (cboBlood.Text.Trim().Equals(""))
            {
                Blood = 1;
            }
            else
            {
                sql = string.Format("select Id from BloodType where BloodType='{0}'", cboBlood.Text.Trim());
                SqlDataReader Reader = mydatabase.GetDataReader(sql);
                if (Reader.Read())
                {
                    Blood = (int)Reader["Id"];
                }
                Reader.Close();
            }
            if (cboStar.Text.Trim().Equals(""))
            {
                star = 1;
            }
            else
            {
                sql = string.Format("select Id from Star where Star='{0}'", cboStar.Text.Trim());
                SqlDataReader data = mydatabase.GetDataReader(sql);
                if (data.Read())
                {
                    star = (int)data["Id"];
                }
                data.Close();
            }

            if (Convert.ToInt32(txtAge.Text.Trim()) > 0 && Convert.ToInt32(txtAge.Text.Trim()) < 100)
            {
                sql = string.Format("Update Users set NickName='{0}',Sex='{1}',Age={2},Name='{3}',BloodTypeId={4},StarId={5} where Id={6}", txtNickName.Text, cboSex.Text, txtAge.Text, txtName.Text, Blood, star, Convert.ToInt32(UserHelper.Loginld));
                count = mydatabase.ExecuteSQL(sql);
                if (count < 1)
                {
                    MessageBox.Show("对不起，服务器没有接受你的资料！", "提示");
                }
                else
                {
                    MessageBox.Show("服务器接受了你的资料！", "提示");
                }
            }
            else 
            {
                MessageBox.Show("年龄不符合规范！！！","年龄提示");
            }
        }

        //链接图像页面
        private void btnPicture_Click(object sender, EventArgs e)
        {
            FacesForm faces = new FacesForm();
            faces.Person = this;
            faces.Show();
        }

        //获得图像索引
        public void picture(int index)
        {
            PicFace.Image = imageList1.Images[index];
        }

        //安全面板
        private void anquan()
        {
            string sql = string.Format("select LoginPwd,FriendshipPolocyId from Users where Id={0}", Convert.ToInt32(UserHelper.Loginld));
            try
            {
                SqlDataReader dataReader = mydatabase.GetDataReader(sql);
                if (dataReader.Read())
                {
                    txtOldPwd.Text = (string)dataReader["LoginPwd"];
                    if (Convert.ToInt32(rdo1.Tag) == (int)dataReader["FriendshipPolocyId"])
                    {
                        rdo1.Checked = true;
                    }
                    else if (Convert.ToInt32(rdo2.Tag) == (int)dataReader["FriendshipPolocyId"])
                    {
                        rdo2.Checked = true;
                    }
                    else if (Convert.ToInt32(rdo3.Tag) == (int)dataReader["FriendshipPolocyId"])
                    {
                        rdo3.Checked = true;
                    }
                }
                dataReader.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //个人信息面板
        private void Person()
        {
            string sql = string.Format("select FaceId from Users where Id={0}", Convert.ToInt32(UserHelper.Loginld));
            try
           {
               SqlDataReader dataRead = mydatabase.GetDataReader(sql);
                while (dataRead.Read())
                {
                    if (dataRead["FaceId"] is DBNull)
                    {
                        PicFace.Image = imageList1.Images[0];
                    }
                    else
                    {
                        PicFace.Image = imageList1.Images[(int)dataRead["FaceId"]];
                    }
                }
                dataRead.Close();
                txtUserId.Text = UserHelper.Loginld.ToString();
                sql = string.Format("select NickName,Sex,Name,Age from Users  where  Id={0}", Convert.ToInt32(UserHelper.Loginld));
                SqlDataReader data = mydatabase.GetDataReader(sql);
                if (data.Read())
                {
                    txtNickName.Text = data["NickName"].ToString();
                    cboSex.Text = data["Sex"].ToString();
                    txtName.Text = data["Name"].ToString();
                    txtAge.Text = data["Age"].ToString();
                }
                data.Close();
                sql = string.Format("select Star from Star");
                SqlDataReader dataR = mydatabase.GetDataReader(sql);
                while (dataR.Read())
                {
                    cboStar.Items.Add((string)dataR["Star"]);
                }
                dataR.Close();
                sql = string.Format("select BloodType from BloodType");
                SqlDataReader reader = mydatabase.GetDataReader(sql);
                while (reader.Read())
                {
                    cboBlood.Items.Add((string)reader["BloodType"]);
                }
                reader.Close();
                int statID = 0;
                sql = "select StarId FROM Users";
                SqlDataReader REDE = mydatabase.GetDataReader(sql);
                if (REDE.Read())
                {
                    statID = (int)REDE[0];
                }
                REDE.Close();
                sql = string.Format("select  Star from Users AS u inner JOIN Star as S on u.StarId=S.Id where u.Id={0}", Convert.ToInt32(UserHelper.Loginld));
                SqlDataReader StarA = mydatabase.GetDataReader(sql);
                if (StarA.Read())
                {
                    cboStar.Text = StarA["Star"].ToString();
                }
                StarA.Close();
                sql = string.Format("select BloodType from Users AS u inner JOIN BloodType as B on u.BloodTypeId=B.Id where u.Id={0}", Convert.ToInt32(UserHelper.Loginld));
                SqlDataReader StarB = mydatabase.GetDataReader(sql);
                if (StarB.Read())
                {
                    cboBlood.Text = StarB["BloodType"].ToString();
                }
                StarB.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        //关闭编辑个人信息窗口
        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //安全确定事件
        private void btnOK1_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (txtNewPwd.Text.Trim() == txtQNewPwd.Text.Trim())
            {
                int friendshipPolocyId = 0;
                if (rdo1.Checked == true)
                {
                    friendshipPolocyId = Convert.ToInt32(rdo1.Tag);
                }
                else if (rdo2.Checked == true)
                {
                    friendshipPolocyId = Convert.ToInt32(rdo2.Tag);
                }
                else
                {
                    friendshipPolocyId = Convert.ToInt32(rdo3.Tag);
                }
                string sql = null;
                if (txtNewPwd.Text.Trim() != "")
                {
                    sql = string.Format("update Users set LoginPwd='{0}',FriendshipPolocyId={1} where Id={2}", txtNewPwd.Text.Trim().ToString(), friendshipPolocyId, Convert.ToInt32(UserHelper.Loginld));
                }
                else
                {
                    sql = string.Format("update Users set FriendshipPolocyId={0} where Id={1}", friendshipPolocyId, Convert.ToInt32(UserHelper.Loginld));
                }
                try
                {
                    result = mydatabase.ExecuteSQL(sql);
                    if (result == 1)
                    {
                        MessageBox.Show("服务器接受了你的请求！", "提示");
                    }
                    else
                    {
                        MessageBox.Show("很抱歉！服务器暂时不能接受你请求！", "提示");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("确认密码与新密码不匹配！", "提示");
            }
        }
    }
}