unit SelectDetailGLsFrm;

interface

uses
  ProjectGLDm, sdDB, mDataRecord,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, sdGridDBA, ZJGrid;

type
  TSelectDetailGLsForm = class(TForm)
    pnlData: TPanel;
    btnOk: TButton;
    btnCancel: TButton;
    leSearch: TLabeledEdit;
    imgSearch: TImage;
    zgGL: TZJGrid;
    saGL: TsdGridDBA;
    procedure zgGLGetCellText(Sender: TObject; const ACoord: TPoint;
      var Value: String; DisplayText: Boolean);
    procedure zgGLSetCellText(Sender: TObject; const ACoord: TPoint;
      var Value: String; DisplayText: Boolean);
    procedure btnOkClick(Sender: TObject);
    procedure imgSearchClick(Sender: TObject);
  private
    FResults: TList;

    function FindProjectGL(AStr: string): TProjectGLRecord;
  public
    constructor Create(AProjectGLData: TProjectGLData);
    destructor Destroy; override;

    property Results: TList read FResults;
  end;

function SelectDetailGLs(AProjectGLData: TProjectGLData; AGLs: TList): Boolean;

implementation

uses
  UtilMethods;

{$R *.dfm}

function SelectDetailGLs(AProjectGLData: TProjectGLData; AGLs: TList): Boolean;
var
  SelectForm: TSelectDetailGLsForm;
begin
  SelectForm := TSelectDetailGLsForm.Create(AProjectGLData);
  try
    Result := SelectForm.ShowModal = mrOk;
    if Result then
      AGLs.Assign(SelectForm.Results);
  finally
    SelectForm.Free;
  end;
end;

{ TAddDetailGLsForm }

constructor TSelectDetailGLsForm.Create(AProjectGLData: TProjectGLData);
begin
  inherited Create(nil);
  saGL.DataView := AProjectGLData.sdvProjectGL;
  FResults := TList.Create;
  ClientHeight := 495;
  ClientWidth := 472;
end;

destructor TSelectDetailGLsForm.Destroy;
begin
  FResults.Free;
  inherited;
end;

procedure TSelectDetailGLsForm.zgGLGetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);
var
  Rec: TsdDataRecord;
begin
  if (ACoord.X = 1) and (ACoord.Y > 0) then
  begin
    Rec := saGL.DataView.Records[ACoord.Y -1];
    if Assigned(Rec) and (FResults.IndexOf(Rec) >= 0) then
      Value := 'True'
    else
      Value := 'False';
  end;
end;

procedure TSelectDetailGLsForm.zgGLSetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);
var
  Rec: TsdDataRecord;
begin
  if (ACoord.X = 1) and (ACoord.Y > 0) then
  begin
    Rec := saGL.DataView.Records[ACoord.Y -1];
    if Value = 'True' then
      FResults.Add(Rec)
    else if Value = 'False' then
      FResults.Remove(Rec);
  end;
end;

procedure TSelectDetailGLsForm.btnOkClick(Sender: TObject);
begin
  if FResults.Count > 0 then
    ModalResult := mrOk
  else
    TipMessage('没有选择任何工料。');
end;

function TSelectDetailGLsForm.FindProjectGL(AStr: string): TProjectGLRecord;

  function SearchProjectGL(ABegin, AEnd: Integer): TProjectGLRecord;
  var
    iGL: Integer;
    Rec: TProjectGLRecord;
  begin
    Result := nil;
    for iGL := ABegin to AEnd do
    begin
      Rec := TProjectGLRecord(saGL.DataView.Records[iGL]);
      if (Pos(AStr, Rec.Code.AsString) > 0) or
          (Pos(AStr, Rec.Name.AsString) > 0) then
      begin
        Result := Rec;
        Break;
      end;
    end;
  end;

var
  iCur, iGL: Integer;
  GLRec: TProjectGLRecord;
begin
  Result := nil;
  if AStr = '' then Exit;

  iCur := saGL.DataView.IndexOf(saGL.DataView.Current);
  Result := SearchProjectGL(iCur + 1, saGL.DataView.RecordCount - 1);
  if not Assigned(Result) then
    Result := SearchProjectGL(0, iCur);
end;

procedure TSelectDetailGLsForm.imgSearchClick(Sender: TObject);
var
  Rec: TProjectGLRecord;
begin
  Rec := FindProjectGL(leSearch.Text);
  if Assigned(Rec) then
    saGL.DataView.LocateInControl(Rec)
  else
    TipMessage(Format('找不到编号、名称含有“%s”的工料。', [leSearch.Text]));
end;

end.
