unit frm_CambiarEstadoWebModal;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,
  Vcl.ExtCtrls, uAppTheme;

type
  TfrmCambiarEstadoWebModal = class(TForm)
    pnlHeader: TPanel;
    lblTitulo: TLabel;
    lblSubtitulo: TLabel;
    pnlBody: TPanel;
    pnlInfo: TPanel;
    lblInfoPedido: TLabel;
    lblInfoCliente: TLabel;
    lblInfoEstadoActual: TLabel;
    lblSelectPrompt: TLabel;
    cbbNuevoEstado: TComboBox;
    lblExplicacion: TLabel;
    pnlFooter: TPanel;
    btnAceptar: TBitBtn;
    btnCancelar: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnAceptarClick(Sender: TObject);
  private
    FOrderId: Int64;
    FClienteNombre: string;
    FEstadoActual: string;
    FEstadoSeleccionado: string;
  public
    class function Execute(AOwner: TComponent; AOrderId: Int64; const AClienteNombre, AEstadoActual: string; out ANewStatus: string): Boolean;
    property OrderId: Int64 read FOrderId write FOrderId;
    property ClienteNombre: string read FClienteNombre write FClienteNombre;
    property EstadoActual: string read FEstadoActual write FEstadoActual;
    property EstadoSeleccionado: string read FEstadoSeleccionado;
  end;

var
  frmCambiarEstadoWebModal: TfrmCambiarEstadoWebModal;

implementation

{$R *.dfm}

{ TfrmCambiarEstadoWebModal }

procedure TfrmCambiarEstadoWebModal.FormCreate(Sender: TObject);
begin
  FOrderId := 0;
  FClienteNombre := '';
  FEstadoActual := '';
  FEstadoSeleccionado := '';
end;

procedure TfrmCambiarEstadoWebModal.FormShow(Sender: TObject);
begin
  TAppTheme.ApplyToForm(Self);

  // Cargar datos informativos de cabecera
  if FOrderId > 0 then
    lblInfoPedido.Caption := Format('Pedido Web #%d', [FOrderId])
  else
    lblInfoPedido.Caption := 'Pedido Web';

  if FClienteNombre <> '' then
    lblInfoCliente.Caption := 'Cliente: ' + FClienteNombre
  else
    lblInfoCliente.Caption := 'Cliente: [No especificado]';

  if FEstadoActual <> '' then
    lblInfoEstadoActual.Caption := 'Estado actual: ' + FEstadoActual
  else
    lblInfoEstadoActual.Caption := '';

  // Configurar opciones de estado
  cbbNuevoEstado.Items.Clear;
  cbbNuevoEstado.Items.Add('Completado (completed) — Notifica por email y adjunta factura');
  cbbNuevoEstado.Items.Add('Procesando (processing) — En preparación en obrador');
  cbbNuevoEstado.Items.Add('En espera (on-hold) — Retenido temporalmente');
  cbbNuevoEstado.Items.Add('Cancelado (cancelled) — Pedido anulado');

  // Seleccionar por defecto la primera opción
  cbbNuevoEstado.ItemIndex := 0;
  cbbNuevoEstado.SetFocus;
end;

procedure TfrmCambiarEstadoWebModal.btnAceptarClick(Sender: TObject);
begin
  case cbbNuevoEstado.ItemIndex of
    0: FEstadoSeleccionado := 'completed';
    1: FEstadoSeleccionado := 'processing';
    2: FEstadoSeleccionado := 'on-hold';
    3: FEstadoSeleccionado := 'cancelled';
  else
    FEstadoSeleccionado := 'completed';
  end;

  ModalResult := mrOk;
end;

class function TfrmCambiarEstadoWebModal.Execute(AOwner: TComponent; AOrderId: Int64; const AClienteNombre, AEstadoActual: string; out ANewStatus: string): Boolean;
var
  LDlg: TfrmCambiarEstadoWebModal;
begin
  Result := False;
  ANewStatus := '';

  LDlg := TfrmCambiarEstadoWebModal.Create(AOwner);
  try
    LDlg.OrderId := AOrderId;
    LDlg.ClienteNombre := AClienteNombre;
    LDlg.EstadoActual := AEstadoActual;

    if LDlg.ShowModal = mrOk then
    begin
      ANewStatus := LDlg.EstadoSeleccionado;
      Result := ANewStatus <> '';
    end;
  finally
    LDlg.Free;
  end;
end;

end.
