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


namespace ajans
{
    public partial class form_customer_request1 : Form
    {
        PersianCalendar PersianDateTime = new PersianCalendar();
        DataView objdv;
        DataView objdv2;
        DataView objdvt;
        DataView objdvm;
        DataView objdvb;
        string time = "";
        string date = "";
        public form_customer_request1()
        {
            InitializeComponent();
        }

        private void binding()
        {
            lblname.DataBindings.Clear();
            txtreqstreet.DataBindings.Clear();
            txtreqaddress.DataBindings.Clear();
            lblname.Text = "";
            txtreqstreet.Text = "";
            txtreqaddress.Text = "";
            lblname.DataBindings.Add("text", objdv, "نام");
            txtreqstreet.DataBindings.Add("text", objdv, "منطقه");
            txtreqaddress.DataBindings.Add("text", objdv, "آدرس دقیق");
        }

        private void insertRequest()
        {
            request Request = new request();

            if (txtcusid.Text == "" || txtreqamount.Text == "")
                FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            else if (lblname.Text == "")
                FarsiMessage.ShowMessageBox("کد اشتراک اشتباه می باشد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            else
            {
                Request.SetReqID(txtreqid.Text);
                Request.SetCusID(txtcusid.Text);
                Request.SetReqCusName(lblname.Text);
                Request.SetReqDate(txtreqdate.Text);
                Request.SetReqTime(txtreqtime.Text);
                Request.SetReqCarClass(txtreqcartype.Text);
                Request.SetReqCarCount(txtreqcarcount.Text);
                Request.SetReqStreet(txtreqstreet.Text);
                Request.SetReqAddress(txtreqaddress.Text);
                Request.SetReqEndName(txtendname.Text);
                Request.SetReqAmount(txtreqamount.Text);
                Request.SetReqEndStreet(txtendstreet.Text);
                Request.SetReqEndAddress(txtendstreet2.Text);
                Request.SetReqSerStatus(txtreqcarcount.Text);
                Request.InsertRequest();
                FarsiMessage.ShowMessageBox("یک درخواست اضافه گردید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                Free();
                MAXReqID();
                reqStatus();

            }
        }

        private void Free()
        {
            txtcusid.Text = "";
            lblname.Text = "";
            txtreqcarcount.Text = "1";
            txtreqcartype.Text = "فرقی نمیکند";
            txtendstreet.Text = "سایر مناطق";
            txtendstreet2.Text = "";
            txtreqaddress.Text = "";
            txtreqamount.Text = "";
            txtamount.Text = "0";
            txtendname.Text = "";
            chkdate.Checked = false;
            chktime.Checked = false;
            chkend.Checked = true;
        }
        private void CorrectTime()
        {
            DateTime tt = DateTime.Now;
            string SX = tt.Hour.ToString("D2");
            string SY = tt.Minute.ToString("D2");
            string SZ = tt.Second.ToString("D2");
            time = SX + ":" + SY + ":" + SZ;
        }


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

        private void ServiceAmount()
        {
            int x = Int32.Parse(txtamountmin2.Text);
            int y = Int32.Parse(txtamountmin3.Text);
            int z = Int32.Parse(txtamountmin.Text);
            if (z == x && z == y)
                txtamount.Text = z.ToString();
            else if (z == y && z < x)
                txtamount.Text = x.ToString();
            else if (z < y && z == x)
                txtamount.Text = y.ToString();
            else if (z < y && z < x)
                txtamount.Text = (((x + y) * 2) / 3).ToString();
        }

        private void reqStatus()
        {
            {
                request Request = new request();
                present Present = new present();

                objdv = new DataView(Request.SelectRequestCount().Tables["request"]);
                lblcountreq.DataBindings.Clear();
                lblcountreq.Text = "";
                lblcountreq.DataBindings.Add("text", objdv, "کد درخواست");

                objdv2 = new DataView(Present.SelectPresentCount().Tables["present"]);
                lblcountprs.DataBindings.Clear();
                lblcountprs.Text = "";
                lblcountprs.DataBindings.Add("text", objdv2, "کد حضور");
            }
            {
                present Present = new present();
                objdvm = new DataView(Present.SelectMiddleCar().Tables["present"]);
                lblmiddlemodel.DataBindings.Clear();
                lblmiddlemodel.Text = "";
                lblmiddlemodel.DataBindings.Add("text", objdvm, "کد حضور");
            }
            {
                present Present = new present();
                objdvb = new DataView(Present.SelectButtonCar().Tables["present"]);
                lblbuttonmodel.DataBindings.Clear();
                lblbuttonmodel.Text = "";
                lblbuttonmodel.DataBindings.Add("text", objdvb, "کد حضور");
            }
            {
                present Present = new present();
                objdvt = new DataView(Present.SelectTopCar().Tables["present"]);
                lbltopmodel.DataBindings.Clear();
                lbltopmodel.Text = "";
                lbltopmodel.DataBindings.Add("text", objdvt, "کد حضور");
            }

        }

        private void MAXReqID()
        {
            request Request = new request();
            objdv = new DataView(Request.MaxReqID().Tables["request"]);
            txtreqid.DataBindings.Clear();
            txtreqid.Text = "";
            txtreqid.DataBindings.Add("text", objdv, "MAX");
            if (txtreqid.Text == "") txtreqid.Text = "0";
            txtreqid.Text = Convert.ToString(Convert.ToInt16(txtreqid.Text) + 1);
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            CorrectTime();
            CorrectDate();
            txtreqtime.Text = time;
            txtreqdate.Text = date;
        }

        private void txtcusid_TextChanged(object sender, EventArgs e)
        {
            {
                customer Customer = new customer();
                objdv = new DataView(Customer.SelectCustomer(txtcusid.Text).Tables["customer"]);
                binding();
            }
            {
                customer Customer = new customer();
                objdv2 = new DataView(Customer.SelectCusAreaAmount(txtcusid.Text).Tables["customer"]);
                txtamountmin2.DataBindings.Clear();
                txtamountmin2.Text = "";
                txtamountmin2.DataBindings.Add("text", objdv2, "هزینه مبدا");
            }

            if (lblname.Text != "") ServiceAmount(); else txtamount.Text = "";
        }
            

        private void form_customer_request1_Load(object sender, EventArgs e)
        {
            MAXReqID();
            MINAmount();
            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())
            {
                txtendstreet.Items.Add(connection2.objreader["area_address"].ToString());
            }
            txtendstreet.Items.Add("سایر مناطق");
            connection2.objconnection.Close();
            reqStatus();
            txtamountmin3.Text = txtamountmin.Text;
            for (int i = 1389; i <= 1450; i++)
            {
                cmby.Items.Add(i);
            }
        }

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

            if (txtendstreet.Text == "سایر مناطق")
            {
                txtareaid.Text = "0";
                txtamountmin3.Text = "0";
            }
            ServiceAmount();
        }

