unit U_MakeEditInterfaceAccordance;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  U_BaseCommon, U_BaseConstants, cxLookAndFeelPainters, StdCtrls, cxButtons, RzRadGrp,
  RzPanel, ExtCtrls, cxRadioGroup, cxControls, cxContainer, cxEdit,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, Grids, DBGrids, SQLMemExcept,
  siComp, siLngLnk, RzButton, RzRadChk, RzCmboBx, Mask, RzEdit, RzSpnEdt, kbmMemTable,
  FIBQuery, pFIBQuery, pFIBProps, cxGraphics, cxLookAndFeels, Menus;

type
  TF_MakeEditInterfaceAccordance = class(TForm)
    RzPanel1: TRzPanel;
    Panel_OkCancel: TRzPanel;
    RzGroupBox1: TRzGroupBox;
    ComboBox_Interface: TcxComboBox;
    Button1_ChoiceInterface: TcxButton;
    Label1: TLabel;
    RzGroupBox2: TRzGroupBox;
    Label2: TLabel;
    lng_Forms: TsiLangLinked;
    RzGroupBox3: TRzGroupBox;
    RzGroupBox4: TRzGroupBox;
    rbComponIsPoint: TRzRadioButton;
    rbComponIsLine: TRzRadioButton;
    rbComponAnyType: TRzRadioButton;
    rbConnectComponIsPoint: TRzRadioButton;
    rbConnectComponIsLine: TRzRadioButton;
    rbConnectComponAnyType: TRzRadioButton;
    RzPanel2: TRzPanel;
    cbInterfAccordance: TRzComboBox;
    RzToolButton1: TRzToolButton;
    Label3: TLabel;
    seKolvo: TRzSpinEdit;
    btOk: TRzBitBtn;
    RzBitBtn1: TRzBitBtn;
    procedure Button1_ChoiceInterfaceClick(Sender: TObject);
    procedure Button1_ChoiceAccordanceClick(Sender: TObject);
    procedure Button_OKClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FIDInterface: Integer;
    FIDAccordance: Integer;

    FMemTable: TkbmMemTable;
    FMemTableInterfaces: TkbmMemTable;

    GForm: TForm;
    GFormMode: TFormMode;

    function GetInterfComponIsLineFromForm(ArbAnyType, ArbIsPoint, arbIsLine: TRzRadioButton): Integer;
    procedure SetInterfComponIsLineToForm(AIsLine: Integer; ArbAnyType, ArbIsPoint, arbIsLine: TRzRadioButton);
  public

    function MakeEdit(AFormMode: TFormMode; AMemTable, AMemTableInterfaces: TkbmMemTable): Boolean;
    procedure LoadInterfFromForm(ATrgCombo, AOtherCombo: TcxComboBox; var AIDInterface: Integer);

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;
  end;

//var
//  F_MakeEditInterfaceAccordance: TF_MakeEditInterfaceAccordance;

implementation
uses U_Main, Unit_DM_SCS, DB;
{$R *.dfm}


constructor TF_MakeEditInterfaceAccordance.Create(AOwner: TComponent;
  AForm: TForm);
begin
    GForm := AForm;
    Inherited Create(AOwner);
end;

destructor TF_MakeEditInterfaceAccordance.Destroy;
begin
    inherited ;
end;


function TF_MakeEditInterfaceAccordance.MakeEdit(AFormMode: TFormMode; AMemTable, AMemTableInterfaces: TkbmMemTable): Boolean;
var
  InterfaceAccordanceID: Integer;
  InterfaceAccordanceInfo: TInterfaceAccordanceInfo;

  InterfComponType: Integer;
  AccordInterfComponType: Integer;
  MakeEdit: TMakeEDit;

  //MemTableBookmark: string;
  MemTableBookmark: TBookMark;

  GUIDInterface: string;
  GUIDAccordance: String;
  IDInterface: Integer;
  IDAccordance: Integer;

  StrIsVisibleFilter: string;
