unit U_frameGDXList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator,
  dxDateRanges, dxScrollbarAnnotations, Data.DB, cxDBData, cxCheckBox,
  cxTextEdit, dxBarBuiltInMenu, Vcl.Menus, cxGridCustomPopupMenu,
  cxGridPopupMenu, Datasnap.DBClient, Data.Win.ADODB, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ToolWin,
  Vcl.ExtCtrls, dxSkinsCore, dxSkinsDefaultPainters, cxMemo, cxLabel, cxMaskEdit,
  cxDropDownEdit, cxButtonEdit, cxButtons;

type
  TframeGDXList = class(TFrame)
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    CDS_HZ: TClientDataSet;
    DS_HZ: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel_dx: TPanel;
    LabelGDX: TLabel;
    cxLabel4: TcxLabel;
    dx_speed: TcxTextEdit;
    cxLabel21: TcxLabel;
    cxLabel23: TcxLabel;
    cxLabel24: TcxLabel;
    cxLabel25: TcxLabel;
    cxLabel6: TcxLabel;
    cxLabel61: TcxLabel;
    cxLabel62: TcxLabel;
    cxLabel63: TcxLabel;
    cxLabel64: TcxLabel;
    cxLabel65: TcxLabel;
    dx_temperature1: TcxTextEdit;
    dx_temperature5: TcxTextEdit;
    dx_temperature9: TcxTextEdit;
    dx_temperature6: TcxTextEdit;
    dx_temperature3: TcxTextEdit;
    dx_temperature4: TcxTextEdit;
    dx_temperature7: TcxTextEdit;
    dx_temperature8: TcxTextEdit;
    dx_temperature10: TcxTextEdit;
    dx_temperature2: TcxTextEdit;
    TbDXReplace2: TcxButton;
    TbDXClear2: TcxButton;
    cxLabel189: TcxLabel;
    dx_temperature11: TcxTextEdit;
    cxLabel5: TcxLabel;
    dx_Precautions: TcxMemo;
    procedure TBRafreshClick(Sender: TObject);

    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TbDXReplace2Click(Sender: TObject);
    procedure TbDXClear2Click(Sender: TObject);

  private
    procedure FillAllTextEditsWithValue(AParent: TWinControl; const AHint, AValue: string; AExclude: TcxTextEdit = nil);
    procedure ClearAllTextEditsByHint(AParent: TWinControl; const AHint: string; AExclude: TcxTextEdit = nil);
    function FindLastEditedTextEdit(AParent: TWinControl; const AHint: string): string;

    { Private declarations }
  public
    procedure InitGrid();
    function SaveData(mPSID: string): Boolean;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;
{$R *.dfm}

procedure TframeGDXList.InitGrid();
begin
//  POSNO.SetFocus;
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Close;
//      SQL.Clear;
//      sql.Add(' select A.* from  BS_Position A order by POSNO ');
//      Open;
//    end;
//    SCreateCDS(ADOQueryMain, CDS_HZ);
//    SInitCDSData(ADOQueryMain, CDS_HZ);
//  finally
//    ADOQueryMain.EnableControls;
//    ToolButton2.Click;
//  end;
end;

procedure TframeGDXList.TbDXClear2Click(Sender: TObject);
begin
  ClearAllTextEditsByHint(panel_DX, '干定型温控');
end;

procedure TframeGDXList.TbDXReplace2Click(Sender: TObject);
begin
  FillAllTextEditsWithValue(panel_dx, '干定型温控', FindLastEditedTextEdit(panel_dx, '干定型温控'));
end;

procedure TframeGDXList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TframeGDXList.ToolButton3Click(Sender: TObject);
var
  RTValues: TArray<string>;
begin

  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;
  RTValues := SelCDSKey(CDS_HZ, ['POSID']);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'Position';
      FFiltration1 := RTValues[0];
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TframeGDXList.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with CDS_HZ do
  begin
    while not Eof do
    begin
      if CDS_HZ.Locate('SSel', True, []) = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete BS_Position where POSID=' + QuotedStr(CDS_HZ.FieldByName('POSID').AsString));
          ExecSQL;
        end;
      end;

      CDS_HZ.Edit;
      CDS_HZ.FieldByName('SSel').Value := False;
      CDS_HZ.Post;
      Next;
    end;
  end;
  InitGrid();
