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 CRUDForm : Form
    {
        private BuildingRepository buildingRepository;
        private int pageSize = 10;
        private int currentPage = 1;
        private string connectionString;

        public CRUDForm(string connectionString)
        {
            InitializeComponent();
            this.connectionString = connectionString;
            buildingRepository = new BuildingRepository(connectionString); // Инициализация BuildingRepository
            LoadData();
        }

        private void LoadData()
        {
            DataTable dt = buildingRepository.ReadBuildings(pageSize, currentPage);
            dataGridView1.DataSource = dt;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            currentPage++;
            LoadData();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                LoadData();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                int id = Convert.ToInt32(row.Cells["id"].Value);
                buildingRepository.DeleteBuilding(id);
            }
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EditFormBuilding editForm = new EditFormBuilding(buildingRepository);
            editForm.ShowDialog();
            LoadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["id"].Value);
                EditFormBuilding editForm = new EditFormBuilding(buildingRepository, id);
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Please select a single row to edit.");
            }
        }

        // another

        private void buildingBtn_Click(object sender, EventArgs e)
        {
            var crudForm = new CRUDForm(this.connectionString);
            crudForm.Show();
        }

        private void DepartmentBtn_Click(object sender, EventArgs e)
        {
            var form = new DepartmentForm(this.connectionString);
            form.Show();
        }

        private void RoomsBtn_Click(object sender, EventArgs e)
        {
            var form = new RoomForm(this.connectionString);
            form.Show();
        }

        private void EquipmentBtn_Click(object sender, EventArgs e)
        {
            var form = new EquipmentForm(this.connectionString);
            form.Show();
        }
    }
}