begin
  Result := false;

  GFormMode := AFormMode;
  MakeEdit := GetMakeEDitByFormMode(AFormMode);
  FMemTable := AMemTable;
  FMemTableInterfaces := AMemTableInterfaces;

  rbComponIsPoint.Checked := false;
  rbComponIsLine.Checked := false;
  rbComponAnyType.Checked := false;
  rbConnectComponIsPoint.Checked := false;
  rbConnectComponIsLine.Checked := false;
  rbConnectComponAnyType.Checked := false;

  InterfaceAccordanceID := 0;
  GUIDInterface := '';
  IDInterface := 0;

  FIDInterface := 0;
  FIDAccordance := 0;

  StrIsVisibleFilter := '';

  case GFormMode of
    fmMake:
      begin
        Caption := cMakeEditInterfaceAccordance_Msg1_1;
        //FillComboBox(ComboBox_Interface, F_NormBase.DM.DataSet_InterfaceLookUp, true, 'ID', 'Name', 0);
        //FillComboBox(ComboBox_Accordance, F_NormBase, true, tnInterface, fnID, fnName, '', 0);
        if AMemTableInterfaces <> nil then
          FillComboBoxRzFromMT(cbInterfAccordance, AMemTableInterfaces, true, fnID, fnName, '', 0)
        else
        begin
          //*** Не отображать невидимые интерфейсы для новых компонент в не рабочей версии
          if Not IsShowUnvisibleInterface then
            StrIsVisibleFilter := GetSQLForIsVisible(tnInterface);
          FillComboBoxRz(cbInterfAccordance, F_NormBase, true, tnInterface, fnID, fnName, '', 0, '', StrIsVisibleFilter);
        end;

        ComboBox_Interface.ItemIndex := 0;
        //ComboBox_Accordance.ItemIndex := 0;
        cbInterfAccordance.ItemIndex := 0;

        seKolvo.Value := 1;

        rbComponAnyType.Checked := true;
        rbConnectComponAnyType.Checked := true;
        //RadioGroup_InterfComponType.ItemIndex := -1;
        //RadioGroup_AccordInterfComponType.ItemIndex := -1;
      end;
    fmEdit:
      begin
        Caption := cMakeEditInterfaceAccordance_Msg1_2;
        if AMemTableInterfaces <> nil then
        begin
          //MemTableBookmark := AMemTable.Bookmark;
          MemTableBookmark := AMemTable.GetBookmark;
          try
            FillComboBoxRzFromMT(cbInterfAccordance, AMemTableInterfaces, false, fnID, fnName, '', 0);
          finally
            //AMemTable.Bookmark := MemTableBookmark;
            AMemTable.GotoBookmark(MemTableBookmark);
            AMemTable.FreeBookMark(MemTableBookmark);
          end;
        end
        else
        begin
          if Not IsShowUnvisibleInterface then
            StrIsVisibleFilter := GetSQLForIsVisibleWithFieldIntValue(tnInterface, fnID, F_NormBase.DM.DataSet_InterfAccordance.FN(fnIDAccordance).AsInteger);
          FillComboBoxRz(cbInterfAccordance, F_NormBase, false, tnInterface, fnID, fnName, '', 0, '', StrIsVisibleFilter);
        end;

        if AMemTable <> nil then
        begin
          InterfaceAccordanceID := AMemTable.FieldByName(fnID).AsInteger;
          GUIDInterface := AMemTable.FieldByName(fnGuidInterface).AsString;
          IDInterface := AMemTable.FieldByName(fnIDInterface).AsInteger;

          GUIDAccordance := AMemTable.FieldByName(fnGuidAccordance).AsString;
          SelectItemByGUIDinComboRz(cbInterfAccordance, GUIDAccordance); //FillComboBoxRzFromMT(cbInterfAccordance, AMemTable, false, fnID, fnName, GUIDAccordance);

          InterfComponType := AMemTable.FieldByName(fnInterfComponIsLine).AsInteger;
          AccordInterfComponType := AMemTable.FieldByName(fnAccordComponIsLine).AsInteger;
          seKolvo.Value := AMemTable.FieldByName(fnKolvo).AsInteger;
        end
        else
        begin
          InterfaceAccordanceID := F_NormBase.DM.DataSet_InterfAccordance.FN(fnID).AsInteger;
          FIDInterface := F_NormBase.DM.DataSet_InterfAccordance.FN(fnIDInterface).AsInteger;
          FIDAccordance := F_NormBase.DM.DataSet_InterfAccordance.FN(fnIDAccordance).AsInteger;

          //FillComboBox(ComboBox_Interface, F_NormBase.DM.DataSet_InterfaceLookUp, false, 'ID', 'Name', FID_Interface);
          //FillComboBoxRz(cbInterfAccordance, F_NormBase, false, tnInterface, fnID, fnName, '', FID_Accordance);
          SelectItemByIDinComboRz(cbInterfAccordance, FIDAccordance);

          //RadioGroup_InterfComponType.ItemIndex := F_NormBase.DM.DataSet_InterfAccordance.FN('INTERF_COMPON_ISLINE').AsInteger;
          //RadioGroup_AccordInterfComponType.ItemIndex := F_NormBase.DM.DataSet_InterfAccordance.FN('ACCORD_COMPON_ISLINE').AsInteger;
          InterfComponType := F_NormBase.DM.DataSet_InterfAccordance.FN(fnInterfComponIsLine).AsInteger;
          AccordInterfComponType := F_NormBase.DM.DataSet_InterfAccordance.FN(fnAccordComponIsLine).AsInteger;
          seKolvo.Value := F_NormBase.DM.DataSet_InterfAccordance.FN(fnKolvo).AsInteger;
        end;

        SetInterfComponIsLineToForm(InterfComponType, rbComponAnyType, rbComponIsPoint, rbComponIsLine);
        SetInterfComponIsLineToForm(AccordInterfComponType, rbConnectComponAnyType, rbConnectComponIsPoint, rbConnectComponIsLine);
      end;
  end;

  if ShowModal = mrOK then
  begin
    Result := true;

    GUIDAccordance := GetGUIDFromComboBoxRz(cbInterfAccordance);
    IDAccordance := GetIDFromComboBoxRz(cbInterfAccordance);
    InterfComponType := GetInterfComponIsLineFromForm(rbComponAnyType, rbComponIsPoint, rbComponIsLine);
    AccordInterfComponType := GetInterfComponIsLineFromForm(rbConnectComponAnyType, rbConnectComponIsPoint, rbConnectComponIsLine);


    if AMemTable <> nil then
    begin
      if AMemTableInterfaces <> nil then
      begin
        GUIDInterface := AMemTableInterfaces.FieldByName(fnGUID).AsString;
        IDInterface := AMemTableInterfaces.FieldByName(fnID).AsInteger;
      end;

      if GFormMode = fmMake then
        AMemTable.Append;
      AMemTable.Edit;
      AMemTable.FieldByName(fnGUIDInterface).AsString := GUIDInterface;
      AMemTable.FieldByName(fnIDInterface).AsInteger := IDInterface;
      AMemTable.FieldByName(fnInterfComponIsLine).AsInteger := InterfComponType; //RadioGroup_InterfComponType.ItemIndex;
      AMemTable.FieldByName(fnGUIDAccordance).AsString := GUIDAccordance;
      AMemTable.FieldByName(fnIDAccordance).AsInteger := IDAccordance;
      AMemTable.FieldByName(fnAccordComponIsLine).AsInteger := AccordInterfComponType; //RadioGroup_AccordInterfComponType.ItemIndex;
      AMemTable.FieldByName(fnKolvo).AsInteger := Trunc(seKolvo.Value);
      AMemTable.FieldByName(fnName).AsString := cbInterfAccordance.Text; 
      AMemTable.FieldByName(fnIsModified).AsBoolean := true;
      AMemTable.Post;
    end
    else
    with TF_Main(GForm).DM do
    begin
      //ID_Interface := GetIDFromComboBox(ComboBox_Interface);
      IDInterface := DataSet_Interface.FN(fnID).AsInteger;

      ZeroMemory(@InterfaceAccordanceInfo, SizeOf(TInterfaceAccordanceInfo));
      InterfaceAccordanceInfo.ID := InterfaceAccordanceID;
      InterfaceAccordanceInfo.IDInterface := IDInterface;
      InterfaceAccordanceInfo.InterfComponIsLine := InterfComponType;
      InterfaceAccordanceInfo.IDAccordance := IDAccordance;
      InterfaceAccordanceInfo.AccordComponIsLine := AccordInterfComponType;
      InterfaceAccordanceInfo.Kolvo := Trunc(seKolvo.Value);
      SaveInterfaceAccordance(MakeEdit, @InterfaceAccordanceInfo);
      DataSet_InterfAccordance.FullRefresh;
      if GFormMode = fmMake then
        DataSet_InterfAccordance.Locate(fnID, InterfaceAccordanceInfo.ID, []);

      {
      if GFormMode = fmMake then
        DataSet_InterfAccordance.Append;
      DataSet_InterfAccordance.Edit;
      DataSet_InterfAccordance.FN(fnIDInterface).AsInteger := IDInterface;
      DataSet_InterfAccordance.FN(fnInterfComponIsLine).AsInteger := InterfComponType; //RadioGroup_InterfComponType.ItemIndex;
      DataSet_InterfAccordance.FN(fnIDAccordance).AsInteger := IDAccordance;
      DataSet_InterfAccordance.FN(fnAccordComponIsLine).AsInteger := AccordInterfComponType; //RadioGroup_AccordInterfComponType.ItemIndex;
      DataSet_InterfAccordance.FN(fnKolvo).AsInteger := Trunc(seKolvo.Value);
      DataSet_InterfAccordance.Post; }
    end;
  end;
