unit U_ZdyCMZ_help;

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, cxCalendar, cxButtonEdit,
  cxTextEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans,
  dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmZdyCMZ_help = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    DSsub: TDataSource;
    CDSsub: TClientDataSet;
    ADOQuerysub: TADOQuery;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Panel1: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    TV1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TV1Column1GetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitSub();
  public
    fDEFstr5: string;
  end;

var
  frmZdyCMZ_help: TfrmZdyCMZ_help;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyAttInputGYS, U_ZdyAttachCP10_help;

{$R *.dfm}

procedure TfrmZdyCMZ_help.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select mainid=cast(mainid as varchar(30)),* from CP_CMYS where Type=''尺码'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZdyCMZ_help.InitSub();
begin
  with ADOQuerysub do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select subid=cast(subid as varchar(30)),* from CP_CMYS_Sub where Mainid=''' + cds_hz.fieldbyname('Mainid').AsString + ''' ');
//    ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQuerysub, CDSsub);
  SInitCDSData20(ADOQuerysub, CDSsub);
end;

procedure TfrmZdyCMZ_help.FormDestroy(Sender: TObject);
begin
  frmZdyCMZ_help := nil;
end;

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

procedure TfrmZdyCMZ_help.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv2, '客户自定义');
  Close;
end;

procedure TfrmZdyCMZ_help.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption, Tv2, '客户自定义');

  InitGrid();
end;

procedure TfrmZdyCMZ_help.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZdyCMZ_help.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if cds_hz.IsEmpty then
    exit;
  self.ModalResult := 1;
end;

procedure TfrmZdyCMZ_help.TV1Column1GetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
begin
  AText := IntToStr(ARecord.Index + 1);
end;

procedure TfrmZdyCMZ_help.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  initsub();
end;

procedure TfrmZdyCMZ_help.ToolButton1Click(Sender: TObject);
var
  FMainid: string;
begin
  if GetLSNo(ADOQueryCmd, FMainid, 'CM', 'CP_CMYS', 3) = False then
  begin
    Application.MessageBox('取号失败！', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_CMYS where 1<>1');
    Open;

    Append;
    fieldbyname('Code').Value := FMainid;
    FieldByName('Type').value := '尺码';
    Post;
  end;

  InitGrid();
end;

procedure TfrmZdyCMZ_help.ToolButton3Click(Sender: TObject);
var
  FMainid: string;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CP_CMYS_Sub where 1<>1');
    Open;

    Append;
    fieldbyname('Mainid').Value := CDS_HZ.fieldbyname('Mainid').AsString;
    Post;
  end;

  InitSub();
end;

procedure TfrmZdyCMZ_help.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除尺码组吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CP_CMYS where Mainid=''' + Trim(CDS_HZ.fieldbyname('Mainid').AsString) + '''');
    sql.Add('delete CP_CMYS_Sub where Mainid=''' + Trim(CDS_HZ.fieldbyname('Mainid').AsString) + '''');
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmZdyCMZ_help.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除尺码吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CP_CMYS_Sub where Subid=''' + Trim(CDSsub.fieldbyname('Subid').AsString) + '''');
    ExecSQL;
  end;
  CDSsub.Delete;
end;

procedure TfrmZdyCMZ_help.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '';
  end;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate CP_CMYS ');
    sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    SQL.Add(' where Mainid=''' + Trim(CDS_HZ.fieldbyname('Mainid').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmZdyCMZ_help.cxGridDBColumn2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin                                                   
    mvalue := '';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDSsub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate CP_CMYS_sub ');
    sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
    SQL.Add(' where Subid=''' + Trim(CDSsub.fieldbyname('subid').AsString) + '''');
//    ShowMessage(sql.text);
    ExecSQL;
  end;
end;

procedure TfrmZdyCMZ_help.Tv2DblClick(Sender: TObject);
begin
  ModalResult:=1;
end;

end.

