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 RoomForm : Form
    {
        private RoomRepository roomRepository;
        private int pageSize = 10;
        private int currentPage = 1;

        public RoomForm(string connectionString)
        {
            InitializeComponent();
            roomRepository = new RoomRepository(connectionString);
            LoadData();
        }

        private void LoadData()
        {
            DataTable dt = roomRepository.ReadRooms(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);
                roomRepository.DeleteRoom(id);
            }
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EditRoomForm editForm = new EditRoomForm(roomRepository);
            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);
                EditRoomForm editForm = new EditRoomForm(roomRepository, id);
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Please select a single row to edit.");
            }
        }
    }
}
