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 setting : Form
    {

        SqlCommand cm = new SqlCommand();
        dbconnect dbcon = new dbconnect();
        SqlDataReader dr;
        string title = "Car Wash Management System";
        bool hasdetail = false;
        public setting()
        {
            InitializeComponent();
            loadManageVeh();
            loadManageCostofGood();
            loadcompanyname();


        }

        #region vehicule method
        private void text_searchVT_TextChanged(object sender, EventArgs e)
        {
            loadManageVeh();
        }

        private void btnaddVT_Click(object sender, EventArgs e)
        {
            ManagevehiculeType managevehicule = new ManagevehiculeType(this);
            managevehicule.btnsave.Enabled = true;
            managevehicule.btnupdate.Enabled = false;
            managevehicule.ShowDialog();
        }
        public void loadManageVeh()
        {
            try
            {
                int i = 0;
                dgvVehicel.Rows.Clear();
                dgvVehicel.ReadOnly = false;
                cm = new SqlCommand("SELECT * FROM tbVehicleType WHERE CONCAT(name,class) LIKE '%" + text_searchVT.Text + "%'", dbcon.getConnection());
                dbcon.openConnection();
                dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    i++;
                    dgvVehicel.Rows.Add(
                        i,
                        dr[0].ToString(),
                        dr[1].ToString(),
                        dr[2].ToString()

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

        private void dgvVehicel_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string colName = dgvVehicel.Columns[e.ColumnIndex].Name;

            if (colName == "Edit")
            {
                ManagevehiculeType managevh = new ManagevehiculeType(this);
                managevh.lblEmpId.Text = dgvVehicel.Rows[e.RowIndex].Cells[1].Value.ToString();
                managevh.txtname.Text = dgvVehicel.Rows[e.RowIndex].Cells[2].Value.ToString();
                managevh.combobox_class.Text = dgvVehicel.Rows[e.RowIndex].Cells[3].Value.ToString();
                managevh.btnsave.Enabled = false;
                managevh.btnupdate.Enabled = true;
                managevh.ShowDialog();

            }
            else if (colName == "Delete")
            {

                try
                {
                    if (MessageBox.Show("Are you sure you want to delete this Vehicle?", "Delete Vehicle", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        cm = new SqlCommand("DELETE FROM tbVehicleType WHERE id LIKE '" + dgvVehicel.Rows[e.RowIndex].Cells[1].Value.ToString() + "'", dbcon.getConnection());
                        dbcon.openConnection();
                        cm.ExecuteNonQuery();
                        dbcon.closeConnection();
                        MessageBox.Show("Vehicle type has been successfully deleted.", title, MessageBoxButtons.OK, MessageBoxIcon.Information);

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

            loadManageVeh();
        }


        #endregion vehicule method

        #region cost method
        //
        //
        private void dgvcostof_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            string colname = dgvcostof.Columns[e.ColumnIndex].Name;

            if (colname == "EditC")
            {
                Managecostofgoodsold manageCG = new Managecostofgoodsold(this);
                manageCG.lblCid.Text = dgvcostof.Rows[e.RowIndex].Cells[1].Value.ToString();
                manageCG.txtname.Text = dgvcostof.Rows[e.RowIndex].Cells[2].Value.ToString();
                manageCG.textBox_cost.Text = dgvcostof.Rows[e.RowIndex].Cells[3].Value.ToString();
                manageCG.dob_costofg.Value = DateTime.Parse(dgvcostof.Rows[e.RowIndex].Cells[4].Value.ToString());
                manageCG.btnsave.Enabled = false;
                manageCG.btnupdate.Enabled = true;
                manageCG.ShowDialog();


            }
            else if (colname == "DeleteC")
            {

                try
                {
                    if (MessageBox.Show("Are you sure you want to delete this cost of good sold?", "Delete Cost of good sold", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        cm = new SqlCommand("DELETE FROM tbcostofgoodsold WHERE id LIKE '" + dgvcostof.Rows[e.RowIndex].Cells[1].Value.ToString() + "'", dbcon.getConnection());
                        dbcon.openConnection();
                        cm.ExecuteNonQuery();
                        dbcon.closeConnection();
                        MessageBox.Show("Cost of good sold type has been successfully deleted.", title, MessageBoxButtons.OK, MessageBoxIcon.Information);

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

            loadManageCostofGood();
        }


        public void loadManageCostofGood()
        {
            try
            {
                int i = 0;
                dgvcostof.Rows.Clear();
                dgvcostof.ReadOnly = false;
                cm = new SqlCommand("SELECT * FROM tbCostofgoodsold WHERE CONCAT(costname,cost) LIKE '%" + txtSearchCoG.Text + "%'", dbcon.getConnection());
                dbcon.openConnection();
                dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    i++;
                    dgvcostof.Rows.Add(
                        i,
                        dr[0].ToString(),
                        dr[1].ToString(),
                        dr[2].ToString(),
                        DateTime.Parse(dr[3].ToString()).ToShortDateString()


                    );
                }
                dbcon.closeConnection();

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




        #endregion vehicule method

        #region cost method

        private void txtSearchCoG_TextChanged(object sender, EventArgs e)
        {
            loadManageCostofGood();
        }

        private void btnCostofGood_Click(object sender, EventArgs e)
        {
            Managecostofgoodsold manageCG = new Managecostofgoodsold(this);
            manageCG.btnsave.Enabled = true;
            manageCG.btnupdate.Enabled = false;
            manageCG.ShowDialog();
        }




        #endregion cost method

        #region company name
        public void loadcompanyname()
        {
            try
            {
                dbcon.openConnection();
                cm = new SqlCommand("SELECT * FROM tbcompanyDetail", dbcon.getConnection());
                dr = cm.ExecuteReader();
                dr.Read();
                if (dr.HasRows)
                {
                    hasdetail = true;
                    txtcomName.Text = dr[0].ToString();
                    txtcomadress.Text = dr[1].ToString();
                }

                else
                {
                    txtcomName.Clear();
                    txtcomadress.Clear();

                }
                dr.Close();
                dbcon.closeConnection();

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



        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to save this company detail?", "Save company detail", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    if (hasdetail)
                    {
                        dbcon.executeQuery("UPDATE tbcompanyDetail SET name='" + txtcomName.Text + "', address='" + txtcomadress.Text + "'");
                    }
                    else
                    {
                        dbcon.executeQuery("INSERT INTO tbcompanyDetail(name,address)VALUES('" + txtcomName.Text + "','" + txtcomadress.Text + "')");

                    }
                    MessageBox.Show("Company detail has been successfully saved.", title);

                }

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

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


        #endregion company name


    }
}

