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 AFDHWEnter : Form
    {
        public AFDHWEnter()
        {
            InitializeComponent();
        }

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

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


        public static string SHS = "";
        private void AFDHWEnter_Load(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = DetectForm.Cod.ToString();
                if (textBox1.Text.ToString() != "")
                {
                    textBox6.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 = 338;
            this.Width = 443;
        }

        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 == "" || textBox6.Text == "" || textBox7.Text == "" || textBox9.Text == "")
                {
                    DialogResult resualt;
                    resualt = MessageBox.Show("!فیلدی را خالی گزاشته اید لطفا اطلاعات را کامل کنید", " ", 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 (radioButton1.Checked == true)
                        {
                            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() + "','" + "HT" + textBox6.Text.ToString() + "'," + Bed + "," + Bes + ",'" + textBox7.Text.ToString() + "')");
                        }

                        if (radioButton2.Checked == true)
                        {
                            if (Bes != 0)
                            {
                                Bes = Bes - Convert.ToInt64(textBox6.Text);
                                if (Bes == 0)
                                {
                                    Bed = 0;
                                    Bes = 0;
                                }
                                else if (Bes < 0)
                                {
                                    Bed = -(Bes);
                                    Bes = 0;
                                }
                                else Bed = 0;
                            }
                            else
                            {
                                Bes = 0;
                                Bed = Bed + 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() + "','" + "HB" + textBox6.Text.ToString() + "'," + Bed + "," + Bes + ",'" + textBox7.Text.ToString() + "')");
                        }
                    }
                    else
                    {
                        if (radioButton1.Checked == true)
                        {
                            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() + "','" + "HT" + textBox6.Text.ToString() + "',0," + Bes + ",'" + textBox7.Text.ToString() + "')");
                        }
                        if (radioButton2.Checked == true)
                        {
                            Bed = 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() + "','" + "HB" + textBox6.Text.ToString() + "'," + Bed + ",0,'" + textBox7.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 = "";
        }

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

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

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox7.Text = ".مبلغ  " + textBox6.Text.ToString() + " ریال بابت " + "    بدهکار شد ";
            textBox7.Select(5 + textBox6.Text.Length + 12, 3);
            textBox7.Focus();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            textBox7.Text = ".مبلغ  " + textBox6.Text.ToString() + " ریال بابت " + "    بستانکار شد ";
            textBox7.Select(6 + textBox6.Text.Length + 12, 3);
            textBox7.Focus();
        }

        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 { }
            textBox6.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)
        {
            Control.Focus(textBox6, e);
        }

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

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

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            int a = 0;
            for (int i = 0; i < textBox6.Text.Length; i++)
            {
                if (!char.IsNumber(Convert.ToChar(textBox6.Text.Substring(i, 1))))
                    a = 1;
            }
            if (a == 1)
            {
                MessageBox.Show("!لطفا عدد وارد کنید", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox6.Select(0, textBox6.Text.Length);
                textBox6.Focus();
            }
            else if (radioButton1.Checked == true)
            {
                textBox7.Text = ".مبلغ  " + textBox6.Text.ToString() + " ریال بابت " + "    بستانکار شد ";
                textBox7.Select(6 + textBox6.Text.Length + 12, 3);
            }
            else
            {
                textBox7.Text = ".مبلغ  " + textBox6.Text.ToString() + " ریال بابت " + "    بدهکار شد ";
                textBox7.Select(6 + textBox6.Text.Length + 12, 3);
            }
        }
    }
}
