using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Aitex.Core.RT.Log;
using MECF.Framework.Common.Account;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;
using OpenSEMI.ClientBase.Command;
namespace MECF.Framework.UI.Client.CenterViews.Configs.Roles
{
public class RoleViewModel : BaseModel
{
#region Variables
private bool _isEnabledRoleName;
private RoleItem _treeSelectedRole;
private CtrlMode _controlMode = CtrlMode.VIEW;
#endregion
#region Constructors
public RoleViewModel()
{
DisplayName = "Role";
}
#endregion
#region Properties
///
/// 返回当前视图是否具有访问权限
///
public bool IsPermission => Permission == 3;
public bool IsEnabledRoleName
{
get => _isEnabledRoleName;
set { _isEnabledRoleName = value; NotifyOfPropertyChange(); }
}
public static RoleManager RoleManager => RoleManager.Instance;
public ObservableCollection PermissionDictionary => RolePermissionMapper.Instance.PermissionDictionary;
public ObservableCollection PermissionTrueFalseDictionary => RolePermissionTrueFalseMapper.Instance.PermissionDictionary;
public ObservableCollection RoleList { get; } = new();
public RoleItem TreeSelectedRole
{
get => _treeSelectedRole;
set
{
_treeSelectedRole = value;
NotifyOfPropertyChange();
}
}
public CtrlMode ControlMode
{
get => _controlMode;
set
{
_controlMode = value;
NotifyOfPropertyChange();
}
}
#endregion
#region Methods
protected override void OnActivate()
{
RoleList.Clear();
_treeSelectedRole = null;
RoleManager.Initialize();
LoadRoleList();
base.OnActivate();
}
protected override void OnDeactivate(bool close)
{
if (ControlMode == CtrlMode.EDIT && IsPermission)
{
if (DialogBox.Confirm("The data has been modified. Do you want to save the change(s)?"))
{
if (SaveChanges())
{
ControlMode = CtrlMode.VIEW;
DialogBox.ShowInfo("Operated successfully.");
}
}
}
base.OnDeactivate(close);
}
public void OnRoleChanged()
{
var roleNameList = new[] { "管理员", "设备工程师", "工艺工程师", "操作员" };
var roleName = roleNameList.Contains(TreeSelectedRole.DisplayRoleName) ? TreeSelectedRole.DisplayRoleName : "";
IsEnabledRoleName = true;
if (!string.IsNullOrEmpty(roleName))
IsEnabledRoleName = false;
if (ControlMode == CtrlMode.EDIT)
{
if (!string.IsNullOrEmpty(TreeSelectedRole.DisplayRoleName) && TreeSelectedRole.DisplayRoleName.Length > 18)
TreeSelectedRole.DisplayRoleName = TreeSelectedRole.DisplayRoleName.Substring(0, 18);
return;
}
//check role to set the mode from view to edit
if (_treeSelectedRole != null && _treeSelectedRole.IsRoleChanged())
ControlMode = CtrlMode.EDIT;
}
public bool OnAutoLogoutTimeChecked(object sender)
{
ControlMode = CtrlMode.EDIT;
return ((CheckBox)(sender)).IsChecked.Value;
}
private bool SaveChanges()
{
if (string.IsNullOrWhiteSpace(TreeSelectedRole.DisplayRoleName))
{
DialogBox.ShowWarning("{0} cannot be empty.", "Role name");
//TreeSelectedRole.DisplayRoleName = "No Name";
return false;
}
if (IsRoleExists(TreeSelectedRole))
{
DialogBox.ShowWarning("{0} already exists.", "Role");
return false;
}
TreeSelectedRole.RoleName = TreeSelectedRole.DisplayRoleName;
TreeSelectedRole.IsAutoLogout = TreeSelectedRole.DisplayIsAutoLogout;
TreeSelectedRole.AutoLogoutTime = TreeSelectedRole.DisplayAutoLogoutTime;
TreeSelectedRole.RoleNameTextSaved = TreeSelectedRole.TimeTextSaved = true;
TreeSelectedRole.Description = TreeSelectedRole.DisplayDescription;
try
{
RoleManager.SaveRole(TreeSelectedRole);
}
catch (Exception ex)
{
LOG.Write(ex);
return false;
}
return true;
}
private bool IsRoleExists(RoleItem role)
{
if (RoleList == null || RoleList.Count == 0)
return false;
var sameNameList = RoleList.Where(t => t.DisplayRoleName == role.DisplayRoleName);
if (sameNameList == null || sameNameList.Count() <= 1)
return false;
return true;
}
private void LoadRoleList()
{
RoleList.Clear();
var roles = RoleManager.GetAllRoles();
if (roles == null || roles.Count == 0)
return;
foreach (var ri in roles)
RoleList.Add(ri);
TreeSelectedRole = RoleList.FirstOrDefault();
TreeSelectedRole.IsSelected = true;
ControlMode = CtrlMode.VIEW;
}
private void OnRoleTreeSelectedChanged(EventCommandParameter