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

namespace Car_wash
{
    public partial class Employer : Form
    {
        SqlCommand cm = new SqlCommand();
        dbconnect dbcon = new dbconnect();
        SqlDataReader dr;
        string title = "Car Wash Management System";
        
        public Employer()
        {
            InitializeComponent();
            loadEmployer();
            
        }

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

            if (colName == "Edit")
            {
                EmployerModule employerModule = new EmployerModule(this);
                employerModule.lblEmpId.Text = dgvEmployer.Rows[e.RowIndex].Cells[1].Value.ToString();
                employerModule.txtName.Text = dgvEmployer.Rows[e.RowIndex].Cells[2].Value.ToString();
                employerModule.txtphone.Text = dgvEmployer.Rows[e.RowIndex].Cells[3].Value.ToString();
                employerModule.txtadress.Text = dgvEmployer.Rows[e.RowIndex].Cells[4].Value.ToString();
                employerModule.datdob.Value = DateTime.Parse(dgvEmployer.Rows[e.RowIndex].Cells[5].Value.ToString());
                employerModule.combobox_Role.Text = dgvEmployer.Rows[e.RowIndex].Cells[6].Value.ToString();
                employerModule.txtSalary.Text = dgvEmployer.Rows[e.RowIndex].Cells[7].Value.ToString();
                employerModule.txt_password.Text = dgvEmployer.Rows[e.RowIndex].Cells[8].Value.ToString();
                employerModule.rdMale.Checked = dgvEmployer.Rows[e.RowIndex].Cells[9].Value.ToString() == "Male" ? true : false;

                employerModule.btnsave.Enabled = false;
                employerModule.btnupdate.Enabled = true;
                employerModule.ShowDialog();
            }
            else if (colName == "Delete")
            {

                try
                {
                    if (MessageBox.Show("Are you sure you want to delete this Employer?", "Delete Employer", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                       
                        cm = new SqlCommand("DELETE FROM tbEmployer WHERE id LIKE '" + dgvEmployer.Rows[e.RowIndex].Cells[1].Value.ToString() + "'", dbcon.getConnection());
                        dbcon.openConnection();
                        cm.ExecuteNonQuery();
                        dbcon.closeConnection();
                        MessageBox.Show("Employer has been successfully deleted.", title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, title);
                }
            }

        }
        #region method
        // list data to the datagridview
        public void LoadEmployer()
        {
            try
            {
                int i = 0;
                dgvEmployer.Rows.Clear();
                cm = new SqlCommand("SELECT * FROM tbEmployer WHERE CONCAT(name,phone,address,role) LIKE '%" + text_search.Text + "%'", dbcon.getConnection());
                dbcon.openConnection();
                dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    i++;
                    dgvEmployer.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), DateTime.Parse(dr[4].ToString()).ToShortDateString(), dr[5].ToString(), dr[6].ToString(), dr[7].ToString(), dr[8].ToString());
                }
                dbcon.closeConnection();



            }

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

        #endregion method
        private void btnadd_Click(object sender, EventArgs e)
        {
            EmployerModule employerModule = new EmployerModule(this);
            employerModule.btnupdate.Enabled = false; // this is for save not Update
            employerModule.ShowDialog();
        }

        private void text_search_TextChanged(object sender, EventArgs e)
        {
            
        }

        public void loadEmployer()
        {
            try
            {
                int i = 0;
                dgvEmployer.Rows.Clear();
                dgvEmployer.ReadOnly = false;
                cm = new SqlCommand("SELECT * FROM tbEmployer WHERE CONCAT(name,phone,address,role) LIKE '%" + text_search.Text + "%'", dbcon.getConnection());
                dbcon.openConnection();
                dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    i++;
                    string dateString = dr[4].ToString();
                    string formattedDate = "";

                    if (DateTime.TryParse(dateString, out DateTime dt))
                    {
                        formattedDate = dt.ToShortDateString();
                    }

                    dgvEmployer.Rows.Add(
                        i,
                        dr[0].ToString(),
                        dr[1].ToString(),
                        dr[2].ToString(),
                        dr[3].ToString(),
                        formattedDate, // sicheres Datum
                        dr[5].ToString(),
                        dr[6].ToString(),
                        dr[7].ToString(),
                        dr[8].ToString()
                    );
                }
                dbcon.closeConnection();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, title);
            }
        }
    }
}