using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.Wpf;
using InABox.WPF;
namespace PRSDesktop
{
///
/// Interaction logic for TreatmentWindow.xaml
///
public partial class ManufacturingTreatmentWindow : ThemableWindow
{
private readonly CoreTable _treatments;
private readonly List selected = new();
private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
public ManufacturingTreatmentWindow(CoreTable treatments)
{
_treatments = treatments;
InitializeComponent();
Packets.OnFilterRecord += Packets_OnFilterRecord;
Packets.HiddenColumns.Add(x => x.ID);
Packets.HiddenColumns.Add(x => x.Packet.SetoutLink.JobLink.ID);
Packets.ActionColumns.Add(new DynamicImageColumn(GetImage, SelectPacket));
Packets.Reconfigure(options => options.AddRange(DynamicGridOption.RecordCount));
Packets.Treatments = treatments;
}
public IEnumerable Selected
{
get
{
return _treatments != null
? _treatments.Rows.Where(row => selected.Contains(row.Get(col => col.ID)))
: new CoreRow[] { };
}
}
public Guid ProductID => (Guid)Treatments.SelectedValue;
//public String ProductName => (Treatments.SelectedItem as KeyValuePair?)?.Value ?? "";
public Guid SupplierID => (Guid)Suppliers.SelectedValue;
public string SupplierName => ((KeyValuePair)Suppliers.SelectedItem).Value;
private bool SelectPacket(CoreRow? arg)
{
if (arg == null)
{
if (selected.Any())
selected.Clear();
else
selected.AddRange(Packets.Data.Rows.Select(row => row.Get(col => col.ID)));
}
else
{
var id = arg.Get(col => col.ID);
if (selected.Contains(id))
selected.Remove(id);
else
selected.Add(id);
}
OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
!Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
return true;
}
private BitmapImage? GetImage(CoreRow? arg)
{
if (arg == null)
return tick;
var id = arg.Get(col => col.ID);
return selected.Contains(id) ? tick : null;
}
private bool Packets_OnFilterRecord(CoreRow row)
{
var id = Treatments.SelectedValue != null ? (Guid)Treatments.SelectedValue : CoreUtils.FullGuid;
return row.Get(col => col.Product.ID).Equals(id);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Packets.Refresh(true, true);
var treatsource = new Dictionary();
foreach (var row in _treatments.Rows)
treatsource[row.Get(col => col.Product.ID)] =
string.Format("{0}", row.Get(col => col.Product.Name));
Treatments.ItemsSource = treatsource;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void Treatments_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var productid = Treatments.SelectedValue != null ? (Guid)Treatments.SelectedValue : CoreUtils.FullGuid;
selected.Clear();
var treatments = _treatments.Rows.Where(row => row.Get(col => col.Product.ID).Equals(productid));
//selected.AddRange(treatments.Select(row => row.Get(col => col.ID)));
var suppdict = new Dictionary();
var _suppliers = new Client().Query(new Filter(x => x.Product.ID).IsEqualTo(productid));
foreach (var row in _suppliers.Rows)
suppdict[row.Get(x => x.SupplierLink.ID)] =
string.Format("{0}", row.Get(col => col.SupplierLink.Name));
if (!suppdict.Any())
{
var allsups = new Client().Query();
foreach (var row in allsups.Rows)
suppdict[row.Get(x => x.ID)] = string.Format("{0}", row.Get(col => col.Name));
}
Suppliers.ItemsSource = suppdict;
using (new WaitCursor())
{
var defsup = new Client()
.Query(new Filter(x => x.ID).IsEqualTo(productid), new Columns(x => x.Supplier.ID))
.Rows.FirstOrDefault();
if (defsup != null)
Suppliers.SelectedValue = defsup.Get(col => col.Supplier.ID);
}
Packets.Refresh(false, true);
OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
!Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
}
private void Suppliers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
!Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
}
}
}