unit U_ProductHelp;

interface

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

type
  TfrmProductHelp = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    ImageList24: TImageList;
    DataSource1: TDataSource;
    ADOQueryTmp: TADOQuery;
    ADOQueryHelp: TADOQuery;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Label2: TLabel;
    cxTabControl1: TcxTabControl;
    Label3: TLabel;
    Edit2: TEdit;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1p_Code: TcxGridDBColumn;
    tv1P_chnName: TcxGridDBColumn;
    tv1P_engName: TcxGridDBColumn;
    tv1P_spec: TcxGridDBColumn;
    tv1P_gram: TcxGridDBColumn;
    tv1P_Breadth: TcxGridDBColumn;
    tv1P_upBreadth: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    tv1Num: TcxGridDBColumn;
    tv1P_Breadthp: TcxGridDBColumn;
    tv1P_upBreadthp: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    procedure BtnOkClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
  private
    SortID: Integer;
    SortCaption, SortField, SortOrder: String;
    procedure IntiQuery();
    procedure IntiQuery10();
  public
    fDipFlag:integer; //1:成品；2：坯布 ;3：贝斯
    fPbCtr:boolean;
    fCustomNo:string;
    fSysCode:string;
  end;

var
  frmProductHelp: TfrmProductHelp;

implementation
uses
  U_global;
{$R *.dfm}

procedure TfrmProductHelp.BtnOkClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;

procedure TfrmProductHelp.BtnCancelClick(Sender: TObject);
begin
  ModalResult := -1;  
end;


procedure TfrmProductHelp.IntiQuery();
var
  whereStr:string;
begin
  whereStr:='';
  if trim(edit1.Text)<>'' then
  begin
    whereStr:='and P_code like '+quotedStr('%'+trim(edit1.text)+'%');
  end;

  if trim(edit2.Text)<>'' then
  begin
    whereStr:='and P_chnName like '+quotedStr('%'+trim(edit2.text)+'%');
  end;
  
  //类型
  if cxTabControl1.TabIndex=1 then
    whereStr:=whereStr+'  and P_Code like ''P%'''
  else if cxTabControl1.TabIndex=2 then
    whereStr:=whereStr+'  and P_Code like ''J%'''
  else if cxTabControl1.TabIndex=3 then
    whereStr:=whereStr+' and P_Code like ''B%''';

  with ADOQueryHelp Do
  Begin
      DisableControls;
      Active := False;
      SQL.Clear;
      SQL.Add('select * from V_MC_Catlog');
      sql.Add('where 1=1 '+whereStr);
      Active := True;
      EnableControls;
   end;
end;
///////////////////////////////////////////
  //坯布控制
///////////////////////////////////////////
procedure TfrmProductHelp.IntiQuery10();
var
  filterStr:string;
begin
  filterStr:='';
  if trim(edit1.Text)<>'' then
  begin
    filterStr:=' and P_code like '+quotedStr('%'+trim(edit1.text)+'%');
  end;
  if trim(edit2.Text)<>'' then
  begin
    filterStr:=' and P_chnName like '+quotedStr('%'+trim(edit2.text)+'%');
  end;
  //类型
  //if cxTabControl1.TabIndex=1 then
  //  filterStr:=filterStr+' and P_type=''C'''
  //else if cxTabControl1.TabIndex=2 then
  //  filterStr:=filterStr+' and P_type=''P''';
  try
    with ADOQueryHelp Do
    Begin
      DisableControls;
      close;
      filtered:=false;
      SQL.Clear;
      if trim(fSysCode)='YH' then
        sql.Add('exec JS_PBCK_CHANGE_YH10')
      else
        SQL.Add('exec JS_PBCK_CHANGE10');
      sql.Add(QuotedStr(fCustomNo));
      Open;

      if trim(filterStr)='' then
      begin
        ADOQueryHelp.Filtered:=false;
        ADOQueryHelp.EnableControls;
        exit;
      end;
      filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
      with ADOQueryHelp do
      begin
        filtered:=false;
        filter:=filterStr;
        filtered:=true;
      end;
      EnableControls;
    end;

  finally
    
  end;
end;

procedure TfrmProductHelp.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
Var
   TmpSql :string;
begin
   if Key = VK_RETURN then
   begin
     if fPbCtr then
       IntiQuery10()
     else
       IntiQuery();
   end;
end;

procedure TfrmProductHelp.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
end;

procedure TfrmProductHelp.FormShow(Sender: TObject);
var
  tabIndex:integer;
begin
  cxTabControl1.TabIndex:=fDipFlag;
  if fPbCtr then
    IntiQuery10()
  else
    IntiQuery();
  Edit1.SetFocus;

end;

procedure TfrmProductHelp.cxTabControl1Change(Sender: TObject);
var
  tabIndex:integer;
begin
  tabIndex:=cxTabControl1.TabIndex;
  if cxTabControl1.Tabs[tabIndex].Caption='坯布仓库' then
    IntiQuery10()
  else
    IntiQuery();
end;

procedure TfrmProductHelp.tv1DblClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult := 1;
end;

end.
