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 EC_ReqEdit { get; set; } [Parameter] public EventCallback EC_ReqUpdate { get; set; } [Parameter] public EventCallback> EC_SaveDict { get; set; } #endregion Public Properties #region Protected Methods protected override void OnParametersSet() { ReloadData(); } #endregion Protected Methods #region Private Fields private Dictionary DictAlias = new(); private List ListReports = new List(); #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 /// /// Creazione nuovo report duplicando il file template /// private async Task DoAddNew() { if (!await JSRuntime.InvokeAsync("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); } } /// /// Creazione nuovo report duplicando il file template /// private async Task DoDelete(string item) { string fullName = FileName(item); if (!await JSRuntime.InvokeAsync("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>(CurrReport.FileConfDataRaw) ?? new(); } } private async Task ReqEdit(string fullPath) { await EC_ReqEdit.InvokeAsync(fullPath); } #endregion Private Methods } }