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_request2 : Form
    {

        PersianCalendar PersianDateTime = new PersianCalendar();
        DataView objdv;
        DataView objdv2;
        DataView objdvt;
        DataView objdvm;
        DataView objdvb;

        string time = "";
        string date = "";

        public form_customer_request2()
        {
            InitializeComponent();
        }


        private void Free()
        {
            txtreqcarcount.Text = "1";
            txtreqcartype.Text = "فرقی نمیکند";
            txtendstreet.Text = "سایر مناطق";
            txtendstreet2.Text = "";
            txtreqamount.Text = "";
            txtamount.Text = "0";
            txtendname.Text = "";
            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 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 form_customer_request2_Load(object sender, EventArgs e)
        {
            MAXReqID();
            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();
        }

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

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

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

        private void txtendstreet_SelectedIndexChanged(object sender, EventArgs e)
        {
            area Area = new area();
            objdv = new DataView(Area.SelectArea3(txtendstreet.Text).Tables["area"]);
            txtamount.DataBindings.Clear();
            txtamount.Text = "";
            txtamount.DataBindings.Add("text", objdv, "مبلغ");


            if (txtendstreet.Text == "سایر مناطق")
            {
                txtamount.Text = "0";
            }
            txtamount.Text = Int32.Parse(txtamount.Text).ToString();
        }

        private void insert_Click(object sender, EventArgs e)
        {
            request Request = new request();

            if (txtreqamount.Text == "")
                FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردد.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            else
            {
                Request.SetReqID(txtreqid.Text);
                Request.SetCusID("0");
                Request.SetReqCusName("حضوری");
                Request.SetReqDate(txtreqdate.Text);
                Request.SetReqTime(txtreqtime.Text);
                Request.SetReqCarClass(txtreqcartype.Text);
                Request.SetReqCarCount(txtreqcarcount.Text);
                Request.SetReqStreet("حضوری");
                Request.SetReqEndStreet(txtendstreet.Text);
                Request.SetReqEndAddress(txtendstreet2.Text);
                Request.SetReqEndName(txtendname.Text);
                Request.SetReqAmount(txtreqamount.Text);
                Request.SetReqSerStatus(txtreqcarcount.Text);

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

                MAXReqID();
                Free();
                reqStatus();

            }
        }

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

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

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

    }
}
