Files
webdoorcreator/WebDoorCreator.UI/Components/Hardware/HwSingleInstance.razor.cs
T
zaccaria.majid 9a144a95ff prova fix bottone
2023-05-02 09:20:20 +02:00

283 lines
8.1 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using WebDoorCreator.Data.DbModels;
using WebDoorCreator.Data.Services;
using WebDoorCreator.UI.Data;
namespace WebDoorCreator.UI.Components.Hardware
{
public partial class HwSingleInstance
{
#region Public Properties
[Parameter]
public DoorOpModel DoorOpInst { get; set; } = null!;
[Parameter]
public EventCallback<DoorOpModel> E_recordUpdate { get; set; }
[Parameter]
public string HwCode { get; set; } = "";
[Parameter]
public int instanceN { get; set; } = 0;
[Parameter]
public string Lingua { get; set; } = "EN";
[CascadingParameter]
public string userId { get; set; } = "";
#endregion Public Properties
#region Public Methods
#endregion Public Methods
#region Protected Properties
protected Dictionary<string, string> _currVal { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Array dati
/// </summary>
protected Dictionary<string, string> currVal
{
get => _currVal;
set
{
if (value != null)
{
_currVal = value;
}
}
}
protected string folderName
{
get => currVal != null ? tryGetCurrVal("Folder") : "";
set
{
if (currVal["Folder"] != value)
{
currVal["Folder"] = value;
var pUpd = Task.Run(async () =>
{
templateName = "";
await SetSelectedData();
});
}
}
}
protected Dictionary<string, List<string>> graphicParams { get; set; } = null!;
protected bool isLoading { get; set; } = false;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected List<ListValuesModel>? listValuesFiles { get; set; } = null!;
protected List<ListValuesModel>? listValuesFname { get; set; } = null!;
[Inject]
protected QueueDataService QDataServ { get; set; } = null!;
protected string templateName
{
get => currVal != null ? tryGetCurrVal("template") : "";
set
{
if (currVal["template"] != value)
{
currVal["template"] = value;
DoorOpInst.userConfirm = "";
DoorOpInst.DtConfirm = null;
if (!string.IsNullOrEmpty(value))
{
var pUpd = Task.Run(async () =>
{
// aggiorno direttamente i graphics parameters...
var newData = await WDCService.DoorOpGetUpdatedVals(DoorOpInst.ObjectId, currVal, graphicParams);
currVal = newData.Item1;
graphicParams = newData.Item2;
await SetSelectedData();
await E_recordUpdate.InvokeAsync(DoorOpInst);
});
}
}
}
}
[Inject]
protected WebDoorCreatorService WDCService { get; set; } = null!;
[Inject]
protected WDCVocabularyService WDVService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected List<string> comboSet(string pKey)
{
List<string> answ = new List<string>();
if (graphicParams.ContainsKey(pKey))
{
answ = graphicParams[pKey].ToList();
}
return answ;
}
protected async Task doCancel()
{
await FullReloadData();
}
protected async Task doClone()
{
var doorOpToAdd = DoorOpInst.ObjClone(userId);
List<DoorOpModel> doorOpAddList = new List<DoorOpModel>() { doorOpToAdd };
// salvo!
await WDCService.DoorOpInsert(DoorOpInst.DoorId, doorOpAddList);
await FullReloadData();
}
protected async Task doConfirm()
{
DoorOpInst.userConfirm = userId;
DoorOpInst.DtConfirm = DateTime.Now;
await WDCService.DoorOpUpdate(DoorOpInst);
// salvo i NUOVI valori...
DoorOpInst.CurrVals = currVal;
DoorOpInst.GraphicParams = graphicParams;
// salvo!
await doSave();
}
protected async Task doDeleteInst()
{
var done = await JSRuntime.InvokeAsync<bool>("confirm", "Delete PERMANENTLY the current record?");
if (done)
{
await WDCService.DoorOpDelete(DoorOpInst);
await FullReloadData();
}
}
protected async Task doSave()
{
// salvo i NUOVI valori...
DoorOpInst.CurrVals = currVal;
DoorOpInst.GraphicParams = graphicParams;
// salvo!
await WDCService.DoorOpUpdate(DoorOpInst);
// invio msg dato "salvabile"
// rileggo
await FullReloadData();
}
protected bool isChangedVal()
{
bool answ = !DoorOpInst.JsoncActValEquals(currVal);
if (answ)
{
DoorOpInst.JsoncActVal = JsonConvert.SerializeObject(currVal);
Task.Run(async () =>
{
await E_recordUpdate.InvokeAsync(DoorOpInst);
});
}
return answ;
}
protected bool isCombo(string pKey)
{
bool answ = false;
if (graphicParams.ContainsKey(pKey))
{
answ = graphicParams[pKey].Count > 1;
}
return answ;
}
protected override async Task OnParametersSetAsync()
{
await FullReloadData();
}
protected string translate(string lemma)
{
string answ = "";
answ = WDVService.Traduci(Lingua, lemma);
return answ;
}
protected string tryGetCurrVal(string key)
{
string answ = "";
if (currVal.ContainsKey(key))
{
answ = currVal[key];
}
return answ;
}
#endregion Protected Methods
#region Private Methods
private async Task FullReloadData()
{
currVal = DoorOpInst.CurrVals;
graphicParams = DoorOpInst.GraphicParams;
await SetSelectedData();
}
private async Task SetSelectedData()
{
isLoading = true;
await Task.Delay(1);
// fix folder
listValuesFiles = null;
var tempListVal = await WDCService.ListValuesGetAll(HwCode, "Folder");
if (tempListVal != null)
{
listValuesFname = tempListVal;
}
if (string.IsNullOrEmpty(folderName))
{
if (listValuesFname != null)
{
folderName = listValuesFname.FirstOrDefault()?.Label!;
}
}
// fix template
var tempListFiles = await WDCService.ListValuesGetAll(HwCode, "template");
if (tempListFiles != null)
{
listValuesFiles = tempListFiles.Where(x => x.Value.Contains(folderName)).ToList();
}
if (string.IsNullOrEmpty(templateName))
{
if (listValuesFiles != null && listValuesFiles.Count > 0)
{
templateName = listValuesFiles.FirstOrDefault()?.Label!;
}
}
isLoading = false;
await InvokeAsync(StateHasChanged);
}
#endregion Private Methods
}
}