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 message : Form
    {   DataBase db = new DataBase();
        
        int fromUserId;  // 发起请求的用户Id
        public message()
        {
            InitializeComponent();
        }

        private void message_Load(object sender, EventArgs e)
        {
            int messageId = 0; // 消息的Id

            // 找到发给当前用户的请求消息
            string sql = string.Format(
                "SELECT Top 1 Id, FromUserId FROM Messages WHERE ToUserId={0} AND MessageTypeId=2 AND MessageState=0",
               ID.Id);
            try
            {
                // 查找一个未读消息
               SqlDataReader dataReader = db.GetDataReader(sql);
           
                if (dataReader.Read())
                {
                    messageId = (int)dataReader["Id"];
                    this.fromUserId = (int)dataReader["FromUserId"];
                }
                dataReader.Close();

                // 将消息状态置为已读
                sql = "UPDATE Messages SET MessageState =1 WHERE Id=" + messageId;
                db.mycom.CommandText = sql;
                db.mycom.ExecuteNonQuery();

                // 读取请求人的信息，显示在窗体上
                sql = "SELECT NickName, FaceId FROM Users WHERE Id=" + this.fromUserId;

                 dataReader = db.GetDataReader(sql);
                if (dataReader.Read())
                {
                    lblMessage.Text = string.Format("{0}({1})请求把你加为好友.", dataReader[0].ToString(), fromUserId);
                    picFace.Image = ilFaces.Images[(int)dataReader[1]];
                }
                else
                {
                    this.lblMessage.Text = "没有系统消息";
                    this.button1.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);;
            }
            finally
            {
                db.close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 先查找是否已经添加过了，防止重复添加
            string sql = string.Format(
                "SELECT COUNT(*) FROM Friends WHERE HostId={0} AND FriendId={1}",
                this.fromUserId,ID.Id);
            try
            {   db.open();
                SqlCommand cmd = new SqlCommand(sql, db.connection);

                int result = (int)cmd.ExecuteScalar();
                if (result != 1)
                {
                    string addSql = string.Format("INSERT INTO Friends(HostId,FriendId)VALUES({0},{1})", fromUserId, ID.Id);
                    cmd.CommandText = addSql;      //重新制定Sql语句
                    cmd.ExecuteNonQuery();
                }
                else
                    MessageBox.Show("对方已经是你的好友,不能进行重复添加!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
            finally
            {
                db.close();
            }
            this.Close();  // 关闭窗体
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}