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;


namespace Apple_Software
{
    public partial class AFHWEnter : Form
    {
        public AFHWEnter()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DateTime Mdate = DateTime.Now;
            PersianCalendar PD = new PersianCalendar();
            try
            {
                DataManagement.DT = DataManagement.Search("select * from properties where Id=" + textBox1.Text.ToString() + "");

                pictureBox2.ImageLocation = DataManagement.DT.Rows[0][11].ToString();
                textBox2.Text = DataManagement.DT.Rows[0][2].ToString();
                textBox3.Text = DataManagement.DT.Rows[0][3].ToString();
                textBox9.Text = string.Format("{2}/{1}/{0}", PD.GetDayOfMonth(Mdate), PD.GetMonth(Mdate), PD.GetYear(Mdate));
            }
            catch { }
            numericUpDown1.Focus();
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            Control.Focus(textBox3, e);
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            Control.Focus(textBox9, e);
        }

        private void textBox9_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.ToString() == "13") numericUpDown1.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DetectForm.F = 1;
            AFPKW AFPKW = new AFPKW();
            this.Close();
            AFPKW.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("osk.exe");
        }

        private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
        {
            Control.Focus(textBox5, e);
        }

        private void radioButton2_KeyDown(object sender, KeyEventArgs e)
        {
            textBox5.Focus();
        }

        private void radioButton1_KeyDown(object sender, KeyEventArgs e)
        {
            textBox5.Focus();
        }

        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                button1.Focus();
            }       
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            int a = 0;

            if (textBox5.Text.ToString().Length >= 1)
            {
                for (int i = 0; i < textBox5.Text.Length; i++)
                {
                    if (!char.IsNumber(Convert.ToChar(textBox5.Text.Substring(i, 1))))
                        a = 1;
                }
                if (a == 1)
                {
                    MessageBox.Show("!لطفا عدد وارد کنید", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox5.Select(0, textBox5.Text.Length);
                    textBox5.Focus();
                }
                else
                {
                    if (radioButton2.Checked == true)
                    {
                        textBox6.Text = textBox5.Text.ToString();
                        textBox7.Text = ".مبلغ  " + textBox6.Text.ToString() + " ریال بابت " + numericUpDown1.Text.ToString() + " ساعت کار روزانه واریز شد ";
                    }
                    else
                    {
                        textBox6.Text = (Convert.ToInt64(numericUpDown1.Text) * Convert.ToInt64(textBox5.Text)).ToString();
                        textBox7.Text = ".مبلغ  " + textBox6.Text.ToString() + " ریال بابت " + numericUpDown1.Text.ToString() + " ساعت کار روزانه واریز شد ";
                    }
                }
            }       
        }

        private void textBox7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.ToString() == "13") button1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataManagement.DT = DataManagement.Search("select * from Hesab where Id=" + textBox1.Text.ToString() + "");

                if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox9.Text == "")
                {
                    DialogResult resualt;
                    resualt = MessageBox.Show("!فیلدی را خالی گزاشته اید لطفا اطلاعات را کامل کنید", "Error To Insert Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    decimal Bed = 0;
                    decimal Bes = 0;
                    if (Convert.ToInt32(DataManagement.DT.Rows.Count.ToString()) > 0)
                    {
                        DataManagement.DT = DataManagement.Search("select  top 1 * from Hesab where Id=" + textBox1.Text.ToString() + " order by SHS desc");
                        Bed = Convert.ToInt64(DataManagement.DT.Rows[0][4]);
                        Bes = Convert.ToInt64(DataManagement.DT.Rows[0][5]);

                        if (Bed != 0)
                        {
                            Bed = Bed - Convert.ToInt64(textBox6.Text);
                            if (Bed == 0)
                            {
                                Bed = 0;
                                Bes = 0;
                            }
                            else if (Bed < 0)
                            {
                                Bes = -(Bed);
                                Bed = 0;
                            }
                            else Bes = 0;
                        }
                        else
                        {
                            Bed = 0;
                            Bes = Bes + Convert.ToInt64(textBox6.Text);
                        }
                        DataManagement.DT = DataManagement.I_U_D("select * from Hesab where Id=" + textBox1.Text.ToString() + "", "Insert Into Hesab(Id,SHS,Data,Temp,Bed,Bes,Dis) Values(" + textBox1.Text.ToString() + "," + SHS + ",'" + textBox9.Text.ToString() + "','" + "WT" + textBox6.Text.ToString() + "'," + Bed + "," + Bes + ",'" + textBox7.Text.ToString() + "')");
                        DataManagement.DT = DataManagement.I_U_D("Select * From Time where Id=" + SHS + "", "Insert Into Time(Id,Time,Price) Values(" + SHS + "," + numericUpDown1.Value.ToString() + "," + textBox6.Text.ToString() + ")");
                    }
                    else
                    {
                        Bed = 0;
                        Bes = Convert.ToInt64(textBox6.Text);
                        DataManagement.DT = DataManagement.I_U_D("select * from Hesab where Id=" + textBox1.Text.ToString() + "", "Insert Into Hesab(Id,SHS,Data,Temp,Bed,Bes,Dis) Values(" + textBox1.Text.ToString() + "," + SHS + ",'" + textBox9.Text.ToString() + "','" + "WT" + textBox6.Text.ToString() + "',0," + Bes + ",'" + textBox7.Text.ToString() + "')");
                        DataManagement.DT = DataManagement.I_U_D("Select * From Time where Id=" + SHS + "", "Insert Into Time(Id,Time,Price) Values(" + SHS + "," + numericUpDown1.Value.ToString() + "," + textBox6.Text.ToString() + ")");
                    }

                    DialogResult resualt;
                    resualt = MessageBox.Show(".ساعت کاری روزانه با شماره سند  " + SHS + " ثبت شد", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DetectForm.F = 0;
                    DetectForm.Cod = "";
                    this.Close();
                }
            }
            catch { }      
        }

        private void button2_Click(object sender, EventArgs e)
        {
            close.Form(this);
            DetectForm.F = 0;
            DetectForm.Cod = "";
        }
        
        public static string SHS = "";
        private void AFHWEnter_Load(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = DetectForm.Cod.ToString();
                button3.Focus();
                if (textBox1.Text.ToString() != "")
                {
                    numericUpDown1.Focus();
                }
                DataManagement.DT = DataManagement.Search("select top 1 * from Hesab Order by SHS desc");
                if (DataManagement.DT.Rows.Count > 0)
                    SHS = (Convert.ToInt32(DataManagement.DT.Rows[0][1].ToString()) + 1).ToString();
                else
                    SHS = "1";
            }
            catch { };
            this.Height = 318;
            this.Width = 506;
        }

        private void AFHWEnter_FormClosed(object sender, FormClosedEventArgs e)
        {
            DetectForm.Cod = "";
        }

        private void AFHWEnter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.ToString() == "113")
                button1_Click(sender, e);
        }        
    }
}
