using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Comal.Classes;
using InABox.Clients;
using InABox.Core;
namespace PRSDesktop
{
///
/// Interaction logic for JobDetails.xaml
///
public partial class JobDetails : UserControl, IPanel, IJobControl
{
private CoreTable Data;
public JobDetails()
{
InitializeComponent();
}
public Guid ParentID { get; set; }
public JobPanelSettings Settings { get; set; }
public event DataModelUpdateEvent OnUpdateDataModel;
public bool IsReady { get; set; }
public void CreateToolbarButtons(IPanelHost host)
{
}
public string SectionName => "Job Details";
public DataModel DataModel(Selection selection)
{
return new JobDetailsDataModel(new Filter(x => x.ID).IsEqualTo(ParentID));
}
public void Heartbeat(TimeSpan time)
{
}
public void Refresh()
{
//var cols = new Columns(col => col.Customer.Code);
new Client().Query(
new Filter(x => x.ID).IsEqualTo(ParentID),
new Columns(
col => col.JobNumber,
col => col.Customer.Code,
col => col.Customer.Name,
col => col.SiteAddress.Street,
col => col.SiteAddress.City,
col => col.SiteAddress.State,
col => col.SiteAddress.PostCode,
col => col.Account.Code,
col => col.Account.Name,
col => col.Name,
col => col.Notes,
col => col.JobStatus.ID,
col => col.ID
),
null,
(o, e) => { UpdateScreen(o); }
);
}
public Dictionary Selected()
{
var rows = Data != null ? Data.Rows.ToArray() : new CoreRow[] { };
return new Dictionary { { typeof(Job).EntityName(), rows } };
}
public void Setup()
{
UpdateScreen(null);
var statuscodes = new Dictionary { { Guid.Empty, "" } };
var statuses = new Client().Query();
foreach (var row in statuses.Rows)
statuscodes[row.Get(x => x.ID)] = row.Get(x => x.Description);
Status.ItemsSource = statuscodes;
}
public void Shutdown()
{
}
private void UpdateScreen(CoreTable o)
{
Data = o;
Dispatcher.Invoke(() =>
{
CustomerCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.Customer.Code) : "";
CustomerName.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.Customer.Name) : "";
Address.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.SiteAddress.Street) : "";
City.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.SiteAddress.City) : "";
State.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.SiteAddress.State) : "";
PostCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.SiteAddress.PostCode) : "";
AccountCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.Account.Code) : "";
AccountName.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.Account.Name) : "";
Title.Text = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.Name) : "";
var notes = o != null && o.Rows.Any() ? o?.Rows.First()?.Get(col => col.Notes) : null;
Notes.Text = notes != null ? string.Join("\n\n", notes)?.Replace("\r\n\r\n", "\r\n").Replace("\n\n", "\n") : "";
var bIsReady = IsReady;
IsReady = false;
Status.SelectedValue = o != null && o.Rows.Any() ? o.Rows.First().Get(col => col.JobStatus.ID) : Guid.Empty;
IsReady = bIsReady;
});
}
private void Status_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var job = new Job { ID = ParentID };
job.JobNumber = Data != null && Data.Rows.Any() ? Data.Rows.First().Get(col => col.JobNumber) : "";
job.JobStatus.ID = Status.SelectedValue != null ? (Guid)Status.SelectedValue : Guid.Empty;
if (IsReady)
new Client().Save(job, "Updated Job Status", (j, err) => { });
}
}
}