using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DaroKhaneh
{
    public partial class message : Form
    {
        public message()
        {
            InitializeComponent();
        }

        private void txtmessage_TextChanged(object sender, EventArgs e)
        {
            if (lblcheck.Text.ToString() == "0")
            {
                txtmessage.Text = "";
                lblcheck.Text = "1";
            }
            else
                return;
        }

        private void btnsend_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "insert into message (uname,suname,message,date,time,subject) values (@uname,@suname,@message,@date,@time,@subject)";
            com.Parameters.AddWithValue("@uname", txtuser.Text);
            com.Parameters.AddWithValue("@suname", login.uname);
            com.Parameters.AddWithValue("@message", txtmessage.Text);
            com.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());
            com.Parameters.AddWithValue("@time", DateTime.Now.ToLongTimeString());
            com.Parameters.AddWithValue("@subject", txtsubject.Text);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
        }

        private void message_Load(object sender, EventArgs e)
        {
            loadmessage("select id,suname,message,date,time,subject,readmessage from message where uname='" + login.uname + "'");
            if (login.uname == "admin")
                cb1.Items.Add("پیام های ربات");
        }

        public void loadmessage(string query)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = query;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = com;

            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[6].Visible = false;
            changeColorAndRead();
        }

        private void changeColorAndRead()
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[1].Value.ToString() == "admin")
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Pink;
                if ((bool)dataGridView1.Rows[i].Cells[6].Value == false)
                {
                    dataGridView1.CurrentRow.DefaultCellStyle.Font=new Font("tahoma",8,FontStyle.Bold);
                }
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "update message set readmessage=" + 1 + " where id=" + dataGridView1.CurrentRow.Cells[0].Value;

                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                dataGridView1.CurrentRow.DefaultCellStyle.Font=new Font("tahoma",8,FontStyle.Regular);
            }
            catch (Exception)
            {
                MessageBox.Show("خطایی در ارتباط با بانک رخ داده است","بروز خطا",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
                txtsname.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                txtshowmessage.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                txtdate.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
                txttime.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
                txtsubjectshow.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void cb1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb1.SelectedIndex == 0)
                loadmessage("select id,suname,message,date,time,subject,readmessage from message where uname='" + login.uname + "'");
            else if (cb1.SelectedIndex == 1)
                loadmessage("select id,suname,message,date,time,subject,readmessage from message where suname='" + login.uname + "'");
            else if(cb1.SelectedIndex == 2)
                loadmessage("select id,suname,message,date,time,subject,readmessage from message where suname like '%Robot_%'");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtmessage.Text = "";
            txtmessage.Select();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "delete from message where id=" + dataGridView1.CurrentRow.Cells[0].Value.ToString();

            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        }

        private void txtuser_TextChanged(object sender, EventArgs e)
        {
            if (txtuser.Text == string.Empty)
                label7.Visible = true;
            else label7.Visible = false;
        }

        private void txtsubject_TextChanged(object sender, EventArgs e)
        {
            if (txtsubject.Text == string.Empty)
                label8.Visible = true;
            else label8.Visible = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }


    }
}
