using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.FileData.DatabaseModels; using MP.Prog.Data; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; namespace MP.Prog.Components { public partial class FileEditor : ComponentBase { #region Public Fields public FileModel _currItem = new FileModel(); #endregion Public Fields #region Public Properties [Parameter] public FileModel currItem { get { return _currItem = null; } set { _currItem = value; CompareRev = false; } } [Parameter] public EventCallback DataReset { get; set; } [Parameter] public EventCallback DataUpdated { get; set; } [Parameter] public List MacList { get; set; } [Parameter] public List TagList { get; set; } #endregion Public Properties private bool CompareRev = false; protected int SelRevSx { get => _selRevSx; set { if (_selRevSx != value) { _selRevSx = value; // ricarico file SX var pUpd = Task.Run(async () => { await LoadRevision(); }); pUpd.Wait(); FixTitles(); } } } protected int SelRevDx { get => _selRevDx; set { if (_selRevDx != value) { _selRevDx = value; // ricarico file DX var pUpd = Task.Run(async () => { await LoadRevision(); }); pUpd.Wait(); FixTitles(); } } } private int _selRevSx = 0; private int _selRevDx = 0; private List ListRevSx { get { List answ = new List() { 0 }; for (int i = 1; i < SelRevDx; i++) { answ.Add(i); } return answ; } } private List ListRevDx { get { List answ = new List(); for (int i = 0; i <= _currItem.Rev; i++) { answ.Add(i); } return answ; } } private async Task DoCompare() { CompareRev = !CompareRev; await LoadRevision(); FixTitles(); } private void FixTitles() { // sistemo titoli TitleSx = CompareRev ? $"Rev. {SelRevSx}" : "Archivio"; TitleDx = CompareRev ? $"Rev. {SelRevDx}" : "Attuale"; } #region Public Methods public string CurrFileContent(string fullPath) { string answ = ""; if (File.Exists(fullPath)) { answ = File.ReadAllText(fullPath); } return answ; } #endregion Public Methods #region Protected Fields protected int numDiff = 0; #endregion Protected Fields #region Protected Properties [Inject] protected FileArchDataService FDService { get; set; } [Inject] protected IJSRuntime JSRuntime { get; set; } [Inject] protected MessageService MServ { get; set; } #endregion Protected Properties #region Protected Methods protected void diffDoneHandler(int numChanges) { #if false numDiff = numChanges; #endif } #endregion Protected Methods #region Private Methods private async Task ApproveChange() { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler accettare la modifica del file selezionato generando una nuova revisione?")) return; if (_currItem != null) { await FDService.FileModApprove(_currItem, MServ.UserName); await DataUpdated.InvokeAsync(1); } else { Console.WriteLine("File null!"); } } private async Task cancelUpdate() { await DataReset.InvokeAsync(0); } private async Task deleteRecord() { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare il file selezionato??")) return; if (_currItem != null) { await FDService.FileDelete(_currItem); await DataUpdated.InvokeAsync(1); } else { Console.WriteLine("File null!"); } } /// /// Esporta da archivio a filesystem /// /// private async Task ExportArchive() { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler sovrascrivere il programma salvato in rete con la versione (inattiva) selezionata?")) return; if (_currItem != null) { await DataReset.InvokeAsync(0); await FDService.FileExport(_currItem); await DataReset.InvokeAsync(0); await FDService.UpdateMachineArchive(_currItem.IdxMacchina, 1, false, false, MServ.UserName, true); await DataUpdated.InvokeAsync(1); } else { Console.WriteLine("File null!"); } } private async Task RejectChange() { if (!await JSRuntime.InvokeAsync("confirm", "Sicuro di voler eliminare la modifica del file selezionato e sovrascrivere la versione in rete?")) return; if (_currItem != null) { await FDService.FileReject(_currItem); await DataUpdated.InvokeAsync(1); } else { Console.WriteLine("File null!"); } } private async Task saveUpdate() { if (_currItem != null) { await FDService.FileUpdate(_currItem); await DataUpdated.InvokeAsync(1); } else { Console.WriteLine("File null!"); } } private string FileContSx { get; set; } = ""; private string FileContDx { get; set; } = ""; private string TitleSx { get; set; } = "Archivio"; private string TitleDx { get; set; } = "Attuale"; protected override void OnParametersSet() { FileContSx = _currItem.FileStringContent; FileContDx = CurrFileContent(_currItem.Path); SelRevDx = _currItem.Rev; FixTitles(); } private async Task LoadRevision() { if (CompareRev) { var reqSx = await FDService.FileGetByKeyRev(_currItem.FileId, SelRevSx); var reqDx = await FDService.FileGetByKeyRev(_currItem.FileId, SelRevDx); FileContSx = reqSx.FileStringContent; FileContDx = reqDx.FileStringContent; } else { FileContSx = _currItem.FileStringContent; FileContDx = CurrFileContent(_currItem.Path); } FixTitles(); } #endregion Private Methods } }