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 DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace amlak_keshavarzi
{
    public partial class tajhizat : DevComponents.DotNetBar.Metro.MetroForm
    {
        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\keshavarzi.mdf;Integrated Security=True");
        public tajhizat()
        {
            InitializeComponent();
        }


        private void add()
        {
            con.Open();
            string s1 = "insert into tajhizat(code,nametajhizat,tedad,price,datesakht,keshvar,sanad,datesabt,tel,mob,address)values('" + txt_code.Text + "','" + txt_nametajhizat.Text + "','" + txt_tedad.Text + "','" + txt_price.Text + "','" + txt_datesakht.Text + "','" + txt_keshvar.Text + "','" + cmb_sanad.Text + "','" + txt_datesabt.Text + "','" + txt_tel.Text + "','" + txt_mob.Text + "','" + txt_address.Text + "')";
            SqlCommand c = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید", "ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                clear();
            }
            con.Close();

        }


        private void edit()
        {
            con.Open();
            string s1 = "update tajhizat set code='" + txt_code.Text + "',nametajhizat='" + txt_nametajhizat.Text + "',tedad='" + txt_tedad.Text + "',price='" + txt_price.Text + "',datesakht='" + txt_datesakht.Text + "',keshvar='" + txt_keshvar.Text + "',sanad='" + cmb_sanad.Text + "',datesabt='" + txt_datesabt.Text + "',tel='" + txt_tel.Text + "',mob='" + txt_mob.Text + "',address='" + txt_address.Text + "' where code='" + txt_code.Text + "'";
            SqlCommand c = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید؟", "ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c.ExecuteNonQuery();
                MessageBox.Show("ویرایش شد");
                clear();
            }
            con.Close();

        }


        private void delete()
        {
            con.Open();
            string s1 = "delete from tajhizat where code='" + txt_code.Text + "'";
            SqlCommand c = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را حذف کنید؟", "حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
                clear();
            }
            con.Close();
        }


        private void clear()
        {
            txt_code.Text = "";
            txt_nametajhizat.Text = "";
            txt_tedad.Text = "";
            txt_price.Text = "";
            txt_datesakht.Text = "";
            txt_keshvar.Text = "";
            cmb_sanad.Text = "";
            txt_datesabt.Text = "";
            txt_tel.Text = "";
            txt_mob.Text = "";
            txt_address.Text = "";
        }


        private void search()
        {
            con.Open();
            string s1 = "select * from tajhizat where code='" + txt_code.Text + "'";
            SqlCommand c = new SqlCommand(s1, con);
            SqlDataReader dr = c.ExecuteReader();
            if (dr.Read())
            {
                txt_code.Text = dr["code"].ToString();
                txt_nametajhizat.Text = dr["nametajhizat"].ToString();
                txt_tedad.Text = dr["tedad"].ToString();
                txt_price.Text = dr["price"].ToString();
                txt_datesakht.Text = dr["datesakht"].ToString();
                txt_keshvar.Text = dr["keshvar"].ToString();
                cmb_sanad.Text = dr["sanad"].ToString();
                txt_datesabt.Text = dr["datesabt"].ToString();
                txt_tel.Text = dr["tel"].ToString();
                txt_mob.Text = dr["mob"].ToString();
                txt_address.Text = dr["address"].ToString();
            }
            con.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            add();
        }

        private void txt_code_TextChanged(object sender, EventArgs e)
        {
            search();
        }

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

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            delete();
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            main m = new main();
            m.Show();
            this.Close();
        }

        public string txt_codeText { get; set; }
    }
}
