unit U_ChooseComponTypesForReport;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxControls, cxContainer, cxEdit, cxLabel, cxLookAndFeelPainters,
  StdCtrls, cxButtons, ComCtrls, Contnrs, cxCheckBox, siComp, siLngLnk,
  RzButton, RzRadChk, cxGraphics, cxLookAndFeels, Menus;

type
  TF_ChooseComponTypesForReport = class(TForm)
    cxLabel1: TcxLabel;
    bOK: TcxButton;
    lvComponTypes: TListView;
    bCancel: TcxButton;
    lng_Forms: TsiLangLinked;
    Edit1: TEdit;
    Label1: TLabel;
    RzCheckBox1: TRzCheckBox;
    procedure RzCheckBox1Click(Sender: TObject);
  private
    { Private declarations  }
  public
    { Public declarations }
    function Execute: Boolean;
  end;

var
  F_ChooseComponTypesForReport: TF_ChooseComponTypesForReport;

implementation
uses U_Main, U_Common, U_BaseCommon, U_SCSComponent, U_ESCadClasess, U_ProjectPlan, U_Constants, U_ResourceReport, U_CAD,
     U_SCSLists, U_SCSClasses, U_SCSInterfPath;


{$R *.dfm}

function TF_ChooseComponTypesForReport.Execute: Boolean;
var
  CurComponType: TNBComponentType;
  ComponTypes: TObjectList;
  i,j: Integer;
  Item: TListItem;
  ListParams: TListParams;
  //CheckedCTSN: TStringList; //CTSN - compon type sys name
  SCSCatalog, SCSChildCatalog: TSCSCatalog;
  SCSComponent: TSCSComponent;
  AddCompon, CheckedAll : boolean;
  ComponList: TStringList;
  WA_CAD: TF_CAD;
  CurrList: TSCSList;
  WasNil: boolean;
 // Figure: TFigure;

{
function GetUsingComponentTypes(ACatalog: TSCSCatalog): TObjectList;
var
  i, j: Integer;
  SCSCatalog: TSCSCatalog;
  SCSComponent: TSCSComponent;
  LookedGUIDESOfComponTypes: TStringList;

  SprComponentType: TNBComponentType;
  GrpComponentType: TNBComponentType;
begin
  Result := TObjectList.Create(true);
  LookedGUIDESOfComponTypes := TStringList.Create;
  try
    for i := 0 to ACatalog.ChildCatalogReferences.Count - 1 do
    begin
      SCSCatalog := ACatalog.ChildCatalogReferences[i];
      if SCSCatalog.ItemType = itSCSConnector then
        for j := 0 to SCSCatalog.SCSComponents.Count - 1 do
        begin
          SCSComponent := SCSCatalog.SCSComponents[j];
          SprComponentType := FSpravochnik.GetComponentTypeWithAssign(SCSComponent.ComponentType.GUID, TF_Main(FActiveForm).GSCSBase.FNBSpravochnik);

          if SprComponentType <> nil then
            if LookedGUIDESOfComponTypes.IndexOf(SprComponentType.ComponentType.GUID) = -1 then
            begin
              GrpComponentType := TNBComponentType.Create(FActiveForm);
              GrpComponentType.AssignOnlyComponentType(SprComponentType);
              Result.Add(GrpComponentType);
              LookedGUIDESOfComponTypes.Add(SprComponentType.ComponentType.GUID);
            end;
        end;
    end;
  finally
    LookedGUIDESOfComponTypes.Free;
  end;
end;  }

begin
  try
    ComponList := TStringList.Create;
    Result := False;
    ComponTypes := TObjectList.Create;
    SCSCatalog := F_ProjMan.GetActualSelectedCatalog;//TF_MAIN.GetActualSelectedCatalog; // лист или проект, на котором стоим

