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

namespace AjanseProject
{
    public partial class ajans : Form
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public ajans()
        {
            InitializeComponent();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            DialogResult dr;
            dr = MessageBox.Show("آیا مایل به خروج از قسمت مشخصات آژانس هستید ؟", "خروج", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {

                this.Close();
            }
        }

        private void ajans_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=" + Environment.MachineName + ";Initial Catalog=ajans;Integrated Security=True";
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select * from Ajanc ";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "ajanc");
            dataGridView1.DataSource=ds;
            dataGridView1.DataMember="ajanc";
            dataGridView1.Columns["aaname"].HeaderText = "نام آژانس";
            dataGridView1.Columns["amalek"].HeaderText = "نام مالک";
            dataGridView1.Columns["atel1"].HeaderText = "تلفن 1";
            dataGridView1.Columns["atel2"].HeaderText = "تلفن 2";
            dataGridView1.Columns["atel3"].HeaderText = "تلفن 3";
            dataGridView1.Columns["afax"].HeaderText = "فکس";
            
               
            textBox1.Select();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" | textBox10.Text == "" | textBox13.Text == "")
            {
                MessageBox.Show("لطفا فیلد های ستاره دار را تکمیل کنید .", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                timer2.Enabled = true;
                cmd.CommandText = "update ajanc set acode = N'" + Convert.ToInt32(textBox1.Text) + "', aaname = N'" + textBox2.Text + "', amalek =N'" + textBox3.Text + "', atel1 = N'" + textBox4.Text + "', atel2=N'" + textBox5.Text + "', atel3 =N'" + textBox7.Text + "', afax = N'" + textBox11.Text + "', aaddress = N'" + textBox14.Text + "', amalekmob =N'" + textBox9.Text + "', shomaresabt = N'" + textBox10.Text + "', tarikhesodor = N'" + textBox12.Text + "', akomision = N'" + textBox13.Text + "'";
                cmd.ExecuteNonQuery();
                MessageBox.Show("اصلاحات با موفقیت انجام شد", "نتیجه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (toolStripProgressBar1.Value > 99)
            {
                toolStripProgressBar1.Value = 0;
                timer2.Enabled = false;
            }
            else
            {
                toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                textBox1.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["acode"].ToString();
                textBox2.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["aaname"].ToString();
                textBox3.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["amalek"].ToString();
                textBox4.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["atel1"].ToString();
                textBox5.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["atel2"].ToString();
                textBox7.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["atel3"].ToString();
                textBox9.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["amalekmob"].ToString();
                textBox11.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["afax"].ToString();
                textBox12.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["tarikhesodor"].ToString();
                textBox13.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["akomision"].ToString();
                textBox10.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["shomaresabt"].ToString();
                textBox14.Text = ds.Tables["ajanc"].Rows[e.RowIndex]["aaddress"].ToString();
            }
            catch
            {
                MessageBox.Show("شما فقط میتوانید یک رکورد را انتخاب کنید . ", "هشدار", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
