using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Car_wash
{
    public partial class Managecostofgoodsold : Form
    {
        

        SqlCommand cm = new SqlCommand();
        dbconnect dbcon = new dbconnect();
        setting Setting;
        string title = "Car Wash Management System";
        bool check = false;
        public Managecostofgoodsold(setting set)
        {
            InitializeComponent();
            Setting = set;
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                checkfield();
                if (check)
                {
                    if (MessageBox.Show("Are you sure you want to save this Cost of Goods Sold?", "Save Cost of Goods Sold", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        cm = new SqlCommand("INSERT INTO tbcostofgoodsold(costname,cost,date)VALUES(@costname,@cost,@date)", dbcon.getConnection());

                        cm.Parameters.AddWithValue("@costname", txtname.Text);
                        cm.Parameters.AddWithValue("@cost", textBox_cost.Text);
                        cm.Parameters.AddWithValue("@date", dob_costofg.Value);
                        dbcon.openConnection(); // to open connection

                        cm.ExecuteNonQuery();

                        dbcon.closeConnection(); // to close connection
                        MessageBox.Show("Cost of Goods Sold has been successfully saved.", title);
                        Clear();
                        Setting.loadManageCostofGood();

                    }

                }

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

        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            try
            {
                checkfield();
                if (check)
                {
                    if (MessageBox.Show("Are you sure you want to update this Cost of Goods Sold?", "Update Cost of Goods Sold", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        cm = new SqlCommand("UPDATE tbcostofgoodsold SET costname=@costname, cost=@cost, date=@date WHERE id=@id ", dbcon.getConnection());
                        cm.Parameters.AddWithValue("@id", lblCid.Text);
                        cm.Parameters.AddWithValue("@costname", txtname.Text);
                        cm.Parameters.AddWithValue("@cost", textBox_cost.Text);
                        cm.Parameters.AddWithValue("@date", dob_costofg.Value);
                        dbcon.openConnection(); // to open connection
                        cm.ExecuteNonQuery();
                        dbcon.closeConnection(); // to close connection
                        MessageBox.Show("Cost of Goods Sold has been successfully updated.", title);
                        Clear();
                        this.Dispose();
                        Setting.loadManageCostofGood();
                    }

                }

                

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

        private void btncancel_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void textBox_cost_KeyPress(object sender, KeyPressEventArgs e)
        {
            // only number
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        #region method cost of goods sold

        public void Clear()
        {
            txtname.Clear();
            textBox_cost.Clear();
            dob_costofg.Value = DateTime.Now;

            btnsave.Enabled = true;
            btnupdate.Enabled = false;
        }

        public void checkfield()
        {
            if (txtname.Text == "" && textBox_cost.Text == "")
            {
               MessageBox.Show("Please enter Cost of Goods Sold name.", "Warning");
                return;
            }
            check = true;

        }

        #endregion method cost of goods sold


    }
}
