unit U_ClientPrintRmf;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, RM_Common, RM_Class,
  RM_GridReport, RM_Dataset, RM_E_llPDF, RM_BarCode, RM_e_Graphic, RM_e_Jpeg,
  RM_e_Xls, cxContainer, cxMaskEdit, cxDropDownEdit, cxMRUEdit, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar, cxButtons, RM_System;

type
  TfrmClientPrintRmf = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOConnection1: TADOConnection;
    ImageList1: TImageList;
    RMDB_1: TRMDBDataSet;
    RM1: TRMGridReport;
    RMDB_2: TRMDBDataSet;
    CDS_Label: TClientDataSet;
    ADO_Prt: TADOQuery;
    RMDB_3: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMJPEGExport1: TRMJPEGExport;
    RMBarCodeObject1: TRMBarCodeObject;
    ADOQueryCmd: TADOQuery;
    ADO_While: TADOQuery;
    RMDB_4: TRMDBDataSet;
    RMDB_5: TRMDBDataSet;
    ADOQueryReport: TADOQuery;
    CDS_1: TClientDataSet;
    CDS_2: TClientDataSet;
    CDS_3: TClientDataSet;
    CDS_4: TClientDataSet;
    CDS_5: TClientDataSet;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
  private
    IsDebug, FPreviewPrint, fIsPreview: Boolean;
    FLMType, FLBName, FQrCodeField: string;
    FExportFileType, FExportFileName: string;
    SqlStr1, SqlStr2, SqlStr3, SqlStr4, SqlStr5: string;
    FSuccessfulFun: string;
    FparamBlclid: string;
    procedure PrintReport();
    procedure ExportReport();
    procedure GetPrtData(Ado: TADOQuery; Cds: TClientDataSet; SqlStr: string);
    procedure InitArgs();

    { Private declarations }
  public
    FPrintJson: PChar;
    FJsonOut: PChar;
    DConString, DCode, DName: string;
    constructor Create(AOwner: TComponent; JsonArgs: PChar);

    { Public declarations }
  end;

var
  frmClientPrintRmf: TfrmClientPrintRmf;

implementation

uses
  U_RTFun, superobject, U_QrCodeFun;

{$R *.dfm}
constructor TfrmClientPrintRmf.Create(AOwner: TComponent; JsonArgs: PChar);
begin
  inherited Create(AOwner);
  FPrintJson := JsonArgs;
end;
                     // 创建二维码存放目录（如果不存在）

procedure TfrmClientPrintRmf.InitArgs;
var
  JSONObject, item: ISuperObject;
  jsonArray: TSuperArray;
  i, loopCount: Integer;
begin
  // 解析 JSON 字符串
  JSONObject := SO(FPrintJson);
  if JSONObject = nil then
  begin
    Application.MessageBox('PrintJson格式异常!', '提示', 0);
    Exit;
  end;

  // 提取基础字段
  fIsPreview := JSONObject.B['IsPreview'];
  IsDebug := JSONObject.B['IsDebug'];
  FSuccessfulFun := JSONObject.S['SuccessfulFun'];
  FLMType := JSONObject.S['LMType'];
  FPreviewPrint := JSONObject.B['PreviewPrint'];
  FLBName := JSONObject.S['LBName'];
  FExportFileType := JSONObject.S['ExportFileType'];
  FExportFileName := JSONObject.S['ExportFileName'];
  FQrCodeField := JSONObject.S['QrCodeField'];
  DConString := JSONObject.S['DConString'];
  DCode := JSONObject.S['DCode'];
  DName := JSONObject.S['DName'];
  
  // 设置默认导出文件名
  if Trim(FExportFileName) = '' then
    FExportFileName := FLBName;



  // 初始化 SQL 字符串变量
  SqlStr1 := '';
  SqlStr2 := '';
  SqlStr3 := '';
  SqlStr4 := '';
  SqlStr5 := '';

  // 获取并处理 PrtArgs 数组
  jsonArray := JSONObject.A['PrtArgs'];
  if Assigned(jsonArray) then
  begin
    // 确定循环次数（最多5个元素）
    loopCount := jsonArray.Length;

    for i := 0 to loopCount - 1 do
    begin
      item := jsonArray.O[i];
      case i of
        0:
          SqlStr1 := item.S['SqlStr'];
        1:
          SqlStr2 := item.S['SqlStr'];
        2:
          SqlStr3 := item.S['SqlStr'];
        3:
          SqlStr4 := item.S['SqlStr'];
        4:
          SqlStr5 := item.S['SqlStr'];
      end;
    end;
  end;

  // 设置报表预览按钮
