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;
using FarsiMsgBox;

namespace ajans
{
    public partial class form_customer : Form
    {
        int a = -175, c = 169, b = 0, d = -6;
        int x = -175, y = 169, z = 0, w = -6;
        int S = 0;
        int R = 0;

        DataView objdv;
        DataView objdv2;
        public form_customer()
        {
            InitializeComponent();
        }

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

        private void Binding2()
        {
            txtnameUpd.DataBindings.Clear();
            txttelUpd.DataBindings.Clear();
            txtaddressUpd.DataBindings.Clear();
            txtaddress1Upd.DataBindings.Clear();
             
            txtnameUpd.Text = "";
            txttelUpd.Text = "";
            txtaddressUpd.Text = "";
            txtaddress1Upd.Text = "";

            txtnameUpd.DataBindings.Add("text", objdv, "نام");
            txttelUpd.DataBindings.Add("text", objdv, "تلفن");
            txtaddressUpd.DataBindings.Add("text", objdv, "منطقه");
            txtaddress1Upd.DataBindings.Add("text", objdv, "آدرس دقیق");
        }

        private void SelectAreaAddress()
        {
            connection2.CreatConnection();
            connection2.objcommand.CommandText = "Select area_address from area where area_id <> 0 and area_delete=0 ORDER BY area_address";
            connection2.objreader = connection2.objcommand.ExecuteReader();

            while (connection2.objreader.Read())
            {
                txtcusaddress.Items.Add(connection2.objreader["area_address"].ToString());
                txtaddressUpd.Items.Add(connection2.objreader["area_address"].ToString());
            }
            connection2.objconnection.Close();
            txtcusaddress.Items.Add("سایر مناطق");
        }

        private void MAXCusID()
        {
            customer Customer = new customer();
            objdv = new DataView(Customer.MaxCusID().Tables["customer"]);
            txtcusid.DataBindings.Clear();
            txtcusid.Text = "";
            txtcusid.DataBindings.Add("text", objdv, "MAX");
            if (txtcusid.Text == "") txtcusid.Text = "100";
            txtcusid.Text = Convert.ToString(Convert.ToInt16(txtcusid.Text) + 1);
        }


        private void Insert_Click(object sender, EventArgs e)
        {
            customer Customer = new customer();
            if (txtcusname.Text == "" || txtcustel.Text == "" || txtcusaddress.Text=="")
            {
                FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردند.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }
            else
            {
                EuqalName();
                if (txt1.Text != "" && txt2.Text != "" && txt3.Text != "")
                {
                    FarsiMessage.ShowMessageBox("مشترکی با این مشخصات قبلا به ثبت رسیده است، لطفا نام مشترک را تغییر دهید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                    txt1.Text = "";
                    txt2.Text = "";
                    txt3.Text = "";
                }
                else
                {
                    Customer.SetCusID(txtcusid.Text);
                    Customer.SetCusName(txtcusname.Text);
                    Customer.SetCusStreet(txtcusaddress.Text);
                    Customer.SetCusAddress(txtcusstreet2.Text);
                    Customer.SetCusPhone(txtcustel.Text);
                    Customer.SetAreaID(txtareaid.Text);
                    Customer.SetCusAmount("0");
                    Customer.SetCusDelete("0");
                    Customer.InsertCustomer();

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

                    txtcusid.Text = "";
                    txtcusname.Text = "";
                    txtcusaddress.Text = "";
                    txtcusstreet2.Text = "";
                    txtcustel.Text = "";
                    txtareaid.Text = "";

                    MAXCusID();
                }
            }
        }

        private void form_customer_Load(object sender, EventArgs e)
        {
            MAXCusID();
            SelectAreaAddress();
        }

        private void txtcusaddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            area Area = new area();
            objdv = new DataView(Area.SelectArea3(txtcusaddress.Text).Tables["area"]);
            txtareaid.DataBindings.Clear();
            txtareaid.Text = "";
            txtareaid.DataBindings.Add("text", objdv, "کد منطقه");
            if (txtcusaddress.Text == "سایر مناطق") txtareaid.Text = "0";
        }

