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 PayrollSystem
{
    public partial class form_modify_department : Form
    {
        int editingId=-1;
        bool isEditing => editingId >= 0;
        public form_modify_department(int editId=-1)
        {
            InitializeComponent();
            editingId = editId;
        }

        private void form_modify_department_Load(object sender, EventArgs e)
        {
            Text = isEditing ? "Edit Department" : "Add new Department";
            btnAdd.Text = isEditing ? "Edit" : "Add";

            if (isEditing)
            {
                using(SqlConnection con = new SqlConnection(Helpers.conString))
                {
                    con.Open();
                    using (SqlCommand cmd= new SqlCommand("SELECT * FROM Departments WHERE ID=@id", con))
                    {
                        cmd.Parameters.AddWithValue("@id", editingId);

                        using(SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                txtName.Text = reader["Name"].ToString();
                                txtBasic.Text = reader["Basic"].ToString();
                            }
                        }
                    }
                    con.Close();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //Validate inputs
            if(txtName.Text.Length <= 1) { MessageBox.Show("Please enter a name longer than 1 letters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
            int basic = 0;
            try
            {
                basic = Convert.ToInt32(txtBasic.Text);
                if(basic <= 0)
                {
                    MessageBox.Show("Enter a valid basic pay amount, Employees must be paid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
                }
            }
            catch
            {
                MessageBox.Show("Enter a number on the basic", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
            }


            using (SqlConnection con = new SqlConnection(Helpers.conString))
            {
                con.Open();
                string query = "INSERT INTO Departments (Name, Basic) VALUES(@name,@basic)";
                if (isEditing)
                {
                    query = "UPDATE Departments SET Name=@name, Basic=@basic WHERE ID=@id";
                }
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    if (isEditing) {
                        cmd.Parameters.AddWithValue("@id", editingId);
                    }
                    cmd.Parameters.AddWithValue("@name", txtName.Text);
                    cmd.Parameters.AddWithValue("@basic", txtBasic.Text);

                    int result = cmd.ExecuteNonQuery();
                    if (result < 0)
                    {
                        MessageBox.Show($"There was an error {(isEditing ? "editing" : "adding new")} Department", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                con.Close();
            }
            this.Close();
        }


        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
