using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DabirKhane
{
    public partial class groh : Form
    {

        public void tohi()
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
        public groh()
        {
            InitializeComponent();
        }

        private void groh_Load(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, grid);
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, GridMokateb);
            textBox1.Text = "";
            switch (Variables.tab)
            {
                case "g1":
                    {
                        tabControl1.SelectTab(0);
                        this.Text = "ثبت و حذف گروه ها";
                        break;
                    }
                case "g2":
                    {
                        tabControl1.SelectTab(1);
                        this.Text = "ثبت و حذف مكاتبه كننده ها";
                        break;
                    }
            }

        }

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            

        }

        private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


        }

        
       

        
        
             
        
        private void button8_Click(object sender, EventArgs e)
        {
            string s2 = string.Format("insert into mokateb values({0},'{1}')", textBox4.Text, textBox3.Text);
            Payegah.Insert(Variables.masir, s2);
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, GridMokateb);
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string s2 = string.Format("insert into groh values({0},'{1}')", textBox1.Text, textBox2.Text);
            Payegah.Insert(Variables.masir, s2);
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, grid);
            tohi();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            string s="select * from groh where kod=" + textBox1.Text + "";
            string s2="delete from groh where kod=" + textBox1.Text + " and gmozohi='" + textBox2.Text + "'";
            Payegah.Delet(Variables.masir, s,textBox1,textBox2 , s2);
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, grid);
            tohi();
        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            if ((textBox1.Text == "") | (textBox2.Text == ""))
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button4.Enabled = false;
            }
            else
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button4.Enabled = true;
            } 
        }

        private void button4_Click(object sender, EventArgs e)
        {
           
            string s = "update groh set kod=" + textBox1.Text + ",gmozohi='" + textBox2.Text + "' where kod=" + textBox1.Text + "";
            Payegah.UpDate(Variables.masir, s);
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, grid);
            tohi();

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grid_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            mask.IntText(e);
        }

        private void textBox1_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            mask.IntText(e);
        }

        private void textBox2_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            mask.StrText(e);
        }

        private void grid_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = grid.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = grid.CurrentRow.Cells[1].Value.ToString();
            if (textBox1.Text !="")
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button4.Enabled = true;
            }
        }

        private void groh_Activated(object sender, EventArgs e)
        {
            //if (Payegah.tt == false)
            ///{
              ///  tabControl1.SelectTab(1);
                //Payegah.tt = true;
            //}
              //  else
                //{
                  //  tabControl1.SelectTab(2);
                    //Payegah.tt = false;
                //}


        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void GridMokateb_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox4.Text = GridMokateb.CurrentRow.Cells[0].Value.ToString();
            textBox3.Text = GridMokateb.CurrentRow.Cells[1].Value.ToString();
            if (textBox4.Text != "")
            {
                button6.Enabled = true;
                button7.Enabled = true;
                button8.Enabled = true;
            }
        }

        private void GridMokateb_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
          
            string s = "update mokateb set kod=" + textBox4.Text + ",mokateb='" + textBox3.Text + "' where kod=" + textBox4.Text + "";
            Payegah.UpDate(Variables.masir, s);
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, GridMokateb );
            textBox4.Text = "";
            textBox3.Text = "";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string s = "select * from mokateb where kod=" + textBox4.Text + "";
            string s2 = "delete from mokateb where kod=" + textBox4.Text + " and mokateb='" + textBox3.Text + "'";
            Payegah.Delet(Variables.masir, s, textBox4,textBox3 , s2);
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, GridMokateb );
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void textBox4_TextChanged_1(object sender, EventArgs e)
        {
            if ((textBox4.Text == "") | (textBox3.Text == ""))
            {
                button6.Enabled = false;
                button7.Enabled = false;
                button8.Enabled = false;
            }
            else
            {
                button6.Enabled = true;
                button7.Enabled = true;
                button8.Enabled = true;
            } 
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            mask.StrText(e);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(tabControl1.SelectedTab ==tabPage1 )
                this.Text = "ثبت و حذف گروه ها";
            if (tabControl1.SelectedTab == tabPage2)
                this.Text = "ثبت و حذف مكاتبه كننده ها";

        }
        
    }
}