using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace projectcsharp
{
    public partial class frm_users : Form
    {
        public frm_users()
        {
            InitializeComponent();
        }
        public SqlConnection con = new SqlConnection("server=.;database=resturan;Integrated Security=True");
        byte case_save = 0;

        private void Enabled_true()
        {
            btn_new.Enabled = true;
            btn_save.Enabled = false;
            btn_edit.Enabled = true;
            btn_del.Enabled = true;
            btn_back.Enabled = true;
            btn_cancel.Enabled = false;
        }
        private void Enabled_false()
        {
            btn_new.Enabled = false;
            btn_save.Enabled = true;
            btn_edit.Enabled = false;
            btn_del.Enabled = false;
            btn_back.Enabled = false;
            btn_cancel.Enabled = true;
        }

        private void clr_txt()
        {
            foreach (var c in this.Controls)
            {
                if (c is TextBox)
                {
                    (c as TextBox).Clear();
                }
            }

        }
        private void show_info()
        {
            SqlDataAdapter da = new SqlDataAdapter("select id_user as 'کد' ,username_ as 'نام کاربری',pass as 'رمزورود',limit as 'کاربر محدود' from tbl_users", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dtg_users.DataSource = dt;

        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            case_save = 0;
            clr_txt();
            Enabled_false();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            byte temp = case_save;
            if ((txt_username.Text == "") || (txt_pas.Text == ""))
            {
                MessageBox.Show("اطلاعات کاربری ثبت نشد", "!خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                case_save = 3;
            }
            string sts_limit = "0";
            if (rbt_admin.Checked == true)
            {
                sts_limit = "0";
            }
            else
            {
                sts_limit = "1";
            }
            if (case_save == 0)
            {

                SqlCommand cmd = new SqlCommand("insert into tbl_users (username_,pass,limit) values (N'" + txt_username.Text + "',N'" + txt_pas.Text + "'," + sts_limit + ")", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                show_info();
                MessageBox.Show("اطلاعات کاربری با موفقیت ثبت شد", ".ثبت شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Enabled_true();
                clr_txt();

            }
            else if (case_save == 1)
            {
                SqlCommand cmd = new SqlCommand("update tbl_users set  username_ =N'" + txt_username.Text + "',pass = N'" + txt_pas.Text + "',limit= " + sts_limit + " where id_user='" + dtg_users[0, dtg_users.CurrentRow.Index].Value.ToString() + "'", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                show_info();
                MessageBox.Show("اطلاعات کاربری با موفقیت بروز رسانی شد", ".بروز رسانی شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Enabled_true();
                clr_txt();
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            case_save = 1;
            Enabled_false();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult ans = MessageBox.Show(" حذف شود؟   " + dtg_users[1, dtg_users.CurrentRow.Index].Value.ToString() + " " + dtg_users[2, dtg_users.CurrentRow.Index].Value.ToString() + " ", "حذف شود؟", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (ans == DialogResult.Yes)
                {
                    SqlCommand cmd = new SqlCommand("delete from tbl_users where id_user='" + dtg_users[0, dtg_users.CurrentRow.Index].Value.ToString() + "'", con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    show_info();
                    MessageBox.Show("اطلاعات کاربری با موفقیت حذف شد", ".حذف شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Enabled_true();
                    clr_txt();
                }
            }
            catch
            {
            }

            Enabled_true();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            clr_txt();
            Enabled_true();
        }

        private void frm_users_Load(object sender, EventArgs e)
        {
            Enabled_true();
            show_info();
        }

        private void dtg_users_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_username.Text = dtg_users[1, dtg_users.CurrentRow.Index].Value.ToString();
            txt_pas.Text = dtg_users[2, dtg_users.CurrentRow.Index].Value.ToString();
        }
    }
}
