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 check
{
    public partial class users : Form
    {
        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\check.mdf;Integrated Security=True");

        private void FillCheckes()
        {
            txt_code.Text = dgv_users.CurrentRow.Cells[0].Value.ToString();
            txt_user.Text = dgv_users.CurrentRow.Cells[1].Value.ToString();
            txt_pass.Text = dgv_users.CurrentRow.Cells[2].Value.ToString();
            ch_account.Checked = Convert.ToBoolean(dgv_users.CurrentRow.Cells[3].Value.ToString());
            ch_bank.Checked = Convert.ToBoolean(dgv_users.CurrentRow.Cells[4].Value.ToString());
            ch_ckeck.Checked = Convert.ToBoolean(dgv_users.CurrentRow.Cells[5].Value.ToString());
            ch_ckecklist.Checked = Convert.ToBoolean(dgv_users.CurrentRow.Cells[6].Value.ToString());
            ch_remider.Checked = Convert.ToBoolean(dgv_users.CurrentRow.Cells[7].Value.ToString());
            ch_users.Checked = Convert.ToBoolean(dgv_users.CurrentRow.Cells[8].Value.ToString());
        }

        public users()
        {
            InitializeComponent();
        }


        private void clear()
        {
            txt_code.Clear();
            txt_user.Clear();
            txt_pass.Clear();
            ch_account.Checked = false;
            ch_ckecklist.Checked = false;
            ch_bank.Checked = false;
            ch_ckeck.Checked = false;
            ch_remider.Checked = false;
            ch_users.Checked = false;
        }

        private void show()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from users";
            adp.Fill(ds, "t1");
            dgv_users.DataSource = ds;
            dgv_users.DataMember = "t1";
        }

        private void add()
        {
            con.Open();
            string s1 = "insert into users(code,username,password,account,bank,ckeck,ckecklist,remider,users)values('" + txt_code.Text + "','" + txt_user.Text + "','" + txt_pass.Text + "','" + ch_account.Checked + "','" + ch_bank.Checked + "','" + ch_ckeck.Checked + "','" + ch_ckecklist.Checked + "','" + ch_remider.Checked + "','" + ch_users.Checked + "')";
            SqlCommand c1 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید", "ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c1.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                show();

            }

            con.Close();

        }

        private void edit()
        {
            con.Open();
            string s2 = "update users set code='" + txt_code.Text + "',username='" + txt_user.Text + "',password='" + txt_pass.Text + "',account='" + ch_account.Checked + "',bank='" + ch_bank.Checked + "',ckeck='" + ch_ckeck.Checked + "',ckecklist='" + ch_ckecklist.Checked + "',remider='" + ch_remider.Checked + "',users='" + ch_users.Checked + "' where code='" + txt_code.Text + "'";
            SqlCommand c2 = new SqlCommand(s2, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید", "ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c2.ExecuteNonQuery();
                MessageBox.Show("ویرایش شد");
                show();

            }

            con.Close();

        }

        private void delete()
        {
            if (dgv_users.Rows.Count == 1)
            {
                MessageBox.Show("باید حداقل یک کاربر داشته باشید");
                return;
            }
            else
            {
                con.Open();
                string s3 = "delete from users where code='" + dgv_users.CurrentRow.Cells[0].Value.ToString() + "'";
                SqlCommand c4 = new SqlCommand(s3, con);
                if (MessageBox.Show("آیا می خواهید این را حذف کنید", "حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    c4.ExecuteNonQuery();
                    MessageBox.Show("حذف شد");
                    show();

                }

                con.Close();
            }
        }


        private void dgv_users_MouseClick(object sender, MouseEventArgs e)
        {
            FillCheckes();
        }

        private void users_Load(object sender, EventArgs e)
        {
            show();
            FillCheckes();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            add();
            clear();
            txt_code.Focus();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
            clear();
            txt_code.Focus();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            delete();
            clear();
            txt_code.Focus();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            clear();
            txt_code.Focus();
            
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            show();
            clear();
            txt_code.Focus();
        }

    }
}
