unit U_PBListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ToolWin, ComCtrls,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, DBClient, ADODB, ImgList,
  StdCtrls, ExtCtrls, cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxButtonEdit, BtnEdit, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL,
  cxTLData, cxContainer, cxCurrencyEdit, cxCheckBox, cxSplitter;

type
  TfrmPBListSel = class(TForm)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBClose: TToolButton;
    ThreeImgList: TImageList;
    DataSource2: TDataSource;
    ML_GYS: TClientDataSet;
    ADOQuery6: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    Panel4: TPanel;
    DataSource4: TDataSource;
    ADOQueryTree: TADOQuery;
    Order_Tree: TClientDataSet;
    Panel6: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    V1BPMF1: TcxGridDBColumn;
    V1BPKZ: TcxGridDBColumn;
    V1BPJS: TcxGridDBColumn;
    v1BPMiDu: TcxGridDBColumn;
    V1BPID: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    Panel7: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ToolBar3: TToolBar;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    V1BPCF: TcxGridDBColumn;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    V1Column2: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BPNameM: TEdit;
    BPCodeM: TEdit;
    V1BPNote: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure BPNamehange(Sender: TObject);
    procedure BPCodeMChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure InitGrid();
    procedure initTree();
    { Private declarations }
  public
    { Public declarations }
  end;
var
  frmPBListSel: TfrmPBListSel;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}
procedure TfrmPBListSel.initTree();
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select *');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' then RTrim(CPNo)+''/''+CPName else CPName end as CP ');
    SQL.Add('from CP_TypePB order by CPlevel,CPOrder,CPName');
    Open;
  end;
  SCreateCDS20(ADOQueryTree,Order_Tree);
  SInitCDSData20(ADOQueryTree,Order_Tree);
  cxDBTreeList1.Items[0].Expand(true);
end;

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

procedure TfrmPBListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_BP :Code,:PState,:CYType,:Stats ');
      Parameters.ParamByName('Code').Value:='';
      Parameters.ParamByName('PState').Value:=0;
      Parameters.ParamByName('CYType').Value:=Trim(Order_Tree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('Stats').Value:='';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBListSel.TBCloseClick(Sender: TObject);
begin
  panel1.SetFocus;
  WriteCxGrid('坯布信息列表',TV1,'采购管理');
  Close;
end;

procedure TfrmPBListSel.FormShow(Sender: TObject);
begin
  initTree();
  InitGrid();
  ReadCxGrid('坯布信息列表',TV1,'采购管理');
end;

procedure TfrmPBListSel.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult:=1;
end;

procedure TfrmPBListSel.BPNamehange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(BPNameM.Text)<>'' then
  begin
    fsj:=' BPName like '''+'%'+Trim(BPNameM.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmPBListSel.BPCodeMChange(Sender: TObject);
var
  fsj:String;
begin
  if Trim(BPCodeM.Text)<>'' then
  begin
    fsj:=' BPCode like '''+'%'+Trim(BPCodeM.Text)+'%'+'''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,fsj);
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  end;
end;

procedure TfrmPBListSel.ToolButton1Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmPBListSel.cxDBTreeList1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPBListSel.FormDestroy(Sender: TObject);
begin
  frmPBListSel:=nil;
end;

end.
