using System;
using System.Drawing;
using System.Windows.Forms;
using АвтотранспортноеПредприятие.Database;
using АвтотранспортноеПредприятие.Models;

namespace АвтотранспортноеПредприятие
{
    public partial class FormEditOrder : Form
    {
        private DatabaseManager db;
        private int orderId;
        private Order currentOrder;

        private Label lblStatus;
        private ComboBox cmbStatus;
        private Label lblOrderId;
        private Label lblOrderInfo;
        private Button btnSave;
        private Button btnCancel;

        public FormEditOrder(int id)
        {
            db = new DatabaseManager();
            orderId = id;
            InitializeComponent();
            CreateCustomControls();
            LoadOrder();
        }

        private void CreateCustomControls()
        {
            this.Text = "Редактирование заказа";
            this.Size = new Size(450, 280);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.BackColor = Color.White;

            lblOrderId = new Label();
            lblOrderId.Text = $"Заказ №{orderId}";
            lblOrderId.Location = new Point(20, 20);
            lblOrderId.Size = new Size(400, 30);
            lblOrderId.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Bold);

            lblOrderInfo = new Label();
            lblOrderInfo.Location = new Point(20, 60);
            lblOrderInfo.Size = new Size(400, 60);
            lblOrderInfo.Font = new Font("Microsoft Sans Serif", 9, FontStyle.Regular);

            lblStatus = new Label();
            lblStatus.Text = "Статус заказа:";
            lblStatus.Location = new Point(20, 140);
            lblStatus.Size = new Size(100, 25);

            cmbStatus = new ComboBox();
            cmbStatus.Location = new Point(130, 140);
            cmbStatus.Size = new Size(250, 25);
            cmbStatus.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbStatus.Items.AddRange(new string[] { "новый", "в работе", "выполнен", "отменён" });

            btnSave = new Button();
            btnSave.Text = "Сохранить";
            btnSave.Location = new Point(100, 200);
            btnSave.Size = new Size(100, 35);
            btnSave.BackColor = Color.LightGreen;
            btnSave.Click += BtnSave_Click;

            btnCancel = new Button();
            btnCancel.Text = "Отмена";
            btnCancel.Location = new Point(220, 200);
            btnCancel.Size = new Size(100, 35);
            btnCancel.BackColor = Color.LightCoral;
            btnCancel.Click += (s, e) => this.Close();

            this.Controls.Add(lblOrderId);
            this.Controls.Add(lblOrderInfo);
            this.Controls.Add(lblStatus);
            this.Controls.Add(cmbStatus);
            this.Controls.Add(btnSave);
            this.Controls.Add(btnCancel);
        }

        private void LoadOrder()
        {
            var orders = db.GetAllOrders();
            currentOrder = orders.Find(o => o.order_id == orderId);

            if (currentOrder != null)
            {
                lblOrderInfo.Text = $"Заказчик: {currentOrder.customer_name}\nАдрес подачи: {currentOrder.pickup_address}\nАдрес назначения: {currentOrder.destination_address}";
                cmbStatus.SelectedItem = currentOrder.status;
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cmbStatus.SelectedItem == null)
                {
                    MessageBox.Show("Выберите статус заказа");
                    return;
                }

                currentOrder.status = cmbStatus.SelectedItem.ToString();
                db.UpdateOrder(currentOrder);
                MessageBox.Show("Заказ успешно обновлён", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка при обновлении заказа: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FormEditOrder_Load(object sender, EventArgs e)
        {

        }
    }
}