unit U_GlideCraftHelp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ExtCtrls, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ComCtrls, ToolWin, cxCheckBox,
  ADODB, StdCtrls, DBClient, cxCalc;

type
  TfrmGlideCraftHelp = class(TForm)
    ToolBar1: TToolBar;
    Tfresh: TToolButton;
    Tcolse: TToolButton;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    v1Idx: TcxGridDBColumn;
    v1CraftType: TcxGridDBColumn;
    v1CraftNo: TcxGridDBColumn;
    v1craftName: TcxGridDBColumn;
    v1price: TcxGridDBColumn;
    v1fitDevice: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Tok: TToolButton;
    ADOQueryHelp: TADOQuery;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    tv1sel: TcxGridDBColumn;
    Label1: TLabel;
    Edit1: TEdit;
    cxGrid2: TcxGrid;
    tv2: TcxGridDBTableView;
    tv2Name: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    DataSource2: TDataSource;
    ADOQueryLeft: TADOQuery;
    cxGrid3: TcxGrid;
    tv3: TcxGridDBTableView;
    tv3CraftType: TcxGridDBColumn;
    tv3CraftNo: TcxGridDBColumn;
    tv3CraftName: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_sel: TDataSource;
    CDS_sel: TClientDataSet;
    CDS_selcraftNo: TStringField;
    CDS_selcraftName: TStringField;
    CDS_selCraftType: TStringField;
    CDS_selprice: TFloatField;
    CDS_selglideNo: TStringField;
    CDS_selRate: TFloatField;
    tv3Rate: TcxGridDBColumn;
    procedure TcolseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TfreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure tv2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TokClick(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure tv3DblClick(Sender: TObject);
  private
    procedure InitCraftGrid();
    procedure InitLeft();
    procedure AddRow();
  public
    fCraftType:string;
  end;

var
  frmGlideCraftHelp: TfrmGlideCraftHelp;

implementation
uses
  U_global;
{$R *.dfm}

procedure TfrmGlideCraftHelp.TcolseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmGlideCraftHelp.FormCreate(Sender: TObject);
begin
  cxGrid1.Align :=alClient;
  with ADOConnection1 do
  begin
    Connected:= False;
    ConnectionString := gConString;
    Connected := True;
  end;
  CDS_sel.Close ;
  CDS_sel.CreateDataSet ;
end;

procedure TfrmGlideCraftHelp.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=cafree;
end;
/////////////////////////////////////////////////////////
  //
/////////////////////////////////////////////////////////
procedure TfrmGlideCraftHelp.InitCraftGrid();
var
  mWhereStr:string;
begin
  if trim(Edit1.Text)<>'' then
    mWhereStr:='and craftNO like '+quotedStr('%'+trim(Edit1.Text)+'%');
  try
    ADOQueryHelp.DisableControls ;
    with ADOQueryHelp do
    begin
      close;
      sql.clear;
      sql.Add('select * from mc_glide_craft');
      sql.Add('where craftType='+quotedStr(trim(ADOQueryLeft.fieldByName('Name').AsString)));
      sql.Add('and valid=''Y''');
      sql.Add(mWhereStr);
      sql.Add('order By Idx');
      Open;
    end;
  finally
    ADOQueryHelp.EnableControls;
  end;
end;
procedure TfrmGlideCraftHelp.TfreshClick(Sender: TObject);
begin
  InitCraftGrid();
end;
////////////////////////////////////////////////
  //
////////////////////////////////////////////////
procedure TfrmGlideCraftHelp.InitLeft();
begin
  try
    with ADOQueryLeft do
    begin
      close;
      sql.Clear ;
      sql.Add('select * ');
      sql.Add('from xc_code');
      sql.Add('where flag=''CRAFTTYPE''');
      sql.Add('and valid=''Y''');
      Open;
      locate('Name',fCraftType,[]);
    end;
  finally
  end;
end;

procedure TfrmGlideCraftHelp.FormShow(Sender: TObject);
begin
  InitLeft();
end;

procedure TfrmGlideCraftHelp.tv2FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitCraftGrid();
end;

procedure TfrmGlideCraftHelp.TokClick(Sender: TObject);
begin
  Edit1.SetFocus ;
  if CDS_sel.IsEmpty then exit;
  {
  try
    ADOQueryHelp.DisableControls ;
    with ADOQueryHelp do
    begin
      filtered:=false;
      filter:='sel=1';
      filtered:=true;      
    end;
  finally
    ADOQueryHelp.EnableControls;
  end;
  }
  ModalResult:=1;
end;

procedure TfrmGlideCraftHelp.Edit1Change(Sender: TObject);
begin
  InitCraftGrid();
end;
/////////////////////////////////////////////
  //
//////////////////////////////////////////////
procedure TfrmGlideCraftHelp.AddRow();
begin
  with CDS_sel do
  begin
    Append;
    fieldByName('craftType').value:= trim(ADOQueryHelp.fieldByName('craftType').asString);
    fieldByName('craftNo').value:= trim(ADOQueryHelp.fieldByName('craftNo').asString);
    fieldByName('craftName').value:= trim(ADOQueryHelp.fieldByName('craftName').asString);
    fieldByName('price').value:= ADOQueryHelp.fieldByName('price').AsFloat;
    fieldByName('Rate').value:= 100;
    Post;
  end;
end;

procedure TfrmGlideCraftHelp.tv1DblClick(Sender: TObject);
begin
  AddRow();
end;

procedure TfrmGlideCraftHelp.tv3DblClick(Sender: TObject);
begin
  IF CDS_sel.IsEmpty THEN EXIT;
  CDS_sel.Delete ;
end;

end.
