unit U_frameBads;

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, 
   Vcl.StdCtrls, Vcl.ExtCtrls, cxTextEdit, MovePanel, Vcl.Buttons,
  cxGeometry, dxFramedControl, Vcl.Menus, cxButtons, dxPanel, cxScrollBox,
  dxLayoutcxEditAdapters, dxLayoutContainer, cxClasses, dxLayoutControl,
  Data.DB, Data.Win.ADODB, cxCheckBox, dxSkinsCore, dxSkinWXI;

type
  TframeBads = class(TFrame)
    dxPanel1: TdxPanel;
    RadioGroup1: TRadioGroup;
    dxPanel3: TdxPanel;
    cxButton1: TcxButton;
    cxButton2: TcxButton;
    CIFBeg: TcxTextEdit;
    CIFEnd: TcxTextEdit;
    CIFQty: TcxTextEdit;
    CIFNumber: TcxTextEdit;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutGroup1: TdxLayoutGroup;
    dxLayoutGroup2: TdxLayoutGroup;
    dxLayoutItem2: TdxLayoutItem;
    lblCIFName: TdxLayoutLabeledItem;
    dxLayoutEmptySpaceItem1: TdxLayoutEmptySpaceItem;
    dxLayoutLabeledItem1: TdxLayoutLabeledItem;
    dxLayoutItem1: TdxLayoutItem;
    dxLayoutItem3: TdxLayoutItem;
    dxLayoutItem4: TdxLayoutItem;
    dxLayoutItem5: TdxLayoutItem;
    dxLayoutItem6: TdxLayoutItem;
    lblLenUnit1: TdxLayoutLabeledItem;
    ADOTmp: TADOQuery;
    IsSubtract: TcxCheckBox;
    cxScrollBox1: TcxScrollBox;
    SubtractQty: TcxTextEdit;
    dxLayoutAutoCreatedGroup1: TdxLayoutAutoCreatedGroup;
    cxCheck_len: TcxCheckBox;
    dxLayoutItem_checkLen: TdxLayoutItem;
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure CIFBegPropertiesEditValueChanged(Sender: TObject);
    procedure IsSubtractClick(Sender: TObject);
    procedure SubtractQtyPropertiesEditValueChanged(Sender: TObject);
    procedure CIFEndPropertiesChange(Sender: TObject);
  private
    procedure SpeedButtonClick(Sender: TObject);
  public
    fBadBtnSize: Integer;
    fRowNumber: Integer;
    procedure InitBadPanel();
  end;

implementation

uses
  U_DataLink;
{$R *.dfm}

procedure TframeBads.CIFBegPropertiesEditValueChanged(Sender: TObject);
var
  mvalue: Double;
begin
  if Trim(CIFBeg.Text) <> '' then
  begin
    if TryStrToFloat(CIFBeg.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    Exit;
  end;
  if Trim(CIFEnd.Text) = '' then
  begin
    CIFQty.Text := '';
    exit;
  end ;

  CIFQty.Text := FloatToStr(StrToFloat(CIFEnd.Text) - StrToFloat(CIFBeg.Text));
end;

procedure TframeBads.CIFEndPropertiesChange(Sender: TObject);
var
  mvalue: Double;
begin
  if Trim(CIFEnd.Text) <> '' then
  begin
    if TryStrToFloat(CIFEnd.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end ;

  if trim(CIFBeg.Text)='' then
    CIFQty.Text :=''
  else
    CIFQty.Text := FloatToStr(StrToFloatDef(trim(CIFEnd.Text),0) - StrToFloatdef(trim(CIFBeg.Text),0));
end;

procedure TframeBads.cxButton1Click(Sender: TObject);
begin
  RadioGroup1.ItemIndex := -1;
end;

procedure TframeBads.cxButton2Click(Sender: TObject);
var
  mvalue: Double;
begin

  if Trim(CIFBeg.Text) <> '' then
  begin
    if Trim(CIFEnd.Text) <> '' then
    begin
      if StrToFloat(CIFEnd.Text) < StrToFloat(CIFBeg.Text) then
      begin
        Application.MessageBox('疵点位置录入错误!', '提示', 0);
        Exit;
      end;
    end;
  end;
  if CIFQty.Text <> '' then
  begin
    if TryStrToFloat(CIFQty.Text, mvalue) = False then
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;

  if strTofloatdef(Trim(SubtractQty.Text),0)>0 then
  begin
    IsSubtract.Checked:=true;
  end
  else
  begin
    IsSubtract.Checked:=false;
  end;

end;

procedure TframeBads.InitBadPanel();
var
  speedBtn: TSpeedButton;
  zdyName: string;
  i, k, j: Integer;
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select ZDYName from KH_Zdy where Type=''WFBCD'' order by ZDYNO ');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    Application.MessageBox('没有定义疵点！', '提示', 0);
    Exit;
  end;
  if fRowNumber = 0 then
    fRowNumber := 5;

  j := fRowNumber;
  if fBadBtnSize < 30 then
    fBadBtnSize := 40;
  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 := fBadBtnSize;


      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;
      Next;
    end;
  end;

end;

procedure TframeBads.IsSubtractClick(Sender: TObject);
begin

end;

//

procedure TframeBads.SpeedButtonClick(Sender: TObject);
begin
  dxPanel1.Visible := true;

  lblCIFName.Caption := Trim(TSpeedButton(Sender).Hint);

 // CIFNumber.SetFocus;

end;

procedure TframeBads.SubtractQtyPropertiesEditValueChanged(Sender: TObject);
begin
  if trim(SubtractQty.Text)<>'' then
     isSubTract.Checked:=true
  else
    isSubTract.Checked:=false;
end;

end.
