unit U_ZdyYS_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,
  cxTLdxBarBuiltInMenu, 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_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;
    Panel1: TPanel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    Tv2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    CPTopName: TEdit;
    CPName: TEdit;
    CPNo: TEdit;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    TBAdd: TToolButton;
    ToolButton4: TToolButton;
    TBDel: TToolButton;
    ToolButton5: TToolButton;
    ADOQueryTree: TADOQuery;
    DataSource1: TDataSource;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    Tv2Column2: TcxGridDBColumn;
    Label4: TLabel;
    CPNameYW: 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 TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure cxDBTreeList1Change(Sender: TObject);
    procedure Tv2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv2Column3PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    PState: Integer;
    FCPID, FTopID: string;
    procedure InitGrid();
    procedure InitSub();
    procedure InitTree();
  public
    fDEFstr5: string;
  end;

var
  frmZdyYS_help: TfrmZdyYS_help;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyAttInputGYS, U_ZdyAttachCP10_help;

{$R *.dfm}

procedure TfrmZdyYS_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=''颜色'' and isnull(CPLevel,0)<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmZdyYS_help.InitTree();
var
  i: Integer;
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from CP_CMYS where Type=''颜色'' order by CPlevel,CPOrder,CodeName ');
    Open;
  end;
  cxDBTreeList1.Items[0].Expand(false);
  //cxDBTreeList1.Items[1].Expand(False);
end;

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

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

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

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

  InitGrid();
  InitTree();
end;

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

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

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

procedure TfrmZdyYS_help.ToolButton1Click(Sender: TObject);
var
  FMainid: string;
begin
  if GetLSNo(ADOQueryCmd, FMainid, 'YS', '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 := '颜色';
    FieldByName('CPLevel').value := '2';
    Post;
  end;

  InitGrid();
end;

procedure TfrmZdyYS_help.ToolButton3Click(Sender: TObject);
begin
  InitTree();
end;

procedure TfrmZdyYS_help.ToolButton2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.FieldByName('CPLevel').AsInteger = 0 then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_CMYS where CPParent=''' + Trim(CDS_HZ.fieldbyname('Mainid').AsString) + '''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经定义子类不能删除！', '提示', 0);
      Exit;
    end;
  end;

  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;

  InitTree();
end;

procedure TfrmZdyYS_help.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
  if Trim(CPName.Text) = '' then
  begin
    Application.MessageBox('类别名称不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(CPNo.Text) = '' then
  begin
    Application.MessageBox('类别编码不能为空！', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
//    if PState = 11 then
//    begin
//      if GetLSNo(ADOQueryCmd, maxId, 'CP', 'CP_CMYS', 4, 1) = False then
//      begin
//        ADOQueryCmd.Connection.RollbackTrans;
//        Application.MessageBox('区最大号失败！', '提示', 0);
//        Exit;
//      end;
//    end
//    else
//    begin
//      maxId := Trim(FCPID);
//    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_CMYS where MainID=''' + Trim(FCPID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if PState = 11 then
      begin
        Append;
//        FieldByName('MainID').Value := Trim(maxId);
        FieldByName('CodeName').Value := Trim(CPName.Text);
        FieldByName('code').Value := Trim(CPNo.Text);
        FieldByName('CPParent').Value := Trim(ADOQueryTree.fieldbyname('Mainid').AsString);
        FieldByName('CPLevel').Value := ADOQueryTree.fieldbyname('CPLevel').AsInteger + 1;
        FieldByName('Type').Value := '颜色';
        Post;
      end
      else if PState = 22 then
      begin
        Edit;
//        FieldByName('MainID').Value := Trim(maxId);
        FieldByName('CodeName').Value := Trim(CPName.Text);
        FieldByName('code').Value := Trim(CPNo.Text);
        //FieldByName('CPParent').Value:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
        //FieldByName('CPOrder').Value:=ADOQueryTree.fieldbyname('CPOrder').AsInteger+1;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!', '提示', 0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
//  InitGrid();
//  InitTree();
end;

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

procedure TfrmZdyYS_help.TBDelClick(Sender: TObject);
begin
  if ADOQueryTree.FieldByName('CPLevel').AsInteger = 0 then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_CMYS where CPParent=''' + Trim(ADOQueryTree.fieldbyname('Mainid').AsString) + '''');
    Open;
    if not IsEmpty then
    begin
      Application.MessageBox('已经定义子类不能删除！', '提示', 0);
      Exit;
    end;
  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from CP_YDang where CYType=''' + Trim(ADOQueryTree.fieldbyname('CPID').AsString) + '''');
//    Open;
//    if not IsEmpty then
//    begin
//      Application.MessageBox('已经有产品属于此类不能删除！', '提示', 0);
//      Exit;
//    end;
//  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CP_CMYS where Mainid=''' + Trim(ADOQueryTree.fieldbyname('Mainid').AsString) + '''');
    sql.Add('delete CP_CMYS where CPParent=''' + Trim(ADOQueryTree.fieldbyname('Mainid').AsString) + '''');
    ExecSQL;
  end;
  InitTree();
end;

procedure TfrmZdyYS_help.TBAddClick(Sender: TObject);
begin
  PState := 11;
  CPTopName.Text := Trim(ADOQueryTree.fieldbyname('CodeName').AsString);
  //FTopID:=Trim(ADOQueryTree.fieldbyname('CPID').AsString);
  FCPID := '';
  CPName.Text := '';
  CPNo.Text := '';
  CPName.SetFocus;
end;

procedure TfrmZdyYS_help.PageControl1Change(Sender: TObject);
begin
  initgrid();
  InitTree();
end;

procedure TfrmZdyYS_help.cxDBTreeList1Change(Sender: TObject);
begin
  PState := 22;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_CMYS where Mainid=''' + Trim(ADOQueryTree.fieldbyname('CPParent').AsString) + '''');
    Open;
  end;
  FCPID := Trim(ADOQueryTree.fieldbyname('Mainid').AsString);
  CPTopName.Text := Trim(ADOQueryTemp.fieldbyname('CodeName').AsString);
  CPName.Text := Trim(ADOQueryTree.fieldbyname('CodeName').AsString);
  CPNameYW.Text := Trim(ADOQueryTree.fieldbyname('CodeNameYW').AsString);
  CPNo.Text := Trim(ADOQueryTree.fieldbyname('Code').AsString);
end;

procedure TfrmZdyYS_help.Tv2Column2PropertiesEditValueChanged(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_help.Tv2Column3PropertiesEditValueChanged(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;

end.

