unit U_CPManage_YPCXSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, RM_BarCode,
  cxButtonEdit, cxTextEdit,jpeg,U_SLT,Math, Menus, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmCPManage_YPCXSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    CYID: TEdit;
    ToolButton2: TToolButton;
    Label1: TLabel;
    CYName: TEdit;
    Label4: TLabel;
    CYEName: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Label3: TLabel;
    CYNO: TEdit;
    Panel2: TPanel;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    Edit1: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    CYKZ: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    CYCF: TEdit;
    CYECF: TEdit;
    Label10: TLabel;
    CYMF: TEdit;
    Label11: TLabel;
    CYHX: TEdit;
    Label12: TLabel;
    DefStr2: TEdit;
    RMBarCodeObject1: TRMBarCodeObject;
    MianLiao: TEdit;
    Label13: TLabel;
    defstr1: TEdit;
    Label14: TLabel;
    Panel3: TPanel;
    adoqueryPicture: TADOQuery;
    defstr10: TEdit;
    Label15: TLabel;
    Panel6: TPanel;
    Label23: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    Label16: TLabel;
    RMDB_Sub: TRMDBDataSet;
    RM2: TRMGridReport;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CYNo: TcxGridDBColumn;
    v1DefStr2: TcxGridDBColumn;
    v1DefStr10: TcxGridDBColumn;
    v1CYName: TcxGridDBColumn;
    v1CYEName: TcxGridDBColumn;
    v1CYCF: TcxGridDBColumn;
    v1CYECF: TcxGridDBColumn;
    v1CYSpec: TcxGridDBColumn;
    v1DefStr6: TcxGridDBColumn;
    v1CYMF: TcxGridDBColumn;
    v1CYKZ: TcxGridDBColumn;
    v1CYHX: TcxGridDBColumn;
    v1CYColor: TcxGridDBColumn;
    v1CYSeHao: TcxGridDBColumn;
    v1DefStr7: TcxGridDBColumn;
    v1CYPrice12: TcxGridDBColumn;
    v1CYPrice3: TcxGridDBColumn;
    v1CYPrice5: TcxGridDBColumn;
    v1CYPrice15: TcxGridDBColumn;
    v1CYPrice: TcxGridDBColumn;
    v1DefStr11: TcxGridDBColumn;
    v1CYNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Ssel: TcxGridDBColumn;
    v1XYPlace: TcxGridDBColumn;
    Panel4: TPanel;
    Label17: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    RadioGroup1: TRadioGroup;
    ADOQuery2: TADOQuery;
    TSsel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQuery3: TADOQuery;
    ADOQuery4: TADOQuery;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    v4CYNo: TcxGridDBColumn;
    v4ColorName: TcxGridDBColumn;
    v4ColorNo: TcxGridDBColumn;
    v4Note: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxSplitter4: TcxSplitter;
    v1XTNo: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYIDKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYSpecChange(Sender: TObject);
    procedure CYMFChange(Sender: TObject);
    procedure CYNameChange(Sender: TObject);
    procedure CYCFChange(Sender: TObject);
    procedure DefStr2Change(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure CYNOKeyPress(Sender: TObject; var Key: Char);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton14Click(Sender: TObject);
    procedure ToolButton15Click(Sender: TObject);
    procedure ToolButton16Click(Sender: TObject);
    procedure ToolButton17Click(Sender: TObject);
    procedure ToolButton18Click(Sender: TObject);
    procedure ToolButton21Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton13Click(Sender: TObject);
    procedure TV2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure TSselClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    { Private declarations }
    CPID:string;
    LP,CP,NP,AP,RN:Integer;
    PState:Integer;
    FCPID,FTopID:String;
    canshu1:string;
    procedure InitTree();
//    procedure InitGrid();
    procedure ReadINIFile();
    procedure InitImage();
    procedure initsub();
    procedure InitImage1();
    procedure InitGrid(CP,NOR:Integer);
    procedure setBtn(CP:Integer);
    procedure setstatus();
    procedure CXInitgrid();
  public
    { Public declarations }
  end;

var
  frmCPManage_YPCXSel: TfrmCPManage_YPCXSel;
  Mach: array of TfrmSlt;

implementation
uses
  U_DataLink,U_Fun,U_CPAdd,U_FileUp, U_YPTypeHelp,U_CPAdd_ZLR;

{$R *.dfm}
procedure TfrmCPManage_YPCXSel.CXInitgrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add('select * from CP_YDang ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    ToolButton2.Click;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPManage_YPCXSel.initsub();
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * ');
    sql.Add(',LBType=(select Top 1 X.CPName from CP_Type X where X.CPID='''+trim(CDS_Main.Fieldbyname('CYType').AsString)+''')');
    sql.Add(' from CP_YDang ');
    sql.Add('where CYType='''+trim(CDS_Main.Fieldbyname('CYID').AsString)+'''');
    open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
end;

procedure TfrmCPManage_YPCXSel.InitImage1();
begin
 
end;

procedure TfrmCPManage_YPCXSel.setstatus();
begin

end;

procedure TfrmCPManage_YPCXSel.InitImage();
var
  i,j:integer;
  jpg:TJpegImage;
  myStream: TADOBlobStream;
begin

end;

procedure TfrmCPManage_YPCXSel.InitTree();
var
  i:Integer;
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_Type order by CPlevel,CPOrder,CPName');
    Open;
  end;
  //cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

procedure TfrmCPManage_YPCXSel.InitGrid(CP,NOR:Integer);
begin
  ToolBar1.SetFocus;
  Panel2.Visible:=True;
  Panel2.Refresh;
  BTLP.Enabled:=False;
  BTNP.Enabled:=False;
  try
    ADOQueryMain.DisableControls;
    LP:= CP-1;
    NP:= CP+1;
    With ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_CP_YDang1 :CYType,:CP,:RON,:WSql,:Stats');
      Parameters.ParamByName('CYType').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('CP').Value:=CP;
      Parameters.ParamByName('RON').Value:=NOR;
      Parameters.ParamByName('WSql').Value:=SGetFilters(Panel1,1,2);
      Parameters.ParamByName('Stats').Value:=1;
      Open;
    end;
    LBCPAP.Caption:= IntToStr(CP)+'/'+ IntToStr(Ceil(ADOQueryTemp.FieldByName('AR').AsInteger/RN));
    AP:= Ceil(ADOQueryTemp.FieldByName('AR').AsInteger/RN);
    Label16.Caption:='共计 '+IntToStr(ADOQueryTemp.FieldByName('AR').AsInteger)+' 条记录';
    if CP>1 then
    begin
      BTLP.Enabled:=true;
    end;
    if CP<Ceil(ADOQueryTemp.FieldByName('AR').AsInteger/RN) then
    begin
      BTNP.Enabled:=true;
    end;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      sql.Add(' exec P_Select_CP_YDang1 :CYType,:CP,:RON,:WSql,:Stats ');
      Parameters.ParamByName('CYType').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
      Parameters.ParamByName('CP').Value:=CP;
      Parameters.ParamByName('RON').Value:=NOR;
      Parameters.ParamByName('WSql').Value:=SGetFilters(Panel1,1,2);
      Parameters.ParamByName('Stats').Value:=2;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible:=False;
end;

procedure TfrmCPManage_YPCXSel.FormDestroy(Sender: TObject);
begin
  frmCPManage_YPCXSel:=nil;
end;

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

procedure TfrmCPManage_YPCXSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品列表',Tv1,'样品管理');
  if  DirectoryExists(ExtractFileDir('D:\Right1209')) then
    winexec('cmd /c rd /s /q D:\Right1209',sw_hide);
  Close;
end;
procedure TfrmCPManage_YPCXSel.FormShow(Sender: TObject);
begin
  //InitTree();
  ReadCxGrid('样品列表',Tv1,'样品管理');
  Label16.Caption:='';
  RN:=StrToInt(Trim(TCBNOR.Text));
  setstatus();
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select CYID='''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_CYNO);
  SInitCDSData20(ADOQueryTemp,CDS_CYNO);
  CDS_CYNO.Delete;
  
  //InitGrid();
end;

procedure TfrmCPManage_YPCXSel.cxDBTreeList1DblClick(Sender: TObject);
begin
  CP:=1;
  //InitGrid(1,RN);
  CXInitgrid();
end;

procedure TfrmCPManage_YPCXSel.TBRafreshClick(Sender: TObject);
begin
  //InitTree();
  //InitGrid(1,RN);
  CXInitgrid();
end;

procedure TfrmCPManage_YPCXSel.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.CYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(CYID.Text)='' then Exit;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYID='''+Trim(CYID.Text)+'''');
      Open;
    end;
    if ADOQuery2.IsEmpty=false then
    begin
      SCreateCDS20(ADOQuery2,CDS_Main);
      SInitCDSData20(ADOQuery2,CDS_Main);
    end
    else
    begin
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.* from CP_YDang A inner join CP_YDang_Color B on A.CYID=B.CYID ');
        sql.Add(' where B.CLRID='''+Trim(CYID.Text)+'''');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    end;
    CYID.Text:='';
  end;
end;

procedure TfrmCPManage_YPCXSel.CYNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('样品档案',cxGrid1);
end;

procedure TfrmCPManage_YPCXSel.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmCPManage_YPCXSel.CYSpecChange(Sender: TObject);
begin
  if Length(Trim(CYSpec.Text))<3 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.CYMFChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<2 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.CYNameChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<2 then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmCPManage_YPCXSel.CYCFChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.DefStr2Change(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.BTLPClick(Sender: TObject);
begin
  CP:=LP;
  InitGrid(CP,RN);
end;

procedure TfrmCPManage_YPCXSel.BTNPClick(Sender: TObject);
begin
  CP:=NP;
  InitGrid(CP,RN);
end;

procedure TfrmCPManage_YPCXSel.setBtn(CP:Integer);
begin
  if (CP=null) or (CP=1)  then
  begin
    CP:=1;
    BTLP.Enabled:=False;
  end else
  begin
   BTLP.Enabled:=True;
  end;
  if NP>AP then
  begin
    BTNP.Enabled:=False;
  end
  else
  begin
    BTNP.Enabled:=True;
  end;
end;

procedure TfrmCPManage_YPCXSel.TCBNORChange(Sender: TObject);
begin
  RN:=StrToInt(TCBNOR.Text);
  InitGrid(1,RN);
end;

procedure TfrmCPManage_YPCXSel.CYNOKeyPress(Sender: TObject; var Key: Char);
var FMXCYID:string;
begin
  if Key=#13 then
  begin
    if Trim(CYNo.Text)='' then Exit;
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CP_YDang where CYNo like '''+'%'+Trim(CYNo.Text)+'%'+'''');
      Open;
    end;
    SCreateCDS20(ADOQuery2,CDS_Main);
    SInitCDSData20(ADOQuery2,CDS_Main);
  end;
end;

procedure TfrmCPManage_YPCXSel.Image2Click(Sender: TObject);
begin
  Panel4.Visible:=False;
end;

procedure TfrmCPManage_YPCXSel.Button1Click(Sender: TObject);
var
  fPrintFile,FFCYID,FFCLRID:string;
  DPS,i,j:Integer;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  CYID.SetFocus;
  if CDS_Main.IsEmpty then Exit;
  if Trim(Edit1.Text)<>'' then
  begin
    if TryStrToInt(Edit1.Text,i)=False then
    begin
      Application.MessageBox('份数录入错误!','提示',0);
      exit;
    end;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+trim(RadioGroup1.Items.Strings[RadioGroup1.ItemIndex])+'.rmf';
  CDS_Main.DisableControls;
  DPS:=0;
  FFCYID:='';
  i:=1;
  if Trim(Edit1.Text)='' then
  begin
    j:=1;
  end else
  begin
    j:=StrToInt(Edit1.Text);
  end;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean=True then
      begin
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(CDS_Main.fieldbyname('CYID').AsString);
          fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
            CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
          if FileExists(fImagePath) then DeleteFile(fImagePath);
          Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
        except
          application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
          exit;
        end;
        RMVariables['QRBARCODE']:=fImagePath;
        for i:=1 to j do
        begin
          if FileExists(fPrintFile) then
          begin
            RM1.LoadFromFile(fPrintFile);
            RM1.PrintReport;
          end else
          begin
            Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+trim(RadioGroup1.Items.Strings[RadioGroup1.ItemIndex])+'.rmf'),'提示',0);
            Exit;
          end;
        end;
        if DPS=0 then
        begin
          FFCYID:=Trim(CDS_Main.fieldbyname('CYID').AsString);
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Main.Locate('CYID',FFCYID,[]);
  Edit1.Text:='1';
  Panel4.Visible:=false;
end;

procedure TfrmCPManage_YPCXSel.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmCPManage_YPCXSel.ToolButton14Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  frmCPAdd:=TfrmCPAdd.Create(self);
  with frmCPAdd do
  begin
    CopyInt:=2;
    FCYID:=Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
    FCPID:=Trim(Self.CDS_Main.fieldbyname('CYID').AsString);
    FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
    FCPName:=trim(Self.ADOQueryTree.fieldbyname('CPName').AsString);
    if ShowModal=1 then
    begin
      initsub();
      Self.CDS_Sub.Locate('CYID',FCYID,[]);
    end;
    free;
  end;
end;

procedure TfrmCPManage_YPCXSel.ToolButton15Click(Sender: TObject);
begin
  if self.CDS_Sub.IsEmpty then exit;
  try
    frmCPAdd:=TfrmCPAdd.Create(Application);
    with frmCPAdd do
    begin
      CopyInt:=1;
      FCYID:=Trim(Self.CDS_Sub.fieldbyname('CYID').AsString);
      FCPID:=Trim(Self.CDS_Sub.fieldbyname('CYType').AsString);
      FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        initsub();
        Self.CDS_Sub.Locate('CYID',FCYID,[]);
      end;
    end;
  finally
    frmCPAdd.Free;
  end;
end;

procedure TfrmCPManage_YPCXSel.ToolButton16Click(Sender: TObject);
var
  i:integer;
  FieldName:string;
begin
  if CDS_Sub.IsEmpty then Exit;
  try
    frmCPAdd:=TfrmCPAdd.Create(Application);
    with frmCPAdd do
    begin
      CopyInt:=0;
      FCYID:=Trim(Self.CDS_Sub.fieldbyname('CYID').AsString);
      FCPID:=Trim(Self.CDS_Sub.fieldbyname('CYType').AsString);
      FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      if ShowModal=1 then
      begin
        initsub();
        Self.CDS_Sub.Locate('CYID',FCYID,[]);
      end;
    end;
  finally
    frmCPAdd.Free;
  end;
end;

procedure TfrmCPManage_YPCXSel.ToolButton17Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  try
    frmCPAdd:=TfrmCPAdd.Create(Application);
    with frmCPAdd do
    begin
      CopyInt:=0;
      FCYID:=Trim(Self.CDS_Sub.fieldbyname('CYID').AsString);
      FCPID:=Trim(Self.CDS_Sub.fieldbyname('CYType').AsString);
      FCPNo:=Trim(Self.ADOQueryTree.fieldbyname('CPNo').AsString);
      TMXSave.Visible:=false;
      frmCPAdd.ToolButton1.Visible:=false;
      frmCPAdd.ToolButton7.Visible:=false;
      frmCPAdd.ToolButton8.Visible:=false;
      frmCPAdd.ToolButton9.Visible:=false;
      frmCPAdd.ToolButton4.Visible:=false;
      frmCPAdd.ToolButton5.Visible:=false;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCPAdd.Free;
  end;
end;

procedure TfrmCPManage_YPCXSel.ToolButton18Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete CP_YDang where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    SQL.Add('delete CP_YDang_Color where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    SQL.Add('delete CP_YDang_ML where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    SQL.Add('delete CP_YDang_JG where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    execsql;
  end;
  CDS_Sub.Delete;
end;

procedure TfrmCPManage_YPCXSel.ToolButton21Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  Panel4.Visible:=true;
end;

procedure TfrmCPManage_YPCXSel.ToolButton12Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  try
    frmFileUp:=TfrmFileUp.Create(Application);
    with frmFileUp do
    begin
      Code.Text:=Trim(Self.CDS_Sub.fieldbyname('CYNO').AsString);
      CYID:=Trim(Self.CDS_Sub.fieldbyname('CYID').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid(NP-1,RN);
        Self.CDS_Sub.Locate('CYID',CYID,[]);
      end;
    end;
  finally
    frmFileUp.Free;
  end;
end;

procedure TfrmCPManage_YPCXSel.ToolButton13Click(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  if CDS_Sub.IsEmpty then exit;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from XD_File where CYNO='''+Trim(CDS_Sub.fieldbyname('CYNO').AsString)+'''');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('样品图片未上传！','提示',0);
      Exit;
    end;
  end;
  try
    ReadINIFile();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ADOQueryTemp.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get(UserDataFlag+'YP\'+Trim(ADOQueryTemp.fieldbyname('FileName').AsString),
                   FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)
                    );
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);
end;

procedure TfrmCPManage_YPCXSel.TV2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Sub.IsEmpty then Exit;
  InitImage1();
end;

procedure TfrmCPManage_YPCXSel.CheckBox1Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmCPManage_YPCXSel.TSselClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('ssel',True,[])=false then
  begin
    Application.MessageBox('没有选择数据','提示');
    exit;
  end;
  ModalResult:=1;
end;

procedure TfrmCPManage_YPCXSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,true);
end;

procedure TfrmCPManage_YPCXSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,false);
end;

procedure TfrmCPManage_YPCXSel.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_YDang_Color where CYID='''+trim(CDS_Main.fieldbyname('CYID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_Sub);
  SInitCDSData20(ADOQuery1,CDS_Sub);
end;

end.
