using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace db_lab
{
    public partial class EditEquipmentForm : Form
    {
        private EquipmentRepository equipmentRepository;
        private int? equipmentId;

        public EditEquipmentForm(EquipmentRepository equipmentRepository, int? equipmentId = null)
        {
            InitializeComponent();
            this.equipmentRepository = equipmentRepository;
            this.equipmentId = equipmentId;
            if (equipmentId.HasValue)
            {
                LoadRecordData(equipmentId.Value);
            }
        }

        private void LoadRecordData(int id)
        {
            DataRow row = equipmentRepository.GetEquipmentById(id);
            if (row != null)
            {
                textBoxIpAddress.Text = row["ip_address"].ToString();
                textBoxSshSettings.Text = row["ssh_settings"].ToString();
                textBoxName.Text = row["name"].ToString();
                textBoxRoomId.Text = row["room_id"].ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string ipAddress = textBoxIpAddress.Text;
            string sshSettings = textBoxSshSettings.Text;
            string name = textBoxName.Text;
            int roomId = int.Parse(textBoxRoomId.Text);

            if (equipmentId.HasValue)
            {
                equipmentRepository.UpdateEquipment(equipmentId.Value, ipAddress, sshSettings, name, roomId);
            }
            else
            {
                equipmentRepository.AddEquipment(ipAddress, sshSettings, name, roomId);
            }
            this.Close();
        }
    }
}