{    if ComponTypes = nil then
      ComponTypes := TObjectList.Create
    else
      ComponTypes.Clear; }
    // формируем список типов точечных компонент
    lvComponTypes.Items.Clear;
    // если стоим на проекте, то берем сразу все точечные компоненты проекта
    if SCSCatalog.ItemType = itProject then
      ComponTypes := GetCurrProectPointComponTypes
    else
    begin
      if SCSCatalog.ItemType = itList then
      begin
        ComponTypes := F_ProjMan.GSCSBase.CurrProject.GetUsingComponentTypes(SCSCatalog);
      end
      else
        ComponTypes := F_ProjMan.GSCSBase.CurrProject.GetUsingComponentTypes(F_ProjMan.GSCSBase.CurrProject.CurrList);
    end;


    for i := 0 to ComponTypes.Count - 1 do
    begin
      CurComponType := TNBComponentType(ComponTypes[i]);
      Item := lvComponTypes.Items.Add;
      if CurComponType.ComponentType.SysName = ctsnCableChannelElement then
        Item.Caption := CurComponType.ComponentType.Name
      else
        Item.Caption := CurComponType.ComponentType.NamePlural;
        if (CurComponType.ComponentType.SysName = ctsnCupboard) or (CurComponType.ComponentType.SysName = ctsnWorkPlace) or
          (CurComponType.ComponentType.SysName = ctsnSocket) then
      Item.Checked := true; //CheckedCTSN.IndexOf(CurComponType.ComponentType.SysName) <> -1; //30.03.2013 True;
      Item.Data := CurComponType;
    end;
    // Если выбраны все компоненты, выставляем галочку "выбрать все в режим checked", иначе сбрасываем ее
    CheckedAll := true;
    for i := 0 to lvComponTypes.Items.Count - 1 do
    begin
      if lvComponTypes.Items[i].Checked = false then
        CheckedAll := False;
    end;
    RzCheckBox1.Checked := CheckedAll;
    //
    if ShowModal = mrOK then
    begin
      Result := True;
      if ComponList <> nil then
        FreeAndNil(ComponList);
      ComponList := TStringList.Create;
      // collect all chected types for report
      for i := 0 to lvComponTypes.Items.Count - 1 do
      begin
        if lvComponTypes.Items[i].Checked then
          ComponList.Add(lvComponTypes.Items[i].Caption);
      end;
      // if checked compontypes found - show the report
      try
        if ComponList.Count > 0 then
        begin
          try
            wasnil := False;
            if F_ProjMan.F_ResourceReport = nil then
            begin
              WasNil := True;
              F_ProjMan.F_ResourceReport := TF_ResourceReport.Create(F_ProjMan, TForm(F_ProjMan));
              {$IF Defined(SCS_RF)}
               F_ProjMan.F_ResourceReport.pnOtherProperties.Visible := false;
              {$IFEND}
              {$IF Defined(SCS_PE) or Defined(SCS_SPA)}
               F_ProjMan.F_ResourceReport.pnOtherProperties.Visible := false;
              {$IFEND}
            end;
            //F_ProjMan.F_ResourceReport.Timer_TimeOutExec.Enabled := True;
            //F_ProjMan.F_ResourceReport.Timer_TimeOutExec.Tag := 3;
            F_ProjMan.F_ResourceReport.ShowWACoordinatesReport(SCSCatalog, ComponList);
            if wasnil then
              FreeAndNil(F_ProjMan.F_ResourceReport);
          except
          end;
        end;
      except
      end;
     { ListParams := GetListParamsForNewList;
      ListParams.Caption := cChooseComponType_Mes1;
      ListParams.Name := ListParams.Caption;
      ListParams.MarkID := 0;
      ListParams.Settings.ListType := lt_ProjectPlan;
      MakeEditList(meMake, ListParams, False);

      for i := 0 to lvcompontypes.items.Count - 1 do
      begin
        item := lvcompontypes.items[i];
        CurComponType := TNBComponentType(item.Data);
        CurComponType.IsSelected := item.Checked;
      end;

   {   DrawProjectPlan(GCadForm, ComponTypes, cbDivideGroupsByJoinedNetTypes.Checked, cbShowGroupContents.Checked);
      DisableOptionsForDesignList;}
    end;
    FreeAndNil(ComponTypes);
    // Tolik -- 11/03/2017 --
    FreeAndNil(ComponList);
    //
    //FreeAndNil(CheckedCTSN);
  except
    on E: Exception do AddExceptionToLogEx('TF_ChooseComponTypesForReport.Execute', E.Message);
  end;
end;

Procedure TF_ChooseComponTypesForReport.RzCheckBox1Click(Sender: TObject);
Var i: Integer;
Begin
  if lvComponTypes.Items.Count > 0 then
  begin
    if RzCheckBox1.Checked then
    begin
      for i := 0 to lvComponTypes.Items.Count - 1 do
      begin
        lvComponTypes.Items[i].Checked := true;
      end;
    end
    else
    begin
       for i := 0 to lvComponTypes.Items.Count - 1 do
      begin
        lvComponTypes.Items[i].Checked := false;
      end;
    end;
  end;
End;

end.
