Files
2026-04-28 18:08:45 +02:00

164 lines
4.8 KiB
C#

using Lux.Report.Data.DbModel;
using Lux.Report.Data.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Newtonsoft.Json;
namespace Lux.Report.Manager.Components.Compo
{
public partial class RepoFileList
{
#region Public Properties
[Parameter]
public ReportModel CurrReport { get; set; } = null!;
[Parameter]
public EventCallback<string> EC_ReqEdit { get; set; }
[Parameter]
public EventCallback<bool> EC_ReqUpdate { get; set; }
[Parameter]
public EventCallback<Dictionary<string, string>> EC_SaveDict { get; set; }
#endregion Public Properties
#region Protected Methods
protected override void OnParametersSet()
{
ReloadData();
}
#endregion Protected Methods
#region Private Fields
private Dictionary<string, string> DictAlias = new();
private List<string> ListReports = new List<string>();
#endregion Private Fields
#region Private Properties
private bool AddEnabled
{
get
{
string tplPathFrom = Path.Combine("reports", CurrReport.Name, ".template.repx");
return FService.FileExists(tplPathFrom);
}
}
[Inject]
private IFileService FService { get; set; } = null!;
[Inject]
private IJSRuntime JSRuntime { get; set; } = null!;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Creazione nuovo report duplicando il file template
/// </summary>
private async Task DoAddNew()
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler creare un nuovo modello report di tipo {CurrReport.Name} ?"))
return;
string tplPathFrom = Path.Combine("reports", CurrReport.Name, ".template.repx");
// se esiste lo duplica
if (FService.FileExists(tplPathFrom))
{
string newName = $"{CurrReport.Name}_{DateTime.Now:yyyyMMdd-HHmmss}.repx";
string tplPathTo = Path.Combine("reports", CurrReport.Name, newName);
FService.FileCopy(tplPathFrom, tplPathTo);
ReloadData();
await EC_ReqUpdate.InvokeAsync(true);
}
}
/// <summary>
/// Creazione nuovo report duplicando il file template
/// </summary>
private async Task DoDelete(string item)
{
string fullName = FileName(item);
if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Confermi di voler eliminare il report {fullName} ?"))
return;
string fullPath = Path.Combine("reports", CurrReport.Name, fullName);
FService.FileDelete(fullPath);
ReloadData();
await EC_ReqUpdate.InvokeAsync(true);
}
private string FileName(string fullName)
{
return Path.GetFileName(fullName);
}
private void SetEditAlias(string fullName)
{
aliasKey = Path.GetFileName(fullName);
aliasVal = GetAlias(aliasKey);
}
private void CancelEditAlias(string fullName)
{
aliasKey = "";
}
private string aliasKey = "";
private string aliasVal = "";
private async Task SaveEditAlias()
{
if (!string.IsNullOrEmpty(aliasKey))
{
if (DictAlias.ContainsKey(aliasKey))
{
DictAlias[aliasKey] = aliasVal;
}
else
{
DictAlias.Add(aliasKey, aliasVal);
}
aliasKey = "";
await EC_SaveDict.InvokeAsync(DictAlias);
}
}
private string GetAlias(string alias)
{
// cerco nel dizionario...
if (DictAlias.ContainsKey(alias))
{
alias = DictAlias[alias];
}
return alias;
}
private void ReloadData()
{
//base.OnParametersSet();
string repPath = Path.Combine("reports", CurrReport.Name);
// elenco file esclusi quelli nascosti (template)
ListReports = FService.ListFile(repPath, "*.repx", true);
// fix alias disponibili
if (!string.IsNullOrEmpty(CurrReport.FileConfDataRaw))
{
DictAlias = JsonConvert.DeserializeObject<Dictionary<string, string>>(CurrReport.FileConfDataRaw) ?? new();
}
}
private async Task ReqEdit(string fullPath)
{
await EC_ReqEdit.InvokeAsync(fullPath);
}
#endregion Private Methods
}
}