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_service : Form
    {
        PersianCalendar PersianDateTime = new PersianCalendar();

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

        int o = 0;
        int p = 0;

        public form_service()
        {
            InitializeComponent();
        }
        DataView objdv;
        DataView objdv2;
        private void MAXSerID()
        {
            service Service = new service();
            objdv = new DataView(Service.MaxSerID().Tables["service"]);
            txtserid.DataBindings.Clear();
            txtserid.Text = "";
            txtserid.DataBindings.Add("text", objdv, "MAX");
            if (txtserid.Text == "") txtserid.Text = "0";
            txtserid.Text = Convert.ToString(Convert.ToInt16(txtserid.Text) + 1);
        }

        private void SelectRequestID()
        {
            connection2.CreatConnection();
            connection2.objcommand.CommandText = "Select request.req_id FROM request WHERE req_ser_status > '0' order by req_date,req_time";
            connection2.objreader = connection2.objcommand.ExecuteReader();

            while (connection2.objreader.Read())
            {
                txtreqid.Items.Add(connection2.objreader["req_id"].ToString());
            }
            connection2.objconnection.Close();
        }

        private void SelectPresentID()
        {
            connection2.CreatConnection();
            connection2.objcommand.CommandText = "Select present.prs_id from present where prs_status='0' ";
            connection2.objreader = connection2.objcommand.ExecuteReader();

            while (connection2.objreader.Read())
            {
                txtprsid.Items.Add(connection2.objreader["prs_id"].ToString());
            }
            connection2.objconnection.Close();
        }

        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 ServicePresent()
        {
            present Present = new present();
            if (txtcarclass.Text == "فرقی نمیکند")
                objdv = new DataView(Present.SelectPrsID().Tables["present"]);
            else
                objdv = new DataView(Present.SelectPrsID(txtcarclass.Text).Tables["present"]);
            txtprsid.DataBindings.Clear();
            txtprsid.DataBindings.Add("text", objdv, "کد حضور");
        }

        private void ServiceRequest()
        {
            request Request = new request();
            objdv = new DataView(Request.SelectReqID().Tables["request"]);
            txtreqid.DataBindings.Clear();
            txtreqid.DataBindings.Add("text", objdv, "req_id");
            if (txtreqid.Text == "")
            {
                timer3.Enabled = true;
                lblreq.Visible = true;
            }
            else
            {
                timer3.Enabled = false;
                lblreq.Visible = false;
            }
        }

        private void form_service_Load(object sender, EventArgs e)
        {
            MAXSerID();
            ServiceRequest();
            ServicePresent();
            SelectPresentID();
            SelectRequestID();

            if (txtprsid.Text == "") timer2.Enabled = true;
            else
            {
                label5.Visible = false;
                timer2.Enabled = false;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            request Request = new request();
            Request.SetReqID(txtreqid.Text);
            Request.UpdateServiceRequest();
        }

        private void form_service_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_service_show = 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 timer1_Tick(object sender, EventArgs e)
        {
            CorrectTime();
            CorrectDate();
            txttime.Text = time;
            txtdate.Text = date;
        }

        private void insert_Click(object sender, EventArgs e)
        {
            if (txtreqid.Text == "" || txtprsid.Text == "" || txtamount.Text == "")
            {
                FarsiMessage.ShowMessageBox("فیلدهای *دار باید تکمیل گردند.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            }
            else
            {
                present Present = new present();
                Present.SetPrsID(txtprsid.Text);
                request Request = new request();
                Request.SetReqID(txtreqid.Text);
                service Service = new service();
                Service.SetSerID(txtserid.Text);
                Service.SetReqID(txtreqid.Text);
                Service.SetPrsID(txtprsid.Text);
                Service.SetAmount(txtamount.Text);
                Service.SetDate(txtdate.Text);
                Service.SetTime(txttime.Text);
                Service.SetValue("0");

                Service.InsertService();

                txtreqid.Items.Remove(txtreqid.Text);
                txtprsid.Items.Remove(txtprsid.Text);

                FarsiMessage.ShowMessageBox("سرویس آغاز گردید.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);

                txtserid.Text = "";
                txtreqid.Text = "";
                txtprsid.Text = "";
                txtamount.Text = "";

                MAXSerID();
                ServiceRequest();
                ServicePresent();
                //SelectPresentID();
                //SelectRequestID();
            }
        }

        private void listcustomer_Click(object sender, EventArgs e)
        {
            request Request = new request();
            dgListRequest.DataSource = Request.SelectRequest();
            dgListRequest.DataMember = "request";
        }

        private void listdriver_Click(object sender, EventArgs e)
        {
            present Present = new present();
            dgListPresent.DataSource = Present.SelectPresent();
            dgListPresent.DataMember = "present";
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label5.Visible = true;
            switch (o)
            {
                case 0:
                    label5.ForeColor = System.Drawing.Color.Red;
                    break;
                case 1:
                    label5.ForeColor = System.Drawing.Color.Green;
                    break;
                case 2:
                    label5.ForeColor = System.Drawing.Color.Coral;
                    break;
                case 3:
                    label5.ForeColor = System.Drawing.Color.Blue;
                    break;
                case 4:
                    label5.ForeColor = System.Drawing.Color.BurlyWood;
                    break;
                case 5:
                    label5.ForeColor = System.Drawing.Color.Brown;
                    break;
            }
            o++;
            if (o == 5) o = 0;
        }

        private void txtprsid_SelectedIndexChanged(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectPresent2(txtprsid.Text).Tables["present"]);
            lbldvrname.DataBindings.Clear();
            lbldvrname.Text = "";
            lbldvrname.DataBindings.Add("text", objdv, "نام راننده");
            lbldvrclass.DataBindings.Clear();
            lbldvrclass.Text = "";
            lbldvrclass.DataBindings.Add("text", objdv, "کلاس ماشین");
        }

        private void txtreqid_SelectedIndexChanged(object sender, EventArgs e)
        {
            request Request = new request();
            objdv2 = new DataView(Request.SelectRequest(txtreqid.Text).Tables["request"]);
            lblcusname.DataBindings.Clear();
            lblcusname.Text = "";
            lblcusname.DataBindings.Add("text", objdv2, "نام مشتری");
            lbldate.DataBindings.Clear();
            lbldate.Text = "";
            lbldate.DataBindings.Add("text", objdv2, "تاریخ");
            lbltime.DataBindings.Clear();
            lbltime.Text = "";
            lbltime.DataBindings.Add("text", objdv2, "ساعت");
            lblcarcount.DataBindings.Clear();
            lblcarcount.Text = "";
            lblcarcount.DataBindings.Add("text", objdv2, "تعداد ماشین");
            lblcusclass.DataBindings.Clear();
            lblcusclass.Text = "";
            lblcusclass.DataBindings.Add("text", objdv2, "کلاس ماشین");
            lblstart.DataBindings.Clear();
            lblstart.Text = "";
            lblstart.DataBindings.Add("text", objdv2, "آدرس مبدا");
            lblend.DataBindings.Clear();
            lblend.Text = "";
            lblend.DataBindings.Add("text", objdv2, "آدرس مقصد");

            txtamount.DataBindings.Clear();
            txtamount.DataBindings.Add("text", objdv2, "req_amount");
            txtcarclass.DataBindings.Clear();
            txtcarclass.DataBindings.Add("text", objdv2, "کلاس ماشین");

            ServicePresent();

            if (txtprsid.Text == "") timer2.Enabled = true;
            else
            {
                label5.Visible = false;
                timer2.Enabled = false;
            }
        }

        private void txtreqid_TextChanged(object sender, EventArgs e)
        {
            request Request = new request();
            objdv2 = new DataView(Request.SelectRequest(txtreqid.Text).Tables["request"]);
            lblcusname.DataBindings.Clear();
            lblcusname.Text = "";
            lblcusname.DataBindings.Add("text", objdv2, "نام مشتری");
            lbldate.DataBindings.Clear();
            lbldate.Text = "";
            lbldate.DataBindings.Add("text", objdv2, "تاریخ");
            lbltime.DataBindings.Clear();
            lbltime.Text = "";
            lbltime.DataBindings.Add("text", objdv2, "ساعت");
            lblcarcount.DataBindings.Clear();
            lblcarcount.Text = "";
            lblcarcount.DataBindings.Add("text", objdv2, "تعداد ماشین");
            lblcusclass.DataBindings.Clear();
            lblcusclass.Text = "";
            lblcusclass.DataBindings.Add("text", objdv2, "کلاس ماشین");
            lblstart.DataBindings.Clear();
            lblstart.Text = "";
            lblstart.DataBindings.Add("text", objdv2, "آدرس مبدا");
            lblend.DataBindings.Clear();
            lblend.Text = "";
            lblend.DataBindings.Add("text", objdv2, "آدرس مقصد");

            txtamount.DataBindings.Clear();
            txtamount.DataBindings.Add("text", objdv2, "req_amount");
            txtcarclass.DataBindings.Clear();
            txtcarclass.DataBindings.Add("text", objdv2, "کلاس ماشین");

            if (txtprsid.Text == "") timer2.Enabled = true;
            else
            {
                label5.Visible = false;
                timer2.Enabled = false;
            }
        }

        private void txtprsid_TextChanged(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectPresent2(txtprsid.Text).Tables["present"]);
            lbldvrname.DataBindings.Clear();
            lbldvrname.Text = "";
            lbldvrname.DataBindings.Add("text", objdv, "نام راننده");
            lbldvrclass.DataBindings.Clear();
            lbldvrclass.Text = "";
            lbldvrclass.DataBindings.Add("text", objdv, "کلاس ماشین");
        }

        private void dgListPresent_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtprsid.Text = dgListPresent.CurrentRow.Cells[0].Value.ToString();
            }
            catch (NullReferenceException ex)
            {
                return;
            }
        }

        private void dgListRequest_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtreqid.Text = dgListRequest.CurrentRow.Cells[0].Value.ToString();
            }
            catch (NullReferenceException ex)
            {
                return;
            }
        }

        private void txtamount_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 btnprint_Click(object sender, EventArgs e)
        {
            form_rpt_service frmReportService = new form_rpt_service();
            frmReportService.Show();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            lblreq.Visible = true;
            switch (p)
            {
                case 0:
                    lblreq.ForeColor = System.Drawing.Color.Red;
                    break;
                case 1:
                    lblreq.ForeColor = System.Drawing.Color.Green;
                    break;
                case 2:
                    lblreq.ForeColor = System.Drawing.Color.Coral;
                    break;
                case 3:
                    lblreq.ForeColor = System.Drawing.Color.Blue;
                    break;
                case 4:
                    lblreq.ForeColor = System.Drawing.Color.BurlyWood;
                    break;
                case 5:
                    lblreq.ForeColor = System.Drawing.Color.Brown;
                    break;
            }
            p++;
            if (p == 5) p = 0;
        }

    }
}
