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 CrystalDecisions.CrystalReports.Engine;

namespace ajans
{
    public partial class form_car : Form
    {

        int a = -135, c = 129, b = 0, d = -6;
        int x = -135, y = 129, z = 0, w = -6;
        int S = 0;
        int R = 0;
        DataView objdv;

        public form_car()
        {
            InitializeComponent();
        }

        private void Binding()
        {
            lblname.DataBindings.Clear();
            lblname.Text = "";
            lblname.DataBindings.Add("text", objdv, "نام ماشین");
        }

        private void Binding2()
        {
            txtnameDU.DataBindings.Clear();
            txtnumberDU.DataBindings.Clear();
            cmbcarUPD.DataBindings.Clear();
            txtnameDU.Text = "";
            txtnumberDU.Text = "";
            cmbcarUPD.Text = "";
            txtnameDU.DataBindings.Add("text", objdv, "نام ماشین");
            txtnumberDU.DataBindings.Add("text", objdv, "نمره ماشین");
            cmbcarUPD.DataBindings.Add("text", objdv, "کلاس ماشین");
        }

        private void MAXCarID()
        {
            car Car = new car();
            objdv = new DataView(Car.MaxCarID().Tables["car"]);
            txtcarid.DataBindings.Clear();
            txtcarid.Text = "";
            txtcarid.DataBindings.Add("text", objdv, "MAX");
            txtcarid.Text = Convert.ToString(Convert.ToInt16(txtcarid.Text) + 1);
        }

        private void insert_Click(object sender, EventArgs e)
        {
            car Car = new car();
            if (txtcartype.Text == "" || txtcarnumber.Text == "" || cmbcarclass.Text == "")
            {
                FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردند.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }
            else
            {
                Car.SetCarID(txtcarid.Text);
                Car.SetCarType(txtcartype.Text);
                Car.SetCarNumber(txtcarnumber.Text);
                Car.SetCarStatus("آزاد");
                Car.SetCarClass(cmbcarclass.Text);

                Car.InsertCar();

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

                txtcarid.Text = "";
                txtcartype.Text = "";
                txtcarnumber.Text = "";
                cmbcarclass.Text = "";
                MAXCarID();
            }
        }



        private void list_Click(object sender, EventArgs e)
        {
            car Car = new car();
            dgListCar.DataSource = Car.SelectCar();
            dgListCar.DataMember = "car";
        }

        private void edit_Click(object sender, EventArgs e)
        {
            car Car = new car();
            Car.SetCarID(txtcarid.Text);
            Car.SetCarType(txtcartype.Text);
            Car.SetCarNumber(txtcarnumber.Text);
            Car.SetCarClass(cmbcarclass.Text);
            Car.UpdateCar();
        }

        private void form_car_Load(object sender, EventArgs e)
        {
            MAXCarID();
            if (rdb1.Checked == true) lblSearch.Text = "لطفا نام ماشین را وارد نمایید";
        }

        private void rdb2_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb2.Checked == true)
            {
                lblSearch.Text = "لطفا کد ماشین را وارد نمایید";
                txtsearch.Text = "";
                txtsearch.Focus();
            }
        }

        private void rdb1_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb1.Checked == true)
            {
                lblSearch.Text = "لطفا نام ماشین را وارد نمایید";
                txtsearch.Text = "";
                txtsearch.Focus();
            }
        }

        private void search_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                FarsiMessage.ShowMessageBox(lblSearch.Text, "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtsearch.Focus();
            }
            else
            {
                car Car = new car();
                if (rdb2.Checked == true)
                {
                    dgListCar.DataSource = Car.SelectCar(txtsearch.Text);
                    dgListCar.DataMember = "car";
                }
                else if (rdb1.Checked == true)
                {
                    dgListCar.DataSource = Car.SelectCar2(txtsearch.Text);
                    dgListCar.DataMember = "car";

                }
            }
        }

        private void txtsearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rdb2.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 update_Click(object sender, EventArgs e)
        {
            if (txtidDU.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد ماشین را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidDU.Focus();
            }
            else if (txtidDU.Text != "" && txtnameDU.Text == "")
            {
                FarsiMessage.ShowMessageBox("ماشین با این کد یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                car Car = new car();
                FarsiMessage.ShowMessageBox("آیا با ویرایش اطلاعات موافق هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Car.SetCarID(txtidDU.Text);
                    Car.SetCarType(txtnameDU.Text);
                    Car.SetCarNumber(txtnumberDU.Text);
                    Car.SetCarClass(cmbcarUPD.Text);
                    Car.UpdateCar();
                    FarsiMessage.ShowMessageBox("اطلاعات ویرایش شد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                }
            }
        }

        private void txtidDU_TextChanged(object sender, EventArgs e)
        {
            car Car = new car();
            objdv = new DataView(Car.SelectCar(txtidDU.Text).Tables["car"]);
            Binding2();
            if (txtidDU.Text == "" || Int16.Parse(txtidDU.Text) == 0)
            {
                txtnameDU.Text = "";
                txtnumberDU.Text = "";
            }
        }

        private void txtidDU_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 txtcodDU_TextChanged(object sender, EventArgs e)
        {
            car Car = new car();
            objdv = new DataView(Car.SelectCar(txtcodDU.Text).Tables["car"]);
            Binding();
            if (txtcodDU.Text == "" || Int16.Parse(txtcodDU.Text) == 0) lblname.Text = "";
        }

        private void txtcodDU_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 delete_Click(object sender, EventArgs e)
        {
            driver Driver = new driver();
            objdv = new DataView(Driver.SelectCarDriver(txtcodDU.Text).Tables["driver"]);
            txtstatus.DataBindings.Clear();
            txtstatus.Text = "";
            txtstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (txtcodDU.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد ماشین را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtcodDU.Focus();
            }
            else if (txtcodDU.Text != "" && lblname.Text == "")
            {
                FarsiMessage.ShowMessageBox("ماشین با این کد یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else if (Int16.Parse(txtstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("این ماشین در حال حاضر کرایه داده شده است و قادر به حذف شدن نمی باشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                car Car = new car();
                FarsiMessage.ShowMessageBox("آیا میخواهید ماشین مورد نظر را حذف نمایید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Car.SetCarID(txtcodDU.Text.ToString());
                    Car.DeleteCar();
                    FarsiMessage.ShowMessageBox("ماشین مورد نظر حذف گردید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    txtcodDU.Text = "";
                    lblname.Text = "";
                }
            }
        }

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


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

            }
            else if (S == 0)
            {

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

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

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

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

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


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

            }
            else if (R == 0)
            {

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

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

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

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

            }
        }

        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;
                p4.Visible = false;
                p3.Visible = true;
                txtidDU.Focus();
            }
            else if (R == 1)
            {
                R = 0;
                timer2.Enabled = true;
                btnUpd.Image = ajans.Properties.Resources.move2;
                btnDel.Enabled = true;
                p3.Visible = false;
                gr2.Enabled = false;
                p4.Visible = true;
            }
        }

        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;
                p3.Visible = false;
                txtcodDU.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 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_car_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_car_show = false;
        }

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

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

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