unit U_OrdColorSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, DB, ADODB, StdCtrls, ExtCtrls, DBClient;

type
  TfrmOrdColorSel = class(TForm)
    ScrollBox1: TScrollBox;
    btn1: TSpeedButton;
    ADOTmp: TADOQuery;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    SpeedButton16: TSpeedButton;
    SpeedButton17: TSpeedButton;
    SpeedButton18: TSpeedButton;
    SpeedButton19: TSpeedButton;
    Panel1: TPanel;
    Label27: TLabel;
    Label1: TLabel;
    Button3: TButton;
    Button1: TButton;
    Order_Main: TClientDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    CurrentPage, RecordsNumber, FTotalCount: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    FlagStr, FSDPerson: string;
    { Public declarations }
  end;

var
  frmOrdColorSel: TfrmOrdColorSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmOrdColorSel.FormDestroy(Sender: TObject);
begin
  frmOrdColorSel := nil;
end;

procedure TfrmOrdColorSel.InitGrid();
var
  i, j: Integer;
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('exec P_Page_ColorSel @pageSize=20,@pageIndex=' + trim(IntToStr(CurrentPage)) + ',@criteria=' + quotedstr(Trim(FlagStr)));
    Open;
  end;
  SCreateCDS20(ADOTmp, Order_Main);
  SInitCDSData20(ADOTmp, Order_Main);

  Label1.Caption := trim(IntToStr(CurrentPage));
  FTotalCount := Order_Main.fieldbyname('TotalCount').AsInteger;
  try
    with ScrollBox1 do
    begin
      for j := 0 to ControlCount-1 do
      begin
        if Controls[j] is TSpeedButton then
        begin
          if Order_Main.Locate('rownumber', j + 1 + ((CurrentPage - 1) * RecordsNumber), []) then
          begin
            TSpeedButton(Controls[j]).Visible := True;
            TSpeedButton(Controls[j]).Caption := Trim(Order_Main.fieldbyname('MLColor').AsString);
            TSpeedButton(Controls[j]).Hint := Trim(Order_Main.fieldbyname('NewSubid').AsString);
          end
          else
          begin
            TSpeedButton(Controls[j]).Visible := False;
            TSpeedButton(Controls[j]).Caption := '';
            TSpeedButton(Controls[j]).Hint := '';
          end;
        end;
      end;
    end;
  except
    ShowMessage(IntToStr(j));
  end;
end;

procedure TfrmOrdColorSel.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 20;
  InitGrid();
end;

procedure TfrmOrdColorSel.btn1Click(Sender: TObject);
begin
  FSDPerson := Trim(TSpeedButton(Sender).Hint);
  ModalResult := 1;
end;

procedure TfrmOrdColorSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmOrdColorSel.Button1Click(Sender: TObject);
begin



  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();

end;

procedure TfrmOrdColorSel.Button3Click(Sender: TObject);
begin
  if CurrentPage < FTotalCount / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmOrdColorSel.FormCreate(Sender: TObject);
var
  j: Integer;
begin

  with ScrollBox1 do
  begin
    for j := 0 to ScrollBox1.ControlCount-1 do
    begin
      if Controls[j] is TSpeedButton then
      begin
        TSpeedButton(Controls[j]).Visible := False;
        TSpeedButton(Controls[j]).Caption := '';
        TSpeedButton(Controls[j]).Hint := '';
      end;
    end;
  end;
end;

end.

