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 logon : Form
    {
        public logon()
        {
            InitializeComponent();
        }

        private void logon_Load(object sender, EventArgs e)
        {
            setshow("select * from logon");
            dataGridView1.Columns[0].HeaderText = "آی دی";
            dataGridView1.Columns[1].HeaderText = "نام سیستم";
            dataGridView1.Columns[2].HeaderText = "تاریخ تلاش برای ورود";
            dataGridView1.Columns[2].Width = 150;
            dataGridView1.Columns[3].HeaderText = "ساعت";
            dataGridView1.Columns[4].HeaderText = "وضعیت";
            dataGridView1.Columns[4].Width = 50;
            dataGridView1.Columns[5].HeaderText = "نام کاربری";
            dataGridView1.Columns[6].HeaderText = "کلمه عبور";
        }
        void setguery(string comandtext)
        {
            SqlConnection con=new SqlConnection();
            con.ConnectionString="server=(local);database=darokhane;trusted_connection=yes";

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

            con.Open();
            com.ExecuteNonQuery();
            con.Close();
        }
        void setshow(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;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[5].Value.ToString() == "admin")
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Pink;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            setguery("delete from logon");
            logon_Load(sender, e);
        }

        private void txtsearch_TextChanged(object sender, EventArgs e)
        {
            setshow("select * from logon where systemname like '%" + txtsearch.Text + "%'");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            setguery("delete from logon where id="+dataGridView1.CurrentRow.Cells[0].Value);
            logon_Load(sender, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void csort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (csort.SelectedIndex==0)
                dataGridView1.Sort(dataGridView1.Columns["id"], ListSortDirection.Ascending);
            if (csort.SelectedIndex == 1)
                dataGridView1.Sort(dataGridView1.Columns["systemname"], ListSortDirection.Ascending);
            if (csort.SelectedIndex == 2)
                dataGridView1.Sort(dataGridView1.Columns["date"], ListSortDirection.Ascending);
            if (csort.SelectedIndex == 3)
                dataGridView1.Sort(dataGridView1.Columns["time"], ListSortDirection.Ascending);
            if (csort.SelectedIndex == 4)
                dataGridView1.Sort(dataGridView1.Columns["vaziat"], ListSortDirection.Ascending);
            if (csort.SelectedIndex == 5)
                dataGridView1.Sort(dataGridView1.Columns["uname"], ListSortDirection.Ascending);
        }

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

        private void حذفگزارشانتخابیToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button2_Click(sender, e);
        }

        private void حذفهمهگزارشهاToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1 && e.Button == MouseButtons.Right)
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