//  if FPreviewPrint then
//    RM1.PreviewButtons := [rmpbZoom, rmpbLoad, rmpbSave, rmpbPrint, rmpbFind, rmpbPageSetup, rmpbExit, rmpbSaveToXLS, rmpbExport, rmpbNavigator]
//  else
//    RM1.PreviewButtons := [rmpbZoom, rmpbLoad, rmpbSave, rmpbFind, rmpbPageSetup, rmpbExit, rmpbSaveToXLS, rmpbExport, rmpbNavigator];
end;

procedure TfrmClientPrintRmf.GetPrtData(Ado: TADOQuery; Cds: TClientDataSet; SqlStr: string);

begin
  if Trim(SqlStr) = '' then
    Exit;

  // 执行ADO查询
  with Ado do
  begin
    Close;
    SQL.Clear;
    SQL.Add(SqlStr);
    if IsDebug then
      ShowMessage(sql.Text);
    Open;
  end;
  IintCDS(Ado, Cds);
  SetQrCodePath(Cds, FQrCodeField);
end;

procedure TfrmClientPrintRmf.ExportReport();
var
  fPrintFile, fExportPath: string;
begin
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLBName + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['LBPrtCode'] := dcode;
    RMVariables['LBPrtName'] := dname;

    fExportPath := ExtractFilePath(Application.ExeName) + FExportFileName + '.' + FExportFileType;
    if not DirectoryExists(ExtractFileDir(fExportPath)) then
      CreateDir(ExtractFileDir(fExportPath));
    RM1.PrepareReport;    //直接导出
//    if FExportFileType = 'pdf' then
//      RM1.ExportTo(RMllPDFExport1, fExportPath)
//    else
    RM1.ExportTo(RMXLSExport1, fExportPath);

    FJsonOut := '{"success":true,"message":"导出成功!"}';
  end
  else
  begin
    FJsonOut := '{"success":false,"message":"报表文件没找到!"}';
  end;

end;

procedure TfrmClientPrintRmf.PrintReport();
var
  fPrintFile: string;
begin
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLBName + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
//    RM1.DefaultCopies := StrToIntDef(ComboBox1.Text, 1);
    RMVariables['LBPrtCode'] := dcode;
    RMVariables['LBPrtName'] := dname;
    if fIsPreview then
      RM1.ShowReport
    else
      RM1.PrintReport;

    FJsonOut := '{"success":true,"message":"打印成功!"}';
  end
  else
  begin
    FJsonOut := '{"success":false,"message":"报表文件没找到!"}';
  end;

end;

procedure TfrmClientPrintRmf.FormCreate(Sender: TObject);
begin
  FPreviewPrint := True;
  InitArgs();

  with ADOConnection1 do
  begin
    Connected := false;
    ConnectionString := DConString;
    Connected := true;
  end;
  // 导出文件操作（保持不变）
  ExportFtErpFile(FLBName + '.rmf', ADOQueryReport);
  EnsureQrCodeDirectory();
  ClearQrCodeDirectory();
end;

procedure TfrmClientPrintRmf.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Action := cafree;
end;

procedure TfrmClientPrintRmf.FormShow(Sender: TObject);
begin

  if flbName = '' then
  begin
    FJsonOut := '{"success":false,"message":"未传入打印标签!"}';
    self.Close;
    exit;
  end;
  if SqlStr1 = '' then
  begin
    FJsonOut := '{"success":false,"message":"未传入打印语句!"}';
    self.Close;
    exit;
  end;

  GetPrtData(ADO_Prt, CDS_1, SqlStr1);
  GetPrtData(ADO_Prt, CDS_2, SqlStr2);
  GetPrtData(ADO_Prt, CDS_3, SqlStr3);
  GetPrtData(ADO_Prt, CDS_4, SqlStr4);
  GetPrtData(ADO_Prt, CDS_5, SqlStr5);

  if FExportFileType = '' then
  begin
    PrintReport();
  end
  else
    ExportReport();

  self.Close;
end;

procedure TfrmClientPrintRmf.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmClientPrintRmf.FormDestroy(Sender: TObject);
begin
  frmClientPrintRmf := nil;
end;

end.

