Files
mapo-core/MP.SPEC/Components/RecipeMan.razor.cs
T
2023-02-09 11:28:12 +01:00

154 lines
4.2 KiB
C#

using Microsoft.AspNetCore.Components;
using MP.Data;
using MP.Data.MgModels;
using MP.SPEC.Data;
using Newtonsoft.Json;
namespace MP.SPEC.Components
{
public partial class RecipeMan
{
#region Public Properties
[Parameter]
public EventCallback<bool> CancelEvent { get; set; }
[Parameter]
public int IdxPODL { get; set; } = 0;
[Parameter]
public string RecipePath { get; set; } = "";
#endregion Public Properties
#region Protected Fields
protected bool isLoading = false;
#endregion Protected Fields
#region Protected Properties
protected RecipeModel? CurrRecipe { get; set; } = null;
protected RecipeModel? OrigRecipe { get; set; } = null;
[Inject]
protected MpDataService MDService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected async Task CancelHeadData()
{
await ReloadData();
await CancelEvent.InvokeAsync(true);
}
protected Dictionary<string, string> GetListByType(string DictType)
{
Dictionary<string, string> answ = new Dictionary<string, string>();
// inn primis il "selezionare"
answ.Add("", "--- Selezionare ---");
// cerco tipo in enums...
if (CurrRecipe != null && CurrRecipe.HeadConf.EnumVal.ContainsKey(DictType))
{
foreach (var item in CurrRecipe.HeadConf.EnumVal[DictType])
{
answ.Add(item.Key, item.Value);
}
}
return answ;
}
protected override async Task OnParametersSetAsync()
{
await ReloadData();
}
protected async Task SaveHeadData()
{
if (CurrRecipe != null)
{
await MDService.RecipeSetByPODL(CurrRecipe);
await ReloadData();
}
}
#endregion Protected Methods
#region Private Fields
private bool showHead = true;
private Dictionary<string, bool> showRows = new Dictionary<string, bool>();
#endregion Private Fields
#region Private Methods
private async Task ReloadData()
{
await Task.Delay(1);
isLoading = true;
if (IdxPODL != 0 && !string.IsNullOrEmpty(RecipePath))
{
CurrRecipe = new RecipeModel();
// effettua ricerca ricetta su MongoDb
CurrRecipe = await MDService.RecipeGetByPODL(IdxPODL);
OrigRecipe = await MDService.RecipeGetByPODL(IdxPODL);
// se non trova crea nuova...
if (CurrRecipe == null)
{
CurrRecipe = MDService.InitRecipe(IdxPODL, RecipePath);
}
// sistemo lista bool x righe
showRows = new Dictionary<string, bool>();
for (int i = 1; i <= CurrRecipe.RowsVal.Count; i++)
{
showRows.Add($"{i}", false);
}
}
await Task.Delay(1);
isLoading = false;
}
private bool needSave
{
get
{
bool answ = false;
if (CurrRecipe != null && OrigRecipe != null)
{
answ = !(CurrRecipe.HeadVal.SequenceEqual(OrigRecipe.HeadVal));
if (!answ)
{
foreach (var item in CurrRecipe.RowsVal)
{
answ = !(item.Value.SequenceEqual(OrigRecipe.RowsVal[item.Key]));
if (answ)
{
break;
}
}
}
}
return answ;
}
}
private void toggleHead()
{
showHead = !showHead;
}
private async Task toggleRow(string rowNum)
{
showRows[rowNum] = !showRows[rowNum];
await Task.Delay(1);
}
#endregion Private Methods
}
}