end;

function TframeGDXList.SaveData(mPSID: string): Boolean;
var
  MaxProcessID, MaxBCPID, MaxPSID, MaxDTMID, mDTMID, DTMPSlist: string;
  maxsubid1, maxsubid2, maxsubid3, maxsubid4, maxsubid5, maxsubid6, mmaxgx: string;
begin
  try


////////////////       保存主表      //////////////////////
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Bs_Cloth_GY_Main where PSID=''' + Trim(mPSID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(mPSID) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('PSID').Value := Trim(mPSID);

      RTSetsavedata(ADOQueryCmd, 'Bs_Cloth_GY_Main', panel_dx, 2);
      ADOQueryCmd.Post;

    end;
////////////////       保存主表结束          //////////////////////



//    FPSID := maxId;
    Result := True;
  except
    Result := False;

    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;


procedure TframeGDXList.FillAllTextEditsWithValue(AParent: TWinControl; const AHint, AValue: string; AExclude: TcxTextEdit = nil);  // 可选：排除特定控件
var
  i: Integer;
  AControl: TControl;
  ATextEdit: TcxTextEdit;
begin
  for i := 0 to AParent.ControlCount - 1 do
  begin
    AControl := AParent.Controls[i];
    if (AControl is TcxTextEdit) and ((AControl as TcxTextEdit).Text = '') then
    begin
      ATextEdit := AControl as TcxTextEdit;
      if (ATextEdit.Hint = AHint) and (ATextEdit <> AExclude) then
        ATextEdit.Text := AValue;
    end;
  end;
end;

procedure TframeGDXList.ClearAllTextEditsByHint(AParent: TWinControl; const AHint: string; AExclude: TcxTextEdit = nil);
var
  i: Integer;
  AControl: TControl;
begin
  for i := 0 to AParent.ControlCount - 1 do
  begin
    AControl := AParent.Controls[i];
    // 检查是否是 TcxTextEdit，并且 Hint 匹配，且不是排除的控件
    if (AControl is TcxTextEdit) and (TcxTextEdit(AControl).Hint = AHint) and (TcxTextEdit(AControl) <> AExclude) then
    begin
      TcxTextEdit(AControl).Text := ''; // 直接清空
    end;
  end;
end;

function TframeGDXList.FindLastEditedTextEdit(AParent: TWinControl; const AHint: string): string;
var
  i, j: Integer;
  AControl: TControl;
  SortedControls: TList;
  TempControl: TControl;
begin
  Result := '';

  // 如果没有子控件，直接返回空
  if AParent.ControlCount = 0 then
    Exit;

  // 使用 TList 存储控件（兼容旧版 Delphi）
  SortedControls := TList.Create;
  try
    // 将所有子控件添加到列表
    for i := 0 to AParent.ControlCount - 1 do
      SortedControls.Add(AParent.Controls[i]);

    // 手动排序：按 Top（Y坐标）和 Left（X坐标）升序排列
    for i := 0 to SortedControls.Count - 1 do
    begin
      for j := i + 1 to SortedControls.Count - 1 do
      begin
        // 比较两个控件的 Top 和 Left
        if (TControl(SortedControls[i]).Top > TControl(SortedControls[j]).Top) or ((TControl(SortedControls[i]).Top = TControl(SortedControls[j]).Top) and (TControl(SortedControls[i]).Left > TControl(SortedControls[j]).Left)) then
        begin
          // 交换位置
          TempControl := TControl(SortedControls[i]);
          SortedControls[i] := SortedControls[j];
          SortedControls[j] := TempControl;
        end;
      end;
    end;
    // 遍历排序后的控件
    for i := 0 to SortedControls.Count - 1 do
    begin
      AControl := TControl(SortedControls[i]);
      if (AControl is TcxTextEdit) and (TcxTextEdit(AControl).Hint = AHint) and (TcxTextEdit(AControl).Text <> '') then
      begin
        Result := TcxTextEdit(AControl).Text; // 最后一个符合条件的会被保留
      end;
    end;
  finally
    SortedControls.Free;
  end;
end;


end.

