unit U_ZdyBtnHelp;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.Win.ADODB, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxScrollBox, Vcl.ExtCtrls,
  Vcl.Buttons, Vcl.StdCtrls;

type
  TfrmZdyBtnHelp = class(TForm)
    ADOConnection1: TADOConnection;
    cxScrollBox1: TcxScrollBox;
    Panel1: TPanel;
    ADOTmp: TADOQuery;
    SpeedButton1: TSpeedButton;
    Button3: TButton;
    Label1: TLabel;
    Label27: TLabel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    fPageSize:integer;
    fCurPageIndex:integer;
    procedure  InitItemPanel(mPageIndex:integer);
    procedure SpeedButtonClick(Sender: TObject);
  public
    fRowNumber:integer;
    fBadBtnSize:integer;
    fSelName:string;
    fFlag, fFlagName, fMainType: string;
  end;

var
  frmZdyBtnHelp: TfrmZdyBtnHelp;

implementation
uses
  U_globalVar,U_DataLink;
{$R *.dfm}

procedure TfrmZdyBtnHelp.Button1Click(Sender: TObject);
begin
  if fCurPageIndex <= 1 then
  begin
    Label1.Caption := '1';
    InitItemPanel(1);
  end
  else
  begin
    fCurPageIndex := fCurPageIndex - 1;

    Label1.Caption := IntToStr(fCurPageIndex);
    InitItemPanel(fCurPageIndex);
  end;
end;

procedure TfrmZdyBtnHelp.Button3Click(Sender: TObject);
begin
  fCurPageIndex := fCurPageIndex + 1;
  Label1.Caption := IntToStr(fCurPageIndex);
  InitItemPanel(fCurPageIndex);
end;

procedure TfrmZdyBtnHelp.FormCreate(Sender: TObject);
begin
  cxScrollBox1.Align:=alClient;
  with ADOConnection1 do
  begin
    Connected := false;
    ConnectionString := DConString;
    Connected := true;
  end;
  fPageSize:=30;
  fCurPageIndex:=1;
end;
//////////////////////////////
procedure TfrmZdyBtnHelp.FormShow(Sender: TObject);
begin
  self.Caption:=fflagName;
  InitItemPanel(1);
end;

procedure  TfrmZdyBtnHelp.InitItemPanel(mPageIndex:integer);
var
  speedBtn: TSpeedButton;
  zdyName: string;
  i, k, j: Integer;
  pageStartIndex:integer;
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
   // sql.Add('select AA.*,row_number () over (order by ZdyName) as keyNo from(');
    sql.Add('select A.*,ZJM=dbo.getPinYin(A.ZdyName) from KH_ZDY A where A.Type=''' + fFlag + '''');
    if Trim(fMainType) <> '' then
    begin
       sql.Add(' and A.MainType=''' + Trim(fMainType) + '''');
    end;
   // sql.Add(')AA');
   sql.Add('and valid=''Y''');
    sql.Add('order by ZdyNo offset '+intTostr((mPageIndex-1)*fpageSize) +' rows fetch next '+intTostr(fpageSize)+' rows only' );
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
   // Application.MessageBox('没有定义疵点！', '提示', 0);
    Exit;
  end;
  //if fRowNumber = 0 then
    fRowNumber := 6;
  pageStartIndex:= (StrToInt(Label1.Caption) - 1) * fPageSize + 1;
  j := fRowNumber;

  fBadBtnSize := 180;
  with ADOTmp do
  begin
    First;
    i := 0;
    while not Eof do
    begin
      k := i div j;
      zdyName := Trim(fieldbyname('ZDYName').AsString);
      speedBtn := TSpeedButton.Create(self);
      speedBtn.OnClick := SpeedButtonClick;

      speedBtn.Width := fBadBtnSize;
      speedBtn.Height := 100;


      speedBtn.Left := 5 + (i mod j) * speedBtn.Width;
      speedBtn.Top := 3 + k * speedBtn.Height;

      speedBtn.Parent := cxScrollBox1;
      speedBtn.Hint := zdyName;
      if Length(zdyName) > 4 then
      begin
        speedBtn.Caption := Copy(zdyName, 1, 4) + #13 +
          Copy(zdyName, 5, Length(zdyName) - 4);
      end
      else
        speedBtn.Caption := zdyName;

      i := i + 1;
      //
      if i=20 then  break;

      Next;
    end;
  end;

end;


procedure TfrmZdyBtnHelp.SpeedButtonClick(Sender: TObject);
begin
  fSelName := Trim(TSpeedButton(Sender).Caption);
  ModalResult := 1;

 // CIFNumber.SetFocus;

end;
end.
