unit U_ZdyYS_Sel;

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
  TfrmZdyYS_Sel = 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;
    Panel1: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Tv2Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel2: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    CodeName: TEdit;
    Code: TEdit;
    Tv2Column2: TcxGridDBColumn;
    Edit1: TEdit;
    Tv2Column3: TcxGridDBColumn;
    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 Tv2Column1GetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
    procedure CodeChange(Sender: TObject);
    procedure CodeNameChange(Sender: TObject);
  private
    { Private declarations }                          
    procedure InitGrid();
    procedure InitSub();                        
  public                                                      
    fDEFstr5: string;
  end;

var
  frmZdyYS_Sel: TfrmZdyYS_Sel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;     //, U_ZdyAttInputGYS, U_ZdyAttachCP10_help

{$R *.dfm}

procedure TfrmZdyYS_Sel.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 TfrmZdyYS_Sel.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 TfrmZdyYS_Sel.FormDestroy(Sender: TObject);
begin
  frmZdyYS_Sel := nil;
end;

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

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

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

  InitGrid();
end;

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

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

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

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

procedure TfrmZdyYS_Sel.ToolButton1Click(Sender: TObject);
var
  Maxno, x, y: string;
begin
  if cds_hz.IsEmpty then
    exit;
  try
    CDS_HZ.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        if CDS_HZ.FieldByName('SSel').AsBoolean then
        begin
          x := CDS_HZ.FieldByName('CodeName').AsString;
          if y = '' then
          begin
            y := x;
          end
          else
          begin
            y := y + ',' + x;
          end;

        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_HZ.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_HZ.EnableControls;
  end;
  Edit1.Text := Trim(y);
  self.ModalResult := 1;
end;

procedure TfrmZdyYS_Sel.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 TfrmZdyYS_Sel.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) + '''');
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmZdyYS_Sel.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 TfrmZdyYS_Sel.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 TfrmZdyYS_Sel.Tv2Column1GetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord; var AText: string);
begin
  AText := IntToStr(ARecord.Index + 1)
end;

procedure TfrmZdyYS_Sel.CodeChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel2, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmZdyYS_Sel.CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel2, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

end.

