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 FarsiMsgBox;
using System.Globalization;

namespace ajans
{      
    public partial class form_driver : Form
    {
        public string dvrcarid ="";
        public string dvrcarname = "";
        public string dvrcarclass = "";
        public string CARID = "";
        int a = -175, c = 169, b = 0, d = -6;
        int x = -319, y = 312, z = 0, w = -6;

        int S = 0;
        int R = 0;

        PersianCalendar PersianDateTime = new PersianCalendar();

        DataView objdv;
        DataView objdv2;
        string date = "";
        public form_driver()
        {
            InitializeComponent();
        }

        private void CorrectDate()
        {
            DateTime dd = DateTime.Now;
            date = PersianDateTime.GetYear(dd).ToString() + "/" + PersianDateTime.GetMonth(dd).ToString("D2") + "/" + PersianDateTime.GetDayOfMonth(dd).ToString("D2");
        }

        private void Binding()
        {
            lblnameDel.DataBindings.Clear();
            lblcarDel.DataBindings.Clear();
            txtCarID.DataBindings.Clear();
            lblnameDel.Text = "";
            lblcarDel.Text = "";
            txtCarID.Text = "";
            lblnameDel.DataBindings.Add("text", objdv, "نام و نام خانوادگی");
            lblcarDel.DataBindings.Add("text", objdv, "نام ماشین");
            txtCarID.DataBindings.Add("text", objdv, "کد ماشین");
        }

        private void Binding2()
        {
            txtnameUpd.DataBindings.Clear();
            txtfnameUpd.DataBindings.Clear();
            txttelUpd.DataBindings.Clear();
            txtmobUpd.DataBindings.Clear();
            txtaddressUpd.DataBindings.Clear();
            txtcaridUpd.DataBindings.Clear();
            txtcarnameUpd.DataBindings.Clear();
            cmbcarclassupd.DataBindings.Clear();
            txtCarID.DataBindings.Clear();
            
            txtnameUpd.Text = "";
            txtfnameUpd.Text = "";
            txttelUpd.Text = "";
            txtmobUpd.Text = "";
            txtaddressUpd.Text = "";
            txtcaridUpd.Text = "";
            txtcarnameUpd.Text = "";
            cmbcarclassupd.Text = "";
            txtCarID.Text = "";

            txtnameUpd.DataBindings.Add("text", objdv, "نام");
            txtfnameUpd.DataBindings.Add("text", objdv, "نام خانوادگی");
            txttelUpd.DataBindings.Add("text", objdv, "تلفن ثابت");
            txtmobUpd.DataBindings.Add("text", objdv, "موبایل");
            txtaddressUpd.DataBindings.Add("text", objdv, "آدرس");
            txtcaridUpd.DataBindings.Add("text", objdv, "کد ماشین");
            txtcarnameUpd.DataBindings.Add("text", objdv, "نام ماشین");
            cmbcarclassupd.DataBindings.Add("text", objdv, "کلاس ماشین");
            txtCarID.DataBindings.Add("text", objdv, "کد ماشین");

            CARID = txtcaridUpd.Text;
        }

        private void Binding3()
        {
            txtnameUpd.DataBindings.Clear();
            txtfnameUpd.DataBindings.Clear();
            txttelUpd.DataBindings.Clear();
            txtmobUpd.DataBindings.Clear();
            txtaddressUpd.DataBindings.Clear();
            txtcaridUpd.DataBindings.Clear();
            lblcarnameUpd.DataBindings.Clear();
            lblcarclassupd.DataBindings.Clear();

            txtnameUpd.Text = "";
            txtfnameUpd.Text = "";
            txttelUpd.Text = "";
            txtmobUpd.Text = "";
            txtaddressUpd.Text = "";
            txtcaridUpd.Text = "";
            lblcarnameUpd.Text = "";
            lblcarclassupd.Text = "";

            txtnameUpd.DataBindings.Add("text", objdv, "نام");
            txtfnameUpd.DataBindings.Add("text", objdv, "نام خانوادگی");
            txttelUpd.DataBindings.Add("text", objdv, "تلفن ثابت");
            txtmobUpd.DataBindings.Add("text", objdv, "موبایل");
            txtaddressUpd.DataBindings.Add("text", objdv, "آدرس");
            txtcaridUpd.DataBindings.Add("text", objdv, "کد ماشین");
            lblcarnameUpd.DataBindings.Add("text", objdv, "نام ماشین");
            lblcarclassupd.DataBindings.Add("text", objdv, "کلاس ماشین");
        }


