using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using RestaurantApp.DAL;
using RestaurantApp.Models;

namespace RestaurantApp
{
    public partial class EmployeeEditForm : Form
    {
        private EmployeeRepository repo = new EmployeeRepository();
        private Employee employee;
        private bool isEdit = false;

        public EmployeeEditForm()
        {
            InitializeComponent();
            isEdit = false;
            employee = new Employee();
            this.Text = "Новый сотрудник";
            LoadPositions();
            dtpHireDate.Value = DateTime.Now;
            chkActive.Checked = true;

            btnSave.Click += BtnSave_Click;
            btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
        }

        public EmployeeEditForm(Employee existingEmployee)
        {
            InitializeComponent();
            isEdit = true;
            employee = existingEmployee;
            this.Text = "Редактирование сотрудника";
            LoadPositions();
            LoadData();

            btnSave.Click += BtnSave_Click;
            btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
        }

        private void LoadPositions()
        {
            var dtPositions = repo.GetPositions();

            var list = dtPositions.AsEnumerable().Select(r => new
            {
                position_id = r.Field<int>("position_id"),
                title = r.Field<string>("title")
            }).ToList();

            cmbPosition.DataSource = list;
            cmbPosition.DisplayMember = "title";
            cmbPosition.ValueMember = "position_id";
        }

        private void LoadData()
        {
            cmbPosition.SelectedValue = employee.PositionId;
            txtLastName.Text = employee.LastName;
            txtFirstName.Text = employee.FirstName;
            txtMiddleName.Text = employee.MiddleName;
            txtPhone.Text = employee.Phone;
            txtEmail.Text = employee.Email;
            dtpHireDate.Value = employee.HireDate;
            numSalary.Value = employee.Salary;
            chkActive.Checked = employee.IsActive;
        }

        private void SaveData()
        {
            employee.PositionId = (int)cmbPosition.SelectedValue;
            employee.LastName = txtLastName.Text.Trim();
            employee.FirstName = txtFirstName.Text.Trim();
            employee.MiddleName = txtMiddleName.Text.Trim();
            employee.Phone = txtPhone.Text.Trim();
            employee.Email = txtEmail.Text.Trim();
            employee.HireDate = dtpHireDate.Value;
            employee.Salary = numSalary.Value;
            employee.IsActive = chkActive.Checked;

            if (string.IsNullOrWhiteSpace(employee.LastName))
                throw new Exception("Введите фамилию");
            if (string.IsNullOrWhiteSpace(employee.FirstName))
                throw new Exception("Введите имя");
            if (employee.Salary <= 0)
                throw new Exception("Оклад должен быть больше 0");

            if (isEdit)
                repo.Update(employee);
            else
                repo.Insert(employee);
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SaveData();
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}