Files
Module.PremiumArea/Client/Modules/SZUAbsolventenverein.Module.PremiumArea/Apply.razor

147 lines
4.3 KiB
Plaintext

@using SZUAbsolventenverein.Module.PremiumArea.Services
@using SZUAbsolventenverein.Module.PremiumArea.Models
@namespace SZUAbsolventenverein.Module.PremiumArea
@inherits ModuleBase
@inject IEngineerApplicationService ApplicationService
@inject NavigationManager NavManager
@inject IStringLocalizer<Apply> Localizer
<div class="row">
<div class="col-md-12">
<h3>@Localizer["Ingenieur Antrag"]</h3>
@if (!string.IsNullOrEmpty(_message))
{
<div class="alert alert-info">@_message</div>
}
</div>
</div>
@if (_showForm)
{
<div class="card p-3">
<p>Bitte laden Sie Ihren Ingenieur-Antrag als PDF-Datei hoch.</p>
@* <div class="mb-3">
<label for="pdfUpload" class="form-label">Antrags-PDF</label>
<InputFile OnChange="@LoadFiles" class="form-control" accept=".pdf"/>
<div class="form-text">Max Größe: 20MB. Format: Nur PDF.</div>
</div> *@
<FileManager OnSelectFile="@OnSelectFile" ShowProgress="true" ShowSuccess="true"/>
<div class="mt-2">
<button class="btn btn-primary" @onclick="SubmitApplication" disabled="@(_existingApp?.FileId == 0)">
@(_existingApp.ApplicationId > 0 ? "Antrag aktualisieren" : "Antrag hochladen")
</button>
<button class="btn btn-secondary" @onclick="Cancel">Abbrechen</button>
</div>
</div>
}
else
{
@if (_existingApp.FileId > 0)
{
<div class="card">
<div class="card-header">Ihr Antrag</div>
<div class="card-body">
<p>
<strong>Status:</strong> <span class="badge bg-success">Veröffentlicht</span>
</p>
<p>
<strong>Datum:</strong> @_existingApp.CreatedOn.ToShortDateString()
</p>
<button class="btn btn-primary" @onclick="EditApp">Antrag aktualisieren</button>
</div>
</div>
}
}
@code {
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.View;
private EngineerApplication _existingApp = new EngineerApplication();
private bool _showForm = true;
private string _message = "";
protected override async Task OnInitializedAsync()
{
try
{
var apps = await ApplicationService.GetApplicationsAsync(ModuleState.ModuleId);
var userId = PageState.User?.UserId ?? -1;
_existingApp.Status = "New";
_existingApp = apps.FirstOrDefault(a => a.UserId == userId, _existingApp);
if (_existingApp.FileId > 0)
{
_showForm = false;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void EditApp()
{
_showForm = true;
}
private async Task SubmitApplication()
{
if (_existingApp == null || _existingApp.FileId == 0)
{
_message = "Bitte wählen Sie eine Datei aus.";
return;
}
try
{
var app = new EngineerApplication
{
ApplicationId = _existingApp?.ApplicationId ?? 0,
ModuleId = ModuleState.ModuleId,
UserId = PageState.User.UserId, // Ensure UserID is set
FileId = _existingApp.FileId,
Status = "Published", // Auto-publish
SubmittedOn = DateTime.UtcNow,
ApprovedOn = DateTime.UtcNow, // Auto-approved
};
if (app.ApplicationId == 0)
{
var result = await ApplicationService.AddApplicationAsync(app);
_existingApp = result;
}
else
{
await ApplicationService.UpdateApplicationAsync(app);
_existingApp = await ApplicationService.GetApplicationAsync(app.ApplicationId, ModuleState.ModuleId);
}
_showForm = false;
_message = "Antrag erfolgreich veröffentlicht.";
}
catch (Exception ex)
{
_message = "Fehler: " + ex.Message;
}
}
private void Cancel()
{
NavManager.NavigateTo(NavigateUrl());
}
private Task OnSelectFile(int fileId)
{
_existingApp.FileId = fileId;
return Task.CompletedTask;
}
}