        private void CountCar()
        {
            connection2.CreatConnection();
            connection2.objcommand.CommandText = "SELECT car_id FROM car WHERE car_status='" + "آزاد" + "' and car_delete=0";
            connection2.objreader = connection2.objcommand.ExecuteReader();

            while (connection2.objreader.Read())
            {
                txtdvrcarid.Items.Add(connection2.objreader["car_id"].ToString());
                txtcaridUpd.Items.Add(connection2.objreader["car_id"].ToString());
            }
            connection2.objconnection.Close();
        }

        private void BindingDriver()
        {
            driver Driver = new driver();
            Driver.SetDvrID(txtdvrid.Text);
            Driver.SetDvrName(txtdvrname.Text);
            Driver.SetDvrFname(txtdvrfname.Text);
            Driver.SetDvrBir(txtdvrbd.Text);
            Driver.SetDvrMarry(chkdvrm.Checked.ToString());
            Driver.SetDvrAddress(txtdvraddress.Text);
            Driver.SetDvrPhone(txtdvrtel.Text);
            Driver.SetDvrMobile(txtdvrmob.Text);
            Driver.SetDvrRec(txtdvrrec.Text);
            Driver.SetDvrCar(txtdvrcarname.Text);
            Driver.SetDvrCarClass(cmbcarclass.Text);
            Driver.SetCarID(txtdvrcarid.Text);
            Driver.SetDvrStatus("0");
            Driver.SetDvrAmount("0");
            Driver.SetDvrDelete("0");

            Driver.InsertDriver();
        }

        private void BindingDriver2()
        {
            driver Driver = new driver();
            Driver.SetDvrID(txtdvrid.Text);
            Driver.SetDvrName(txtdvrname.Text);
            Driver.SetDvrFname(txtdvrfname.Text);
            Driver.SetDvrBir(txtdvrbd.Text);
            Driver.SetDvrMarry(chkdvrm.Checked.ToString());
            Driver.SetDvrAddress(txtdvraddress.Text);
            Driver.SetDvrPhone(txtdvrtel.Text);
            Driver.SetDvrMobile(txtdvrmob.Text);
            Driver.SetDvrRec(txtdvrrec.Text);
            Driver.SetDvrCar(lblcarname.Text);
            Driver.SetDvrCarClass(lblcarclass.Text);
            Driver.SetCarID(txtdvrcarid.Text);
            Driver.SetDvrStatus("0");
            Driver.SetDvrAmount("0");
            Driver.SetDvrDelete("0");

            Driver.InsertDriver();
        }

        private void BindingFree()
        {
            txtdvrid.Text = "";
            txtdvrname.Text = "";
            txtdvrfname.Text = "";
            txtdvrbd.Text = "";
            chkdvrm.Checked = false;
            txtdvraddress.Text = "";
            txtdvrtel.Text = "";
            txtdvrmob.Text = "";
            txtdvrcarname.Text = "";
            cmbcarclass.Text = "";
        }

        private void binding()
        {
            lblcarclass.DataBindings.Clear();
            lblcarname.DataBindings.Clear();

            lblcarclass.DataBindings.Add("text", objdv, "کلاس ماشین");
            lblcarname.DataBindings.Add("text", objdv, "نام ماشین");
        }

