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

        private void add()
        {
            con.Open();
            string s1 = "insert into costtajhizat(code,nametajhizat,tedad,price,tamir,ghataat,stehlak,bimh,sokt,others,samall)values('" + txt_code.Text + "','" + txt_nametajhizat.Text + "','" + txt_tedad.Text + "','" + txt_price.Text + "','" + txt_tamir.Text + "','" + txt_ghataat.Text + "','" + txt_stehlak.Text + "','" + txt_bimh.Text + "','" + txt_sokt.Text + "','" + txt_others.Text + "','"+txt_samall.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 costtajhizat set code='" + txt_code.Text + "',nametajhizat='" + txt_nametajhizat.Text + "',tedad='" + txt_tedad.Text + "',price='" + txt_price.Text + "',tamir='" + txt_tamir.Text + "',ghataat='" + txt_ghataat.Text + "',stehlak='" + txt_stehlak.Text + "',bimh='" + txt_bimh.Text + "',sokt='" + txt_sokt.Text + "',others='" + txt_others.Text + "','"+txt_samall.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 costtajhizat 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 = "0";
            txt_price.Text = "0";
            txt_tamir.Text = "0";
            txt_ghataat.Text = "0";
            txt_stehlak.Text = "0";
            txt_bimh.Text = "0";
            txt_sokt.Text = "0";
            txt_others.Text = "0";
            txt_samall.Text = "0";

        }


        private void search()
        {
            con.Open();
            string s1 = "select * from costtajhizat 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_tamir.Text = dr["tamir"].ToString();
                txt_ghataat.Text = dr["ghataat"].ToString();
                txt_stehlak.Text = dr["stehlak"].ToString();
                txt_bimh.Text = dr["bimh"].ToString();
                txt_sokt.Text = dr["sokt"].ToString();
                txt_others.Text = dr["others"].ToString();
                txt_samall.Text = dr["samall"].ToString();

            }
            con.Close();
        }


        public void sumall()
        {
            try
            {


                txt_samall.Text = (Int32.Parse(txt_tedad.Text) * Int32.Parse(txt_price.Text) + Int32.Parse(txt_tamir.Text) + Int32.Parse(txt_ghataat.Text) + Int32.Parse(txt_stehlak.Text) + Int32.Parse(txt_bimh.Text) + Int32.Parse(txt_sokt.Text) + Int32.Parse(txt_others.Text)).ToString();

            }

            catch (Exception)
            {

            }
        }

        public string txt_codeText { get; set; }

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

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

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

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

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

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

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

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

        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 costtajhizat_Load(object sender, EventArgs e)
        {
           
            txt_tedad.Text = "0";
            txt_price.Text = "0";
            txt_tamir.Text = "0";
            txt_ghataat.Text = "0";
            txt_stehlak.Text = "0";
            txt_bimh.Text = "0";
            txt_sokt.Text = "0";
            txt_others.Text = "0";
            txt_samall.Text = "0";
        }

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

    }
}
