unit U_MakeEditInterface;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,
  U_BaseCommon, U_BaseConstants, ExtCtrls, RzPanel, RzButton, RzEdit, StdCtrls, RzCmboBx,
  Mask, kbmMemTable, siComp, siLngLnk, RzRadChk;

type
  TF_MakeEditInterface = class(TForm)
    gbMain: TRzGroupBox;
    gbOkCancel: TRzGroupBox;
    edName: TRzEdit;
    cbNetType: TRzComboBox;
    neConstructiveWidth: TRzNumericEdit;
    btOK: TRzBitBtn;
    btCancel: TRzBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    lbConstructiveWidth: TLabel;
    lng_Forms: TsiLangLinked;
    Label4: TLabel;
    meDescription: TRzMemo;
    cbIsVisible: TRzCheckBox;
    procedure btOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    GForm: TForm;
  public
    NewID: Integer;

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;
    function Execute(AMakeEdit: TMakeEdit; AMemTable, AMemTableNetType: TkbmMemTable): Boolean;
  end;

//var
//  F_MakeEditInterfRel: TF_MakeEditInterfRel;

implementation
Uses Unit_DM_SCS, U_Main, DB;

{$R *.dfm}

{ TF_MakeEditInterfRel }

constructor TF_MakeEditInterface.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_MakeEditInterface.Destroy;
begin
  inherited;
end;

function TF_MakeEditInterface.Execute(AMakeEdit: TMakeEdit; AMemTable, AMemTableNetType: TkbmMemTable): Boolean;
var
  InterfaceInfo: TInterfaceInfo;
  InterfID: Integer;
