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_c : Form
    {
        public frm_c()
        {
            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_customer as 'کد' ,name as 'نام' , family as [نام خانوادگی],codemeli as [کد ملی], tell as 'تلفن',address as 'آدرس' from tbl_customer", 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 frm_c_Load(object sender, EventArgs e)
        {
            show_info();
            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_family.Text == "") || (txt_tell.Text == "" )|| (txt_codemeli .Text == ""))
            {
                MessageBox.Show("اطلاعات مشتری ثبت نشد", "!خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                case_save = 3;
            }
            if (case_save == 0)
            {
                SqlCommand cmd = new SqlCommand("insert into tbl_customer (name,family,tell,codemeli,address) values (N'" + txt_name.Text + "',N'" + txt_family.Text + "',N'" + txt_tell.Text + "',N'" + txt_codemeli.Text + "',N'" + txt_address.Text + "' )", 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_customer set  name =N'" + txt_name.Text + "',family = N'" + txt_family.Text + "',tell=N'" + txt_tell.Text + "',codemeli =N'" + txt_codemeli.Text + "',address =N'" + txt_address.Text + "' where id_customer='" + 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();
            }
            if (case_save == 3)
                case_save = temp;
        }

        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_customer where id_customer='" + 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 txt_name_Enter(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo fa;
            fa = InputLanguage.InstalledInputLanguages[1].Culture;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(fa);
        }

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

        private void txt_family_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                string sqlcomand = "select id_customer as 'کد' ,name as 'نام' , family as [نام خانوادگی], tell as 'تلفن',codemeli as [کد ملی],address as 'آدرس' from tbl_customer where family like N'" + txt_family.Text + "%'  ";
                SqlDataAdapter da = new SqlDataAdapter(sqlcomand, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void txt_codemeli_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                string sqlcomand = "select id_customer as 'کد' ,name as 'نام' , family as [نام خانوادگی], tell as 'تلفن',codemeli as [کد ملی],address as 'آدرس' from tbl_customer where codemeli like N'" + txt_codemeli.Text + "%'  ";
                SqlDataAdapter da = new SqlDataAdapter(sqlcomand, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void txt_tell_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                string sqlcomand = "select id_customer as 'کد' ,name as 'نام' , family as [نام خانوادگی], tell as 'تلفن',codemeli as [کد ملی],address as 'آدرس' from tbl_customer where tell like N'" + txt_tell.Text + "%'  ";    
                SqlDataAdapter da = new SqlDataAdapter(sqlcomand, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void txt_tell_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_codemeli_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_family_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!((char.IsDigit(e.KeyChar) == false)))
            {
                e.Handled = true;
            }
        }

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

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_name.Text = dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();
            txt_family.Text = dataGridView1[2, dataGridView1.CurrentRow.Index].Value.ToString();
            txt_tell.Text = dataGridView1[3, dataGridView1.CurrentRow.Index].Value.ToString();
            txt_codemeli.Text = dataGridView1[4, dataGridView1.CurrentRow.Index].Value.ToString();
            txt_address.Text = dataGridView1[5, dataGridView1.CurrentRow.Index].Value.ToString();
        }

        private void txt_name_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                string sqlcomand = "select id_customer as 'کد' ,name as 'نام' , family as [نام خانوادگی], tell as 'تلفن',codemeli as [کد ملی],address as 'آدرس' from tbl_customer where name like N'" + txt_name.Text + "%'  ";
                SqlDataAdapter da = new SqlDataAdapter(sqlcomand, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void ch_like_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void txt_address_TextChanged(object sender, EventArgs e)
        {
            if (ch_like.Checked == true)
            {
                string sqlcomand = "select id_customer as 'کد' ,name as 'نام' , family as [نام خانوادگی], tell as 'تلفن',codemeli as [کد ملی],address as 'آدرس' from tbl_customer where address like N'" + txt_address.Text + "%'  ";
                SqlDataAdapter da = new SqlDataAdapter(sqlcomand, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

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

    }
}
