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

namespace Ajans
{
    public partial class Form3 : Form
    {
        String StrConn;
        String StrQury;
        public Form3()
        {
            InitializeComponent();
        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            LBTH.Text = "13" + comboBox3.Text + "/" + comboBox2.Text + "/" + comboBox1.Text;
            CodeMosafer.Text = CodeMosafer.Text + comboBox3.Text+100;
            comboBox3.Enabled = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            CodeMosafer.Text = CodeMosafer.Text + comboBox2.Text;
            comboBox2.Enabled = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CodeMosafer.Text=CodeMosafer.Text +comboBox1.Text;
            comboBox1.Enabled = false;
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'havapeymaDataSet2.Mosafer' table. You can move, or remove it, as needed.
            this.mosaferTableAdapter1.Fill(this.havapeymaDataSet2.Mosafer);
            // TODO: This line of code loads data into the 'havapeymaDataSet1.Havapeyma' table. You can move, or remove it, as needed.
            this.havapeymaTableAdapter.Fill(this.havapeymaDataSet1.Havapeyma);
            // TODO: This line of code loads data into the 'havapeymaDataSet.Mosafer' table. You can move, or remove it, as needed.
           //this.mosaferTableAdapter.Fill(this.havapeymaDataSet.Mosafer);

                
                 SqlConnection myConn;
                SqlCommand myComm;
                SqlDataReader myDR;

                StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
                StrQury = "Select CodeH From  Havapeyma";

                myConn = new SqlConnection(StrConn);

                myComm = new SqlCommand();
                myComm.Connection = myConn;
                myComm.CommandType = CommandType.Text;
                myComm.CommandText = StrQury;
                myConn.Open();
                myDR = myComm.ExecuteReader();

                while (myDR.Read())
                {
                    this.comboBox4.Items.AddRange(new object[] {   myDR["CodeH"].ToString()      });
                }
   
                myConn.Close();
            }
            
                

        

        private void timer1_Tick(object sender, EventArgs e)
        {
           



        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            CodeMosafer.Text = CodeMosafer.Text + "10";
            LBsex.Text = "زن";
           radioButton1.Enabled = false;
           radioButton2.Enabled = false;

        }


        private void radioButton2_Click(object sender, EventArgs e)
        {
            CodeMosafer.Text = CodeMosafer.Text + "20";
            LBsex.Text = "مرد";
            radioButton2.Enabled = false;
            radioButton1.Enabled = false;
        }

        private void textBox4_Leave(object sender, EventArgs e)
        {
            if (int.Parse(textBox4.Text) >= 5 && int.Parse(textBox4.Text) <= 40)
            {
                CodeMosafer.Text = CodeMosafer.Text + "8";
                textBox4.Enabled = false;
            }

            if (int.Parse(textBox4.Text) >= 41 && int.Parse(textBox4.Text) <= 80)
            {
                CodeMosafer.Text = CodeMosafer.Text + "9";
                textBox4.Enabled = false;
            }
            
         
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox4.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection myConn;
            SqlCommand myComm;
            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Insert Into Mosafer (CodeMosafer,Name,Family,TelNumber,Age,Sex,SHarekat,Tharekat,MablaghD,Mabda,Maghsad,CodeHavapeyma,NSandali) Values (@Code,@Name,@Family,@TelNumber,@Age,@Sex,@SHarekat,@Tharekat,@MablaghD,@Mabda,@Maghsad,@CodeHavapeyma,@NSandali)";
            myConn = new SqlConnection(StrConn);
            myComm = new SqlCommand();
            myComm.Connection = myConn;
            myComm.CommandType = CommandType.Text;
            myComm.CommandText = StrQury;
           
            myComm.Parameters.Add("Code", SqlDbType.NVarChar ,50).Value = this.CodeMosafer.Text;
            myComm.Parameters.Add("Name", SqlDbType.NVarChar, 50).Value = this.textBox1.Text;
            myComm.Parameters.Add("Family", SqlDbType.NVarChar, 50).Value = this.textBox2.Text;
            myComm.Parameters.Add("TelNumber", SqlDbType.NVarChar, 50).Value = this.textBox3.Text;
            myComm.Parameters.Add("Age", SqlDbType.NVarChar, 50).Value = this.textBox4.Text;
            myComm.Parameters.Add("Sex", SqlDbType.NVarChar, 50).Value = this.LBsex.Text;
            myComm.Parameters.Add("SHarekat", SqlDbType.NVarChar, 50).Value = this.textBox5.Text;
            myComm.Parameters.Add("Tharekat", SqlDbType.NVarChar, 50).Value = this.LBTH.Text;
            myComm.Parameters.Add("MablaghD", SqlDbType.NVarChar, 50).Value = this.textBox6.Text;
            myComm.Parameters.Add("Mabda", SqlDbType.NVarChar, 50).Value = this.textBox7.Text;
            myComm.Parameters.Add("Maghsad", SqlDbType.NVarChar, 50).Value = this.textBox8.Text;
            myComm.Parameters.Add("CodeHavapeyma", SqlDbType.NVarChar, 50).Value = this.comboBox4.Text;
            myComm.Parameters.Add("NSandali", SqlDbType.NVarChar, 50).Value = this.textBox9.Text;

            myConn.Open();
            myComm.ExecuteNonQuery();
            myConn.Close();
            Show1();
            MessageBox.Show(" اطلاعات مسافر با موفقیت ثبت شد","ثبت اطلاعات ");

        }

        private void Show1()
        {
            SqlDataAdapter myDA;
            DataSet myDS;

            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Select * From Mosafer";
            myDA = new SqlDataAdapter(StrQury, StrConn);

            myDS = new DataSet();

            myDA.Fill(myDS, "Mosafer");

            this.dataGridView1.DataSource = myDS;
            this.dataGridView1.DataMember = "Mosafer";
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.RightToLeft = RightToLeft.Yes;
            this.dataGridView1.Refresh();
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            textBox2.Enabled = false;
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            textBox3.Enabled = false;
        }

        private void textBox5_Click(object sender, EventArgs e)
        {
            textBox5.Enabled = false;
        }

        private void textBox6_Leave(object sender, EventArgs e)
        {
            textBox6.Enabled = false;
        }

        private void textBox7_Leave(object sender, EventArgs e)
        {
            textBox7.Enabled = false;
        }

        private void textBox8_Leave(object sender, EventArgs e)
        {
            textBox8.Enabled = false;
        }

        private void textBox9_Leave(object sender, EventArgs e)
        {
            textBox9.Enabled = false;
        }

        private void textBox5_Leave(object sender, EventArgs e)
        {
            textBox5.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = ""; textBox1.Enabled = true;
            textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; LBTH.Text = ""; LBsex.Text = ""; textBox5.Text = ""; textBox6.Text = "";
            textBox7.Text = ""; textBox8.Text = ""; textBox9.Text = ""; comboBox1.Text = "Day"; comboBox2.Text = "Month"; comboBox3.Text = "Year";
            comboBox4.Text = "";
            radioButton1.Enabled = true;
            radioButton2.Enabled = true;
            textBox2.Enabled = true; textBox3.Enabled = true; textBox4.Enabled = true; textBox5.Enabled = true; textBox6.Enabled = true; textBox7.Enabled = true;
            textBox8.Enabled = true; textBox9.Enabled = true;
            comboBox1.Enabled = true; comboBox2.Enabled = true; comboBox3.Enabled = true; comboBox4.Enabled = true;
            CodeMosafer.Text = "";



        }

     
       

       
    }
}