end;                                       


// #####  #####
procedure TF_MakeEditInterfaceAccordance.LoadInterfFromForm(ATrgCombo, AOtherCombo: TcxComboBox; var AIDInterface: Integer);
var IDTrgCombo: Integer;
    //IDFromOtherCombo: Integer;
    CanEmptyLine: Boolean;
begin
  try
    CanEmptyLine := false;
    case GFormMode of
      fmMake:
        CanEmptyLine := true;
      fmEdit:
        CanEmptyLine := false;
    end;

    IDTrgCombo := F_NormBase.DM.GetInterfaceIDFromGuide(AIDInterface, GFormMode);
    if IDTrgCombo <> -1 then
    begin
      FillComboBox(ATrgCombo, F_NormBase, CanEmptyLine, tnInterface, fnID, fnName, '', IDTrgCombo);
      AIDInterface := IDTrgCombo;
    end
    else
      FillComboBox(ATrgCombo, F_NormBase, CanEmptyLine, tnInterface, fnID, fnName, '', AIDInterface);

    //IDFromOtherCombo := GetIDFromComboBox(AOtherCombo);
    FillComboBox(AOtherCombo, F_NormBase, CanEmptyLine, tnInterface, fnID, fnName, '', -1);

  except
    on E: Exception do AddExceptionToLog('TF_MakeEditInterfaceAccordance.LoadInterfFromForm: '+E.Message);
  end;
