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

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

        private void servise_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 rcode , rname , rfamily , isfree from ranande";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "ranande");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ranande";
            dataGridView1.Columns["rcode"].HeaderText = "کد راننده ";
            dataGridView1.Columns["rname"].HeaderText = "نام راننده ";
            dataGridView1.Columns["rfamily"].HeaderText = "نام خانوادگی  ";
            dataGridView1.Columns["isfree"].HeaderText = "در حال سرویس ";
            dataGridView1.Columns["isfree"].ReadOnly = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView1.Visible = true;
            ds.Clear();
            cmd.Connection = con;
            cmd.CommandText = "select rcode , rname , rfamily , isfree , olaviat from ranande order by ranande.olaviat ";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "ranande");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ranande";
            dataGridView1.Columns["rcode"].HeaderText = "کد راننده ";
            dataGridView1.Columns["rname"].HeaderText = "نام راننده ";
            dataGridView1.Columns["rfamily"].HeaderText = "نام خانوادگی  ";
            dataGridView1.Columns["isfree"].HeaderText = " در حال سرویس ";
            dataGridView1.Columns["olaviat"].HeaderText = " اولویت ";
            dataGridView1.Columns["isfree"].ReadOnly = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView1.Visible = true;

            ds.Clear();
            cmd.Connection = con;
            cmd.CommandText = "select mcode , mname , mfamily from moshtari";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "moshtari");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "moshtari";
            dataGridView1.Columns["mcode"].HeaderText = "کد مشتری ";
            dataGridView1.Columns["mname"].HeaderText = "نام مشتری";
            dataGridView1.Columns["mfamily"].HeaderText = "نام خانوادگی  ";



        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView1.Visible = true;

            ds.Clear();
            cmd.Connection = con;
            cmd.CommandText = "select carcode , carname , carsaheb , carranande  , isfree from car";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "car");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "car";
            dataGridView1.Columns["carcode"].HeaderText = "کد خودرو ";
            dataGridView1.Columns["carname"].HeaderText = "نام خودرو";
            dataGridView1.Columns["carsaheb"].HeaderText = "مالک خودرو  ";
            dataGridView1.Columns["carranande"].HeaderText = "نام راننده  ";
            dataGridView1.Columns["isfree"].HeaderText = "در حال استفاده ؟  ";

        }

        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView1.Visible = true;

            ds.Clear();
            cmd.Connection = con;
            cmd.CommandText = "select code , maghsad , asasegheymat , gheymat from gheymat";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "gheymat");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "gheymat";
            dataGridView1.Columns["code"].HeaderText = "کد مقصد ";
            dataGridView1.Columns["maghsad"].HeaderText = "نام مقصد";
            dataGridView1.Columns["asasegheymat"].HeaderText = "اساس قیمت  ";
            dataGridView1.Columns["gheymat"].HeaderText = "قیمت  ";
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.CommandText = "insert into service (servicecode , customercode , drivercode , mashincode , maghsad , mablagh , saateraft , saatebargasht , tarikheservise ) values(" + Convert.ToInt32(textBox1.Text) + " , " + Convert.ToInt32(textBox2.Text) + " , " + Convert.ToInt32(textBox3.Text) + " , " + Convert.ToInt32(textBox4.Text) + " , '" + textBox5.Text + "' , '" + textBox7.Text + "' , '" + textBox9.Text + "' , '" + textBox10.Text + "' ,'" + textBox11.Text + "')";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "update ranande set isfree = 'true' where rcode = " + Convert.ToInt32(textBox3.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("یک سرویس با موفقیت ثبت شد" + "لطفا ساعت بازگشت را برای اتمام سرویس در هنگام بازگشت راننده وارد کنید", "نتیجه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("لطفا در وارد کردن کد ها دقت نمائید", "نتیجه", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                textBox12.Text = ds.Tables["ranande"].Rows[e.RowIndex]["rcode"].ToString();
                checkBox1.Checked = Convert.ToBoolean(ds.Tables["ranande"].Rows[e.RowIndex]["isfree"].ToString());

            }
            catch
            {

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                
                cmd.CommandText = "update ranande set isfree = 'true' where rcode = " + Convert.ToInt32(textBox12.Text);
                cmd.ExecuteNonQuery();

            }
            catch
            {
                MessageBox.Show("لطفا یکی از رانندگان را انتخاب نمائید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView2.Visible = true;
            ds.Clear();
            cmd.Connection = con;
            cmd.CommandText = "select servicecode , drivercode , maghsad , mablagh , saateraft , saatebargasht  from service ";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "service");
            dataGridView2.DataSource = ds;
            dataGridView2.DataMember = "service";
            dataGridView2.Columns["servicecode"].HeaderText = "کد سرویس  ";
            dataGridView2.Columns["drivercode"].HeaderText = "کد راننده";
            dataGridView2.Columns["maghsad"].HeaderText = "مقصد  ";
            dataGridView2.Columns["mablagh"].HeaderText = "مبلغ  ";
            dataGridView2.Columns["saateraft"].HeaderText = "ساعت رفت   ";
            dataGridView2.Columns["saatebargasht"].HeaderText = "ساعت بازگشت  ";

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtservicecode.Text = ds.Tables["service"].Rows[e.RowIndex]["servicecode"].ToString();
            
            

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            cmd.CommandText = "update ranande set isfree = 'false' , olaviat = " + Convert.ToInt32( dataGridView1.RowCount) + " where rcode = " + Convert.ToInt32(textBox12.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("سرویس راننده با کد مورد نظر پایان یافت و به صف انتظار انتقال یافت", "نتیجه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ds.Clear();
            cmd.Connection = con;
            cmd.CommandText = "select rcode , rname , rfamily , isfree , olaviat from ranande order by ranande.olaviat ";
            cmd.ExecuteNonQuery();
            da.SelectCommand = cmd;
            da.Fill(ds, "ranande");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ranande";
            
             
            //cmd.CommandText = "update service set saatebargasht = '"+ time
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (txtservcode.Text == "")
            {
                MessageBox.Show("لطفا کد سرویس را برای حذف سرویس وارد نمائید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                cmd.CommandText = "delete from service where servicecode =" + Convert.ToInt32(txtservcode.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("سرویس مورد نظر با موفقیت حذف شد", "نتیجه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtservcode_DoubleClick(object sender, EventArgs e)
        {
            txtservcode.Text = "";
        }
    }
}
