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

namespace DabirKhane
{
    public partial class SabtErsali : Form
    {

        
        public int x, y;
        public string s, s2, s3, s4;
        PersianCalendar cal = new PersianCalendar();
        
        public void tohi()
        {
            t1.Text = "";
            t2.Text = "";
            //t3.Text = "";
            t4.Text = "";
            t5.Text = "";
            t6.Text = "";
            t7.Text = "";
            textBox9.Text = "";
            textBox10.Text ="";
            comboBox1.Text = "";
            comboBox2.Text="";
        }
        public void tohi2()
        {
            textBox11.Text = ""; textBox8.Text = "";  textBox6.Text = ""; textBox5.Text = ""; textBox4.Text = ""; textBox3.Text = ""; textBox1.Text = ""; textBox2.Text = ""; comboBox3.Text = ""; comboBox4.Text = "";
        }
        public void tohi3()
        {
            textBox20.Text = ""; textBox19.Text = "";  textBox17.Text = ""; textBox16.Text = ""; textBox15.Text = ""; textBox14.Text = ""; textBox12.Text = ""; textBox13.Text = ""; comboBox5.Text = ""; comboBox6.Text = "";
        }
        public SabtErsali()
        {
            InitializeComponent();
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            
           
        }

        private void SabtErsali_Load(object sender, EventArgs e)
        {
            switch (Variables.tab)
            {
                case "a":
                    {
                        tabControl1.SelectTab(0);
                        this.Text = "ثبت نامه هاي ارسالي";
                        break;
                    }
                case "b":
                    {
                        tabControl1.SelectTab(1);
                        this.Text = "ثبت نامه هاي دريافتي";
                        break;
                    }
                case "c":
                    {
                        tabControl1.SelectTab(2);
                        this.Text = "اصلاح و بازيابي نامه ها";
                        break;
                    }
            }
            tabControl1.RightToLeftLayout = true;
       
            Payegah.FindNumberORpassWord(Variables.masir, Variables.StrCommandErsali);
            x = Convert.ToInt32(Payegah.LastNumber);
            x = x + 1;
            t1.Text = Convert.ToString(x);

   
            Payegah.FindNumberORpassWord(Variables.masir, Variables.StrCommandDaryafti);
            y = Convert.ToInt32(Payegah.LastNumber);
            y = y + 1;
            textBox11.Text = Convert.ToString(y);
                    
            Payegah.ComboAddItem(Variables.masir , Variables.StrCommandGroh , 2, comboBox2 );
            Payegah.ComboAddItem(Variables.masir, Variables.StrCommandMokateb, 2, comboBox1);
            Payegah.ComboAddItem(Variables.masir, Variables.StrCommandGroh, 2, comboBox3);
            Payegah.ComboAddItem(Variables.masir, Variables.StrCommandMokateb, 2, comboBox4);
            Payegah.ComboAddItem(Variables.masir, Variables.StrCommandMokateb, 2, comboBox6);
            Payegah.ComboAddItem(Variables.masir, Variables.StrCommandGroh, 2, comboBox5);
            t3.Text = cal.GetYear(DateTime.Now).ToString() + "/" + cal.GetMonth(DateTime.Now).ToString() + "/" + cal.GetDayOfMonth(DateTime.Now).ToString();
            textBox7.Text = cal.GetYear(DateTime.Now).ToString() + "/" + cal.GetMonth(DateTime.Now).ToString() + "/" + cal.GetDayOfMonth(DateTime.Now).ToString();
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, dataGridView1);
             }

        private void t1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

          

             
      private void t7_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {


        }