end;

function TF_MakeEditInterfaceAccordance.GetInterfComponIsLineFromForm(ArbAnyType, ArbIsPoint, arbIsLine: TRzRadioButton): Integer;
begin
  Result := -1;
  if ArbIsLine.Checked then
    Result := ltIsLine;
  if ArbIsPoint.Checked then
    Result := ltIsPoint;
  if ArbAnyType.Checked then
    Result := ltAnyType;
end;

procedure TF_MakeEditInterfaceAccordance.SetInterfComponIsLineToForm(AIsLine: Integer; ArbAnyType, ArbIsPoint, ArbIsLine: TRzRadioButton);
begin
  case AIsLine of
    ltIsLine:
      ArbIsLine.Checked := true;
    ltIsPoint:
      ArbIsPoint.Checked := true;
    ltAnyType:
      ArbAnyType.Checked := true;
  end;
end;

procedure TF_MakeEditInterfaceAccordance.Button1_ChoiceInterfaceClick(
  Sender: TObject);
begin
    //LoadInterfFromForm(ComboBox_Interface, ComboBox_Accordance, FID_Interface);
end;

procedure TF_MakeEditInterfaceAccordance.Button1_ChoiceAccordanceClick(
  Sender: TObject);
begin
    //LoadInterfFromForm(ComboBox_Accordance, ComboBox_Interface, FID_Accordance);
