TourIAm/TIAMSharedUI/Pages/User/SysAdmins/ToursFromTransferDestinatio...

241 lines
7.8 KiB
Plaintext

@page "/sysadmin/manage-tours"
@using System.ComponentModel.DataAnnotations
@using TIAM.Entities.Transfers
@using TIAMWebApp.Shared.Application.Interfaces
@using TIAMWebApp.Shared.Application.Models
@using TIAMWebApp.Shared.Application.Services
@using TIAMSharedUI.Shared
@inject ITransferDataService TransferDataService
@inject TourService TourService
@inject NavigationManager Navigation
@layout AdminLayout
<PageTitle>Manage Tours</PageTitle>
<h1 class="text-center m-4">Manage Tours</h1>
<div class="container">
<div class="mb-4">
<h4>Create New Tour</h4>
<EditForm Model="@newTour" OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label>Select Transfer Destination</label>
<InputSelect @bind-Value="newTour.TransferDestinationId" class="form-control">
<option value="">-- Select --</option>
@foreach (var dest in TransferDestinations)
{
<option value="@dest.Id">@dest.Name (@dest.AddressString)</option>
}
</InputSelect>
</div>
<div class="mb-3">
<label>Tour name</label>
<InputTextArea @bind-Value="newTour.Name" class="form-control" />
</div>
<div class="mb-3">
<label>Description</label>
<InputTextArea @bind-Value="newTour.Description" class="form-control" />
</div>
<div class="mb-3">
<label>Cover Photo</label>
<InputFile OnChange="HandleFileChange" />
@if (!string.IsNullOrEmpty(UploadStatus))
{
<div class="text-muted mt-1">@UploadStatus</div>
}
</div>
<button class="btn btn-primary me-2" type="submit">
@(IsEditing ? "Update Tour" : "Save Tour")
</button>
@if (IsEditing)
{
<button class="btn btn-secondary" type="button" @onclick="CancelEdit">Cancel</button>
}
</EditForm>
</div>
<hr />
<div>
<h4>Existing Tours</h4>
@if (Tours.Any())
{
<ul class="list-group">
@foreach (var tour in Tours)
{
var dest = TransferDestinations.FirstOrDefault(d => d.Id == tour.TransferDestinationId);
<li class="list-group-item d-flex justify-content-between align-items-center">
<img src="@tour.CoverImageUrl" style="max-height: 100px;"/>
<span>
<strong>@tour?.Title</strong> — @tour.FancyDescription
</span>
<button class="btn btn-sm btn-secondary me-2" @onclick="() => EditTour(tour)">Edit</button>
<button class="btn btn-sm btn-danger" @onclick="() => DeleteTourAsync(tour.Id)">Delete</button>
</li>
}
</ul>
}
else
{
<p>No tours available.</p>
}
</div>
</div>
@code {
private List<TransferDestination> TransferDestinations = [];
private List<TourInfo> Tours = [];
private TourFormModel newTour = new();
private IBrowserFile? uploadedFile;
private string UploadStatus = string.Empty;
private bool IsEditing = false;
private Guid? EditingTourId = null;
protected override async Task OnInitializedAsync()
{
TransferDestinations = (await TransferDataService.GetDestinationsAsync()).ToList();
Tours = (await TourService.GetAllAsync()).ToList();
}
private void EditTour(TourInfo tour)
{
newTour = new TourFormModel
{
TransferDestinationId = tour.TransferDestinationId,
Name = tour.Title,
Description = tour.FancyDescription,
ImageBytes = null,
ImageFileName = null
};
IsEditing = true;
EditingTourId = tour.Id;
UploadStatus = string.Empty;
}
private void CancelEdit()
{
newTour = new TourFormModel();
uploadedFile = null;
IsEditing = false;
EditingTourId = null;
UploadStatus = string.Empty;
}
// private async Task SaveTourAsync()
// {
// if (uploadedFile is not null)
// {
// using var stream = uploadedFile.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
// using var ms = new MemoryStream();
// await stream.CopyToAsync(ms);
// newTour.ImageBytes = ms.ToArray();
// newTour.ImageFileName = uploadedFile.Name;
// }
// TourInfo tourToCreate = new TourInfo();
// tourToCreate.TransferDestinationId = newTour.TransferDestinationId;
// tourToCreate.FancyDescription = newTour.Description;
// tourToCreate.Title = newTour.Name;
// tourToCreate.CoverImageUrl = "";
// if (tourToCreate.TransferDestinationId == Guid.Empty)
// {
// throw new InvalidOperationException("Transfer Destination must be selected.");
// }
// await TourService.CreateAsync(tourToCreate, uploadedFile);
// newTour = new();
// uploadedFile = null;
// UploadStatus = string.Empty;
// Tours = (await TourService.GetAllAsync()).ToList();
// }
private async Task HandleSubmit()
{
if (uploadedFile is not null)
{
using var stream = uploadedFile.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
using var ms = new MemoryStream();
await stream.CopyToAsync(ms);
newTour.ImageBytes = ms.ToArray();
newTour.ImageFileName = uploadedFile.Name;
}
if (IsEditing && EditingTourId.HasValue)
{
var updatedTour = new TourInfo
{
Id = EditingTourId.Value,
TransferDestinationId = newTour.TransferDestinationId,
Title = newTour.Name,
FancyDescription = newTour.Description,
CoverImageUrl = "" // assume your service handles image URL logic
};
await TourService.UpdateAsync(updatedTour, uploadedFile);
}
else
{
var tourToCreate = new TourInfo
{
TransferDestinationId = newTour.TransferDestinationId,
Title = newTour.Name,
FancyDescription = newTour.Description,
CoverImageUrl = newTour.ImageFileName
};
await TourService.CreateAsync(tourToCreate, uploadedFile);
}
// Reset form
newTour = new TourFormModel();
uploadedFile = null;
IsEditing = false;
EditingTourId = null;
UploadStatus = string.Empty;
Tours = (await TourService.GetAllAsync()).ToList();
}
private void HandleFileChange(InputFileChangeEventArgs e)
{
uploadedFile = e.File;
UploadStatus = $"Selected: {uploadedFile.Name}";
}
private async Task DeleteTourAsync(Guid id)
{
await TourService.DeleteAsync(id);
Tours = (await TourService.GetAllAsync()).ToList();
}
public class TourFormModel
{
[Required]
public Guid TransferDestinationId { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Tour name cannot exceed 100 characters.")]
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public byte[]? ImageBytes { get; set; }
public string? ImageFileName { get; set; }
}
}