ExternalEntityLinkClientFunctions.cs 1.49 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using DirRX.LongTermArchive.ExternalEntityLink;
using Sungero.Domain.Shared;

namespace DirRX.LongTermArchive.Client
{
  partial class ExternalEntityLinkFunctions
  {
    // Выбрать сущность в диалоге.
    public void SelectEntityItem()
    {
      if (string.IsNullOrWhiteSpace(_obj.EntityType))
        Dialogs.ShowMessage(ExternalEntityLinks.Resources.EnterEntityTypeWarning, MessageType.Warning);
      else
      {
        var allItems = Functions.ExternalEntityLink.Remote.GetEntities(_obj).ToArray();
        
        if (allItems.Any())
        {
          var dialog = Dialogs.CreateInputDialog(ExternalEntityLinks.Resources.SelectEntityRecordDialogTitle);
         
          var itemLabel = allItems.First().Info.LocalizedName;
          var itemIds = allItems.Select(x => x.Id).ToArray();
          var itemNames = allItems.Select(x => x.DisplayValue).ToArray();
          
          var itemSelector = dialog.AddSelect(itemLabel, true).From(itemNames);
          
          if (_obj.EntityId.HasValue)
            itemSelector.ValueIndex = Array.IndexOf(itemIds, _obj.EntityId.Value);
          
          if (dialog.Show() == DialogButtons.Ok)
          {
            _obj.EntityId = itemIds[itemSelector.ValueIndex];
          }
        }
        else
          Dialogs.ShowMessage(ExternalEntityLinks.Resources.ErrorEntityNotFound, MessageType.Error);
      }
    }

  }
}