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 costcostzamin : DevComponents.DotNetBar.Metro.MetroForm
    {
        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\keshavarzi.mdf;Integrated Security=True");
       
        public costcostzamin()
        {
            InitializeComponent();
        }

        private void add()
        {
            con.Open();
            string s1 = "insert into costzamin(code,abyari,kod,pricekargar,bazr,sam,other,sumall,name,datesabt)values('" + txt_code.Text + "','" + txt_abyari.Text + "','" + txt_kod.Text + "','" + txt_pricekargar.Text + "','" + txt_bazr.Text + "','" + txt_sam.Text + "','" + txt_other.Text + "','" + txt_sumall.Text + "','" + txt_name.Text + "','" + txt_datesabt.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 costzamin set code='" + txt_code.Text + "',abyari='" + txt_abyari.Text + "',kod='" + txt_kod.Text + "',pricekargar='" + txt_pricekargar.Text + "',bazr='" + txt_bazr.Text + "',sam='" + txt_sam.Text + "',other='" + txt_other.Text + "',sumall='" + txt_sumall.Text + "',name='" + txt_name.Text + "',datesabt='" + txt_datesabt.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 costzamin 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_codeText = "";
            txt_abyari.Text = "0";
            txt_kod.Text = "0";
            txt_pricekargar.Text = "0";
            txt_bazr.Text = "0";
            txt_sam.Text = "0";
            txt_other.Text = "0";
            txt_sumall.Text = "0";
            txt_name.Text = "";
            txt_datesabt.Text = "";
            
        }

        private void search()
        {
            con.Open();
            string s1 = "select * from costzamin 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_abyari.Text = dr["abyari"].ToString();
                txt_kod.Text = dr["kod"].ToString();
                txt_pricekargar.Text = dr["pricekargar"].ToString();
                txt_bazr.Text = dr["bazr"].ToString();
                txt_sam.Text = dr["sam"].ToString();
                txt_other.Text = dr["other"].ToString();
                txt_sumall.Text = dr["sumall"].ToString();
                txt_datesabt.Text = dr["datesabt"].ToString();
                txt_name.Text = dr["name"].ToString();
               
            }
            con.Close();
        }

        public void sumall()
        {
            try
            {


                txt_sumall.Text = (Int32.Parse(txt_abyari.Text) + Int32.Parse(txt_kod.Text) + Int32.Parse(txt_pricekargar.Text) + Int32.Parse(txt_bazr.Text) + Int32.Parse(txt_sam.Text) + Int32.Parse(txt_other.Text)).ToString();

            }

            catch (Exception)
            {

            }
        }

        public string txt_codeText { get; set; }

        private void txt_abyari_TextChanged(object sender, EventArgs e)
        {
            sumall(); 
        }

        public string txt { get; set; }

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

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

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

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

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

        private void costcostzamin_Load(object sender, EventArgs e)
        {
            txt_abyari.Text = "0";
            txt_kod.Text = "0";
            txt_pricekargar.Text = "0";
            txt_bazr.Text = "0";
            txt_sam.Text = "0";
            txt_other.Text = "0";
            txt_sumall.Text = "0";

         
        }

        private void txt_kod_TextChanged(object sender, EventArgs e)
        {
            sumall();
        }

        private void txt_pricekargar_TextChanged(object sender, EventArgs e)
        {
            sumall();
        }

        private void txt_bazr_TextChanged(object sender, EventArgs e)
        {
            sumall();
        }

        private void txt_sam_TextChanged(object sender, EventArgs e)
        {
            sumall();
        }

        private void txt_other_TextChanged(object sender, EventArgs e)
        {
            sumall();
        }

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