unit U_CustModelLabel;

interface

uses                                
  Windows, Messages, SysUtils, StrUtils,Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, ExtCtrls, DB, ADODB,
  RM_System, RM_Common, RM_Class, RM_GridReport, Buttons, FTComboBox,
  RM_Preview, RM_e_Xls, RM_e_Graphic, RM_e_bmp, RM_BarCode,
  RM_DsgGridReport, RM_Dataset, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, DBClient;

type
  TfrmCustModelLabel = class(TForm)
    ADOQueryCmd: TADOQuery;
    OpenDialog1: TOpenDialog;
    RMGridReport1: TRMGridReport;
    ADOQueryTmp: TADOQuery;
    RMPreview1: TRMPreview;
    ADOQuery1: TADOQuery;
    DS_Label: TDataSource;
    ADOQueryLabel: TADOQuery;
    ToolBar2: TToolBar;
    Tadd: TToolButton;
    Tupd: TToolButton;
    Tdel: TToolButton;
    TOK: TToolButton;
    Tclose: TToolButton;
    Panel4: TPanel;
    ToolButton1: TToolButton;
    Label10: TLabel;
    LabelCaption: TEdit;
    ClientDataSet1: TClientDataSet;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1labelId: TcxGridDBColumn;
    tv1labeltype: TcxGridDBColumn;
    tv1labelCaption: TcxGridDBColumn;
    tv1labelFile: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure TcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TaddClick(Sender: TObject);
    procedure TupdClick(Sender: TObject);
    procedure TdelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure LabelTypeChange(Sender: TObject);
    procedure tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    isLoad:Boolean;
    function DeleteData():Boolean;
    procedure InitGrid();
    procedure OpenLabel();
  public
    fSelLabelId:String;
    fKeyNo:string;
    fchg:Boolean;
    fIsShowModal:Boolean;
    fWinStatus:integer;
  end;

var
  frmCustModelLabel: TfrmCustModelLabel;

implementation
uses
  U_DataLink, U_LabelAdd,U_RTFun;
{$R *.dfm}

procedure TfrmCustModelLabel.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmCustModelLabel.FormCreate(Sender: TObject);
begin
  fSelLabelId := '';
end;

procedure TfrmCustModelLabel.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCustModelLabel.TaddClick(Sender: TObject);
begin

  frmLabelAdd:=TfrmLabelAdd.create(self);
  with frmLabelAdd do
  begin
    if showModal =1 then
    begin
      fchg:=true;
      InitGrid();
    end;
    free;
  end;
end;

procedure TfrmCustModelLabel.TupdClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then exit;
  frmLabelAdd:=TfrmLabelAdd.create(self);
  with frmLabelAdd do
  begin
    fKeyNo:=self.ClientDataSet1.fieldByName('LabelId').AsString ;
    fWinstatus:=1;
    if showModal =1 then
    begin
      fchg:=true;
      InitGrid();
    end;
    free;
  end;
end;
/////////////////////////////////////////////
  //
/////////////////////////////////////////////
procedure TfrmCustModelLabel.InitGrid();
begin
  try
    isLoad:=false;
    ADOQuerylabel.DisableControls ;
    with ADOQuerylabel do
    begin
      close;
      sql.Clear ;
      sql.Add('select labelCaption,labelId from P_Label');
      sql.Add('where valid=''Y''');
      sql.Add(' order by FillTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryLabel,ClientDataSet1);
    SInitCDSData20(ADOQueryLabel,ClientDataSet1);
  finally
    ADOQuerylabel.EnableControls;
    isLoad:=true;
  end;
end;
////////////////////////////////////////////////////////
  //函数功能：打开标签文件
////////////////////////////////////////////////////////
procedure TfrmCustModelLabel.OpenLabel();
begin
  if ClientDataSet1.IsEmpty then exit;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from P_Label where labelId='+ClientDataSet1.fieldbyname('labelId').AsString);
    Open;
  end;
  try
    with RMGridReport1 do
    begin
      LoadFromBlobField(tblobfield(ADOQuery1.fieldbyname('labelFile')));
      ShowReport ;
      RMPreview1.Refresh;
    end;
  except
  end;
end;
function TfrmCustModelLabel.DeleteData():Boolean;
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('delete P_Label');
      sql.Add('where labelId='+ClientDataSet1.fieldByName('LabelID').asString);
      execSql;
    end;
    result:=true;
  except
    result:=false;
    application.MessageBox('删除失败!','警告信息',0);
  end;
end;
procedure TfrmCustModelLabel.TdelClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then exit;
  if application.MessageBox('确定要删除此标签吗？','警告信息',1)=2 then exit;
  if  DeleteData() then
  begin
    fchg:=true;
    ClientDataSet1.Delete;
  end;
end;

procedure TfrmCustModelLabel.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if fIsShowModal then
    Application:=MainApplication ;
  Action:=caFree;
end;

procedure TfrmCustModelLabel.FormDestroy(Sender: TObject);
begin
  frmCustModelLabel:=nil;
end;

procedure TfrmCustModelLabel.TOkClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  ModalResult:=1;
end;

procedure TfrmCustModelLabel.tv1DblClick(Sender: TObject);
begin
  TOk.Click ;
end;

procedure TfrmCustModelLabel.ToolButton1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCustModelLabel.LabelTypeChange(Sender: TObject);
begin
  if ADOQueryLabel.Active=False then Exit;
  SDofilter(ADOQueryLabel,SGetFilters(Panel4,1,2));
  SCreateCDS20(ADOQueryLabel,ClientDataSet1);
  SInitCDSData20(ADOQueryLabel,ClientDataSet1);
end;

procedure TfrmCustModelLabel.tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if isLoad then
    OpenLabel();
end;

end.
