unit frm_FormatosEnvio;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Grids,
  Vcl.DBGrids, Data.DB, Vcl.Buttons, uAppTheme, FireDAC.Stan.Param, FireDAC.Comp.Client,
  frm_BaseList;

type
  TfrmFormatosEnvio = class(TfrmBaseList)
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject); override;
    procedure btnNewClick(Sender: TObject); override;
    procedure btnEditClick(Sender: TObject); override;
    procedure btnDeleteClick(Sender: TObject); override;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmFormatosEnvio: TfrmFormatosEnvio;

implementation

uses
  dmg_Main, frm_FormatosEnvioEditor, uDbErrorHandler;

{$R *.dfm}

procedure TfrmFormatosEnvio.FormShow(Sender: TObject);
begin
  inherited;
  frmFormatosEnvio := Self;
  OnDestroy := FormDestroy;
  pnlSearch.Visible := True;
  lblSearch.Caption := 'Buscar Formato Envío:';
end;

procedure TfrmFormatosEnvio.FormDestroy(Sender: TObject);
begin
  if frmFormatosEnvio = Self then
    frmFormatosEnvio := nil;
end;

procedure TfrmFormatosEnvio.btnRefreshClick(Sender: TObject);
var
  LSql: string;
begin
  FQryMain.Close;
  LSql := 'SELECT id, descripcion, capacidad_unidades, activo FROM ge_formatos_envio WHERE 1=1 ';
  
  if Trim(edtSearch.Text) <> '' then
  begin
    LSql := LSql + 'AND descripcion LIKE :search ';
  end;
  LSql := LSql + 'ORDER BY descripcion';
  
  FQryMain.SQL.Text := LSql;
  
  if Trim(edtSearch.Text) <> '' then
    FQryMain.ParamByName('search').AsString := '%' + Trim(edtSearch.Text) + '%';
    
  FQryMain.Open;
  
  dbGridMain.Columns.Clear;
  with dbGridMain.Columns.Add do
  begin
    FieldName := 'id';
    Title.Caption := 'ID';
    Width := 80;
  end;
  with dbGridMain.Columns.Add do
  begin
    FieldName := 'descripcion';
    Title.Caption := 'Descripción';
    Width := 350;
  end;
  with dbGridMain.Columns.Add do
  begin
    FieldName := 'capacidad_unidades';
    Title.Caption := 'Capacidad (Unidades)';
    Width := 150;
  end;
  with dbGridMain.Columns.Add do
  begin
    FieldName := 'activo';
    Title.Caption := 'Activo';
    Width := 70;
  end;
end;

procedure TfrmFormatosEnvio.btnEditClick(Sender: TObject);
begin
  if FQryMain.IsEmpty then Exit;
  
  if not Assigned(frmFormatosEnvioEditor) then
    Application.CreateForm(TfrmFormatosEnvioEditor, frmFormatosEnvioEditor);
  
  frmFormatosEnvioEditor.ParentForm := Self;
  frmFormatosEnvioEditor.EnvioId := FQryMain.FieldByName('id').AsInteger;
  if frmFormatosEnvioEditor.ShowModal = mrOk then
    btnRefreshClick(nil);
end;

procedure TfrmFormatosEnvio.btnNewClick(Sender: TObject);
begin
  if not Assigned(frmFormatosEnvioEditor) then
    Application.CreateForm(TfrmFormatosEnvioEditor, frmFormatosEnvioEditor);
  
  frmFormatosEnvioEditor.ParentForm := Self;
  frmFormatosEnvioEditor.EnvioId := 0;
  if frmFormatosEnvioEditor.ShowModal = mrOk then
    btnRefreshClick(nil);
end;

procedure TfrmFormatosEnvio.btnDeleteClick(Sender: TObject);
begin
  if FQryMain.IsEmpty then Exit;
  
  if MessageDlg('¿Está seguro de que desea eliminar este formato de envío/embalaje final?' + sLineBreak +
                'Se eliminarán también todos sus materiales asociados.', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    try
      dmgMain.qryExec.Close;
      dmgMain.qryExec.SQL.Text := 'DELETE FROM ge_formatos_envio WHERE id = :id';
      dmgMain.qryExec.ParamByName('id').AsInteger := FQryMain.FieldByName('id').AsInteger;
      dmgMain.qryExec.ExecSQL;
      btnRefreshClick(nil);
    except
      on E: Exception do
        TDbErrorHandler.HandleException(E, 'Error al eliminar el formato de envío');
    end;
  end;
end;

end.