        private void MAXDvrID()
        {
            driver Driver = new driver();
            objdv = new DataView(Driver.MaxDvrID().Tables["driver"]);
            txtdvrid.DataBindings.Clear();
            txtdvrid.Text = "";
            txtdvrid.DataBindings.Add("text", objdv, "MAX");
            if (txtdvrid.Text == "") txtdvrid.Text = "10";
            txtdvrid.Text = Convert.ToString(Convert.ToInt16(txtdvrid.Text) + 1);
        }

        private void insert_Click(object sender, EventArgs e)
        {          
            if (rdb1.Checked == true)
            {
                if (txtdvrname.Text == "" || txtdvrfname.Text == "" || txtdvrbd.Text == "" || txtdvrtel.Text == "" || txtdvraddress.Text == "" || txtdvrcarname.Text == "" || cmbcarclass.Text=="")
                {
                    FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردند.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                }
                else if (chkdvrm.Checked == false)
                {
                    FarsiMessage.ShowMessageBox("راننده حتما باید متاهل باشد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                }
                else
                {
                    BindingDriver();

                    FarsiMessage.ShowMessageBox("یک راننده به لیست رانندگان اضافه گردید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);

                    BindingFree();
                    MAXDvrID();
                }
            }

            else if (rdb2.Checked == true)
            {
                if (txtdvrname.Text == "" || txtdvrfname.Text == "" || txtdvrbd.Text == "" || txtdvrtel.Text == "" || txtdvraddress.Text == "" || txtdvrcarid.Text=="")
                {
                    FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردند.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                }
                else if (chkdvrm.Checked == false)
                {
                    FarsiMessage.ShowMessageBox("راننده حتما باید متاهل باشد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                }
                else
                {
                    BindingDriver2();

                    FarsiMessage.ShowMessageBox("یک راننده به لیست رانندگان اضافه گردید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    car Car = new car();
                    Car.SetCarID(txtdvrcarid.Text);
                    Car.UpdateCarStatus();
                    txtdvrcarid.Text = "";
                    lblcarname.Text = "";
                    lblcarclass.Text = "";
                    txtdvrcarid.Items.Clear();
                    CountCar();

                    BindingFree();
                    MAXDvrID();
                }
            }

        }

        private void list_Click(object sender, EventArgs e)
        {
            driver Driver = new driver();
            dgListDriver.DataSource = Driver.SelectDriver();
            dgListDriver.DataMember = "driver";
        }

        private void form_driver_Load(object sender, EventArgs e)
        {
            MAXDvrID();
                        
            CountCar();
            txtdvrcarid.Enabled = false;
            txtdvrcarid.Text = "0";

            for (int i = 1340; i <= 1400; i++)
            {
                txtdvrbd.Items.Add(i);
            }

        }

        private void rdb1_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb1.Checked == true)
            {
                txtdvrcarid.Enabled = false;
                txtdvrcarname.Enabled = true;
                txtdvrcarid.Text = "0";
                label15.Visible = true;
                label31.Visible = false;
                cmbcarclass.Enabled = true;
                label43.Visible = true;
                lblcarclass.Text = "";
                lblcarname.Text = "";
            }
        }

        private void rdb2_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb2.Checked == true)
            {
                txtdvrcarname.Enabled = false;
                txtdvrcarid.Enabled = true;
                if (txtdvrcarid.Items.Count == 0)
                {
                    FarsiMessage.ShowMessageBox("هیچ ماشین آزادی موجود نیست", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                    rdb1.Checked = true;
                }
                else
                {
                    txtdvrcarid.SelectedIndex = txtdvrcarid.SelectedIndex + 1;
                    label31.Visible = true;
                    label15.Visible = false;
                    cmbcarclass.Enabled = false;
                    label43.Visible = false;
                }
            }
            else
            {
                txtdvrcarid.SelectedIndex = -1;
            }
        }

        private void txtdvrcarid_SelectedIndexChanged(object sender, EventArgs e)
        {
            car Car = new car();
            objdv = new DataView(Car.SelectCar(txtdvrcarid.Text).Tables["car"]);

            binding();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            CorrectDate();
            txtdvrrec.Text = date;
        }

        private void txtdvrtel_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void txtdvrmob_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void rdbS1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (rdbS1.Checked == true)
            {
                lblSearch.Text = "لطفا نام یا نام خانوادگی را وارد نمایید";
                txtsearch.Text = "";
                txtsearch2.Text = "";
                label27.Visible = true;
                txtsearch2.Visible = true;
                label45.Visible = true;
                label46.Visible = true;
            }
        }

        private void rdbS2_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbS2.Checked == true)
            {
                lblSearch.Text = "لطفا کد راننده را وارد نمایید";
                txtsearch.Text = "";
                txtsearch2.Text = "";
                label27.Visible = false;
                txtsearch2.Visible = false;
                label45.Visible = false;
                label46.Visible = false;
            }
        }

