using HMI.Core.Entity;
using HMI.Core.Util;
using HMI.UI.DpUtil;
using HMI.UI.Util;
using Microsoft.Win32;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Controls;
namespace HMI.ParameterManage
{
///
/// ModalWindow.xaml 的交互逻辑
///
public partial class SettingModal : WindowX
{
public SettingModal()
{
this.DataContext = this;
InitializeComponent();
}
public ParameterData Data { get; set; }
private ObservableCollection 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;
}
SaveOrUpdateParameter(Data);
}
catch (Exception ex)
{
ToastHelper.Error(this, $"Error: {ex.Message}");
e.Cancel = true;
return;
}
finally
{
_isProcessing = false;
ButtonHelper.SetIsPending(okButton, false);
}
}
base.OnClosing(e);
}
public bool ValidateName(object value, out string err)
{
err = null;
var data = Datasource?.FirstOrDefault(e => e.Name == value.ToString());
if (data != null && data != Data)
{
err = this.lang("App.MustBeUnique", this.lang("Pm.Parameter.Name"));
return false;
}
return true;
}
private void SaveOrUpdateParameter(ParameterData data)
{
}
private bool ValidateForm()
{
bool flag = FormGroupHelper.ValidateForm(formPanel, new Dictionary {
{ "FgValue",GetTargetValue()}
});
if (flag)
{
Data.Name = TbName.Text;
Data.Type = CbType.Text;
Data.Value = GetTargetValue();
Data.Unit = TbUnit.Text;
Data.Description = TbDescription.Text;
Data.PGroup = TbGroup.Text;
Data.Source = TbSource.Text;
}
return flag;
}
public bool? Open(ParameterData data, ObservableCollection datasource, List groups)
{
Datasource = datasource;
Data = data;
TbGroup.ItemsSource = groups;
TbName.Text = Data.Name;
CbType.SelectedValue = Data.Type;
UpdateWindow(Data.Type);
UpdateForm(Data);
TbUnit.Text = Data.Unit;
TbDescription.Text = Data.Description;
TbGroup.Text = Data.PGroup;
TbSource.Text = Data.Source;
return this.ShowDialog();
}
private string GetTargetValue()
{
if (CbType.Text == "Int" || Data.Type == "Double")
{
return NiNumberValue.Text;
}
else if (CbType.Text == "Bool")
{
return CbBoolValue.Text;
}
else
{
return TbStringValue.Text;
}
}
private void UpdateForm(ParameterData data)
{
if (Data.Type == "Int" || Data.Type == "Int")
{
NiNumberValue.Text = Data.Value;
}
else if (Data.Type == "Bool")
{
CbBoolValue.SelectedValue = Data.Value;
}
else
{
TbStringValue.Text = Data.Value;
}
}
private void UpdateWindow(string type)
{
if (type == "Int" || type == "Double")
{
NiNumberValue.Visibility = System.Windows.Visibility.Visible;
StringValueGrid.Visibility = System.Windows.Visibility.Collapsed;
CbBoolValue.Visibility = System.Windows.Visibility.Collapsed;
}
else if (type == "Bool")
{
NiNumberValue.Visibility = System.Windows.Visibility.Collapsed;
StringValueGrid.Visibility = System.Windows.Visibility.Collapsed;
CbBoolValue.Visibility = System.Windows.Visibility.Visible;
}
else
{
NiNumberValue.Visibility = System.Windows.Visibility.Collapsed;
StringValueGrid.Visibility = System.Windows.Visibility.Visible;
CbBoolValue.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void CbType_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string val = null;
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
val = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
}
UpdateWindow(val);
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
{
TbStringValue.Text = ofd.FileName;
}
}
}
}