unit U_TJHXMX;

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, ExtCtrls, cxContainer, cxEdit, cxImage, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdFTP,jpeg,IniFiles,ShellAPI,
  cxFilter, cxData, cxDataStorage, cxDBData, cxTextEdit, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, DBClient, Menus, BtnEdit,
  cxGridCustomPopupMenu, cxGridPopupMenu;

type
  TfrmTJHXMX = class(TForm)
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Panel2: TPanel;
    Panel1: TPanel;
    SaveDialog1: TSaveDialog;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    Panel16: TPanel;
    Label3: TLabel;
    YW: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    HXNO: TEdit;
    SeTao: TEdit;
    TJPrice: TEdit;
    Button1: TButton;
    Label2: TLabel;
    Note: TEdit;
    cxImage1: TcxImage;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1MXJGFactoryName: TcxGridDBColumn;
    V1MXJGFactoryNameHX: TcxGridDBColumn;
    V1MXSeHao: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel3: TPanel;
    V1MXTJPrice: TcxGridDBColumn;
    V1MXTJNote: TcxGridDBColumn;
    DataSource2: TDataSource;
    Ord_TJHX: TClientDataSet;
    V1ZCType: TcxGridDBColumn;
    Label7: TLabel;
    Panel5: TPanel;
    Label11: TLabel;
    Label12: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    V1Column1: TcxGridDBColumn;
    N2: TMenuItem;
    Button2: TButton;
    Color: TBtnEditA;
    cxStyle2: TcxStyle;
    N3: TMenuItem;
    cxStyle3: TcxStyle;
    Label1: TLabel;
    ImageNameKH: TBtnEditA;
    Label6: TLabel;
    IMColor: TBtnEditA;
    Label10: TLabel;
    IMNote: TMemo;
    Panel4: TPanel;
    Label13: TLabel;
    Label14: TLabel;
    GYS: TBtnEditA;
    Label16: TLabel;
    Label17: TLabel;
    IMPTID: TEdit;
    Panel6: TPanel;
    Label9: TLabel;
    Label8: TLabel;
    Panel7: TPanel;
    Label15: TLabel;
    ImageName: TEdit;
    Label18: TLabel;
    PTID: TEdit;
    cxGridPopupMenu1: TcxGridPopupMenu;
    LXCode: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxImage1DblClick(Sender: TObject);
    procedure TV1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button2Click(Sender: TObject);
    procedure HXNOKeyPress(Sender: TObject; var Key: Char);
    procedure SeTaoKeyPress(Sender: TObject; var Key: Char);
    procedure TJPriceKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure V1MXTJNoteCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure NoteKeyPress(Sender: TObject; var Key: Char);
    procedure N2Click(Sender: TObject);
    procedure ColorBtnClick(Sender: TObject);
    procedure ColorKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure ImageNameKHBtnClick(Sender: TObject);
    procedure IMColorBtnClick(Sender: TObject);
    procedure IMColorExit(Sender: TObject);
    procedure IMNoteExit(Sender: TObject);
    procedure GYSBtnClick(Sender: TObject);
    procedure ImageNameKHExit(Sender: TObject);
  private
    { Private declarations }
    PState:Integer;
    FCPID,FTopID,FMXID:String;
    lstPat: TStringList;
    procedure InitData();
    procedure InitColor(Panel99:TPanel;FPTID:String);
    procedure CSHLB();
    procedure initGridGYS();

  public
    FileName:string;
    //FSHID,FMXSKID,FMXCPID,FMXXFID,FMXWBID:string;
    FIMID,FWBID:string;
    { Public declarations }
  end;

var
  frmTJHXMX: TfrmTJHXMX;

implementation
uses
  U_DataLink,U_RTFun,U_RTPTColorTJ,U_ZDYHelp,U_GYSListSel;  //$0080FFFF

{$R *.dfm}

procedure TfrmTJHXMX.InitData();
var jpg:TJpegImage;
    myStream: TADOBlobStream;
