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

namespace TinyHeels
{
    public partial class ManagePayroll : Form
    {
        string emp;
        public ManagePayroll(string emp)
        {
            InitializeComponent();
            this.emp = emp;
            int year = Int32.Parse(DateTime.Now.ToString("yyyy"));
            cboYear.Items.Add(year + "");

            cboMonth.SelectedIndex = 0;
            cboYear.SelectedIndex = 0;
            cboType.SelectedIndex = 0;

            loadDatas();
            bindGrid();
            getBalance();
        }
        public void getBalance()
        {
            try
            {
                Payrolls p = new Payrolls();
                p.Emp = this.emp;
                double bx = p.getBalance();

                lblBalance.Text = bx + " Rs. Debit";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void bindGrid()
        {
            try
            {
                Payrolls p = new Payrolls();
                p.Emp = this.emp;
                p.Month = DateTime.Now.ToString("MMMM");
                p.Year = Int32.Parse(DateTime.Now.ToString("yyyy"));
                DataTable dt = p.getPayroll();
                salGrid.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        double balance;
        double sal;
        double grandSalary;
        public void loadDatas()
        {
            try
            {
                Employee e = new Employee();
                e.EmpId = this.emp;
                DataTable dt = e.getSpecificEmployee();
                txtEmpId.Text = this.emp;
                txtName.Text = dt.Rows[0]["EmployeeName"].ToString();

                if (cboType.Text.Equals("Salary"))
                {
                    sal = double.Parse(dt.Rows[0]["EmployeeSalary"].ToString());
                    Payrolls p = new Payrolls();
                    p.Emp = emp;
                    balance = p.getBalance();
                    txtBalance.Text = balance.ToString();
                    //-----------
                    grandSalary = sal - balance;
                    txtSalary.Text = sal.ToString();
                    txtGrandTotal.Text = grandSalary.ToString();
                }
                else
                {
                    sal = 0;
                    balance = 0;
                    txtBalance.Text = balance.ToString();
                    //-------
                    grandSalary = balance;
                    //txtSalary.Text = sal.ToString();
                    txtGrandTotal.Text = grandSalary.ToString();
                }

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

            }

        }
        public void loadData()
        {
            string type = cboType.Text;
            if (type.Equals("Others"))
            {
                txtSalary.Text = "0";
                txtSalary.Enabled = false;
                txtBalance.Enabled = true;
                balance = double.Parse(txtBalance.Text);
                txtBalance.Text = balance.ToString();
                //-------
                grandSalary = 0 + balance;
                txtSalary.Text = 0.ToString();
                txtGrandTotal.Text = grandSalary.ToString();
            }
            else
            {
                txtSalary.Enabled = true;

                Payrolls p = new Payrolls();
                p.Emp = emp;
                balance = p.getBalance();

                txtBalance.Text = balance.ToString();
                txtBalance.Enabled = false;
                //-----------
                grandSalary = sal - balance;
                txtSalary.Text = sal.ToString();
                txtGrandTotal.Text = grandSalary.ToString();
            }

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

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadData();

        }

        private void txtBalance_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                loadData();
            }
            catch (Exception ex)
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Payrolls p = new Payrolls();
                p.Emp = this.emp;
                p.Month = cboMonth.Text;
                p.Year = Int32.Parse(cboYear.Text);
                p.PayrollType = cboType.Text;
                if (cboType.Text == "Salary")
                {
                    bool x = p.checkPaiment();
                    if (x)
                    {
                        MessageBox.Show("The current month has been paid");
                    }
                    else
                    {

                        p.Amount = double.Parse(txtGrandTotal.Text);
                        p.Date = DateTime.Now.ToShortDateString();

                        int id;

                        double dd = double.Parse(txtBalance.Text);
                        if (dd >= 0 && double.Parse(txtGrandTotal.Text) >= 0)
                        {
                            p.Amount = double.Parse(txtGrandTotal.Text);
                            p.Date = DateTime.Now.ToShortDateString();
                            id = p.paySalary();
                            p.degrade(dd);
                            //-------------------
                            //Dakhal dz = new Dakhal();
                            //dz.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                            //int dakhalId = dz.getIdByDate();
                            //DakhalDetail ddz = new DakhalDetail();
                            //ddz.DakhalId = dakhalId;
                            //ddz.DakhalAmount = double.Parse(txtGrandTotal.Text);
                            //ddz.Particulars = "Out";
                            //ddz.Note = "Salary";
                            //ddz.insertDetail();


                            //---------------------------- update ledger
                            Ledger led = new Ledger();
                            led.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                            led.Note = "Salary";
                            led.Particualrs = "Debit";

                            led.Debit = double.Parse(txtGrandTotal.Text);
                            led.insertDebit();
                        }
                        else
                        {
                            p.Amount = 0;
                            p.Date = DateTime.Now.ToShortDateString();
                            id = p.paySalary();
                            p.degrade(double.Parse(txtSalary.Text));
                            //Dakhal dz = new Dakhal();
                            //dz.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                            //int dakhalId = dz.getIdByDate();
                            //DakhalDetail ddz = new DakhalDetail();
                            //ddz.DakhalId = dakhalId;
                            //ddz.DakhalAmount = 0;
                            //ddz.Particulars = "Out";
                            //ddz.Note = "Salary";
                            //ddz.insertDetail();

                            //---------------------------- update ledger
                            Ledger led = new Ledger();
                            led.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                            led.Note = "Salary";
                            led.Particualrs = "Debit";

                            led.Debit = 0;
                            led.insertDebit();
                        }

                        MessageBox.Show("Done");
                        this.Close();
                    }
                }
                else
                {

                    //p.Amount = double.Parse(txtGrandTotal.Text);
                    p.Date = DateTime.Now.ToShortDateString();

                    //int id = p.paySalary();
                    double dd = double.Parse(txtBalance.Text);
                    p.upgrade(dd);


                    //Dakhal dz = new Dakhal();
                    //dz.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                    //int dakhalId = dz.getIdByDate();
                    //DakhalDetail ddz = new DakhalDetail();
                    //ddz.DakhalId = dakhalId;
                    //ddz.DakhalAmount = double.Parse(txtBalance.Text);
                    //ddz.Particulars = "Out";
                    //ddz.Note = "Salary";
                    //ddz.insertDetail();

                    //---------------------------- update ledger
                    Ledger led = new Ledger();
                    led.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
                    led.Note = "Salary";
                    led.Particualrs = "Debit";

                    led.Credit = double.Parse(txtBalance.Text);
                    led.insertDebit();


                    MessageBox.Show("Done");
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
