Files
LogisticsMgr-Mobile/LogisticsMgrMobile/LogisticsMgrMobile/Mobile/Forms/Panels/Main/SlmMainPanel_ShipFile.cs
T
2019-08-05 09:02:30 +01:00

459 lines
18 KiB
C#

using Sarand.Core.App;
using Sarand.Core.Base;
using Sarand.Core.Bus;
using Sarand.Core.Mobile.Forms.Controls;
using Sarand.Core.Mobile.Forms.Panels;
using Sarand.Core.Tasks;
using Sarand.LogisticsMgr.Core.App;
using Sarand.LogisticsMgr.Core.Bus;
using Sarand.LogisticsMgr.Core.WebService;
using Sarand.LogisticsMgr.Mobile.Core.App;
using Sarand.LogisticsMgr.Mobile.Core.Forms.Base;
using Sarand.LogisticsMgr.Mobile.Forms.Components;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Sarand.LogisticsMgr.Mobile.Forms.Panels.Main
{
//=======================================================================
public class SlmMainPanel_ShipFile : SlmMainPanel_ItemList
{
//-------------------------------------------------------------------
// Definitions.
public enum FilterProperty
{
Vessel,
}
public enum PanelMode
{
Last2Weeks
}
public new class Config : SlmMainPanel_ItemList.Config
{
public PanelMode Mode { get; set; } = PanelMode.Last2Weeks;
}
//-------------------------------------------------------------------
// Data members
private Config config;
private class Columns
{
public ColumnDfn Id { get; set; }
public ColumnDfn VesselId { get; set; }
public ColumnDfn VesselName { get; set; }
public ColumnDfn AgentId { get; set; }
public ColumnDfn AgentName { get; set; }
public ColumnDfn CustomerName { get; set; }
public ColumnDfn CustomerId { get; set; }
public ColumnDfn Berth { get; set; }
public ColumnDfn Bollard { get; set; }
public ColumnDfn Expected { get; set; }
public ColumnDfn Arrived { get; set; }
public ColumnDfn Completed { get; set; }
public ColumnDfn Departed { get; set; }
public ColumnDfn Flags { get; set; }
public ColumnDfn Status { get; set; }
}
private Columns cols = new Columns();
private String berthingDoubleClickedPrefix = "BerthingDblClick#";
//-------------------------------------------------------------------
// Construction.
public SlmMainPanel_ShipFile (Config config) :
base(config)
{
this.config = config;
// Columns.
cols.Id = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Id);
cols.VesselId = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.VesselId);
cols.VesselName = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Vessel_Name);
cols.AgentId = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.AgentId);
cols.AgentName = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Agent_Name);
cols.CustomerId = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.CustomerId);
cols.CustomerName = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Customer_Name);
cols.Berth = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Berth);
cols.Bollard = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Bollard);
cols.Expected = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Expected);
cols.Arrived = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Arrived);
cols.Completed = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Completed);
cols.Departed = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Departed);
cols.Flags = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Flags);
cols.Status = BerthingMgr.GetBerthingColumnDfn(SlmBerthing.Column.Status);
// Initialise components.
InitialiseComponents();
}
//-------------------------------------------------------------------
// Properties.
protected override String HeaderCaption { get { return "Ship File"; } }
//-------------------------------------------------------------------
// I n i t i l i a s e C o m p o n e n t s
//-------------------------------------------------------------------
/// <summary>Control initialisation on construction.</summary>
protected override void InitialiseComponents ()
{
base.InitialiseComponents();
ConfigureComponents();
}
//-------------------------------------------------------------------
// C o n f i g u r e C o m p o n e n t s
//-------------------------------------------------------------------
/// <summary>Control configuration that can be changed dynamically.</summary>
protected override void ConfigureComponents ()
{
base.ConfigureComponents();
// Initial population.
RefreshItemList();
}
//-------------------------------------------------------------------
// P a n e l A c t i o n s
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// C o n t r o l A c t i v i t y
//-------------------------------------------------------------------
protected override void SetControlActivity ()
{
}
protected override void SetControlsReadOnly ()
{
}
//-------------------------------------------------------------------
// I t e m L i s t
//-------------------------------------------------------------------
protected override List<SacsFilterPropertyType> GetFilterPropertyTypes ()
{
List<SacsFilterPropertyType> filterPropertyTypes = new List<SacsFilterPropertyType>();
filterPropertyTypes.Add(new SacsFilterPropertyType((int)FilterProperty.Vessel, "Vessel") { Mode = SacsFilterPropertyType.FilterMode.Text });
return filterPropertyTypes;
}
public SlmBerthing.Params GetBerthingParams (uint pageNo, ushort pageLength)
{
SlmBerthing.Params berthingParams = new SlmBerthing.Params();
// Filter.
berthingParams.VesselPartial = HeaderPanel.PanelFilter.GetFilterPropertyValue((int)FilterProperty.Vessel);
berthingParams.StatusCat = SlmBerthing.StatusCat.NotDeleted;
// Paging.
berthingParams.ResultsPaging.PageNo = pageNo;
berthingParams.ResultsPaging.PageLength = pageLength;
// Mode.
if (config.Mode == PanelMode.Last2Weeks)
berthingParams.ArrivedFrom = DateUtil.IncrementDate(DateTime.Today, -14);
return berthingParams;
}
private SacsTaskData taskDataRefreshList = null;
protected override void RefreshItemList (uint pageNo = 1)
{
String fn = GetType().Name + ".RefreshItemList";
// Kill previous request.
if (taskDataRefreshList != null)
{
taskDataRefreshList.TriggerCancellation();
taskDataRefreshList = null;
}
// Clear existing list.
DisplayItemList(null, "Refreshing list. Please wait...");
// Refresh the list.
RefreshItemList(GetBerthingParams(pageNo, ItemsPerPage), taskDataRefreshList = new SacsTaskData(null));
}
private void RefreshItemList (SlmBerthing.Params berthingParams, SacsTaskData taskDataRefreshList_Local)
{
String fn = GetType().Name + ".RefreshItemList";
// Query data after delay.
Device.StartTimer(TimeSpan.FromMilliseconds(250), () =>
{
if (SacsSystem.DebugMode) Logger.Debug(fn + " Timer fired.");
Task.Run(async () =>
{
try
{
// Configure request.
SlmWebServiceRequest.Config configRequest = new SlmWebServiceRequest.Config();
configRequest.DataType = SlmWebServiceRequest.DataType.Berthing;
configRequest.RequestType = SlmWebServiceRequest.RequestType.List;
configRequest.Context.SiteDefinition = SlmGlobals_Mobile.SessionProfile.SiteDefinition;
configRequest.Context.Debug = SacsSystem.DebugMode;
// Filter.
Dictionary<String, String> filter = BerthingMgr.GetBerthingQueryFilter_Mobile(berthingParams);
// Columns.
List<String> queryColumns = new List<String>()
{
cols.Id.DataName,
cols.VesselId.DataName,
cols.VesselName.DataName,
cols.AgentId.DataName,
cols.AgentName.DataName,
cols.CustomerName.DataName,
cols.CustomerId.DataName,
cols.Berth.DataName,
cols.Bollard.DataName,
cols.Expected.DataName,
cols.Arrived.DataName,
cols.Completed.DataName,
cols.Departed.DataName,
cols.Flags.DataName,
cols.Status.DataName,
};
// Request User profile.
if (SacsSystem.DebugMode) Logger.Debug(fn + " Querying data - START");
SlmWebServiceClient.ItemListResponseData responseData =
await new SlmWebServiceClient().GetItemList(configRequest, filter, queryColumns, taskDataRefreshList_Local);
if (SacsSystem.DebugMode) Logger.Debug(fn + " Querying data - END Items: " + (responseData?.Items.Count.ToString() ?? "None") + (taskDataRefreshList_Local.IsCancellationRequested ? " [Cancelled]" : ""));
// Display item list.
Device.BeginInvokeOnMainThread(() =>
{
DisplayItemList(responseData);
});
}
catch (Exception exc) { if (SacsSystem.DebugMode) Logger.Warn(fn + " " + exc.GetLogMessage(6)); }
}, taskDataRefreshList_Local.CancellationToken)
.ContinueWith(task =>
{
taskDataRefreshList_Local.Dispose();
if (taskDataRefreshList == taskDataRefreshList_Local)
taskDataRefreshList = null;
});
if (SacsSystem.DebugMode) Logger.Debug(fn + " Timer method complete.");
return false;
});
}
//-------------------------------------------------------------------
protected override void DisplayItemList (SlmWebServiceClient.ItemListResponseData responseData, SacsMessage message = null)
{
// Refresh paging properties.
if (responseData != null)
HeaderPanel.SetPaging(responseData.PageNo, responseData.PageLength, responseData.Total);
else HeaderPanel.SetPaging(0, 0, 0);
// Construct item list HTML.
string html = "<html><head>"+GetItemListStyles().Concat("\n")+GetItemListScripts().Concat("\n")+"</head><body>";
if (message != null)
{
String messageClass = "inf";
switch (message.MessageLevel)
{
case SaMessageLevel.Fatal:
case SaMessageLevel.Exception:
case SaMessageLevel.Error:
messageClass = "err";
break;
case SaMessageLevel.Warning:
messageClass = "wrn";
break;
}
html += "<div class='msg " + messageClass + "'>" + message.Message + "</div>";
}
if (responseData == null)
{ }
else if (responseData.Items.Count > 0)
{
//html += "<div class='msg inf'>Movements found: " + responseData.Items.Count + "</div>";
foreach (var item in responseData.Items)
{
html += "<div id='AssMov-" + SysId.ToString(item.GetIdValue(cols.Id.DataName)) + "' class='item' ondblclick='slm.openBerthing(" + SysId.ToString(item.GetIdValue(cols.Id.DataName)) + ")'>";
html += "<table class='fullwidth'>";
html += " <tr>";
html += " <td class='label'>Vessel</td><td class='value bold'>" + item.GetValue(cols.VesselName.DataName) + "</td>";
html += " <td class='label'>Agent</td><td class='value'>" + item.GetValue(cols.AgentName.DataName) + "</td>";
html += " </tr>";
html += " <tr>";
html += " <td class='label'>Customer</td><td class='value'>" + item.GetValue(cols.CustomerName.DataName) + "</td>";
html += " <td class='label'></td><td class='value'>" + "</td>";
html += " </tr>";
html += " <tr>";
html += " <td class='label'>Berth</td><td class='value'>" + item.GetValue(cols.Berth.DataName) + "</td>";
html += " <td class='label'>Bollard</td><td class='value'>" + item.GetValue(cols.Bollard.DataName) + "</td>";
html += " </tr>";
html += " <tr>";
html += " <td class='label'>Arrived</td><td class='value'>" + DateUtil.Format(item.GetDateTimeValue(cols.Arrived.DataName), "ddd d-MMM-yyyy") + "</td>";
SaStatus berthingStatus = SacsSystem.GetStatusForCode(item.GetValue(cols.Status.DataName));
html += " <td class='label'>Status</td><td class='value'>" + SacsSystem.GetStatusDesc(berthingStatus) + "</td>";
html += " </tr>";
html += " <tr>";
html += " <td class='label'>Completed</td><td class='value'>" + DateUtil.Format(item.GetDateTimeValue(cols.Completed.DataName), "ddd d-MMM-yyyy") + "</td>";
html += " <td class='label'>Departed</td><td class='value'>" + DateUtil.Format(item.GetDateTimeValue(cols.Departed.DataName), "ddd d-MMM-yyyy") + "</td>";
html += " </tr>";
if (SacsSystem.DebugMode)
{
html += " <tr>";
html += " <td class='label'>Id</td><td class='value'>" + SysId.ToString(item.GetIdValue(cols.Id.DataName)) + "</td>";
html += " </tr>";
}
html += "</table>";
html += "</div>";
}
}
else html += "<div class='msg inf'>No movements found</div>";
html += "</body></html>";
ItemListWebView.Html = html;
//Logger.Debug("HTML: " + html);
}
private List<String> GetItemListStyles ()
{
return new List<string>()
{
"<style>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.JQueryUiCss) + "</style>",
"<style>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.SaBaseCss) + "</style>",
"<style>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.SlmBaseCss) + "</style>",
"<style>",
"body{padding:4px;font-size:" + SlmMobile.HtmlFontSize + ";border:1px solid #808080;border-radius:8px;overflow-y:auto;}",
"div.msg{margin-bottom:4px;padding:4px;border:1px solid #C0C0C0;border-radius:4px;}",
"div.msg.inf{color:green;}",
"div.msg.wrn{color:orange;}",
"div.msg.err{color:red;}",
"td{padding:4px 4px;}",
"tr.other th, tr.other td{background-color:#f0f0f0;}",
"div.item{margin:0 0 4px 0;padding:4px;background-color:#f8f8f8;border:2px solid #808080;border-radius:8px;}",
"</style>"
};
}
private List<String> GetItemListScripts ()
{
return new List<string>()
{
"<script>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.JQueryJs) + "</script>",
"<script>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.JQueryUiJs) + "</script>",
"<script>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.SaBaseJs) + "</script>",
"<script>" + SlmResourceConsole.GetTextResource(SlmResourceConsole.TextResource.SlmBaseJs) + "</script>",
"<script>",
"slm.openBerthing=function(id)",
"{",
//" alert('slm.openAssetMovement Id: '+id)",
" document.location=\"" + SaxWebView.AppActionPrefix + berthingDoubleClickedPrefix + "\"+id;",
"}",
"$(function()",
"{",
" ",
"});",
"</script>"
};
}
//-------------------------------------------------------------------
// P a n e l E v e n t s
//-------------------------------------------------------------------
private SacsTaskData taskDataAppAction;
/// <summary>'App' navigation events on Item List web view control.</summary>
protected override void ItemListWebView_AppNavigating (object sender, SaxWebView.AppNavigatingEventArgs eventArgs)
{
String fn = GetType().Name + ".ItemListWebView_AppNavigating";
if (SacsSystem.DebugMode) Logger.Debug(fn + " AppAction: " + eventArgs.AppAction);
// Cancel previous action.
if (taskDataAppAction != null)
{
taskDataAppAction.TriggerCancellation();
taskDataAppAction = null;
}
taskDataAppAction = new SacsTaskData(null);
if (eventArgs.AppAction.StartsWithCI(berthingDoubleClickedPrefix))
{
SacsTaskData taskDataAppAction_Local = taskDataAppAction;
SysId berthingId = new SysId(StringUtil.ToInt(eventArgs.AppAction.Right(eventArgs.AppAction.Length - berthingDoubleClickedPrefix.Length)));
Task.Run(async () =>
{
try
{
await showBerthingForm(berthingId, taskDataAppAction_Local);
}
catch (Exception exc) { if (SacsSystem.DebugMode) Logger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc)); }
}, taskDataAppAction_Local.CancellationToken)
.ContinueWith(task =>
{
taskDataAppAction_Local.Dispose();
if (taskDataAppAction == taskDataAppAction_Local)
taskDataAppAction = null;
});
}
}
//-------------------------------------------------------------------
private async Task showBerthingForm (SysId berthingId, SacsTaskData taskDataAppAction_Local)
{
String fn = GetType().Name + ".ItemListWebView_AppNavigating";
if (!SysId.IsValid(berthingId))
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Ship File", "Invalid berthing selected.", "OK");
});
return;
}
// Retrieve Berthing and associated details.
SlmBerthing berthing = null;
SlmVessel vessel = null;
try
{
SacsTaskData taskData = new SacsTaskData(null) { Debug = SacsSystem.DebugMode };
// Retrieve Berthing.
SlmBerthing.Params prmBerthing = new SlmBerthing.Params();
prmBerthing.IncludedIds.Add(berthingId);
prmBerthing.StatusCat = SlmBerthing.StatusCat.Any;
// ToDo berthing = await BerthingMgr.GetBerthing_Mobile(prmBerthing, taskData);
// Retrieve Vessel.
if (berthing != null)
{
if (SysId.IsValid(berthing.VesselId))
{
SlmVessel.Params prmVessel = new SlmVessel.Params();
prmVessel.IncludedIds.Add(berthing.VesselId);
prmVessel.StatusCat = SlmVessel.StatusCat.Any;
// ToDo vessel = await BerthingMgr.GetVessel_Mobile(prmVessel, taskData);
}
}
}
catch (Exception exc) { Logger.Debug(fn + " " + exc.GetLogMessage(6)); }
if (berthing == null)
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Ship File", "Failed to retrieve Berthing data.", "OK");
});
return;
}
if (vessel == null)
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Ship File", "Failed to retrieve Vessel data.", "OK");
});
return;
}
Device.BeginInvokeOnMainThread(async () =>
{
// ToDo Configure Asset Movement page.
/*
SlmPage_ShipFile_Berthing.Config pageConfig = new SlmPage_ShipFile_Berthing.Config();
pageConfig.BerthingToUpdate = berthing;
pageConfig.Berthing_Vessel = vessel;
pageConfig.ReadOnly = true;
if (pageConfig.ReadOnly) pageConfig.NavigationMode = Core.Forms.Base.SlmFormPage.NavigationMode.Close;
SlmPage_ShipFile_Berthing assetRepairForm = new SlmPage_ShipFile_Berthing (pageConfig);
assetRepairForm.Disappearing += (sender2, e2) =>
{
};
await SlmGlobals_Mobile.Application.MainPage.Navigation.PushModalAsync(assetRepairForm, false);
*/
});
}
//-------------------------------------------------------------------
}
//=======================================================================
}