unit frm_CalidadPrerrequisitosEditor;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls,
  Vcl.ExtCtrls, dmg_Calidad, dmg_Main;

type
  TfrmCalidadPrerrequisitosEditor = class(TForm)
    pcPrerreq: TPageControl;
    tsAgua: TTabSheet;
    tsQuebradizos: TTabSheet;
    lblFechaAgua: TLabel;
    dtpFechaAgua: TDateTimePicker;
    lblGrifo: TLabel;
    edtGrifo: TEdit;
    lblCloro: TLabel;
    edtCloro: TEdit;
    lblObsAgua: TLabel;
    memObsAgua: TMemo;
    lblArea: TLabel;
    edtArea: TEdit;
    lblElemento: TLabel;
    edtElemento: TEdit;
    lblRiesgo: TLabel;
    cboRiesgo: TComboBox;
    chkIntacto: TCheckBox;
    pnlBottom: TPanel;
    btnGuardar: TButton;
    btnCancelar: TButton;
    procedure FormShow(Sender: TObject);
    procedure btnGuardarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCalidadPrerrequisitosEditor: TfrmCalidadPrerrequisitosEditor;

implementation

{$R *.dfm}

procedure TfrmCalidadPrerrequisitosEditor.FormShow(Sender: TObject);
begin
  dtpFechaAgua.Date := Date;
end;

procedure TfrmCalidadPrerrequisitosEditor.btnGuardarClick(Sender: TObject);
var
  LCloroVal: Double;
  LConformeAgua: Boolean;
begin
  if pcPrerreq.ActivePage = tsAgua then
  begin
    LCloroVal := StrToFloatDef(edtCloro.Text, 0.0);
    if not dmgCalidad.ValidateAguaCloro(LCloroVal, LConformeAgua) then
    begin
      ShowMessage(Format('⚠ ALERTA DE AGUA POTABLE: El valor de Cloro Libre Residual (%.2f ppm) está fuera del rango legal (0.20 - 1.00 ppm).', [LCloroVal]));
    end;

    dmgCalidad.qryExecCalidad.Close;
    dmgCalidad.qryExecCalidad.SQL.Text :=
      'INSERT INTO ge_calidad_agua_potable (' +
      '  fecha, hora, nro_grifo, cloro_ppm, organoleptico_color, organoleptico_olor, ' +
      '  organoleptico_sabor, resultado_cloro, id_operario, observaciones' +
      ') VALUES (' +
      '  :fecha, :hora, :grifo, :cloro, ''CONFORME'', ''CONFORME'', ' +
      '  ''CONFORME'', :res, 1, :obs' +
      ')';
    dmgCalidad.qryExecCalidad.ParamByName('fecha').AsDate := dtpFechaAgua.Date;
    dmgCalidad.qryExecCalidad.ParamByName('hora').AsTime := Time;
    dmgCalidad.qryExecCalidad.ParamByName('grifo').AsString := edtGrifo.Text;
    dmgCalidad.qryExecCalidad.ParamByName('cloro').AsFloat := LCloroVal;
    if LConformeAgua then
      dmgCalidad.qryExecCalidad.ParamByName('res').AsString := 'CONFORME'
    else
      dmgCalidad.qryExecCalidad.ParamByName('res').AsString := 'NO_CONFORME';
    dmgCalidad.qryExecCalidad.ParamByName('obs').AsString := memObsAgua.Text;
    dmgCalidad.qryExecCalidad.ExecSQL;
  end
  else if pcPrerreq.ActivePage = tsQuebradizos then
  begin
    dmgCalidad.qryExecCalidad.Close;
    dmgCalidad.qryExecCalidad.SQL.Text :=
      'INSERT INTO ge_calidad_quebradizos (' +
      '  fecha_inspeccion, area_obrador, elemento, nivel_riesgo, cumple, id_inspector' +
      ') VALUES (' +
      '  :fecha, :area, :elem, :riesgo, :cumple, 1' +
      ')';
    dmgCalidad.qryExecCalidad.ParamByName('fecha').AsDate := dtpFechaAgua.Date;
    dmgCalidad.qryExecCalidad.ParamByName('area').AsString := edtArea.Text;
    dmgCalidad.qryExecCalidad.ParamByName('elem').AsString := edtElemento.Text;
    dmgCalidad.qryExecCalidad.ParamByName('riesgo').AsString := cboRiesgo.Text;
    dmgCalidad.qryExecCalidad.ParamByName('cumple').AsBoolean := chkIntacto.Checked;
    dmgCalidad.qryExecCalidad.ExecSQL;
  end;

  ModalResult := mrOk;
end;

end.
