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.OleDb;

namespace car
{
    public partial class userfrm : Form
    {
        static OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=cardb.mdb;jet oledb:Database Password=☺☻♥♦♣♠");
        OleDbDataAdapter da = new OleDbDataAdapter("", con);
        DataSet ds = new DataSet();

        public userfrm()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (textBoxX1.Text == "" || textBoxX2.Text == "")
            {
                MessageBox.Show("پرشون کن");
            }
            else
            {
                string s;
                if (comboBoxEx1.SelectedIndex == 0)
                    s = "admin";
                else
                    s = "user";

                ds.Clear();
                da.SelectCommand.CommandText = "select id from login where id='" + textBoxX2.Text + "'";
                da.Fill(ds, "d1");
                if (ds.Tables["d1"].Rows.Count > 0)
                {
                    DialogResult d = MessageBox.Show("این رکورد قبلا ثبت شده است ", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxX1.Clear();
                    textBoxX2.Clear();

                }
                else
                {
                    DataBase db = new DataBase();
                    db.DoCommand("insert into login(type,users,id) values('" + s + "','" + textBoxX1.Text + "','" + textBoxX2.Text + "')");
                    userfrm_Load(sender, e);
                    dataGridViewX1.CurrentCell = dataGridViewX1.Rows[dataGridViewX1.RowCount - 2].Cells[0];
                    MessageBox.Show("Inserted");
                }
        }
    }

        private void userfrm_Load(object sender, EventArgs e)
        {
                        comboBoxEx1.SelectedIndex = 0;
            DataTable dt = new DataTable();
            DataBase db = new DataBase();
            if (variable.i == 2)
            {
                buttonX1.Enabled = false;
                buttonX2.Enabled = false;
                buttonX3.Enabled = false;
                buttonX4.Enabled = false;
                dt = db.MySelect("select type,users from login");
                dataGridViewX1.DataSource = dt;
                dataGridViewX1.Columns[0].HeaderText = "دسترسی";
                dataGridViewX1.Columns[1].HeaderText = "نام کاربری";
            }
            else
            {

                dt = db.MySelect("select *from login");
                dataGridViewX1.DataSource = dt;
                dataGridViewX1.Columns[0].HeaderText = "دسترسی";
                dataGridViewX1.Columns[1].HeaderText = "نام کاربری";
                dataGridViewX1.Columns[2].HeaderText = "رمز عبور";
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            string s;
            if (comboBoxEx1.SelectedIndex == 0)
                s = "admin";
            else
                s = "user";
            DataBase db = new DataBase();
            db.DoCommand("update login set users='" + textBoxX1.Text + "',id='" + textBoxX2.Text + "',type='" + s + "' where users='" + textBoxX3.Text + "'");
            userfrm_Load(sender, e);
            MessageBox.Show("Updated"); 
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("are you sure to delete?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                DataBase db = new DataBase();
                db.DoCommand("delete from login where users='" + textBoxX3.Text + "'");
                userfrm_Load(sender, e);
                MessageBox.Show("Deleted");
            }
            else { }
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            string str = "select * from login where ";
            if (textBoxX3.Text != "") str += "users='" + textBoxX3.Text + "' and ";
            if (str == "select * from login where ")
                str = "select * from login";
            else
                str = str.Remove(str.Length - 4, 4);
            DataTable dt = new DataTable();
            DataBase db = new DataBase();
            dt = db.MySelect(str);
            MessageBox.Show(dt.Rows.Count.ToString() + " Rows founded.");
            dataGridViewX1.DataSource = dt;

        }
    }
}
