TourIAm/TIAMSharedUI/Pages/User/SysAdmins/ProductDetailGridComponent....

281 lines
12 KiB
Plaintext

@using AyCode.Core.Helpers
@using TIAM.Entities.Products
@using TIAM.Entities.ServiceProviders
@using TIAM.Entities.Transfers
@using TIAM.Entities.Drivers
@using TIAM.Entities.Users
@using TIAM.Models.Dtos.Users
@using TIAM.Resources
@using TIAM.Services
@using TIAMWebApp.Shared.Application.Interfaces
@using TIAMWebApp.Shared.Application.Utility
@using AyCode.Services.Loggers
@using TIAM.Core.Loggers
@using Address = TIAM.Entities.Addresses.Address
@using Profile = TIAM.Entities.Profiles.Profile
@using TIAMSharedUI.Shared.Components.Grids
@using TIAMSharedUI.Pages.Components.EditComponents
@using TIAMWebApp.Shared.Application.Services
@using AyCode.Interfaces.Addresses
@using AyCode.Core
@using AyCode.Core.Extensions
@inject IStringLocalizer<TIAMResources> Localizer
@inject IEnumerable<IAcLogWriterClientBase> LogWriters
@inject AdminSignalRClient AdminSignalRClient;
<ProductDetailGrid @ref="_productGrid"
ContextIds="@(ContextId.IsNullOrEmpty() ? throw new InvalidDataException($"ContextId.IsNullOrEmpty(); ContextId: {ContextId}") : [ContextId.Value])"
DataSource="ParentData?.Products ?? []"
GetAllMessageTag="GetAllTag"
Logger="_logger"
SignalRClient="AdminSignalRClient"
CustomizeEditModel="CustomizeEditModel"
OnGridEditModelSaving="DataItemSaving"
OnGridItemDeleting="DataItemDeleting"
OnGridItemChanged="DataItemChanged"
ValidationEnabled="false"
EditMode="GridEditMode.EditForm"
ColumnResizeMode="GridColumnResizeMode.NextColumn"
DetailExpandButtonDisplayMode="DetailExpandButtonDisplayMode">
<Columns>
<DxGridCommandColumn NewButtonVisible="true" Width="135" MinWidth="135" FixedPosition="GridColumnFixedPosition.Left" />
<DxGridDataColumn FieldName="Id" ShowInColumnChooser="AcDomain.IsDeveloperVersion" Visible="AcDomain.IsDeveloperVersion" DisplayFormat="N"/>
<DxGridDataColumn FieldName="@nameof(Product.ServiceProvider.Name)" Caption="Company name" />
<DxGridDataColumn Caption=@Localizer.GetString(ResourceKeys.ProductName) FieldName="Name" SortIndex="0"/>
<DxGridDataColumn Caption=@Localizer.GetString(ResourceKeys.ProductType) FieldName="ProductType" Width="130" />
<DxGridDataColumn FieldName="Id" Visible="@ShowManageButtons" Width="180">
<CellDisplayTemplate>
@{
Product product = (Product)context.DataItem;
if (product.ProductType == TIAM.Core.Enums.ProductType.Hotel)
{
<a class="btn btn-primary" href="user/hoteladmin/@context.Value">Manage hotel</a>
}
else if (product.ProductType == TIAM.Core.Enums.ProductType.Transfer)
{
<a class="btn btn-primary" href="user/transferadmin/@context.Value">Manage transfer service </a>
}
}
</CellDisplayTemplate>
</DxGridDataColumn>
<DxGridDataColumn Caption=@Localizer.GetString(ResourceKeys.Price) FieldName="Price" Width="100" />
<DxGridDataColumn Caption="ReferralLink" FieldName="Id" Width="100" >
<CellDisplayTemplate>
@*
/public/transfer/{referralId:guid}/{productId:guid}
*@
@{
Product product = (Product)context.DataItem;
<p>@Setting.BaseUrl/public/transfer/@product.ServiceProvider.AffiliateId/@product.Id</p>
}
</CellDisplayTemplate>
</DxGridDataColumn>
<DxGridDataColumn Caption="Options">
<CellDisplayTemplate>
@{
//check if has transferdestination
var AddressId = ((Product)context.DataItem).Profile.AddressId;
var result = CheckDestinations(AddressId);
//if not, display button
if (!result)
{
// <p>Address:</p>
// <p>@(((Product)context.DataItem).Profile.Address.AddressText)</p>
<DxButton Click="() => SaveAsDestination(((Product)context.DataItem).Profile.Address, (Product)context.DataItem)" Text="Save as destination" RenderStyle="ButtonRenderStyle.Primary" />
}
else
{
<p>Address:</p>
<p>@(((Product)context.DataItem).Profile.Address.AddressText)</p>
}
}
</CellDisplayTemplate>
</DxGridDataColumn>
<DxGridDataColumn Caption=@Localizer.GetString(ResourceKeys.ProductDescription) FieldName="Description" />
<DxGridDataColumn FieldName="Created" DisplayFormat="g" Width="140" CaptionAlignment="GridTextAlignment.Center" TextAlignment="GridTextAlignment.Center" />
<DxGridDataColumn FieldName="Modified" DisplayFormat="g" Width="140" CaptionAlignment="GridTextAlignment.Center" TextAlignment="GridTextAlignment.Center" />
</Columns>
<DetailRowTemplate>
<DxTabs>
<DxTabPage Text="Permissions">
<UserProductMappingGridComponent DetailExpandButtonDisplayMode="GridDetailExpandButtonDisplayMode.Never" ContextIds="new [] {((Product)context.DataItem).Id}" GetAllTag="SignalRTags.GetUserProductMappingsByProductId">
</UserProductMappingGridComponent>
</DxTabPage>
<DxTabPage Text="Profile">
<ProfileGridComponent ParentData="((Product)context.DataItem)" />
</DxTabPage>
</DxTabs>
</DetailRowTemplate>
<EditFormTemplate Context="editFormContext">
@{
var transfer2 = (Product)editFormContext.EditModel;
}
<DxFormLayout CssClass="w-100">
<DxFormLayoutItem Caption=@Localizer.GetString(ResourceKeys.ProductName) ColSpanMd="4">
@editFormContext.GetEditor("Name")
</DxFormLayoutItem>
<DxFormLayoutItem Caption=@Localizer.GetString(ResourceKeys.ProductType) ColSpanMd="4">
@editFormContext.GetEditor("ProductType")
</DxFormLayoutItem>
<DxFormLayoutItem Caption=@Localizer.GetString(ResourceKeys.Price) ColSpanMd="4">
@editFormContext.GetEditor("Price")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Company Id" ColSpanMd="4">
<p>@(((Product)editFormContext.EditModel).ServiceProviderId)</p>
</DxFormLayoutItem>
<DxFormLayoutItem Caption=@Localizer.GetString(ResourceKeys.ProductDescription) ColSpanMd="12">
@editFormContext.GetEditor("Description")
</DxFormLayoutItem>
</DxFormLayout>
</EditFormTemplate>
</ProductDetailGrid>
@code {
[Parameter] public Guid? ContextId { get; set; }
[Parameter] public IProductsRelation? ParentData { get; set; } = null!;
[Parameter] public EventCallback<GridEditModelSavingEventArgs> OnGridEditModelSaving { get; set; }
[Parameter] public int GetAllTag { get; set; } = SignalRTags.GetProductsByOwnerId;
[Parameter] public GridDetailExpandButtonDisplayMode DetailExpandButtonDisplayMode { get; set; } = GridDetailExpandButtonDisplayMode.Never;
[Parameter] public bool ShowManageButtons { get; set; } = false;
private List<TransferDestination> destinations = [];
private ProductDetailGrid _productGrid = null!;
private LoggerClient<ProductDetailGridComponent> _logger = null!;
protected override void OnInitialized()
{
_logger = new LoggerClient<ProductDetailGridComponent>(LogWriters.ToArray());
AdminSignalRClient.GetAllIntoAsync<TransferDestination>(destinations, SignalRTags.GetAllTransferDestinations).Forget();
//DataSource = new List<Address>();
}
private bool CheckDestinations(Guid addressId)
{
if(destinations!=null)
{
if (destinations.Any(d => d.AddressId == addressId))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private async Task SaveAsDestination(Address address, Product product)
{
TransferDestination transferDestination = new TransferDestination();
transferDestination.Id = Guid.NewGuid();
transferDestination.Name = product.Name;
if (!string.IsNullOrEmpty(product.Profile.Description))
{
transferDestination.Description = product.Profile.Description;
}
else
{
transferDestination.Description = "No description available";
}
transferDestination.AddressId = address.Id;
transferDestination.AddressString = address.AddressText;
var result = await AdminSignalRClient.PostDataAsync<TransferDestination>(SignalRTags.CreateTransferDestination, transferDestination);
_productGrid.Reload();
await InvokeAsync(StateHasChanged);
}
protected override async Task OnParametersSetAsync()
{
// if (ParentData != null)
// {
// productList.AddRange(ParentData.Products);
// }
// else if (ContextId != null)
// {
// //_productGrid.ContextIds = new Guid[1];
// //_productGrid.ContextIds[0] = (Guid)ContextId;
// var data = await serviceProviderDataService.GetProductByIdAsync((Guid)ContextId);
// List<Product> result = new List<Product>();
// result.Add(data);
// _productGrid.DataSource = result;
// }
// else
await base.OnParametersSetAsync();
}
private void DataItemChanged(GridDataItemChangedEventArgs<Product> args)
{
_logger.Debug($"Saving: {args.DataItem.Name}, {args.DataItem.ServiceProviderId}");
}
public async Task DataItemSaving(GridEditModelSavingEventArgs e)
{
await OnGridEditModelSaving.InvokeAsync(e);
if (e.Cancel) return;
var product = ((Product)e.EditModel);
if (e.IsNew)
{
_logger.Debug($"DataItemSaving");
// product.ServiceProviderId = ((Company)ParentData).Id;
// var profileId = Guid.NewGuid();
// product.Profile = new Profile(profileId, product.Name);
// product.ProfileId = profileId;
// var addressId = Guid.NewGuid();
// product.Profile.Address = new Address(addressId);
// product.Profile.AddressId = addressId;
//((Product)e.EditModel).UserProductMappings.Add(new UserProductMapping(Guid.NewGuid, ParentData.));
}
else
{
}
_logger.Debug($"Saving: {product.Name}, {product.ServiceProviderId}");
//var result = serviceProviderDataService.CreateProductAsync((Product)e.EditModel);
//_logger.Debug($"saved product: {product.ServiceProviderId}");
}
private void DataItemDeleting(GridDataItemDeletingEventArgs obj)
{
_logger.Debug($"DataItemDeleting");
}
void CustomizeEditModel(GridCustomizeEditModelEventArgs e)
{
if (!e.IsNew) return;
var newProduct = (Product)e.EditModel;
newProduct.Id = Guid.NewGuid();
newProduct.Name = "Type a name";
newProduct.ServiceProviderId = (Guid)ContextId!;
newProduct.Price = 0;
newProduct.ProductType = TIAM.Core.Enums.ProductType.Hotel;
newProduct.Description = "Type a description";
}
}