        private void txtcusaddress_TextChanged(object sender, EventArgs e)
        {
            area Area = new area();
            objdv = new DataView(Area.SelectArea3(txtcusaddress.Text).Tables["area"]);
            txtareaid.DataBindings.Clear();
            txtareaid.Text = "";
            txtareaid.DataBindings.Add("text", objdv, "کد منطقه");
        }

        private void EuqalName()
        {
            customer Customer = new customer();
            objdv = new DataView(Customer.SelectCustomer(txtcusname.Text, txtcustel.Text,txtareaid.Text).Tables["customer"]);
            txt1.DataBindings.Clear();
            txt1.DataBindings.Add("text", objdv, "نام مشترک");
            txt2.DataBindings.Clear();
            txt2.DataBindings.Add("text", objdv, "تلفن");
            txt3.DataBindings.Clear();
            txt3.DataBindings.Add("text", objdv, "نام منطقه");
        }

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

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

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

        private void txtsearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!rdb1.Checked)
            {
                int i = e.KeyChar;
                if (i < 8 || 8 < i && i < 48 || i > 57)
                {
                    e.Handled = true;
                    err.Clear();
                    err.SetError(txtsearch, lblSearch.Text);
                    System.Console.Beep();
                }
                else err.Clear();
            }
            else
                e.Handled = false;
        }

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

                }
                else if (rdb3.Checked == true)
                {
                    dgListCustomer.DataSource = Customer.SelectCustomer3(txtsearch.Text);
                    dgListCustomer.DataMember = "customer";
                }
            }
        }

        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;
                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 timer1_Tick(object sender, EventArgs e)
        {
            if (S == 1)
            {
                btnDel.Enabled = false;
                gr.Location = new Point(73, a);
                if (a < -6)
                    a += 10;
                if (a >= -6)
                {
                    gr.Location = new Point(73, -6);
                    a = (-175);
                }


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

            }
            else if (S == 0)
            {

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

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

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

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

        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 = (-175);
                }


                btnUpd.Location = new Point(92, z);
                if (z < 169)
                    z += 10;
                if (z >= 169)
                {
                    timer2.Enabled = false;
                    btnUpd.Location = new Point(92, 169);
                    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 <= (-175))
                {
                    gr2.Location = new Point(15, (-175));
                    w = -6;
                }

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

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

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

        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 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 txtidDel_TextChanged_1(object sender, EventArgs e)
        {
            p1.Visible = true;
            customer Customer = new customer();
            objdv = new DataView(Customer.SelectCustomer(txtidDel.Text).Tables["customer"]);
            Binding();
        }

        private void txtidUpd_TextChanged(object sender, EventArgs e)
        {
            customer Customer = new customer();
            objdv = new DataView(Customer.SelectCustomer(txtidUpd.Text).Tables["customer"]);
            Binding2();
        }

        private void delete_Click(object sender, EventArgs e)
        {
            request Request = new request();
            objdv = new DataView(Request.SelectCustomerRequest(txtidDel.Text).Tables["request"]);
            txtstatus.DataBindings.Clear();
            txtstatus.Text = "";
            txtstatus.DataBindings.Add("text", objdv, "وضعیت");
            if (txtidDel.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد اشتراک را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidDel.Focus();
            }
            else if (txtidDel.Text != "" && lblname.Text == "")
            {
                FarsiMessage.ShowMessageBox("مشترکی با این کد یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else if (Int16.Parse(txtstatus.Text) > 0)
            {
                FarsiMessage.ShowMessageBox("این مشترک درخواست سرویس داده است و قادر به حذف شدن نمی باشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                customer Customer = new customer();
                FarsiMessage.ShowMessageBox("آیا میخواهید مشترک مورد نظر را حذف نمایید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Customer.SetCusID(txtidDel.Text.ToString());
                    Customer.DeleteCustomer();
                    FarsiMessage.ShowMessageBox("مشترک مورد نظر حذف گردید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    txtidDel.Text = "";
                    lblname.Text = "";
                    lbltel.Text = "";
                    lblarea.Text = "";
                }
            }
        }

        private void update_Click(object sender, EventArgs e)
        {
            if (txtidUpd.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد اشتراک را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtidUpd.Focus();
            }
            else if (txtidUpd.Text != "" && txtnameUpd.Text == "")
                FarsiMessage.ShowMessageBox("مشترک با این کد یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            else
            {
                customer Customer = new customer();
                FarsiMessage.ShowMessageBox("آیا با ویرایش اطلاعات موافق هستید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    EuqalName();
                    if (txt1.Text != "" && txt2.Text != "" && txt3.Text != "")
                    {
                        FarsiMessage.ShowMessageBox("مشترکی با این مشخصات قبلا به ثبت رسیده است، لطفا نام مشترک را تغییر دهید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                        txt1.Text = "";
                        txt2.Text = "";
                        txt3.Text = "";
                    }
                    else
                    {
                        Customer.SetCusID(txtidUpd.Text);
                        Customer.SetCusName(txtnameUpd.Text);
                        Customer.SetCusPhone(txttelUpd.Text);
                        Customer.SetCusStreet(txtaddressUpd.Text);
                        Customer.SetCusAddress(txtaddress1Upd.Text);
                        Customer.SetAreaID(txtareaUpd.Text);

                        Customer.UpdateCustomer();

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

        private void txtaddressUpd_SelectedIndexChanged(object sender, EventArgs e)
        {
            area Area = new area();
            objdv2 = new DataView(Area.SelectArea3(txtaddressUpd.Text).Tables["area"]);
            txtareaUpd.DataBindings.Clear();
            txtareaUpd.Text = "";
            txtareaUpd.DataBindings.Add("text", objdv2, "کد منطقه");
        }

       private void txtaddressUpd_TextChanged(object sender, EventArgs e)
        { 
            area Area = new area();
            objdv2 = new DataView(Area.SelectArea3(txtaddressUpd.Text).Tables["area"]);
            txtareaUpd.DataBindings.Clear();
            txtareaUpd.Text = "";
            txtareaUpd.DataBindings.Add("text", objdv2, "کد منطقه");
        }

       private void txtcustel_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 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();
           }
       }


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

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

       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 list_Click(object sender, EventArgs e)
       {
           customer Customer = new customer();
           dgListCustomer.DataSource = Customer.SelectCustomer();
           dgListCustomer.DataMember = "customer";
       }

       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 txtaddressUpd_KeyPress(object sender, KeyPressEventArgs e)
       {
           e.Handled = true;
       }

       private void btndeleteall_Click(object sender, EventArgs e)
       {
           request Request = new request();
           objdv = new DataView(Request.SelectCustomerRequest().Tables["request"]);
           txtstatus.DataBindings.Clear();
           txtstatus.Text = "";
           txtstatus.DataBindings.Add("text", objdv, "وضعیت");
           if (Int16.Parse(txtstatus.Text) > 0)
           {
               FarsiMessage.ShowMessageBox("شما قادر به حذف کردن کل مشترکین نیستید. چون برخی از آنها درخواست سرویس داده اند.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
           }
           else
           {
               customer Customer = new customer();
               FarsiMessage.ShowMessageBox("آیا برای حذف کل مشترکین اطمینان دارید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
               if (FarsiMessage.ClickButton == ButtonClicked.بله)
               {
                   Customer.DeleteAllCustomer();
                   FarsiMessage.ShowMessageBox("کل مشترکین حذف گردیدند", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                   txtidDel.Text = "";
                   lblname.Text = "";
                   lbltel.Text = "";
                   lblarea.Text = "";
               }
           }
       }
    }
}
