using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using Org.BouncyCastle.Asn1.Ocsp;
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 EditAdd : Form
    {
        private string connectionString;
        private bool isEditMode;
        private int requestId;

        public EditAdd(string connString, bool editMode, int id)
        {
            InitializeComponent();
            connectionString = connString;
            isEditMode = editMode;
            requestId = id;

            if (isEditMode)
            {
                Text = "Редактирование заявки №" + id;
                Edit();
            }
            else
            {
                Text = "Новая заявка";
                dtpDate.Value = DateTime.Now;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    string query;
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = conn;
                    if (isEditMode)
                    {
                        query = @"UPDATE Pacient SET 
                                  DateOfBirth = @DateOfBirth,
                                  Name = @Name,
                                  NumberPhone = @NumberPhone,
                                  Polisy = @Polisy,
                                  Email = @Email,
                                  WHERE idPacient = @idPacient";
                        cmd.Parameters.AddWithValue("@idPacient", requestId);
                    }
                    else
                    {
                        query = @"INSERT INTO Pacient 
                                  (DateOfBirth, Name, NumberPhone, Polisy, Email) 
                                  VALUES 
                                  (@DateOfBirth, @Name, @NumberPhone, @Polisy, @Email)";
                    }
                    cmd.CommandText = query;
                    cmd.Parameters.AddWithValue("@DateOfBirth", dtpDate.Value.Date);
                    cmd.Parameters.AddWithValue("@Name", tbName.Text.Trim());
                    cmd.Parameters.AddWithValue("@NumberPhone", tbNumber.Text.Trim());
                    cmd.Parameters.AddWithValue("@Polisy", tbPolis.Text.Trim());
                    cmd.Parameters.AddWithValue("@Email", tbEmail.Text.Trim());

                    int rows = cmd.ExecuteNonQuery();
                    if (rows > 0)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка сохранения: " + ex.Message);
            }
        }

        private void Edit()
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    string query = "SELECT * FROM Pacient WHERE idPacient = @idPacient";
                    MySqlCommand cmd = new MySqlCommand(query, conn);
                    cmd.Parameters.AddWithValue("@idPacient", requestId);
                    MySqlDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        dtpDate.Value = Convert.ToDateTime(reader["DateOfBirth"]);
                        tbName.Text = reader["Name"].ToString();
                        tbNumber.Text = reader["NumberPhone"].ToString();
                        tbPolis.Text = reader["Polisy"].ToString();
                        tbEmail.Text = reader["Email"].ToString();

                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка загрузки данных заявки: " + ex.Message);
            }
        }
    }
}
