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_f : Form
    {
        public frm_f()
        {
            InitializeComponent();
        }
        byte case_save = 0;
        public SqlConnection con = new SqlConnection("server=.;database=resturan;Integrated Security=True");

        private void show_info()
        {
            SqlDataAdapter da = new SqlDataAdapter("select id_food as 'کد' ,name as 'نام',pri as 'قیمت' from tbl_food", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;

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

        }
        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 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_name.Text == "") || (txt_price.Text == ""))
            {
                MessageBox.Show("اطلاعات غذا ثبت نشد", "!خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                case_save = 3;
            }
            if (case_save == 0)
            {
                SqlCommand cmd = new SqlCommand("insert into tbl_food values (N'" + txt_name.Text + "'," + txt_price.Text + ")", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                show_info();
                MessageBox.Show("اطلاعات غذا با موفقیت ثبت شد", ".ثبت شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Enabled_true();
                clr_txt();
            }
            if (case_save == 1)
            {
                SqlCommand cmd = new SqlCommand("update tbl_food set  name =N'" + txt_name.Text + "',pri = N'" + txt_price.Text + "' where id_food='" + dataGridView1[0, dataGridView1.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 res = MessageBox.Show(" حذف شود؟   " + dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString() + " " + dataGridView1[2, dataGridView1.CurrentRow.Index].Value.ToString() + " ", "حذف شود؟", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    SqlCommand cmd = new SqlCommand("delete from tbl_food where id_food='" + dataGridView1[0, dataGridView1.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 btn_back_Click(object sender, EventArgs e)
        {
            Close();
        }

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

        private void txt_name_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                SqlDataAdapter da = new SqlDataAdapter("select id_food as 'کد' ,name as 'نام' , pri as 'قیمت' from tbl_food where name like N'" + txt_name.Text + "%'", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_name.Text = dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();

            txt_price.Text = dataGridView1[2, dataGridView1.CurrentRow.Index].Value.ToString();
        }

        private void txt_name_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((char.IsDigit(e.KeyChar) == false)))
            {
                e.Handled = true;
            }
        }

        private void txt_price_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((char.IsDigit(e.KeyChar) == false) && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Delete))
            {
                e.Handled = true;
            }
        }

        private void txt_price_Enter(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo fa;
            fa = InputLanguage.InstalledInputLanguages[1].Culture;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(fa);
        }

        private void txt_name_Enter(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo fa;
            fa = InputLanguage.InstalledInputLanguages[1].Culture;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(fa);
        }

        private void txt_price_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                SqlDataAdapter da = new SqlDataAdapter("select id_food as 'کد' ,name as 'نام' , pri as 'قیمت' from tbl_food where pri like N'" + txt_price.Text + "%'", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
