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;
using FarsiMsgBox;
namespace ajans
{
    
    public partial class form_area : 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_area()
        {
            InitializeComponent();
        }

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

        private void Binding2()
        {
            txtnameDU.DataBindings.Clear();
            txtamountDU.DataBindings.Clear();
            txtnameDU.Text = "";
            txtamountDU.Text = ""; 
            txtnameDU.DataBindings.Add("text", objdv, "نام منطقه");
            txtamountDU.DataBindings.Add("text", objdv, "مبلغ");
        }

        private void MAXAreaID()
        {
            area Area = new area();
            objdv = new DataView(Area.MaxAreaID().Tables["area"]);
            txtareaid.DataBindings.Clear();
            txtareaid.Text = "";
            txtareaid.DataBindings.Add("text", objdv, "MAX");
            if (txtareaid.Text == "") txtareaid.Text = "0";
            txtareaid.Text = Convert.ToString(Convert.ToInt16(txtareaid.Text) + 1);
        }

        private void insert_Click_1(object sender, EventArgs e)
        {
            area Area = new area();
            if (txtareaaddress.Text == "" || txtareaamount.Text == "")
            {
                FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردند.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }
            else
            {
                EuqalAddress();
                if (textBox5.Text != "")
                {
                    FarsiMessage.ShowMessageBox("نام این منطقه قبلا به ثبت رسیده است.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                    textBox5.Text = "";
                }
                else
                {
                    Area.SetAreaID(txtareaid.Text);
                    Area.SetAreaAddress(txtareaaddress.Text);
                    Area.SetAreaAmount(txtareaamount.Text);

                    Area.InsertArea();

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

                    txtareaid.Text = "";
                    txtareaaddress.Text = "";
                    txtareaamount.Text = "";
                    MAXAreaID();
                }
            }
        }

        private void form_area_Load(object sender, EventArgs e)
        {
            MAXAreaID();
            area Area = new area();
            if (rdb1.Checked == true) lblSearch.Text = "لطفا نام منطقه را وارد نمایید";
        }

        private void delete_Click(object sender, EventArgs e)
        {
            customer Customer = new customer();
            objdv = new DataView(Customer.SelectCustomerArea(txtcodDU.Text).Tables["customer"]);
            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
            {
                area Area = new area();
                FarsiMessage.ShowMessageBox("آیا میخواهید منطقه مورد نظر را حذف نمایید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Area.SetAreaID(txtcodDU.Text.ToString());
                    Area.DeleteArea();
                    p1.Visible = false;
                    p2.Visible = true;
                    FarsiMessage.ShowMessageBox("منطقه مورد نظر حذف گردید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);

                    txtcodDU.Text = "";
                    lblname.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 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 list_Click(object sender, EventArgs e)
        {
            area Area = new area();
            dgsearch.DataSource = Area.SelectArea();
            dgsearch.DataMember = "area";
        }

        private void search_Click_1(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                FarsiMessage.ShowMessageBox(lblSearch.Text, "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtsearch.Focus();
            }
            else
            {
                area Area = new area();
                if (rdb2.Checked == true)
                {
                    dgsearch.DataSource = Area.SelectArea(txtsearch.Text);
                    dgsearch.DataMember = "area";
                }
                else if (rdb1.Checked == true)
                {
                    dgsearch.DataSource = Area.SelectArea2(txtsearch.Text);
                    dgsearch.DataMember = "area";

                }
            }
        }

        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 txtcodDU_TextChanged(object sender, EventArgs e)
        {
            p1.Visible = true;
            p2.Visible = false;
            area Area = new area();
            objdv = new DataView(Area.SelectArea(txtcodDU.Text).Tables["area"]);
            Binding();
        }

        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
            {
                area Area = new area();
                FarsiMessage.ShowMessageBox("آیا با ویرایش اطلاعات موافق هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    EuqalAddress2();

                    if (textBox6.Text != "" && textBox7.Text != txtidDU.Text)
                    {
                        FarsiMessage.ShowMessageBox("نام این منطقه قبلا به ثبت رسیده است. لطفا نام دیگری انتخاب نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                    }
                    else
                    {
                        Area.SetAreaID(txtidDU.Text);
                        Area.SetAreaAddress(txtnameDU.Text);
                        Area.SetAreaAmount(txtamountDU.Text);
                        Area.UpdateArea();
                        FarsiMessage.ShowMessageBox("اطلاعات ویرایش شد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    }
                }
            }
        }

        private void txtidDU_TextChanged(object sender, EventArgs e)
        {
            area Area = new area();
            objdv = new DataView(Area.SelectArea(txtidDU.Text).Tables["area"]);
            Binding2();
            textBox6.Text = "";
        }

        private void EuqalAddress()
        {
            area Area = new area();
            objdv = new DataView(Area.SelectArea3(txtareaaddress.Text).Tables["area"]);
            textBox5.DataBindings.Clear();
            textBox5.DataBindings.Add("text", objdv, "نام منطقه");
        }

        private void EuqalAddress2()
        {
            area Area = new area();
            objdv = new DataView(Area.SelectArea2(txtnameDU.Text).Tables["area"]);
            textBox6.DataBindings.Clear();
            textBox6.DataBindings.Add("text", objdv, "نام منطقه");
            textBox7.DataBindings.Clear();
            textBox7.DataBindings.Add("text", objdv, "کد منطقه");
        }

        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 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_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;
                txtcodDU.Focus();
            }
            else if (S == 1)
            {
                S = 0;
                timer1.Enabled = true;
                btnDel.Image = ajans.Properties.Resources.move2;
                p1.Visible = false;
                p2.Visible = false;
                btnUpd.Enabled = true;
                gr.Enabled = false;
            }
        }

        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;
                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;
            }
        }

        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 form_area_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_area_show = false;
        }

        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 txtareaamount_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 btndeleteall_Click(object sender, EventArgs e)
        {
            customer Customer = new customer();
            objdv = new DataView(Customer.SelectCustomerArea().Tables["customer"]);
            txtstatus.DataBindings.Clear();
            txtstatus.Text = "";
            txtstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (Int16.Parse(txtstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("شما قادر به حذف کردن کل مناطق نیستید. چون آنها در جداول دیگری نیز استفاده شده اند.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                area Area = new area();
                FarsiMessage.ShowMessageBox("آیا برای حذف کل مناطق اطمینان دارید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Area.DeleteAllArea();
                    p1.Visible = false;
                    p2.Visible = true;
                    FarsiMessage.ShowMessageBox("کل مناطق حذف گردیدند.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);

                    txtcodDU.Text = "";
                    lblname.Text = "";
                }
            }
        }

    }
}