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 EmployerModule : Form
    {
        SqlCommand cm = new SqlCommand();
        dbconnect dbcon = new dbconnect();
        string title = "Car Wash Management System";
        bool check = false;
        Employer employer;
        public EmployerModule(Employer em)
        {
            InitializeComponent();
            combobox_Role.SelectedIndex = 3;
            employer = em;
        }

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

        // to insert Employer data into database
        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                checkfield();
                if (check)
                {
                    if (MessageBox.Show("Are you sure you want to save this Employer?", "Save Employer", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        cm = new SqlCommand("INSERT INTO tbEmployer(name,phone,address,dob,role,salary,password,gender)VALUES(@name,@phone,@address,@dob,@role,@salary,@password,@gender)", dbcon.getConnection());
                        cm.Parameters.AddWithValue("@name", txtName.Text);
                        cm.Parameters.AddWithValue("@phone", txtphone.Text);
                        cm.Parameters.AddWithValue("@address", txtadress.Text);
                        cm.Parameters.AddWithValue("@dob", datdob.Value);
                        cm.Parameters.AddWithValue("@role", combobox_Role.Text);
                        cm.Parameters.AddWithValue("@salary", txtSalary.Text);
                        cm.Parameters.AddWithValue("@password", txt_password.Text);
                        cm.Parameters.AddWithValue("@gender", rdMale.Checked ? "Male" : "Female");

                        dbcon.openConnection(); // to open connection
                        cm.ExecuteNonQuery();
                        dbcon.closeConnection(); // to close connection

                        MessageBox.Show("Employer has been successfully saved.", title);
                        Clear();
                        check = false;
                        employer.loadEmployer(); // refresh data in datagridview
                    }

                }

            }
            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 Employer?", "Update Employer", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        cm = new SqlCommand("Update tbEmployer SET name=@name,phone=@phone,address=@address,dob=@dob,role=@role,salary=@salary,password=@password,gender=@gender WHERE id=@id", dbcon.getConnection());

                        cm.Parameters.AddWithValue("@id", lblEmpId.Text);
                        cm.Parameters.AddWithValue("@name", txtName.Text);
                        cm.Parameters.AddWithValue("@phone", txtphone.Text);
                        cm.Parameters.AddWithValue("@address", txtadress.Text);
                        cm.Parameters.AddWithValue("@dob", datdob.Value);
                        cm.Parameters.AddWithValue("@role", combobox_Role.Text);
                        cm.Parameters.AddWithValue("@salary", decimal.Parse(txtSalary.Text));
                        cm.Parameters.AddWithValue("@password", txt_password.Text);
                        cm.Parameters.AddWithValue("@gender", rdMale.Checked ? "Male" : "Female");

                        dbcon.openConnection(); // to open connection
                        cm.ExecuteNonQuery();
                        dbcon.closeConnection(); // to close connection

                        MessageBox.Show("Employer has been successfully saved.", title);
                        Clear();
                        this.Dispose();
                        employer.loadEmployer(); // refresh data in datagridview
                    }

                }

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

        }

        private void btncancel_Click(object sender, EventArgs e)
        {

            Clear();
            btnupdate.Enabled = false;
            btnsave.Enabled = true;

        }

        private void txtSalary_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;
            }

        }

        // function for Clear all filed

        # region method
        public void Clear()
        {
            txtName.Clear();
            txtphone.Clear();
            txtadress.Clear();
            txtSalary.Clear();
            txt_password.Clear();
            datdob.Value = DateTime.Now;
            rdMale.Checked = true;
            btnsave.Enabled = true;
            btnupdate.Enabled = false;
        }

        // To check data field
        public void checkfield()
        {
            if (string.IsNullOrWhiteSpace(txtName.Text) || string.IsNullOrWhiteSpace(txtphone.Text) || string.IsNullOrWhiteSpace(txtadress.Text) || string.IsNullOrWhiteSpace(txtSalary.Text) || string.IsNullOrWhiteSpace(txt_password.Text) || combobox_Role.SelectedIndex == -1)
            {
                MessageBox.Show("Please fill in all required fields.", "Warning");
                return;
            }


            if (checkage(datdob.Value) < 18)
            {
                MessageBox.Show("Employer must be at least 18 years old.", "Warning");
                return;
            }
            check = true;
        }

        // to check the age
        private static int checkage(DateTime dateofbirth)
        {
            int age = DateTime.Now.Year - dateofbirth.Year;
            if (DateTime.Now.DayOfYear < dateofbirth.DayOfYear)
                age = age - 1;
            return age;

        }

        private void combobox_Role_SelectedIndexChanged(object sender, EventArgs e)
        {
          if(combobox_Role.Text =="Supervisor" || combobox_Role.Text == "Worker")
            {
                this.Height = 621 - 32;
                txt_password.Clear();
                lblpass.Visible = false;
                txt_password.Visible = false;

            }
            else
            {
                lblpass.Visible = true;
                txt_password.Visible = true;
                this.Height = 621;
            }
        }





        #endregion method

        
    }
}
