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

        private void rbt_pri_CheckedChanged(object sender, EventArgs e)
        {
            if (rbt_azta.Checked == true)
            {
                txt_ta.Visible = true;
                lbl_az.Visible = true;
                lbl_ta.Visible = true;
            }
            else
            {
                txt_ta.Visible = false;
                lbl_az.Visible = false;
                lbl_ta.Visible = false;
            }
        }

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

        private void txt_code_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_pri_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_ta_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 frm_rep_foods_Load(object sender, EventArgs e)
        {
            if (rbt_azta.Checked == true)
            {
                txt_ta.Visible = true;
                lbl_az.Visible = true;
                lbl_ta.Visible = true;
            }
            else
            {
                txt_ta.Visible = false;
                lbl_az.Visible = false;
                lbl_ta.Visible = false;
            }
        }

        private void rbt_azta_CheckedChanged(object sender, EventArgs e)
        {
            if (rbt_azta.Checked == true)
            {
                txt_ta.Visible = true;
                lbl_az.Visible = true;
                lbl_ta.Visible = true;
            }
            else
            {
                txt_ta.Visible = false;
                lbl_az.Visible = false;
                lbl_ta.Visible = false;
            }
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this.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 btn_search_Click_1(object sender, EventArgs e)
        {
            string str = "";
            bool bol = false;

            if (txt_code.Text != "")
            {
                str += "id_food = '" + txt_code.Text + "' ";
                bol = true;
            }
            if (txt_name.Text != "")
            {
                if (bol == true)
                    str += "and name= N'" + txt_name.Text + "' ";
                else
                    str += "name= N'" + txt_name.Text + "' ";
                bol = true;
            }
            if (rbt_azta.Checked == true)
            {
                if (txt_pri.Text != "" && txt_ta.Text != "")
                {
                    if (bol == true)
                        str += "and pri >= '" + txt_pri.Text + "' and pri <= '" + txt_ta.Text + "' ";
                    else
                        str += " pri >= '" + txt_pri.Text + "' and pri <= '" + txt_ta.Text + "' ";
                }
                bol = true;
            }
            else if (rbt_pri.Checked == true)
            {
                if (txt_pri.Text != "")
                {
                    if (bol == true)
                        str += " and pri = '" + txt_pri.Text + "' ";
                    else
                        str += " pri = '" + txt_pri.Text + "' ";
                }
                bol = true;
            }
            try
            {
                str = "SELECT id_food AS کد, name AS نام, pri AS قیمت from tbl_food where " + str;
                SqlDataAdapter da = new SqlDataAdapter(str, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            catch
            {
            }
        }
    }
}
