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

namespace TinyHeels
{
    public partial class controlExpenditure : UserControl
    {
        private static controlExpenditure _instance;
        public static controlExpenditure Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new controlExpenditure();
                }
                return _instance;
            }
        }
        public controlExpenditure()
        {
            InitializeComponent();
            txtDate.Value = DateTime.Parse(DateTime.Now.ToShortDateString());
            cboType.SelectedIndex = 0;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                string id = (string.IsNullOrWhiteSpace(txtId.Text)) ? "" : txtId.Text;
                if (id.Length == 0)
                {
                    //insert
                    string reason = (string.IsNullOrWhiteSpace(txtReason.Text)) ? "" : txtReason.Text;
                    string note = (string.IsNullOrWhiteSpace(txtNote.Text)) ? "" : txtNote.Text;
                    string type = cboType.Text;
                    DateTime date = DateTime.Parse(txtDate.Value.ToShortDateString());
                    double amount = double.Parse((string.IsNullOrWhiteSpace(txtAmount.Text)) ? "0.0." : txtAmount.Text);

                    //---------------------------- update ledger
                    Ledger led = new Ledger();
                    led.Date = DateTime.Parse(txtDate.Value.ToShortDateString());
                    led.Note = reason;
                    led.Particualrs = type;

                    if (type.Equals("Debit"))
                    {
                        led.Debit = amount;
                        led.insertDebit();

                    }
                    else
                    {
                        led.Credit = amount;
                        led.insertCredit();
                    }





                    ledgerGrid.DataSource = null;
                    ErrorBox d = new ErrorBox("Data Submitted Successfully!");
                    d.ShowDialog();

                    resetData();
                }
                else
                {
                    //update
                    ErrorBox error = new ErrorBox("You can not update Ledger. Delete and Add new.");
                    error.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void resetData()
        {
            try
            {
                txtId.Text = "";
                txtAmount.Text = "0";
                txtDate.Value = DateTime.Parse(DateTime.Now.ToShortDateString());
                txtReason.Text = "";
                txtNote.Text = "";
                cboType.SelectedIndex = 0;

                ledgerGrid.DataSource = null;

                lblBalance.Text = "0.0";
                lblCredit.Text = "0.0";
                lblDebit.Text = "0.0";
            }
            catch (Exception ex)
            {
                //update
                ErrorBox error = new ErrorBox("Error Reset");
                error.ShowDialog();
            }
        }

        private void dailyLedger(object sender, EventArgs e)
        {
            try
            {
                SelectDate sd = new SelectDate();
                sd.ShowDialog();
                if (Common.command == true)
                {
                    DateTime d = Common.firstDate;
                    Ledger led = new Ledger();
                    led.Date = d;
                    DataTable dt = led.getDailyLedger();
                    ledgerGrid.DataSource = dt;

                    if (dt.Rows.Count > 0)
                        calculateAmount();

                }
                else
                {
                    ledgerGrid.DataSource = null;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void MonthlyLedger(object sender, EventArgs e)
        {
            try
            {
                SelectMonth sd = new SelectMonth();
                sd.ShowDialog();
                if (Common.command == true)
                {
                    string month = Common.month;
                    int year = Common.year;
                    Ledger led = new Ledger();
                    DataTable dt = led.getMonthlyLedger();
                    ledgerGrid.DataSource = dt;

                    if (dt.Rows.Count > 0)
                        calculateAmount();
                }
                else
                {
                    ledgerGrid.DataSource = null;
                }
            }
            catch (Exception ex)
            {

            }
        }
        double credit = 0;
        double debit = 0;
        double balance = 0;
        public void calculateAmount()
        {
            credit = 0;
            debit = 0;
            balance = 0;
            for (int i = 0; i < ledgerGrid.Rows.Count; i++)
            {
                debit += double.Parse(ledgerGrid.Rows[i].Cells[1].Value.ToString());
                credit += double.Parse(ledgerGrid.Rows[i].Cells[2].Value.ToString());
            }
            balance = credit - debit;
            lblCredit.Text = credit.ToString();
            lblDebit.Text = debit.ToString();
            lblBalance.Text = balance.ToString();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (ledgerGrid.Rows.Count > 0)
                {

                    int ledgerCode = Int32.Parse(ledgerGrid.SelectedRows[0].Cells[0].Value.ToString());
                    Ledger led = new Ledger();
                    led.LedgerId = ledgerCode;
                    led.deleteLedger();

                    ErrorBox errror = new ErrorBox("Done Deletion");
                    errror.ShowDialog();

                    resetData();
                }
                else
                {
                    ErrorBox error = new ErrorBox("Sorry No Item To Delete");
                    error.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        
    }
}
