using System;
using System.Data;
using System.Windows.Forms;
using TinyHeels.App_Code;

namespace TinyHeels
{
    public partial class ManageDakhal : Form
    {
        public ManageDakhal()
        {
            InitializeComponent();
            txtDate.Value = DateTime.Parse(DateTime.Now.ToShortDateString());

            try
            {
                Dakhal d = new Dakhal();
                d.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                bool x = d.checkVoucher();
                if (x)
                {
                    lblNotification.Text = "There is one open voucher";
                }
                else
                {
                    lblNotification.Text = "There are not any open voucher";
                    double latest = d.getRecentAmount();
                    txtDakhalAmount.Text = latest.ToString();
                }
            }
            catch (Exception ex)
            {
            }


        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double amount = double.Parse((string.IsNullOrWhiteSpace(txtDakhalAmount.Text)) ? "0" : txtDakhalAmount.Text);

                DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
                ////------- firs check todays starting balance
                DakhalDetail obj = new DakhalDetail();
                obj.Date = date;
                bool check = obj.checkStartingBalance();
                //-------- if exists sorry already opened
                if (check)
                {
                    YesNo are = new YesNo("Open already want to update?");
                    are.ShowDialog();
                    if (Common.command == true)
                    {
                        obj.DakhalAmount = amount;
                        obj.updateStartingBalance();

                    }
                    else
                    {
                        ErrorBox error = new ErrorBox("Voucher update cancelled");
                        error.ShowDialog();
                    }
                }
                //--------- else enter new balance
                else
                {
                    obj.DakhalAmount = amount;
                    obj.Particulars = "In";
                    obj.Note = "Starting Balance";
                    obj.insertStartingBalance();
                }

                ErrorBox d = new ErrorBox("Done");
                d.ShowDialog();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime date = DateTime.Parse(txtDate.Value.ToShortDateString());
                Dakhal d = new Dakhal();
                d.Date = date;
                DataTable dt = d.getVoucherByDate();
                accountGrid.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void open(object sender, EventArgs e)
        {
            try
            {
                string id = accountGrid.SelectedRows[0].Cells[0].Value.ToString();
                Dakhal d = new Dakhal();
                d.Id = Int32.Parse(id);
                d.openVoucher();

                Dakhal dx = new Dakhal();
                DataTable dt = dx.getOpenAccounts();
                accountGrid.DataSource = dt;

            }
            catch (Exception ex)
            {
                MessageBox.Show("You haev not selected any record" + ex);
            }
        }

        private void close(object sender, EventArgs e)
        {
            try
            {
                string id = accountGrid.SelectedRows[0].Cells[0].Value.ToString();
                Dakhal d = new Dakhal();
                d.Id = Int32.Parse(id);
                d.closeVoucher();

                Dakhal dx = new Dakhal();
                DataTable dt = dx.getOpenAccounts();
                accountGrid.DataSource = dt;

            }
            catch (Exception ex)
            {
                MessageBox.Show("You haev not selected any record" + ex);
            }
        }

        private void viewopen(object sender, EventArgs e)
        {
            try
            {
                Dakhal d = new Dakhal();
                DataTable dt = d.getOpenAccounts();
                accountGrid.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