        private void t6_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string s2 = string.Format("insert into ersali values( {0},{1},'{2}','{3}','{4}','{5}','{6}')", t1.Text, t2.Text, t3.Text, t4.Text, t5.Text, t6.Text, t7.Text);
            Payegah.Insert(Variables.masir , s2);
            tohi();
            Payegah.FindNumberORpassWord(Variables.masir, Variables.StrCommandErsali);
            x = Convert.ToInt32(Payegah.LastNumber);
            x = x + 1;
            t1.Text = Convert.ToString(x);
        }

        private void t1_TextChanged_1(object sender, EventArgs e)
        {
            if ((t1.Text == "") | (t2.Text == "") | (t3.Text == "") | (t4.Text == "") | (t5.Text == "") | (t6.Text == "") | (t7.Text == "") )
            {
                button1.Enabled = false;
            }
            else { button1.Enabled = true; } 
                      
        }

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

            
        }

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

        private void button4_Click(object sender, EventArgs e)
        {
            string s2 = string.Format("insert into daryafti values( {0},{1},'{2}','{3}','{4}','{5}','{6}')",textBox11.Text ,textBox8.Text ,textBox7.Text,textBox6.Text,textBox5.Text,textBox4.Text,textBox3.Text );
            Payegah.Insert(Variables.masir, s2);
            tohi2();
            Payegah.FindNumberORpassWord(Variables.masir, Variables.StrCommandDaryafti);
            y = Convert.ToInt32(Payegah.LastNumber);
            y = y + 1;
            textBox11.Text = Convert.ToString(y);

         }

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

        private void GridViwe_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            if ((textBox11.Text == "") | (textBox8.Text == "") | (textBox7.Text == "") | (textBox6.Text == "") | (textBox5.Text == "") | (textBox4.Text == "") | (textBox3.Text == ""))
            {
                button4.Enabled = false;
            }
            else { button4.Enabled = true; }
            
        }

        private void t3_TextChanged(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            t4.Text = comboBox1.SelectedItem.ToString();
        }

        private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            t7.Text = comboBox2.SelectedItem.ToString();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, dataGridView1);
            Payegah.ShowKeyPress(dataGridView1, textBox2, textBox6);
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, dataGridView1);
            Payegah.ShowKeyPress(dataGridView1,textBox9 , t4);
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, dataGridView1);
            Payegah.ShowKeyPress(dataGridView1, textBox10, t7);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, dataGridView1);
            Payegah.ShowKeyPress(dataGridView1, textBox1, textBox3);
        }

        private void textBox13_TextChanged(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandMokateb, 2, dataGridView1);
            Payegah.ShowKeyPress(dataGridView1, textBox13, textBox17);
        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {
            Payegah.ShowData(Variables.masir, Variables.StrCommandGroh, 2, dataGridView1);
            Payegah.ShowKeyPress(dataGridView1, textBox12, textBox14);
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox17.Text = comboBox6.SelectedItem.ToString();
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox14.Text = comboBox5.SelectedItem.ToString();
        }

        private void textBox20_TextChanged(object sender, EventArgs e)
        {
            if ((textBox20.Text == "") | (textBox19.Text == "") | (textBox18.Text == "") | (textBox17.Text == "") | (textBox16.Text == "") | (textBox15.Text == "") | (textBox14.Text == ""))
            {
                button6.Enabled = false;
            }
            else { button6.Enabled = true; }
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox7.Text)
            {
                case "دريافتي":
                    {
                        SabtErsali frmsabt = new SabtErsali();
                        frmsabt.Text = "اصلاح نامه هاي دريافتي";
                        label18.Text = "مكاتبه كننده";
                        label19.Text = "تاریخ نامه وارده";
                        Variables.List = "bazyabiDaryafti";
                        textBox21.Text = "";
                        tohi3();
                        break;
                    }
                case "ارسالي":
                    {
                        SabtErsali frmsabt = new SabtErsali();
                        frmsabt.Text = "اصلاح نامه هاي ارسالي";
                        label18.Text = "از";
                        label19.Text = "تاریخ نامه صادره";
                        Variables.List ="bazyabiErsali";
                        textBox21.Text = "";
                        tohi3();
                        break;
                    }
            }
        }

        private void textBox21_TextChanged(object sender, EventArgs e)
        {
            if (comboBox7.Text == "ارسالي")
            {
                 s = "select * from ersali where shandikatori like'" + textBox21.Text + "'";
            }
            else
            {
                 s = "select * from daryafti where shandikatori like'" + textBox21.Text + "'";
            }
            SqlConnection conection = new SqlConnection(Variables.masir );
            conection.Open();
            SqlCommand Comand = new SqlCommand(s, conection);
            SqlDataReader dr = Comand.ExecuteReader();
            object[] Obj = new object[7];
            try
            {
                dr.Read();
                dr.GetValues(Obj);
                textBox20.Text = Obj[0].ToString();
                textBox19.Text = Obj[1].ToString();
                textBox18.Text = Obj[2].ToString();
                textBox17.Text = Obj[3].ToString();
                textBox16.Text = Obj[4].ToString();
                textBox15.Text = Obj[5].ToString();
                textBox14.Text = Obj[6].ToString();
            }
            catch
            {
                //MessageBox.Show("nat found");
                tohi3();
            }
            conection.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (comboBox7.Text == "ارسالي")
            {
                s2 = "update ersali set shvarede='" + textBox19.Text + "',tarikh='" + textBox18.Text + "',az='" + textBox17.Text + "',mozoh='" + textBox16.Text + "',shohrat='" + textBox15.Text + "',gmozohi='" + textBox14.Text + "' where shandikatori=" + textBox21.Text + "";
                s3 = string.Format("insert into bazyabiersali values( {0},{1},'{2}','{3}','{4}','{5}','{6}')", textBox20.Text, textBox19.Text, textBox18.Text, textBox17.Text, textBox16.Text, textBox15.Text, textBox14.Text);
                s4 = "update bazyabiersali set shvarede='" + textBox19.Text + "',tarikh='" + textBox18.Text + "',az='" + textBox17.Text + "',mozoh='" + textBox16.Text + "',shohrat='" + textBox15.Text + "',gmozohi='" + textBox14.Text + "' where shandikatori=" + textBox21.Text + "";
                tohi3();
            }
            else
            {
                s2 = "update daryafti set shvarede='" + textBox19.Text + "',tarikh='" + textBox18.Text + "',mokateb='" + textBox17.Text + "',mozoh='" + textBox16.Text + "',shohrat='" + textBox15.Text + "',gmozohi='" + textBox14.Text + "' where shandikatori=" + textBox21.Text + "";
                s3 = string.Format("insert into bazyabidaryafti values( {0},{1},'{2}','{3}','{4}','{5}','{6}')", textBox20.Text, textBox19.Text, textBox18.Text, textBox17.Text, textBox16.Text, textBox15.Text, textBox14.Text);
                s4 = "update bazyabidaryafti set shvarede='" + textBox19.Text + "',tarikh='" + textBox18.Text + "',mokateb='" + textBox17.Text + "',mozoh='" + textBox16.Text + "',shohrat='" + textBox15.Text + "',gmozohi='" + textBox14.Text + "' where shandikatori=" + textBox21.Text + "";
                tohi3();
            }
           
            Payegah.UpDate(Variables.masir, s2);
            ///////////////////////////////////////////////////////////
            SqlConnection conection = new SqlConnection(Variables.masir );
            conection.Open();
            SqlCommand Comand = new SqlCommand(s3, conection);
            try
            {
                Comand.ExecuteNonQuery();
            }
            catch
            {
                SqlCommand Command = new SqlCommand(s4, conection);
                Command.ExecuteNonQuery();
            }
            conection.Close();
             
            tohi3();
        }

        private void button8_Click(object sender, EventArgs e)
        {
           
            Variables.List = "ersali";
            Form9 frm9 = new Form9();
            frm9.ShowDialog();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Variables.List = "daryafti";
            Form9 frm9 = new Form9();
            frm9.ShowDialog();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (comboBox7.Text == "ارسالي")
            {
                Variables.List = "bazyabiErsali";
            }
            else
            {
                Variables.List = "bazyabiDaryafti";
            }
            Form9 frm9 = new Form9();
            frm9.ShowDialog();
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox6.Text = comboBox4.SelectedItem.ToString();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox3.Text = comboBox3.SelectedItem.ToString();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabPage1)
                this.Text = "ثبت نامه هاي ارسالي";

            if (tabControl1.SelectedTab == tabPage2)
                this.Text = "ثبت نامه هاي دريافتي";
            if (tabControl1.SelectedTab == tabPage3)
                this.Text = "اصلاح و بازيابي نامه ها";





        }

 

       



    }
}