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 System.Data.SqlClient;
using System.Collections;
using FarsiMsgBox;

namespace ajans
{
    public partial class form_queue : Form
    {
        PersianCalendar PersianDateTime = new PersianCalendar();
        DateTime str = new DateTime();

        public form_queue()
        {
            InitializeComponent();
        }
        DataView objdv;
        int iq = 0;

        public void Queue()
        {
            DataGridViewCheckBoxColumn dgvcc = new DataGridViewCheckBoxColumn();
            dgvcc.Name = "انتخاب";
            request Request = new request();
            dgqueue.Columns.Add(dgvcc);
            dgqueue.DataSource = Request.SelectQueue2();
            dgqueue.DataMember = "request";
            for (int i = 1; i < 5; i++) dgqueue.Columns[i].ReadOnly = true;
            dgvcc.Width = 40;
        }
        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_queue_FormClosed(object sender, FormClosedEventArgs e)
        {
            static_variables.form_queue_show = false;
        }
        private void form_queue_Load(object sender, EventArgs e)
        {
            if (static_variables.dtQueue.Columns.Count == 0)
            {
                static_variables.dtQueue.Columns.Add("کد");
                static_variables.dtQueue.Columns.Add("زمان");
                static_variables.dtQueue.Columns.Add("dtqueue");
            }
            dataGridView1.DataSource = static_variables.dtQueue;
            dataGridView1.Columns[2].Visible = false;
            Queue();
            Queue2();
            timer1.Enabled = true;
        }
        private void Queue2()
        {
            request Request = new request();
            objdv = new DataView(Request.SelectQueue().Tables["request"]);
            txtqueue2.DataBindings.Clear();
            txtqueue2.Text = "";
            txtqueue2.DataBindings.Add("text", objdv, "req_datetime");
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            txtnow.Text = PersianDateTime.GetYear(dt).ToString() + "/" + PersianDateTime.GetMonth(dt).ToString("D2") + "/" + PersianDateTime.GetDayOfMonth(dt).ToString("D2") + "," + dt.Hour.ToString("D2") + ":" + dt.Minute.ToString("D2") + ":" + dt.Second.ToString("D2");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                int C = string.Compare(txtqueue2.Text, txtnow.Text);
                if (C > 0)
                {
                    lbldays.Text = (Convert.ToDateTime(txtqueue2.Text) - Convert.ToDateTime(txtnow.Text)).Days.ToString();
                    lblhours.Text = (Convert.ToDateTime(txtqueue2.Text) - Convert.ToDateTime(txtnow.Text)).Hours.ToString();
                    lblminutes.Text = (Convert.ToDateTime(txtqueue2.Text) - Convert.ToDateTime(txtnow.Text)).Minutes.ToString();
                    lblsecondes.Text = (Convert.ToDateTime(txtqueue2.Text) - Convert.ToDateTime(txtnow.Text)).Seconds.ToString();
                    label8.Text = "ثانیه مانده به درخواست";
                }
                else
                {
                    lbldays.Text = (Convert.ToDateTime(txtnow.Text) - Convert.ToDateTime(txtqueue2.Text)).Days.ToString();
                    lblhours.Text = (Convert.ToDateTime(txtnow.Text) - Convert.ToDateTime(txtqueue2.Text)).Hours.ToString();
                    lblminutes.Text = (Convert.ToDateTime(txtnow.Text) - Convert.ToDateTime(txtqueue2.Text)).Minutes.ToString();
                    lblsecondes.Text = (Convert.ToDateTime(txtnow.Text) - Convert.ToDateTime(txtqueue2.Text)).Seconds.ToString();
                    label8.Text = "ثانیه گذشته از درخواست";
                }
            }
            catch (FormatException ex)
            {
                return;
            }
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            for (iq = 0; iq < dgqueue.RowCount; iq++)
            {
                if (Convert.ToBoolean(dgqueue.Rows[iq].Cells[0].Value) == true)
                {
                    if (!static_variables.arrayQueue.Contains(dgqueue.Rows[iq].Cells[1].Value.ToString()))
                    {
                        str = (Convert.ToDateTime(dgqueue.Rows[iq].Cells[3].Value.ToString() + "," + dgqueue.Rows[iq].Cells[4].Value.ToString()).AddMinutes(-(Double.Parse(txtalarm.Text))));
                        static_variables.dtQueue.Rows.Add(dgqueue.Rows[iq].Cells[1].Value.ToString(), txtalarm.Text, str);
                        static_variables.arrayQueue.Add(dgqueue.Rows[iq].Cells[1].Value.ToString());
                        static_variables.arrayQueue2.Add(str);
                        static_variables.arrayQueue3.Add(txtalarm.Text);
                    }
                }
                dgqueue.Rows[iq].Cells[0].Value = false;
            }
            dataGridView1.DataSource = static_variables.dtQueue;
            dataGridView1.Columns[2].Visible = false;

        }
        private void dgqueue_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtqueue2.Text = dgqueue.CurrentRow.Cells[3].Value.ToString() + ',' + dgqueue.CurrentRow.Cells[4].Value.ToString();
        }

        private void dgqueue_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dgqueue.CurrentRow.Cells[0].Value = !(Convert.ToBoolean(dgqueue.CurrentRow.Cells[0].Value));
        }

        private void btnselect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgqueue.RowCount; i++)
            {
                dgqueue.Rows[i].Cells[0].Value = true;
            }
        }

        private void btndeselect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgqueue.RowCount; i++)
            {
                dgqueue.Rows[i].Cells[0].Value = false;
            }
        }

        public void timer_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            string str = PersianDateTime.GetYear(dt).ToString() + "/" + PersianDateTime.GetMonth(dt).ToString("D2") + "/" + PersianDateTime.GetDayOfMonth(dt).ToString("D2") + "," + dt.Hour.ToString("D2") + ":" + dt.Minute.ToString("D2") + ":" + dt.Second.ToString("D2");
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            static_variables.arrayQueue2.RemoveAt(Int32.Parse(dataGridView1.CurrentRow.Index.ToString()));
            static_variables.arrayQueue3.RemoveAt(Int32.Parse(dataGridView1.CurrentRow.Index.ToString()));
            static_variables.arrayQueue.RemoveAt(Int32.Parse(dataGridView1.CurrentRow.Index.ToString()));
            static_variables.dtQueue.Rows[Int32.Parse(dataGridView1.CurrentRow.Index.ToString())].Delete();
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            static_variables.arrayQueue2.Clear();
            static_variables.arrayQueue3.Clear();
            static_variables.arrayQueue.Clear();
            static_variables.dtQueue.Clear();
        }

        private void txtalarm_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 txtalarm_Leave(object sender, EventArgs e)
        {
            if (txtalarm.Text == "")
            {
                FarsiMessage.ShowMessageBox("دقیقه نباید خالی بماند", "پیغام", ShowButton.تائید_تنها, MsgIcon.Information_1);
                txtalarm.Focus();
            }
        }
    }
}