begin
  ReadCxGrid('GYSInfo',TV1,'烫金花型管理');
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,B.ImageNameKH,B.IMColor,B.IMPTID,B.IMNote,B.ImageName,B.IMPTID from Image_File A ');
    sql.Add(' inner join Image_Info B on A.IMID=B.IMID');
    sql.Add(' where A.IMID='''+Trim(FIMID)+''' and A.IFTYpe=''大''');
    Open;
  end;
  ImageNameKH.Text:=Trim(ADOQueryTemp.fieldbyname('ImageNameKH').AsString);
  IMColor.Text:=Trim(ADOQueryTemp.fieldbyname('IMColor').AsString);
  IMNote.Text:=Trim(ADOQueryTemp.fieldbyname('IMNote').AsString);
  ImageName.Text:=Trim(ADOQueryTemp.fieldbyname('ImageName').AsString);
  ImageName.Text:=Copy(ImageName.Text,1,Length(ImageName.Text)-4);
  IMPTID.Text:=Trim(ADOQueryTemp.fieldbyname('IMPTID').AsString);
  jpg:=TJpegImage.Create();
  myStream:=tadoblobstream.Create(tblobfield(ADOQueryTemp.fieldbyname('ImageFile')),bmread);
  jpg.LoadFromStream(myStream);
  cxImage1.Picture.Assign(jpg);
  initGridGYS();
  InitColor(Panel4,IMPTID.Text);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_TypeTJ A where CPID=(select CPParent from CP_TypeTJ B where B.CPID='''+Trim(FWBID)+''')');
    Open;
  end;
  LXCode.Caption:=Trim(ADOQueryTemp.fieldbyname('CPNO').AsString);