end;



// ##### OK #####
procedure TF_MakeEditInterfaceAccordance.Button_OKClick(Sender: TObject);
var CanSave: Boolean;
    MessgError: String;

    ID_Interface: Integer;
    ID_Accordance: Integer;

    ResGUIDInterface: String;
    ResGUIDAccordance: String;
    ResIDInterface: Integer;
    ResIDAccordance: Integer;
    GUIDInterface: String;
    GUIDAccordance: String;
    IDInterface: Integer;
    IDAccordance: Integer;
    NameInterf: String;
    NameAccordance: String;

    IsExistsRelation: Boolean;
    IsSameInterfaces: Boolean;
    SavedMasterSource: TDataSource;
    //SavedBookMark: String;
    SavedBookMark: TBookMark;
    SameRelCount: Integer;
    FieldList: TStringList;
    SQLWhere: String;
    QSelect: TpFIBQuery;

  procedure AddErrorMessg(AError: String);
  begin
    MessgError := MessgError + #13 + '  - ' + AError;
    CanSave := False;
  end;

begin
  try
    CanSave := true;
    MessgError := cMakeEditInterfaceAccordance_Msg2_1+':'+#13+#13;

    //if ComboBox_Interface.Text = '' then
     //  AddErrorMessg('интерфейс');
    if cbInterfAccordance.Text = '' then
      AddErrorMessg(cMakeEditInterfaceAccordance_Msg2_2);
    //if RadioGroup_InterfComponType.ItemIndex = -1 then
    if GetInterfComponIsLineFromForm(rbComponAnyType, rbComponIsPoint, rbComponIsLine) = -1 then
      AddErrorMessg(cMakeEditInterfaceAccordance_Msg2_3);
    //if RadioGroup_AccordInterfComponType.ItemIndex = -1 then
    if GetInterfComponIsLineFromForm(rbConnectComponAnyType, rbConnectComponIsPoint, rbConnectComponIsLine) = -1 then
      AddErrorMessg(cMakeEditInterfaceAccordance_Msg2_4);


    if Not CanSave then
    begin
      ShowMessageByType(Self.Handle, smtDisplay, MessgError, Caption, MB_OK or MB_ICONINFORMATION);
      ModalResult := mrNone;
      Exit; ///// EXIT /////
    end
    else
    begin
      // Проверить на существование такой связи
      IsExistsRelation := false;
      IsSameInterfaces := false;
      SameRelCount := 0;
      NameInterf := '';
      NameAccordance := '';
      if FMemTable <> nil then
      begin
        ResGUIDInterface := FMemTableInterfaces.FieldByName(fnGUID).AsString;
        ResGUIDAccordance := GetGUIDFromComboBoxRz(cbInterfAccordance);

        if (ResGUIDInterface <> ResGUIDAccordance) then
        begin
          FMemTable.DisableControls;
          try
            //SavedBookMark := FMemTable.Bookmark;
            SavedBookMark := FMemTable.GetBookmark;
            SavedMasterSource := FMemTable.MasterSource;
            FMemTable.MasterSource := nil;
            try
              FMemTable.First;
              while Not FMemTable.Eof do
              begin
                GUIDInterface := FMemTable.FieldByName(fnGuidInterface).AsString;
                GUIDAccordance := FMemTable.FieldByName(fnGuidAccordance).AsString;

                if (GUIDInterface = ResGUIDInterface) and (GUIDAccordance = ResGUIDAccordance) then
                begin
                  SameRelCount := SameRelCount + 1;
                  if ((SameRelCount > 0) and (GFormMode = fmMake)) or
                     ((SameRelCount > 1) and (GFormMode = fmEdit)) then
                  begin
                    IsExistsRelation := true;
                    NameInterf := GetNameFromComboByGUIDRz(cbInterfAccordance, GUIDInterface);
                    NameAccordance := GetNameFromComboByGUIDRz(cbInterfAccordance, GUIDAccordance);
                  end;
                end
                else
                if (GUIDInterface = ResGUIDAccordance) and (GUIDAccordance = ResGUIDInterface) then
                begin
                  IsExistsRelation := true;
                  NameInterf := GetNameFromComboByGUIDRz(cbInterfAccordance, GUIDInterface);
                  NameAccordance := GetNameFromComboByGUIDRz(cbInterfAccordance, GUIDAccordance);
                end;
                if IsExistsRelation then
                  Break; //// BREAK ////
                FMemTable.Next;
              end;
            finally
              FMemTable.MasterSource := SavedMasterSource;
              //FMemTable.Bookmark := SavedBookMark;
              FMemTable.GotoBookmark(SavedBookMark);
              FMemTable.FreeBookmark(SavedBookMark);
            end;
          finally
            FMemTable.EnableControls;
          end;
        end
        else
          IsSameInterfaces := true;
      end
      else
      begin
        FieldList := TStringList.Create;
        FieldList.Add(fnIDInterface);
        FieldList.Add(fnIDAccordance);

        ResIDInterface := TF_Main(GForm).DM.DataSet_Interface.FN(fnID).AsInteger;
        ResIDAccordance := GetIDFromComboBoxRz(cbInterfAccordance);

        if (ResIDInterface <> ResIDAccordance) then
        begin
          SQLWhere := '('+fnIDInterface+' = '''+IntToStr(ResIDInterface)+''') or ('+fnIDAccordance+' = '''+IntToStr(ResIDInterface)+''')';
          QSelect := TF_Main(GForm).DM.Query_Select;
          SetSQLToFIBQuery(QSelect, GetSQLByParams(qtSelect, tnInterfaceAccordance, SQLWhere, FieldList, ''));
          while Not QSelect.Eof do
          begin
            IDInterface := QSelect.FN(fnIDInterface).AsInteger;
            IDAccordance := QSelect.FN(fnIDAccordance).AsInteger;

            if (IDInterface = ResIDInterface) and (IDAccordance = ResIDAccordance) then
            begin
              SameRelCount := SameRelCount + 1;
              if ((SameRelCount > 0) and (GFormMode = fmMake)) or
                 ((SameRelCount > 1) and (GFormMode = fmEdit)) then
              begin
                IsExistsRelation := true;
                NameInterf := GetNameFromComboByIDRz(cbInterfAccordance, IDInterface);
                NameAccordance := GetNameFromComboByIDRz(cbInterfAccordance, IDAccordance);
              end;
            end
            else
            if (IDInterface = ResIDAccordance) and (IDAccordance = ResIDInterface) then
            begin
              IsExistsRelation := true;
              NameInterf := GetNameFromComboByIDRz(cbInterfAccordance, IDInterface);
              NameAccordance := GetNameFromComboByIDRz(cbInterfAccordance, IDAccordance);
            end;
            if IsExistsRelation then
              Break; //// BREAK ////
            QSelect.Next;
          end;
          FreeAndNil(FieldList);
        end
        else
          IsSameInterfaces := true;
      end;

      if IsSameInterfaces then
      begin
        MessageModal(cMakeEditInterfaceAccordance_Msg2_5, ApplicationName, MB_ICONINFORMATION or MB_OK);
        ModalResult := mrNone;
      end
      else
      if IsExistsRelation then
      begin
        MessageModal(cMakeEditInterfaceAccordance_Msg2_6 +'"'+NameInterf+'"'+ cMakeEditInterfaceAccordance_Msg2_7 +
            '"'+NameAccordance+'"'+ cMakeEditInterfaceAccordance_Msg2_8, ApplicationName, MB_ICONINFORMATION or MB_OK);
        ModalResult := mrNone;
      end;
    end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditInterfaceAccordance.Button_OKClick: '+E.Message);
  end;
end;

procedure TF_MakeEditInterfaceAccordance.FormDestroy(Sender: TObject);
begin
  ClearComboBoxRz(cbInterfAccordance);
end;

end.