        private void MINAmount()
        {
            area Area = new area();
            objdv = new DataView(Area.SelectMinAmount().Tables["area"]);
            txtamountmin.DataBindings.Clear();
            txtamountmin.Text="";
            txtamountmin.DataBindings.Add("text", objdv, "هزینه شهری");
        }


        private void chkend_CheckedChanged(object sender, EventArgs e)
        {
            if (chkend.Checked == true) gbend.Enabled = true;
            else gbend.Enabled = false;
        }

        private void insert_Click(object sender, EventArgs e)
        {
            int comD = string.Compare(txtreqdate.Text, txtnewdate.Text);
            int comT = string.Compare(txtreqtime.Text, txtnewtime.Text);
            if (chkdate.Checked == true && chktime.Checked == true)
            {
                if (comD > 0) FarsiMessage.ShowMessageBox("زمان تاریخ انتخابی گذشته است", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                else if (comD == 0 && comT > 0) FarsiMessage.ShowMessageBox("زمان ساعت انتخابی گذشته است", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                else
                {
                    txtreqdate.Text = txtnewdate.Text;
                    txtreqtime.Text = txtnewtime.Text;
                    insertRequest();
                }
            }
            else if (chkdate.Checked == true && chktime.Checked == false)
            {
                if (comD > 0) FarsiMessage.ShowMessageBox("زمان تاریخ انتخابی گذشته است", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                else
                {
                    txtreqdate.Text = txtnewdate.Text;
                    insertRequest();
                }
            }
            else if (chkdate.Checked == false && chktime.Checked == true)
            {
                if (comT > 0) FarsiMessage.ShowMessageBox("زمان ساعت انتخابی گذشته است", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                else
                {
                    txtreqtime.Text = txtnewtime.Text;
                    insertRequest();
                }
            }

            else insertRequest();
        }

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

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

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

        private void cmby_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtnewdate.Text = cmby.Text + "/" + cmbm.Text + "/" + cmbd.Text;
        }

        private void cmbhur_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtnewtime.Text = cmbhur.Text + ":" + cmbmin.Text;
        }

        private void chkdatetime_CheckedChanged(object sender, EventArgs e)
        {
            if (chkdate.Checked == true)
            {
                cmby.Visible = true;
                cmbm.Visible = true;
                cmbd.Visible = true;
                label17.Visible = true;
                label18.Visible = true;
                label28.Visible = true;
            }
            else
            {
                cmby.Visible = false;
                cmbm.Visible = false;
                cmbd.Visible = false;
                label17.Visible = false;
                label18.Visible = false;
                label28.Visible = false;
            }
        }

        private void chktime_CheckedChanged(object sender, EventArgs e)
        {
            if (chktime.Checked == true)
            {
                cmbhur.Visible = true;
                cmbmin.Visible = true;
                label29.Visible = true;
                label30.Visible = true;
            }
            else
            {
                cmbhur.Visible = false;
                cmbmin.Visible = false;
                label29.Visible = false;
                label30.Visible = false;
            }
        }

        private void cmbm_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbm.Text == "07" || cmbm.Text == "08" || cmbm.Text == "09" || cmbm.Text == "10" || cmbm.Text == "11" || cmbm.Text == "12") cmbd.Items.Remove("31");
            else cmbd.Items.Add("31");
        }

        private void txtcusid_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 txtreqamount_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 cmbd_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

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

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

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

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

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

        private void form_customer_request1_Enter(object sender, EventArgs e)
        {
            MAXReqID();
        }

        private void form_customer_request1_Activated(object sender, EventArgs e)
        {
            MAXReqID();
            reqStatus();
        }


    }
}
