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

        SqlCommand cm = new SqlCommand();
        dbconnect dbcon = new dbconnect();
        setting Setting;
        string title = "Car Wash Management System";

        public ManagevehiculeType(setting sett)
        {
            InitializeComponent();
            Setting = sett;
            combobox_class.SelectedIndex = 0;
        }

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

        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtname.Text == "")
                {
                    MessageBox.Show("Please enter Vehicle Type name.", "Warning");
                    return;
                }

                if (MessageBox.Show("Are you sure you want to save this Vehicle Type?", "Save Vehicle Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cm = new SqlCommand("INSERT INTO tbVehicleType(class,name)VALUES(@class,@name)" , dbcon.getConnection());
                    cm.Parameters.AddWithValue("@class", combobox_class.Text);
                    cm.Parameters.AddWithValue("@name", txtname.Text);
                    dbcon.openConnection(); // to open connection
                    cm.ExecuteNonQuery();
                    dbcon.closeConnection(); // to close connection
                    MessageBox.Show("Vehicle Type has been successfully saved.", title);
                    Clear();
                    Setting.loadManageVeh();

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

        private void btnupdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure you want to Update this Vehicle Type?", "Update Vehicle Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    cm = new SqlCommand("Update tbVehicleType SET name= @name, class= @class WHERE id=@id", dbcon.getConnection());
                    cm.Parameters.AddWithValue("@id", lblEmpId.Text);
                    cm.Parameters.AddWithValue("@class", combobox_class.Text);
                    cm.Parameters.AddWithValue("@name", txtname.Text);
                    dbcon.openConnection(); // to open connection
                    cm.ExecuteNonQuery();
                    dbcon.closeConnection(); // to close connection
                    MessageBox.Show("Vehicle Type has been successfully Upadte.", title);
                    Clear();
                    Setting.loadManageVeh();
                    this.Dispose();

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

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

        #region method

        



        public void Clear()
        {
            txtname.Clear();
            combobox_class.SelectedIndex = 0;

            btnsave.Enabled = true;
            btnupdate.Enabled = false;
        }
        #endregion method
    }
}