        private void rdbS3_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbS3.Checked == true)
            {
                lblSearch.Text = "لطفا نام ماشین را وارد نمایید";
                txtsearch.Text = "";
                txtsearch2.Text = "";
                label27.Visible = false;
                txtsearch2.Visible = false;
                label45.Visible = false;
                label46.Visible = false;
            }
        }

        private void txtsearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rdbS2.Checked == true)
            {
                int i = e.KeyChar;
                if (i < 8 || 8 < i && i < 48 || i > 57)
                {
                    e.Handled = true;
                    err.Clear();
                    err.SetError(txtsearch, "لطفا کد راننده را وارد نمایید");
                    System.Console.Beep();
                }
                else err.Clear();
            }
            else
                e.Handled = false;
        }

        private void search_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "" && txtsearch2.Text == "")
            {
                FarsiMessage.ShowMessageBox(lblSearch.Text, "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtsearch.Focus();
            }
            else
            {
                driver Driver = new driver();
                if (rdbS2.Checked == true)
                {
                    dgListDriver.DataSource = Driver.SelectDriver(txtsearch.Text);
                    dgListDriver.DataMember = "driver";
                }
                else if (rdbS1.Checked == true)
                {
                    dgListDriver.DataSource = Driver.SelectDriver(txtsearch.Text, txtsearch2.Text);
                    dgListDriver.DataMember = "driver";

                }
                else if (rdbS3.Checked == true)
                {
                    dgListDriver.DataSource = Driver.SelectDriver2(txtsearch.Text);
                    dgListDriver.DataMember = "driver";
                }
            }
        }

        private void txtidDel_TextChanged(object sender, EventArgs e)
        {
            driver Driver = new driver();
            objdv = new DataView(Driver.SelectDriver(txtidDel.Text).Tables["driver"]);
            Binding();
        }

        private void delete_Click(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectDriverPresent(txtidDel.Text).Tables["present"]);
            txtdvrstatus.DataBindings.Clear();
            txtdvrstatus.Text = "";
            txtdvrstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (txtidDel.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidDel.Focus();
            }
            else if (txtidDel.Text != "" && lblnameDel.Text == "")
            {
                FarsiMessage.ShowMessageBox("راننده ای با این کد یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else if (Int16.Parse(txtdvrstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("این راننده در حال انجام دادن سرویس است و قادر به حذف شدن نمی باشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                driver Driver = new driver();
                FarsiMessage.ShowMessageBox("آیا میخواهید راننده مورد نظر را حذف نمایید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Driver.SetDvrID(txtidDel.Text.ToString());
                    Driver.DeleteDriver();
                    if (Int16.Parse(txtCarID.Text) != 0)
                    {
                        car Car = new car();
                        Car.SetCarID(txtCarID.Text);
                        Car.UpdateCarStatus2();
                    }
                    FarsiMessage.ShowMessageBox("راننده مورد نظر حذف گردید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    txtidDel.Text = "";
                    lblnameDel.Text = "";
                    lblcarDel.Text = "";
                }
            }
        }

        private void txtidDel_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (S == 0)
            {
                S = 1;
                timer1.Enabled = true;
                btnDel.Image = ajans.Properties.Resources.move;
                btnUpd.Enabled = false;
                gr2.Enabled = false;
                gr.Enabled = true;
                p1.Visible = true;
                p2.Visible = false;
                p3.Visible = false;
                txtidDel.Focus();
            }
            else if (S == 1)
            {
                S = 0;
                timer1.Enabled = true;
                btnDel.Image = ajans.Properties.Resources.move2;
                btnUpd.Enabled = true;
                p1.Visible = false;
                gr.Enabled = false;
            }
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (S == 1)
            {
                btnDel.Enabled = false;
                gr.Location = new Point(39, a);
                if (a < -6)
                    a += 10;
                if (a >= -6)
                {
                    gr.Location = new Point(39, -6);
                    a = (-175);
                }


                btnDel.Location = new Point(263, b);
                if (b < 169)
                    b += 10;
                if (b >= 169)
                {
                    timer1.Enabled = false;
                    btnDel.Location = new Point(263, 169);
                    b = 0;
                    btnDel.Enabled = true; ;
                }
                btnDel.Enabled = true;

            }
            else if (S == 0)
            {

                btnDel.Enabled = false;
                gr.Location = new Point(39, d);

                d -= 10;
                if (d <= (-175))
                {
                    gr.Location = new Point(39, (-175));
                    d = -6;
                }

                btnDel.Location = new Point(263, c);

                c -= 10;
                if (c <= 0)
                {
                    timer1.Enabled = false;
                    btnDel.Location = new Point(263, 0);
                    btnDel.Enabled = true;
                    c = 169;
                }
            }
        }

        private void txtidUpd_TextChanged(object sender, EventArgs e)
        {
            driver Driver = new driver();
            objdv = new DataView(Driver.SelectDriver3(txtidUpd.Text).Tables["driver"]);
            Binding2();
            objdv2 = new DataView(Driver.SelectCountDriver(txtidUpd.Text).Tables["driver"]);
            count.DataBindings.Clear();
            count.Text = "";
            count.DataBindings.Add("text", objdv2, "count");

            if (txtcaridUpd.Text == "" || Int32.Parse(txtcaridUpd.Text) == 0)
            {
                rdbUpd1.Checked = true;
            }
            else if (txtcaridUpd.Text != "" || Int32.Parse(txtcaridUpd.Text) != 0)
            {
                rdbUpd2.Checked = true;
                Binding3();
                txtcarnameUpd.Text = "";
                cmbcarclassupd.Text = "";
            }
            if (txtidUpd.Text == "")
            {
                rdbUpd1.Checked = false;
                rdbUpd2.Checked = false;
                lblcarnameUpd.Text = "";
            }
        }

        private void rdbUpd1_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbUpd1.Enabled == false)
            {
                txtcarnameUpd.Enabled = false;
                cmbcarclassupd.Enabled = false;
            }
            else if (rdbUpd1.Checked == true)
            {
                txtcarnameUpd.Enabled = true;
                txtcaridUpd.Enabled = false;
                txtcaridUpd.Text = "0";
                cmbcarclassupd.Enabled = true;
            }
        }

        private void rdbUpd2_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbUpd2.Enabled == false)
            {
                txtcaridUpd.Enabled = false;
            }
            if (rdbUpd2.Checked == true)
            {
                txtcaridUpd.Enabled = true;
                txtcarnameUpd.Enabled = false;
                cmbcarclassupd.Enabled = false;
                txtcaridUpd.Text = dvrcarid.ToString();
            }
        }

        private void exit_Click(object sender, EventArgs e)
        {
            this.Opacity = 1;
            for (int i = 100; i > 1; i--)
            {
                this.Opacity -= 0.01;
                Application.DoEvents();
            }
            this.Close();
        }

        private void form_driver_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_driver_show = false;
        }

        private void txtdvrbd_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cmbcarclass_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtdvrcarid_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtsearch_Leave(object sender, EventArgs e)
        {
            err.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Opacity = 1;
            for (int i = 100; i > 1; i--)
            {
                this.Opacity -= 0.01;
                Application.DoEvents();
            }
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Opacity = 1;
            for (int i = 100; i > 1; i--)
            {
                this.Opacity -= 0.01;
                Application.DoEvents();
            }
            this.Close();
        }

        private void txtidUpd_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectDriverPresent(txtidUpd.Text).Tables["present"]);
            txtdvrstatus.DataBindings.Clear();
            txtdvrstatus.Text = "";
            txtdvrstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (Int32.Parse(txtdvrstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("این راننده در حال انجام سرویس می باشد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }
            else if (txtidUpd.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidUpd.Focus();
            }

            else if (Int32.Parse(count.Text) == 0)
            {
                FarsiMessage.ShowMessageBox("راننده با کد مربوطه یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                FarsiMessage.ShowMessageBox("آیا مطمئن هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Information_1);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    if (rdbUpd2.Checked == true)
                    {
                        txtcaridUpd.Text = "";
                        lblcarnameUpd.Text = "";
                        FarsiMessage.ShowMessageBox("ماشین مورد نظر حذف گردید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                        rdbUpd1.Enabled = true;
                        rdbUpd2.Enabled = true;
                    }
                    else if (rdbUpd1.Checked == true)
                    {
                        txtcarnameUpd.Text = "";
                        cmbcarclassupd.Text = "";
                        FarsiMessage.ShowMessageBox("ماشین مورد نظر حذف گردید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                        rdbUpd1.Enabled = true;
                        rdbUpd2.Enabled = true;
                    }
                }
            }
        }

        private void btnUpd_Click(object sender, EventArgs e)
        {
            if (R == 0)
            {
                R = 1;
                timer2.Enabled = true;
                btnUpd.Image = ajans.Properties.Resources.move;
                btnDel.Enabled = false;
                gr.Enabled = false;
                gr2.Enabled = true;
                p1.Visible = false;
                p2.Visible = false;
                p3.Visible = true;
                txtidUpd.Focus();
            }
            else if (R == 1)
            {
                R = 0;
                timer2.Enabled = true;
                btnUpd.Image = ajans.Properties.Resources.move2;
                btnDel.Enabled = true;
                gr.Enabled = true;
                gr2.Enabled = false;
                p3.Visible = false;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (R == 1)
            {
                btnUpd.Enabled = false;
                gr2.Location = new Point(15, x);
                if (x < -6)
                    x += 10;
                if (x >= -6)
                {
                    gr2.Location = new Point(15, -6);
                    x = (-319);
                }


                btnUpd.Location = new Point(76, z);
                if (z < 312)
                    z += 10;
                if (z >= 312)
                {
                    timer2.Enabled = false;
                    btnUpd.Location = new Point(76, 312);
                    z = 0;
                    btnUpd.Enabled = true; ;
                }
                btnUpd.Enabled = true;

            }
            else if (R == 0)
            {

                btnUpd.Enabled = false;
                gr2.Location = new Point(15, w);

                w -= 10;
                if (w <= (-319))
                {
                    gr2.Location = new Point(15, (-319));
                    w = -6;
                }

                btnUpd.Location = new Point(76, y);

                y -= 10;
                if (y <= 0)
                {
                    timer2.Enabled = false;
                    btnUpd.Location = new Point(76, 0);
                    btnUpd.Enabled = true;
                    y = 312;
                }
            }
        }

        private void cmbcarclassupd_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txttelUpd_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void txtmobUpd_KeyPress(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i < 8 || 8 < i && i < 48 || i > 57)
            {
                e.Handled = true;
                System.Console.Beep();
            }

            else
                e.Handled = false;
        }

        private void txtcaridUpd_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            form_rpt_driver frmRptDriver = new form_rpt_driver();
            frmRptDriver.Show();
        }

        private void btndeleteall_Click(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectDriverPresent().Tables["present"]);
            txtdvrstatus.DataBindings.Clear();
            txtdvrstatus.Text = "";
            txtdvrstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (Int16.Parse(txtdvrstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("شما قادر به حذف کردن کل رانندگان نیستید. چون برخی از آنها حضور زده اند.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                driver Driver = new driver();
                FarsiMessage.ShowMessageBox("آیا برای حذف کل رانندگان اطمینان دارید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Driver.DeleteAllDriver();
                    car Car = new car();
                    Car.UpdateAllCarStatus();

                    FarsiMessage.ShowMessageBox("کل رانندگان حذف گردیدند", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    txtidDel.Text = "";
                    lblnameDel.Text = "";
                    lblcarDel.Text = "";
                }
            }
        }

        private void txtcaridUpd_TextChanged(object sender, EventArgs e)
        {
            if (txtcaridUpd.Text == "0")
            {
                lblcarclassupd.Text = "شخصی";
                lblcarnameUpd.Text = "شخصی";
            }
        }

        private void btnupdatecar_Click(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectDriverPresent(txtidUpd.Text).Tables["present"]);
            txtdvrstatus.DataBindings.Clear();
            txtdvrstatus.Text = "";
            txtdvrstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (Int32.Parse(txtdvrstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("این راننده در حال انجام سرویس می باشد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }

            else if (txtidUpd.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidUpd.Focus();
            }

            else if (Int32.Parse(count.Text) == 0)
            {
                FarsiMessage.ShowMessageBox("راننده با کد مربوطه یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }

            else if (rdbUpd1.Checked == true)
            {
                if (txtcarnameUpd.Text == "" || cmbcarclassupd.Text == "")
                    FarsiMessage.ShowMessageBox("لطفا نام و کلاس ماشین را وارد نمایید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                else
                {
                    driver Driver = new driver();
                    FarsiMessage.ShowMessageBox("آیا با ویرایش اطلاعات موافق هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                    if (FarsiMessage.ClickButton == ButtonClicked.بله)
                    {
                        if (CARID.ToString() != "0")
                        {
                            car Car = new car();
                            Car.SetCarID(CARID.ToString());
                            Car.UpdateCarStatus2();
                        }
                        Driver.SetDvrID(txtidUpd.Text);
                        Driver.SetDvrCar(txtcarnameUpd.Text);
                        Driver.SetDvrCarClass(cmbcarclassupd.Text);
                        Driver.SetCarID("0");
                        Driver.UpdateDriver();

                        txtcaridUpd.Items.Clear();
                        txtdvrcarid.Items.Clear();
                        CountCar();
                        FarsiMessage.ShowMessageBox("اطلاعات راننده ویرایش شد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    }
                }
            }

            else if (rdbUpd2.Checked == true)
            {
                if (lblcarnameUpd.Text == "")
                    FarsiMessage.ShowMessageBox("اطلاعات اصلی را نباید پاک نمود. اطلاعات را وارد کنید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                else
                {
                    driver Driver = new driver();
                    FarsiMessage.ShowMessageBox("آیا با ویرایش اطلاعات موافق هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                    if (FarsiMessage.ClickButton == ButtonClicked.بله)
                    {
                        if (txtcaridUpd.Text != CARID.ToString() && CARID.ToString() != "0")
                        {
                            car Car = new car();
                            Car.SetCarID(CARID.ToString());
                            Car.UpdateCarStatus2();
                            MessageBox.Show("ok");
                        }
                        Driver.SetDvrID(txtidUpd.Text);
                        Driver.SetDvrCar(lblcarnameUpd.Text);
                        Driver.SetDvrCarClass(lblcarclassupd.Text);
                        Driver.SetCarID(txtcaridUpd.Text);
                        Driver.UpdateDriver();

                        car Car2 = new car();
                        Car2.SetCarID(txtcaridUpd.Text);
                        Car2.UpdateCarStatus();

                        txtcaridUpd.Items.Clear();
                        txtdvrcarid.Items.Clear();
                        CountCar();
                        FarsiMessage.ShowMessageBox("اطلاعات راننده ویرایش شد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    }
                }
            }
        }

        private void btnupdatedvr_Click(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectDriverPresent(txtidUpd.Text).Tables["present"]);
            txtdvrstatus.DataBindings.Clear();
            txtdvrstatus.Text = "";
            txtdvrstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (Int32.Parse(txtdvrstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("این راننده در حال انجام سرویس می باشد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }

            else if (txtidUpd.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidUpd.Focus();
            }

            else if (Int32.Parse(count.Text) == 0)
            {
                FarsiMessage.ShowMessageBox("راننده با کد مربوطه یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                if (txtnameUpd.Text == "" || txtfnameUpd.Text == "" || txttelUpd.Text == "" || txtaddressUpd.Text == "")
                {
                    FarsiMessage.ShowMessageBox("اطلاعات اصلی نباید پاک شوند", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                }
                else
                {
                    driver Driver = new driver();
                    FarsiMessage.ShowMessageBox("آیا با ویرایش اطلاعات موافق هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                    if (FarsiMessage.ClickButton == ButtonClicked.بله)
                    {
                        Driver.SetDvrID(txtidUpd.Text);
                        Driver.SetDvrName(txtnameUpd.Text);
                        Driver.SetDvrFname(txtfnameUpd.Text);
                        Driver.SetDvrAddress(txtaddressUpd.Text);
                        Driver.SetDvrPhone(txttelUpd.Text);
                        Driver.SetDvrMobile(txtmobUpd.Text);
                        Driver.UpdateDriver2();

                        FarsiMessage.ShowMessageBox("اطلاعات راننده ویرایش شد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    }
                }
            }
        }

        private void rdbUpd1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (rdbUpd1.Checked == true)
            {
                dvrcarid = "0";
                dvrcarname = txtcarnameUpd.Text;
                dvrcarclass = cmbcarclassupd.Text;
                txtcarnameUpd.Enabled = true;
                cmbcarclassupd.Enabled = true;
                lblcarnameUpd.Enabled = false;
                lblcarclassupd.Enabled = false;
                txtcaridUpd.Enabled = false;
                lblcarclassupd.Text = "";
                lblcarnameUpd.Text = "";
            }
        }

        private void rdbUpd2_CheckedChanged_1(object sender, EventArgs e)
        {
            if (rdbUpd2.Checked == true)
            {
                dvrcarid = txtcaridUpd.Text;
                dvrcarname = lblcarnameUpd.Text;
                dvrcarclass = lblcarclassupd.Text;
                txtcarnameUpd.Enabled = false;
                cmbcarclassupd.Enabled = false;
                lblcarnameUpd.Enabled = true;
                lblcarclassupd.Enabled = true;
                txtcaridUpd.Enabled = true;
                txtcarnameUpd.Text = "";
                cmbcarclassupd.Text = "";
            }
        }

        private void txtcaridUpd_SelectedIndexChanged(object sender, EventArgs e)
        {
            car Car = new car();
            objdv = new DataView(Car.SelectCar(txtcaridUpd.Text).Tables["car"]);
            lblcarnameUpd.DataBindings.Clear();
            lblcarnameUpd.Text = "";
            lblcarnameUpd.DataBindings.Add("text", objdv, "نام ماشین");
            lblcarclassupd.DataBindings.Clear();
            lblcarclassupd.Text = "";
            lblcarclassupd.DataBindings.Add("text", objdv, "کلاس ماشین");
        }
    }
}
