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


        string time = "";
        string date = "";
        public form_present()
        {
            InitializeComponent();
        }

        DataView objdv;
        DataView objdv2;

        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 MAXPrsID()
        {
            present Present = new present();
            objdv = new DataView(Present.MaxPrsID().Tables["present"]);
            txtprsid.DataBindings.Clear();
            txtprsid.Text = "";
            txtprsid.DataBindings.Add("text", objdv, "MAX");
            if (txtprsid.Text == "") txtprsid.Text = "0";
            txtprsid.Text = Convert.ToString(Convert.ToInt16(txtprsid.Text) + 1);
        }

        private void bindingDvr()
        {
            driver Driver = new driver();
            objdv = new DataView(Driver.SelectDriver(txtdvrid.Text).Tables["driver"]);
            lbldvrname.DataBindings.Clear();
            lbldvrcarname.DataBindings.Clear();
            lbldvrname.Text = "";
            lbldvrcarname.Text = "";
            lbldvrname.DataBindings.Add("text", objdv, "نام و نام خانوادگی");
            lbldvrcarname.DataBindings.Add("text", objdv, "کلاس ماشین");
        }

        private void binding()
        {
            lbldvrid.DataBindings.Clear();
            lbldvrnamedel.DataBindings.Clear();
            lblcarclassdel.DataBindings.Clear();

            lbldvrid.Text = "";
            lbldvrnamedel.Text = "";
            lblcarclassdel.Text = "";

            lbldvrid.DataBindings.Add("text", objdv, "کد راننده");
            lbldvrnamedel.DataBindings.Add("text", objdv, "نام راننده");
            lblcarclassdel.DataBindings.Add("text", objdv, "کلاس ماشین");  
        }

        private void SelectDriverID()
        {
            connection2.CreatConnection();
            connection2.objcommand.CommandText = "Select driver.dvr_id from driver where driver.dvr_delete=0 ";
            connection2.objreader = connection2.objcommand.ExecuteReader();

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

        private void CountDriver()
        {
            present Present = new present();
            objdv = new DataView(Present.SelectCarCount().Tables["count"]);
            lblcountdvr.DataBindings.Clear();
            lblcountdvr.DataBindings.Add("text", objdv, "تعداد");
            if (lblcountdvr.Text == "") lblcountdvr.Text = "0";
        }


        private void insert_Click(object sender, EventArgs e)
        {
            present Present = new present();
            driver Driver = new driver();
            if (txtdvrid.Text == "")
                FarsiMessage.ShowMessageBox("لطفا کد راننده را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
            else
            {
                if (txtprsdvrstatus.Text == "0")
                {
                    FarsiMessage.ShowMessageBox("امکان دو بار حضور زدن برای یک راننده وجود ندارد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                }
                else
                {
                    objdv = new DataView(Driver.SelectDriverID(txtdvrid.Text).Tables["driver"]);
                    objdv2 = new DataView(Present.SelectPresent(txtdvrid.Text).Tables["present"]);
                    txtdvrid.DataBindings.Clear();
                    txtdvrstatus.DataBindings.Clear();
                    txtdvrstatus.Text = "";
                    txtdvrstatus.DataBindings.Add("text", objdv, "وضعیت");
                    if (txtdvrstatus.Text == "1")
                    {
                        FarsiMessage.ShowMessageBox("آیا این راننده سرویس قبل خود را انجام داده است؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                        if (FarsiMessage.ClickButton == ButtonClicked.بله)
                        {
                            Driver.SetDvrID(txtdvrid.Text);
                            Driver.UpdateDriverStatus();
                            Present.SetPrsID(txtprsid.Text);
                            Present.SetDvrID(txtdvrid.Text);
                            Present.SetPrsDate(txtprsdate.Text);
                            Present.SetPrsTime(txtprstime.Text);
                            Present.SetPrsStatus("0");
                            Present.InsertPresent();

                            FarsiMessage.ShowMessageBox("یک راننده حضور خود را ثبت نمود.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);

                            Present.UpdateCountPresent2();
                            CountDriver();
                            txtprsid.Text = "";
                            txtdvrid.Text = "";
                            lbldvrname.Text = "";
                            lbldvrcarname.Text = "";


                            MAXPrsID();
                        }
                        else if (FarsiMessage.ClickButton == ButtonClicked.خیر)
                        {
                            FarsiMessage.ShowMessageBox("راننده حتما باید سرویس قبل خود را انجام داده باشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                        }
                    }
                    else
                    {
                        Present.SetPrsID(txtprsid.Text);
                        Present.SetDvrID(txtdvrid.Text);
                        Present.SetPrsDate(txtprsdate.Text);
                        Present.SetPrsTime(txtprstime.Text);
                        Present.SetPrsStatus("0");
                        Present.InsertPresent();

                        FarsiMessage.ShowMessageBox("یک راننده حضور خود را ثبت نمود.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                        Present.UpdateCountPresent2();
                        CountDriver();
                        txtprsid.Text = "";
                        txtdvrid.Text = "";
                        lbldvrname.Text = "";
                        lbldvrcarname.Text = "";

                        MAXPrsID();
                    }
                }
            }

        }

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

        private void form_present_Load(object sender, EventArgs e)
        {
            MAXPrsID();
            CountDriver();
            SelectDriverID();
        }

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

        private void delete_Click(object sender, EventArgs e)
        {
            present Present = new present();
            Present.SetPrsID(txtprsid.Text.ToString());
            Present.DeletePresent();
        }

        private void txtdvrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            present Present = new present();
            objdv2 = new DataView(Present.SelectPresent(txtdvrid.Text).Tables["present"]);
            bindingDvr();
            txtprsdvrstatus.Text = "";
            txtprsdvrstatus.DataBindings.Clear();
            txtprsdvrstatus.DataBindings.Add("text", objdv2, "وضعیت سرویس");
            
            if (txtprsdvrstatus.Text == "0")
            {
                FarsiMessage.ShowMessageBox("این راننده حضور خود را قبلا به ثبت رسانیده است.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                if (FarsiMessage.ClickButton == ButtonClicked.تائید) txtdvrid.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtdvrid.Text = "";
        }

        private void txtdvrid_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 form_present_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_present_show = false;
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        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 txtsearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            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();
        }

        private void txtsearch_Leave(object sender, EventArgs e)
        {
            err.Clear();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Opacity = 1;
            for (int i = 100; i > 1; i--)
            {
                this.Opacity -= 0.01;
                Application.DoEvents();
            }
            this.Close();
        }

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

                }
            }
        }

        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 txtiddel_TextChanged(object sender, EventArgs e)
        {
            present Present = new present();
            objdv = new DataView(Present.SelectPresent2(txtiddel.Text).Tables["present"]);
            binding();
        }

        private void delete_Click_1(object sender, EventArgs e)
        {
            if (txtiddel.Text == "")
            {
                FarsiMessage.ShowMessageBox("لطفا کد حضور را وارد نمایید.", "هشدار", ShowButton.تائید_تنها, MsgIcon.Warning);
                txtiddel.Focus();
            }
            else if (txtiddel.Text != "" && lbldvrid.Text == "")
            {
                FarsiMessage.ShowMessageBox("حضوری با این کد یافت نشد.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
            }
            else
            {
                present Present = new present();
                FarsiMessage.ShowMessageBox("آیا میخواهید حضور مورد نظر را لغو نمایید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
                if (FarsiMessage.ClickButton == ButtonClicked.بله)
                {
                    Present.SetPrsID(txtiddel.Text);
                    Present.DeletePresent();
                    FarsiMessage.ShowMessageBox("حضور مورد نظر لغو گردید", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                    Present.UpdateCountPresent();
                    txtiddel.Text = "";
                    lbldvrid.Text = "";
                    lbldvrnamedel.Text = "";
                    lblcarclassdel.Text = "";
                }
            }
        }

        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 btncancleall_Click(object sender, EventArgs e)
        {
            present Present = new present();
            FarsiMessage.ShowMessageBox("آیا میخواهید کل حضورها را لغو نمایید؟", "هشدار", ShowButton.بله_خیر, MsgIcon.Warning);
            if (FarsiMessage.ClickButton == ButtonClicked.بله)
            {
                Present.DeleteAllPresent();
                FarsiMessage.ShowMessageBox("کل حضورها لغو گردیدند.", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                Present.UpdateCountNull();
                txtiddel.Text = "";
                lbldvrid.Text = "";
                lbldvrnamedel.Text = "";
                lblcarclassdel.Text = "";
            }
        }
    }
}