end;
procedure TfrmTJHXMX.initGridGYS();
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.ColDaiMa from TJ_GYS_Info A ');
    SQL.Add(' left join RT_PTColor_M B on A.PTID=B.PTID');
    sql.Add(' where A.IMID='''+trim(FIMID)+'''');
    SQL.Add(' and A.Valid=''Y'' order by TIType desc ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,Ord_TJHX);
  SInitCDSData20(ADOQueryTemp,Ord_TJHX);
end;
procedure TfrmTJHXMX.FormDestroy(Sender: TObject);
begin
  frmTJHXMX:=nil;
end;

procedure TfrmTJHXMX.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  GYS.SetFocus;
  WriteCxGrid('GYSInfo',TV1,'烫金花型管理');
  Action:=caFree;
end;

procedure TfrmTJHXMX.CSHLB();
begin
  
end;
procedure TfrmTJHXMX.FormShow(Sender: TObject);
begin
  InitData();
end;

procedure TfrmTJHXMX.Button1Click(Sender: TObject);
var
  maxno:string;
  FReal:Double;
begin
  if Trim(GYS.Text)='' then
  begin
    Application.MessageBox('加工厂不能为空!','提示',0);
    Exit;
  end;
  if Trim(TJPrice.Text)<>'' then
  begin
    if TryStrToFloat(TJPrice.Text,FReal)=False then
    begin
      Application.MessageBox('单价非法数字!','提示',0);
      Exit;
    end;
  end;
  if Button1.Caption='修改保存' then
  begin
    if Ord_TJHX.IsEmpty then Exit;
  end;
  if application.MessageBox('确定要保存吗','提示',1)=2 then exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from TJ_GYS_Info ');
      sql.Add('where TIID='''+trim(FMXID)+'''');
      open;
    end;
    with ADOQueryCmd do
    begin
      if IsEmpty then
      begin
        append;
        if GetLSNo(ADOQueryTemp,maxno,'TI','TJ_GYS_Info',4,1)=False then
        begin
          Application.MessageBox('取ID最大号失败！','提示',0);
          Exit;
        end;
        FieldByName('TIID').Value:=Trim(maxno);
      end
      else
      begin
        edit;
        maxno:=Trim(FMXID);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('IMID').Value:=Trim(FIMID);
      RTSetsavedata(ADOQueryCmd,'TJ_GYS_Info',Panel1,2);
      FieldByName('Valid').Value:='Y';
      post;
    end;
    if Trim(PTID.Text)<>'' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update RT_PTColor_M Set ColNameKH='''+Trim(Color.Text)+'''');
        sql.Add(' where PTID='''+Trim(PTID.Text)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据保存失败!','提示',0);
    Exit;
  end;
  initGridGYS();
  Ord_TJHX.Locate('TIID',maxno,[]);
  Button2.Click;
end;

procedure TfrmTJHXMX.cxImage1DblClick(Sender: TObject);
var
  IdFTP1: TIdFTP;
  FPath,FFName:string;
  FInt:integer;
  maxno:string;
  j,i:Integer;
  fPrintFile,fFileName:string;
begin
  if Application.MessageBox('原图下载需要等待时间，确定要查看原图吗？','提示',32+4)<>IDYES then Exit;
  try
    IdFTP1:=TIdFTP.Create(self);
    IdFTP1.Host :=PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    IdFTP1.Free;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  Panel16.Visible:=true;
  Panel16.Refresh;
  fPrintFile:= ExtractFilePath(Application.ExeName)+'Photo\';
  if not DirectoryExists(ExtractFileDir(FPrintFile)) then
    CreateDir(ExtractFileDir(fPrintFile));
  FFName:=Trim(FileName);
  FFName:=fPrintFile+FFName;
  if not FileExists(FFName) then
  begin
    IdFTP1.Get('TJ\'+FileName,FFName);
  end;
  if IdFTP1.Connected then
  begin
    IdFTP1.Quit;
    IdFTP1.Free;
  end;
  Panel16.Visible:=false;
  ShellExecute(Handle, 'open',PChar(FFName),'', '', SW_SHOWNORMAL);
end;

procedure TfrmTJHXMX.TV1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

  if Ord_TJHX.IsEmpty then
  begin
    FMXID:='';
    Exit;
  end else
  begin
    FMXID:=Trim(Ord_TJHX.fieldbyname('TIID').AsString);
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.ColDaiMa from TJ_GYS_Info A ');
    SQL.Add(' left join RT_PTColor_M B on A.PTID=B.PTID');
    sql.Add(' where A.TIID='''+trim(FMXID)+'''');
    if Ord_TJHX.IsEmpty then
    begin
      sql.Add(' and 1=2');
    end;
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel1,2);
  InitColor(Panel5,PTID.Text);
  Button1.Caption:='修改保存';
end;

procedure TfrmTJHXMX.Button2Click(Sender: TObject);
begin
  SClearData(Panel1,2);
  FMXID:='';
  Button1.Caption:='新增保存';
  InitColor(Panel5,Color.TxtCode);
end;

procedure TfrmTJHXMX.HXNOKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    SeTao.SetFocus;
  end;
end;

procedure TfrmTJHXMX.SeTaoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    TJPrice.SetFocus;
  end;
end;

procedure TfrmTJHXMX.TJPriceKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    Color.SetFocus;
  end;
end;

procedure TfrmTJHXMX.N1Click(Sender: TObject);
var
  FFFTIID:string;
begin
  if Ord_TJHX.IsEmpty then Exit;
  FFFTIID:=trim(Ord_TJHX.fieldbyname('TIID').AsString);
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update TJ_GYS_Info set TIType='''' ');
    sql.add(' where IMID='''+trim(FIMID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update TJ_GYS_Info set TIType=''主'' ');
    sql.add(' where TIID='''+trim(FFFTIID)+'''');
    ExecSQL;
  end;
  initGridGYS();
  Ord_TJHX.Locate('TIID',FFFTIID,[]);
  button2.Click;
end;

procedure TfrmTJHXMX.V1MXTJNoteCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var Id:Integer;
begin
  Id:=TV1.GetColumnByFieldName('TIType').Index;
  if AViewInfo.GridRecord.Values[Id]='主' then
  begin
    ACanvas.Font.Color:=clBlue;
  end;
end;

procedure TfrmTJHXMX.NoteKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    Button1.SetFocus;
  end;
end;

procedure TfrmTJHXMX.InitColor(Panel99:TPanel;FPTID:String);
var
  fsj:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from RT_PTColor_M where PTID='''+Trim(FPTID)+'''');
    Open;
  end;
  fsj:=Trim(ADOQueryTemp.fieldbyname('ColDaiMa').AsString);
  if Trim(fsj)<>'' then
  begin
    Panel99.Color:=StrToInt('$'+Trim(fsj));
  end else
  begin
    Panel99.Color:=clBtnFace;
  end;

end;

procedure TfrmTJHXMX.N2Click(Sender: TObject);
begin
  if Application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('UPdate  TJ_GYS_Info  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
    sql.Add('where TIID='''+trim(FMXID)+'''');
    ExecSQL;
  end;
  Ord_TJHX.Delete;
  Button2.Click;
end;

procedure TfrmTJHXMX.ColorBtnClick(Sender: TObject);
begin
  try
    frmRTPTColorTJ:=TfrmRTPTColorTJ.Create(Application);
    with frmRTPTColorTJ do
    begin
      if ShowModal=1 then
      begin
        Self.PTID.Text:=Trim(frmRTPTColorTJ.FPTID);
        Self.Color.Text:=Trim(frmRTPTColorTJ.FColNameKH);
      end;
    end;
  finally
    frmRTPTColorTJ.Free;
  end;
  InitColor(Panel5,PTID.Text);
end;

procedure TfrmTJHXMX.ColorKeyPress(Sender: TObject; var Key: Char);
begin
  {if Key=#13 then
  begin
    Note.SetFocus;
  end; }
end;

procedure TfrmTJHXMX.N3Click(Sender: TObject);
begin
  SClearData(Panel1,2);
  FMXID:='';
  Button1.Caption:='新增保存';
  InitColor(Panel5,Color.TxtCode);
end;

procedure TfrmTJHXMX.ImageNameKHBtnClick(Sender: TObject);
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ImageNameKHTJ';
      flagname:='烫金花型名称';
      if ShowModal=1 then
      begin
        self.ImageNameKH.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Image_Info Set ImageNameKH='''+Trim(ImageNameKH.Text)+'''');
    sql.Add(' where IMID='''+Trim(FIMID)+'''');
    ExecSQL;
  end;}
end;

procedure TfrmTJHXMX.IMColorBtnClick(Sender: TObject);
begin
  try
    frmRTPTColorTJ:=TfrmRTPTColorTJ.Create(Application);
    with frmRTPTColorTJ do
    begin
      if ShowModal=1 then
      begin
        Self.IMColor.Text:=Trim(frmRTPTColorTJ.FColNameKH);
        Self.IMPTID.Text:=Trim(frmRTPTColorTJ.FPTID);
      end;
    end;
  finally
    frmRTPTColorTJ.Free;
  end;
  InitColor(Panel4,IMPTID.Text);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Image_Info Set IMPTID='''+Trim(IMPTID.Text)+'''');
    sql.Add(' where IMID='''+Trim(FIMID)+'''');
    ExecSQL;
  end;
end;

procedure TfrmTJHXMX.IMColorExit(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Image_Info Set IMColor='''+Trim(IMColor.Text)+'''');
    sql.Add(' where IMID='''+Trim(FIMID)+'''');
    ExecSQL;
  end;
  if Trim(IMPTID.Text)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update RT_PTColor_M Set ColNameKH='''+Trim(IMColor.Text)+'''');
      sql.Add(' where PTID='''+Trim(IMPTID.Text)+'''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmTJHXMX.IMNoteExit(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Image_Info Set IMNote='''+Trim(IMNote.Text)+'''');
    sql.Add(' where IMID='''+Trim(FIMID)+'''');
    ExecSQL;
  end;
end;

procedure TfrmTJHXMX.GYSBtnClick(Sender: TObject);
begin
  try
    frmGYSListSel:=TfrmGYSListSel.Create(Application);
    with frmGYSListSel do
    begin
      FGYSType:='烫金厂';
      if ShowModal=1 then
      begin
        self.GYS.Text:=Trim(CDS_HZ.fieldbyname('KHNameJC').AsString);
        self.GYS.TxtCode:=Trim(CDS_HZ.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmGYSListSel.Free;
  end;
end;

procedure TfrmTJHXMX.ImageNameKHExit(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update Image_Info Set ImageNameKH='''+Trim(ImageNameKH.Text)+'''');
    sql.Add(' where IMID='''+Trim(FIMID)+'''');
    ExecSQL;
  end;
end;

end.
