using GWMS.Data; using GWMS.Data.DatabaseModels; using GWMS.Data.DTO; using GWMS.UI.Data; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace GWMS.UI.Pages { public partial class WeekPlan : ComponentBase { #region Private Fields private int _endHour = 22; private int _startHour = 8; private WeekPlanModel currRecord = null; private List ListRecords; private List PlantsList; private List SuppliersList; private List TransportersList; #endregion Private Fields #region Private Properties private int _currPage { get; set; } = 1; private int _numRecord { get; set; } = 10; private UserLevel _selLevel { get; set; } = UserLevel.ND; private int _selPlantId { get; set; } = 0; private int _selSuppId { get; set; } = 0; private int _selTraspId { get; set; } = 0; private int currPage { get => _currPage; set { if (_currPage != value) { _currPage = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private int endHour { get => _endHour; set { if (_endHour != value) { _endHour = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private bool isLoading { get; set; } = false; private int numRecord { get => _numRecord; set { if (_numRecord != value) { _numRecord = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private UserLevel SelLevel { get => _selLevel; set { if (_selLevel != value) { _selLevel = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private int SelPlantId { get => _selPlantId; set { if (_selPlantId != value) { _selPlantId = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } private int SelSupplierId { get => _selSuppId; set { if (_selSuppId != value) { _selSuppId = value; checkHourRange(); } } } private int SelTransporterId { get => _selTraspId; set { if (_selTraspId != value) { _selTraspId = value; checkHourRange(); } } } private int startHour { get => _startHour; set { if (_startHour != value) { _startHour = value; var pUpd = Task.Run(async () => await ReloadData()); pUpd.Wait(); } } } #endregion Private Properties #region Protected Properties [Inject] protected MessageService AppMService { get; set; } [Inject] protected GWMSDataService DataService { get; set; } [Inject] protected IJSRuntime JSRuntime { get; set; } [Inject] protected NavigationManager NavManager { get; set; } protected int totalCount { get { int answ = 0; if (ListRecords != null) { answ = ListRecords.Count; } return answ; } } #endregion Protected Properties #region Private Methods private void checkHourRange() { int minHour = 7; int maxHour = 21; var minRecord = ListRecords.OrderBy(x => x.DeliveryHour).FirstOrDefault(); if (minRecord != null) { minHour = ListRecords.OrderBy(x => x.DeliveryHour).FirstOrDefault().DeliveryHour; _startHour = _startHour < minHour ? _startHour : minHour; } var maxRecord = ListRecords.OrderByDescending(x => x.DeliveryHour).FirstOrDefault(); if (maxRecord != null) { maxHour = ListRecords.OrderByDescending(x => x.DeliveryHour).FirstOrDefault().DeliveryHour; _endHour = _endHour > maxHour ? _endHour : maxHour; } } private async Task ReloadData() { isLoading = true; ListRecords = await DataService.WeekPlanGet(); // calcolo min/max... checkHourRange(); isLoading = false; } #endregion Private Methods #region Protected Methods protected void CreateNew() { // creo nuovo record WeekPlanModel newRecord = new WeekPlanModel() { DayNum = DayOfWeek.Sunday, DeliveryHour = 14, Note = "18K" }; currRecord = newRecord; } protected void Edit(WeekPlanModel selRecord) { currRecord = selRecord; } protected void EditRecord(int WeekPlanId) { currRecord = null; var findRecord = ListRecords .Where(x => x.WeekPlanId == WeekPlanId) .FirstOrDefault(); currRecord = findRecord; } protected override async Task OnInitializedAsync() { AppMService.ShowSearch = false; AppMService.PageName = "Planner Consegne"; AppMService.PageIcon = "fas fa-calendar pr-2"; await ReloadAllData(); } protected async Task ReloadAllData() { PlantsList = await DataService.PlantsGetAll(); SuppliersList = await DataService.SuppliersGetAll(); TransportersList = await DataService.TransportersGetAll(); await ReloadData(); } protected async Task ResetData() { DataService.rollBackEdit(currRecord); currRecord = null; await ReloadData(); } protected void Select(WeekPlanModel selRecord) { // applico filtro da selezione currRecord = selRecord; } protected List SlotList(DayOfWeek Giorno, int Ora) { List result = new(); if (ListRecords != null && ListRecords.Count > 0) { result = ListRecords .Where(x => x.DayNum == Giorno && x.DeliveryHour == Ora) .ToList(); } return result; } protected async Task UpdateData() { currRecord = null; await ReloadData(); } #endregion Protected Methods } }