using HMI.Core.Util;
|
using HMI.UI.DpUtil;
|
using HMI.UI.Util;
|
using Panuon.WPF.UI;
|
using Plugin.ServoManage.Entity;
|
using System;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.Linq;
|
|
namespace Plugin.ServoManage
|
{
|
/// <summary>
|
/// ModalWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class ServoProductModal : WindowX
|
{
|
public ServoProductModal()
|
{
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
|
public ServoProductData Data { get; set; }
|
private ObservableCollection<ServoProductData> Datasource;
|
|
private bool _isProcessing;
|
|
protected override void OnClosing(CancelEventArgs e)
|
{
|
//Press Continue Button
|
if (DialogResult == true)
|
{
|
if (_isProcessing)
|
{
|
e.Cancel = true;
|
return;
|
}
|
var okButton = WindowXModalDialog.GetOKButton(this);
|
try
|
{
|
_isProcessing = true;
|
ButtonHelper.SetIsPending(okButton, true);
|
if (!ValidateForm())
|
{
|
e.Cancel = true;
|
return;
|
}
|
|
SaveOrUpdateData();
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error(this, $"Error!{ex.Message}");
|
e.Cancel = true;
|
return;
|
}
|
finally
|
{
|
_isProcessing = false;
|
ButtonHelper.SetIsPending(okButton, false);
|
}
|
}
|
base.OnClosing(e);
|
}
|
|
private bool ValidateModel(object value, out string err)
|
{
|
err = null;
|
var data = Datasource?.FirstOrDefault(d => d.Model == value.ToString());
|
if (data != null && data != Data)
|
{
|
err = string.Format(LangUtil.Get("App.MustBeUnique"), LangUtil.Get("svo.product.model"));
|
return false;
|
}
|
return true;
|
}
|
|
private bool ValidateProgramCode(object value, out string err)
|
{
|
err = null;
|
var data = Datasource?.FirstOrDefault(d => d.ProgramCode == value.ToString());
|
if (data != null && data != Data)
|
{
|
err = string.Format(LangUtil.Get("App.MustBeUnique"), LangUtil.Get("svo.product.programCode"));
|
return false;
|
}
|
return true;
|
}
|
|
private bool ValidateForm()
|
{
|
bool flag = FormGroupHelper.ValidateForm(formPanel);
|
if (flag)
|
{
|
Data.Model = TbModel.Text;
|
Data.ProgramCode = NiProgramCode.Value?.ToString();
|
}
|
|
return flag;
|
}
|
|
public bool? Open(ServoProductData data, ObservableCollection<ServoProductData> datasource)
|
{
|
Datasource = datasource ?? new ObservableCollection<ServoProductData>();
|
Data = data;
|
TbModel.Text = Data.Model;
|
NiProgramCode.Value = int.Parse(Data.ProgramCode ?? "0");
|
return this.ShowDialog();
|
}
|
|
private void SaveOrUpdateData()
|
{
|
int index = Datasource.IndexOf(Data);
|
if (index < 0)
|
{
|
InsertData(Data);
|
}
|
else
|
{
|
UpdateData(index, Data);
|
}
|
}
|
|
public void InsertData(ServoProductData data)
|
{
|
Datasource.Add(data);
|
XmlHelper.SerializeToXml(ServoManage.ProductFilePath, Datasource);
|
}
|
|
public void UpdateData(int index, ServoProductData data)
|
{
|
var temp = XmlHelper.DeepClone(Datasource);
|
temp[index] = data;
|
XmlHelper.SerializeToXml(ServoManage.ProductFilePath, Datasource);
|
}
|
}
|
}
|