begin
  Result := false;
  try
    Caption := '';
    edName.Text := '';
    neConstructiveWidth.Value := 0;
    NewID := 0;
    InterfID := 0;

    lbConstructiveWidth.Caption := cCaseForm_Msg39 + ', ' + GetNameUOM(TF_Main(GForm).FUOM, true);
    //lbConstructiveWidthIzm.Caption := GetNameUOM(TF_Main(GForm).FUOM, true);

    if AMemTableNetType = nil then
      FillComboBoxRz(cbNetType, F_NormBase, true, tnNetType, fnID, fnName, '', 0)
    else
      FillComboBoxRzFromMT(cbNetType, AMemTableNetType, true, fnID, fnName, '', 0);
    with TF_Main(GForm).DM do
    begin
      case AMakeEdit of
        meMake:
          begin
            Caption := cMakeEditInterface_Msg1_1;
            edName.Text := '';
            neConstructiveWidth.Value := 0;
            meDescription.Lines.Text := '';
            cbIsVisible.Checked := false;
          end;
        meEdit:
          begin
            Caption := cMakeEditInterface_Msg1_2;
            if AMemTable <> nil then
            begin
              InterfID := AMemTable.FieldByName(fnID).AsInteger;
              edName.Text := AMemTable.FieldByName(fnName).AsString;
              SelectItemByGUIDinComboRz(cbNetType, AMemTable.FieldByName(fnGUIDNetType).AsString);
              neConstructiveWidth.Value := FloatInUOM(AMemTable.FieldByName(fnConstructiveWidth).AsFloat, umMetr, TF_Main(GForm).FUOM);
              meDescription.Lines.Text := AMemTable.FieldByName(fnDescription).AsString;
            end
            else
            begin
              InterfID := DataSet_Interface.FN(fnID).AsInteger;
              edName.Text := DataSet_Interface.FN(fnName).AsString;
              SelectItemByIDinComboRz(cbNetType, DataSet_Interface.FN(fnIDNetType).AsInteger);
              neConstructiveWidth.Value := FloatInUOM(DataSet_Interface.FN(fnConstructiveWidth).AsFloat, umMetr, TF_Main(GForm).FUOM);
              meDescription.Lines.Text := DataSet_Interface.FN(fnDescription).AsString;
              cbIsVisible.Checked := IntToBool(DataSet_Interface.FN(fnIsVisible).AsInteger);
            end;
          end;
      end;

      if ShowModal = mrOK then
      begin
        Result := true;
        if AMemTable <> nil then
        begin
          case AMakeEdit of
            meMake:
              AMemTable.Append;
            meEdit:
              AMemTable.Edit;
          end;
          if AMemTable.State <> dsBrowse then
          begin
            AMemTable.FieldByName(fnName).AsString := edName.Text;
            AMemTable.FieldByName(fnGUIDNetType).AsString := GetGUIDFromComboBoxRz(cbNetType);
            AMemTable.FieldByName(fnIDNetType).AsInteger := GetIDFromComboBoxRz(cbNetType);
            if neConstructiveWidth.Value <> 0 then
              AMemTable.FieldByName(fnConstructiveWidth).AsFloat := FloatInUOM(neConstructiveWidth.Value, TF_Main(GForm).FUOM, umMetr)
            else
              AMemTable.FieldByName(fnConstructiveWidth).Value := null;
            AMemTable.FieldByName(fnNameNetType).AsString := cbNetType.Text;
            AMemTable.FieldByName(fnDescription).AsString := meDescription.Lines.Text;
            AMemTable.FieldByName(fnIsModified).AsBoolean := true;
            AMemTable.Post;

            if AMakeEdit = meMake then
              NewID := 0; AMemTable.FieldByName(fnID).AsInteger;
          end;
        end
        else
        begin
          ZeroMemory(@InterfaceInfo, SizeOf(TInterfaceInfo));
          InterfaceInfo.ID := InterfID;
          InterfaceInfo.Name := edName.Text;
          InterfaceInfo.IDNetType := GetIDFromComboBoxRz(cbNetType);
          InterfaceInfo.ConstructiveWidth := FloatInUOM(neConstructiveWidth.Value, TF_Main(GForm).FUOM, umMetr);
          InterfaceInfo.Description := meDescription.Lines.Text;
          InterfaceInfo.IsVisible := biTrue;
          if IsShowUnvisibleInterface then
            InterfaceInfo.IsVisible := BoolToInt(cbIsVisible.Checked);
          SaveInterface(AMakeEdit, @InterfaceInfo);
          if AMakeEdit = meMake then
            NewID := InterfaceInfo.ID
          else
          if AMakeEdit = meEdit then
            DataSet_Interface.FullRefresh;


          {case AMakeEdit of
            meMake:
              DataSet_Interface.Append;
            meEdit:
              DataSet_Interface.Edit;
          end;
          if DataSet_Interface.State <> dsBrowse then
          begin
            DataSet_Interface.FN(fnName).AsString := edName.Text;
            DataSet_Interface.FN(fnIDNetType).AsInteger := GetIDFromComboBoxRz(cbNetType);
            if neConstructiveWidth.Value <> 0 then
              DataSet_Interface.FN(fnConstructiveWidth).AsFloat := neConstructiveWidth.Value
            else
              DataSet_Interface.FN(fnConstructiveWidth).Value := null;
            DataSet_Interface.FN(fnDescription).AsString := meDescription.Lines.Text;
            DataSet_Interface.Post;

            if AMakeEdit = meMake then
              NewID := DataSet_Interface.FN(fnID).AsInteger;
          end;}
        end;
      end;
    end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditInterface.Execute: '+E.Message);
  end;
end;

procedure TF_MakeEditInterface.btOKClick(Sender: TObject);
var
  Messg: String;

  procedure AddTextToMessg(AText: String);
  begin
    Messg := Messg + #13+#10+' -'+AText;
  end;
begin
  Messg := '';
  if edName.Text = '' then
    AddTextToMessg(cMakeEditInterface_Msg2_2);
  if cbNetType.Text = '' then
    AddTextToMessg(cMakeEditInterface_Msg2_3);
  if Messg <> '' then
  begin
    Messg := cMakeEditInterface_Msg2_1+':' + #13+#10 + Messg;
    MessageModal(Messg, ApplicationName, MB_ICONINFORMATION or MB_OK);
    ModalResult := mrNone; 
  end;
end;

procedure TF_MakeEditInterface.FormCreate(Sender: TObject);
var
  InterfIsVisibleEnabled: Boolean;
begin
  neConstructiveWidth.DisplayFormat := GetDisplayFormatForFloat;

  InterfIsVisibleEnabled := (IsShowUnvisibleInterface = true) and (TF_Main(GForm).GDBMode = bkNormBase);
  cbIsVisible.Enabled := InterfIsVisibleEnabled;
  cbIsVisible.Visible := InterfIsVisibleEnabled;
end;

procedure TF_MakeEditInterface.FormShow(Sender: TObject);
begin
  edName.SetFocus;
end;

end.
