unit U_TatClothInfoSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList, U_BaseInput, cxTL,
  cxMaskEdit, cxTLdxBarBuiltInMenu, cxCheckBox, cxInplaceContainer, cxDBTL,
  cxTLData, math, dxScrollbarAnnotations, Vcl.Menus;

type
  TfrmTatClothInfoSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    DS_Tree: TDataSource;
    CDS_Tree: TClientDataSet;
    ADOQueryTree: TADOQuery;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column12: TcxGridDBColumn;
    v1CYNo: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel7: TPanel;
    Panel4: TPanel;
    Label14: TLabel;
    LBCPAP1: TLabel;
    Button1: TButton;
    Button2: TButton;
    TCBNOR1: TComboBox;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label8: TLabel;
    C_Code: TEdit;
    C_Name: TEdit;
    C_GramWeight: TEdit;
    C_Width: TEdit;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_Sub: TClientDataSet;
    DataSource3: TDataSource;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    Tv1SSel: TcxGridDBColumn;
    Tv2C_Color: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TCBNOR1Change(Sender: TObject);
    procedure C_NameChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure N1Click(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitTree();
    procedure InitGrid2();
    { Private declarations }
  public
    FCoType: string;
    { Public declarations }
  end;

var
  frmTatClothInfoSel: TfrmTatClothInfoSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}
procedure TfrmTatClothInfoSel.InitTree();
var
  i: Integer;
begin

  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_Type  where CTType=''梭织'' ');
    Open;
  end;

  SCreateCDS(ADOQueryTree, CDS_Tree);
  SInitCDSData(ADOQueryTree, CDS_Tree);
  cxDBTreeList1.Items[0].Expand(false);
end;

procedure TfrmTatClothInfoSel.N1Click(Sender: TObject);
begin
  if CDS_2.IsEmpty then
  begin
    Exit;
  end;
  CDS_2.Delete;
end;

procedure TfrmTatClothInfoSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    {if Application.MessageBox('网络连接失败,是否要再次连接?','提示',32+4)=IDYES then
    begin
      try
        with ADOConnection1 do
        begin
          Connected:=false;
          ConnectionString:=DConString;
          //ConnectionString:='23242';
          Connected:=true;
        end;
      except
      end;
    end; }
    //frmZDYHelp.Free;
  end;
end;

procedure TfrmTatClothInfoSel.Button1Click(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmTatClothInfoSel.Button2Click(Sender: TObject);
begin
  if CurrentPage < CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmTatClothInfoSel.cxDBTreeList1DblClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothInfoSel.C_NameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmTatClothInfoSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  C_Code.SetFocus;

  Action := cahide;
end;

procedure TfrmTatClothInfoSel.InitGrid();
var
  fwhere, MBCIID, Pwhere: string;
begin

  if not CDS_1.IsEmpty then
    MBCIID := Trim(CDS_1.FieldByName('BCIID').AsString)
  else
    MBCIID := '';

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere)
    else
      fwhere := ' where ' + trim(Pwhere);
  end;
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' exec P_BS_CloInfo_Get  ');
      sql.Add(' @CTID=' + quotedstr(Trim(CDS_Tree.fieldbyname('CTID').AsString)));
      sql.Add(',@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(sql.Text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
    TV1.DataController.Filter.Clear;
    LBCPAP1.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;

  if MBCIID <> '' then
    CDS_1.Locate('BCIID', MBCIID, []);

end;


procedure TfrmTatClothInfoSel.InitGrid2();
begin

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,C_MXMF=cast('''' as varchar(30)),CTName =cast('''' as varchar(30)) from BS_Cloth_Info A where 1=2 ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);


  finally
    ADOQueryMain.EnableControls;

  end;

end;


procedure TfrmTatClothInfoSel.FormShow(Sender: TObject);
begin
  inherited;

  RecordsNumber := 500;
  CurrentPage := 1;

  ReadCxGrid('单位名称' + Trim(FCoType), TV1, '自定义数据');

  InitTree();
  InitGrid();
  InitGrid2();
end;

procedure TfrmTatClothInfoSel.TBCloseClick(Sender: TObject);
begin
  Close;
  ;

end;

procedure TfrmTatClothInfoSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid('单位名称' + Trim(FCoType), TV1, '自定义数据');

end;

procedure TfrmTatClothInfoSel.TCBNOR1Change(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR1.Text);
  CurrentPage := 1;
  C_Code.SetFocus;
  InitGrid();

end;

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



procedure TfrmTatClothInfoSel.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*  from BS_Cloth_mf A ');
    sql.Add(' where  A.BCIID=''' + CDS_1.FieldByName('BCIID').AsString + '''');
    OPEN;
  end;
  SCreateCDS(ADOQueryCmd, CDS_sub);
  SInitCDSData(ADOQueryCmd, CDS_sub);
end;

procedure TfrmTatClothInfoSel.TV3DblClick(Sender: TObject);
begin
  with CDS_2 do
  begin
    Append;
    FieldByName('CTName').AsString := CDS_1.FieldByName('CTName').AsString;
    FieldByName('C_Code').AsString := CDS_1.FieldByName('C_Code').AsString;
    FieldByName('C_Name').AsString := CDS_1.FieldByName('C_Name').AsString;
    FieldByName('C_Color').AsString := CDS_1.FieldByName('C_Color').AsString;
    FieldByName('C_GramWeight').AsString := CDS_1.FieldByName('C_GramWeight').AsString;
    FieldByName('C_MXMF').AsString := CDS_Sub.FieldByName('C_MXMF').AsString;
    post;
  end;
end;

procedure TfrmTatClothInfoSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatClothInfoSel := nil;
end;

end.

