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 EditRoomForm : Form
    {
        private RoomRepository roomRepository;
        private int? roomId;

        public EditRoomForm(RoomRepository roomRepository, int? roomId = null)
        {
            InitializeComponent();
            this.roomRepository = roomRepository;
            this.roomId = roomId;
            if (roomId.HasValue)
            {
                LoadRecordData(roomId.Value);
            }
        }

        private void LoadRecordData(int id)
        {
            DataRow row = roomRepository.GetRoomById(id);
            if (row != null)
            {
                textBoxRoomType.Text = row["room_type"].ToString();
                textBoxRoomNumber.Text = row["room_number"].ToString();
                textBoxFloor.Text = row["floor"].ToString();
                textBoxCreatingUserId.Text = row["creating_user_id"].ToString();
                textBoxBuildingId.Text = row["building_id"].ToString();
                textBoxRoomName.Text = row["room_name"].ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string roomType = textBoxRoomType.Text;
            int roomNumber = int.Parse(textBoxRoomNumber.Text);
            int floor = int.Parse(textBoxFloor.Text);
            int creatingUserId = int.Parse(textBoxCreatingUserId.Text);
            int buildingId = int.Parse(textBoxBuildingId.Text);
            string roomName = textBoxRoomName.Text;

            if (roomId.HasValue)
            {
                roomRepository.UpdateRoom(roomId.Value, roomType, roomNumber, floor, creatingUserId, buildingId, roomName);
            }
            else
            {
                roomRepository.AddRoom(roomType, roomNumber, floor, creatingUserId, buildingId, roomName);
            }
            this.Close();
        }
    }
}
