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 EditFormBuilding : Form
    {
        private BuildingRepository buildingRepository;
        private int? recordId;

        public EditFormBuilding(BuildingRepository buildingRepository, int? recordId = null)
        {
            this.buildingRepository = buildingRepository;
            this.recordId = recordId;
            InitializeComponent();
            if (recordId.HasValue)
            {
                LoadRecordData(recordId.Value);
            }
        }

        private void LoadRecordData(int id)
        {
            DataRow row = buildingRepository.GetBuildingById(id);
            if (row != null)
            {
                textBoxFloorCount.Text = row["floor_count"].ToString();
                textBoxAddress.Text = row["address"].ToString();
                textBoxCreatingUserId.Text = row["creating_user_id"].ToString();
                textBoxBuildingType.Text = row["building_type"].ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int floorCount = int.Parse(textBoxFloorCount.Text);
            string address = textBoxAddress.Text;
            int creatingUserId = int.Parse(textBoxCreatingUserId.Text);
            string buildingType = textBoxBuildingType.Text;

            if (recordId.HasValue)
            {
                buildingRepository.UpdateBuilding(recordId.Value, floorCount, address, creatingUserId, buildingType);
            }
            else
            {
                buildingRepository.AddBuilding(floorCount, address, creatingUserId, buildingType);
            }
            this.Close();
        }
    }
}
