using MySql.Data.MySqlClient;
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 _0603NeshinaRogova
{
    public partial class Doctor : Form
    {
        private int currentUserId;
        private string connectionString = "Server=cfif31.ru; port=3306; Database=ISPr25-21_NeshinaPV_Doctor; username=ISPr25-21_NeshinaPV; password=ISPr25-21_NeshinaPV";

        public Doctor(int userId)
        {
            InitializeComponent();
            currentUserId = userId;
            LoadDataGrid();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
            this.Close();
        }

        private void LoadDataGrid()
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    string query = @"SELECT * from Pacient
                                        ";
                    MySqlCommand cmd = new MySqlCommand(query, conn);
                    DataTable dt = new DataTable();
                    MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                    adapter.Fill(dt);
                    dataGridViewRequests.DataSource = dt;
                    dataGridViewRequests.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGridViewRequests.ReadOnly = true;
                    dataGridViewRequests.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки данных: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EditAdd editForm = new EditAdd(connectionString, false, 0);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
                MessageBox.Show("Заявка успешно добавлена!", "Успех");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridViewRequests.CurrentRow == null)
            {
                MessageBox.Show("Выберите заявку для редактирования!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int requestId = Convert.ToInt32(dataGridViewRequests.CurrentRow.Cells["idPacient"].Value);
            EditAdd editForm = new EditAdd(connectionString, true, requestId);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridViewRequests.CurrentRow == null)
            {
                MessageBox.Show("Выберите пациента для удаления!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int requestId = Convert.ToInt32(dataGridViewRequests.CurrentRow.Cells["idPacient"].Value);
            DialogResult result = MessageBox.Show(
                $"Вы действительно хотите удалить заявку №{requestId} ()?\nЭто действие нельзя отменить!",
                "Подтверждение удаления",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                try
                {
                    using (MySqlConnection conn = new MySqlConnection(connectionString))
                    {
                        conn.Open();
                        string deleteComments = "DELETE FROM Pacient WHERE idPacient = @idPacient";
                        MySqlCommand cmdComm = new MySqlCommand(deleteComments, conn);
                        cmdComm.Parameters.AddWithValue("@id", requestId);
                        cmdComm.ExecuteNonQuery();
                       
                       
                            LoadDataGrid();
                        
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка при удалении: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
