using VberAdmin.Web.Models.Input;
using VberAdmin.Web.Models.Search;
using VberZero.Tools.StringModel;
namespace VberAdmin.Web.Models.Modals;
using VberAdmin.Web.Models.Table;
public class VmQueryModal
{
///
///
///
///
///
///
///
/// "id,name,name2,displayName"
/// "id,name" "1:name,3:displayName" "1:name|name2,3:displayName"
///
///
///
///
///
///
///
///
public VmQueryModal(string name, string url, string modalId, List queryItems, string originField,
string targetField,
string ajaxSelectNameField = "", string submitEventName = null, string itemDbClickEventName = null,
string itemClickEventName = null, bool isSingleSelect = true, VmQueryTreeSearch vmQueryTreeSearch = null,
string searchBindFun = "", int modalWidth = 900)
{
ModalName = name;
QueryUrl = url;
QueryItems = queryItems;
SearchBindFun = searchBindFun;
AjaxSelectNameField = ajaxSelectNameField;
VmQueryTreeSearch = vmQueryTreeSearch;
OriginFields = originField.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var targets = new string[OriginFields.Length];
TargetFields = new List();
IsSingleSelect = isSingleSelect;
if (string.IsNullOrEmpty(targetField))
{
for (var i = 0; i < targets.Length; i++) TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
}
else
{
var targetArr = targetField.Split(new[] { ',' }, StringSplitOptions.None);
for (var i = 0; i < targets.Length; i++)
if (i < targetArr.Length)
{
if (!string.IsNullOrEmpty(targetArr[i]))
{
var unitArr = targetArr[i].Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (unitArr.Length > 1)
{
if (int.TryParse(unitArr[0], out var index) && index < OriginFields.Length && index >= i)
{
for (; i < index; i++) TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
AddTargetField(unitArr[1], i);
}
else
{
AddTargetField(unitArr[1], i);
}
}
else
{
AddTargetField(targetArr[i], i);
}
}
else
{
TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
}
}
else
{
TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
}
}
SubmitEventName = submitEventName;
ModalId = modalId;
ModalWidth = modalWidth;
ItemClickEventName = itemClickEventName;
ItemDbClickEventName = itemDbClickEventName ?? submitEventName;
}
public string QueryUrl { get; set; }
public List QueryItems { get; set; }
public string ModalId { get; set; }
public string ModalName { get; set; }
public int ModalWidth { get; set; }
public string RowId { get; set; }
public string[] OriginFields { get; set; }
public List TargetFields { get; set; }
public string SubmitEventName { get; set; }
public string ItemClickEventName { get; set; }
public string ItemDbClickEventName { get; set; }
public VmQueryTreeSearch VmQueryTreeSearch { get; set; }
public string SearchBindFun { get; set; }
public bool IsSingleSelect { get; set; }
public bool IsMultiplePageSelect { private get; set; }
public string PageSelect => IsMultiplePageSelect ? "true" : "false";
public string SingleSelect => IsSingleSelect ? "true" : "false";
public string AjaxSelectNameField { get; set; }
private void AddTargetField(string targetStr, int index)
{
var target = targetStr.Split(new[] { '|' }, StringSplitOptions.None);
for (var i = 0; i < target.Length; i++)
target[i] = string.IsNullOrEmpty(target[i])
? $"[name='{OriginFields[index]}']"
: target[i].StartsWith("#") || target[i].StartsWith(".")
? target[i]
: $"[name='{target[i]}']";
TargetFields.Add(target);
}
public VmQueryModal WithSearchFun(string funName)
{
SearchBindFun = funName;
return this;
}
public VmQueryModal WithSearchTree(VmQueryTreeSearch searchTree)
{
VmQueryTreeSearch = searchTree;
return this;
}
public VmSearch Search
{
get
{
var list = QueryItems.Where(a => a.IsSearch).ToList();
if (list.Any())
{
var search = new List();
foreach (var item in list)
{
var s = new VmSearchItem(item.FiledName, item.DisplayName)
{
IsDisabled = item.IsDisabled,
IsHidden = item.IsHidden,
IsReadOnly = item.IsReadonly,
IsOnlyView = item.IsOnlyView,
FieldType = item.FieldType,
ExpType = item.ExpType,
};
if (item.SelectStr.NotEmpty())
{
s.WithSelect(item.SelectStr, false, item.IsSelectTree);
}
search.Add(s);
}
return new VmSearch(search, ModalId + "_search");
;
}
return null;
}
}
}
public class VmQueryItem : VmTableItem
{
public VmQueryItem(
string filedName,
string displayName,
bool isSearch = false,
bool sort = false,
int tipLength = 0,
bool isDisabled = false,
bool isHidden = false,
FType fieldType = FType.S,
EType expType = EType.Contains,
string formatter = "")
: base(filedName, displayName, formatter, sort, tipLength)
{
IsSearch = isSearch;
IsDisabled = isDisabled;
IsHidden = isHidden;
FieldType = fieldType;
ExpType = expType;
}
public bool IsSearch { get; set; }
public bool IsHidden { get; set; }
public bool IsFieldHidden { get; set; }
public bool IsOnlyView { get; set; }
public bool IsDisabled { get; set; }
public bool IsReadonly { get; set; }
public string DisabledStr => IsDisabled ? "disable" : "";
public bool IsSelectTree { get; set; }
public string SelectStr { get; set; }
public FType FieldType { get; set; }
public EType ExpType { get; set; }
//public string FiledName { get; set; }
//public string DisplayName { get; set; }
//public string Formatter { get; set; }
//public string FormatterStr => string.IsNullOrEmpty(Formatter) ? "" : "data-formatter=\"" + Formatter + "\"";
//public string ClassName { get; set; }
//public int? Width { get; set; }
//public string WidthUnit { get; set; }
//public string WidthStr => Width != null ? $" data-width=\"{Width}{WidthUnit}\"" : "";
//public bool IsSort { get; set; }
//public string Sort => IsSort ? " data-sortable=\"true\"" : "";
//private int TipLength { get; set; }
//public string Tip => TipLength > 0 ? $"data-tip=\"{TipLength}\"" : "";
public new VmQueryItem WithWidth(int width, string unit = "px")
{
Width = width;
WidthUnit = unit;
return this;
}
///
/// 超过指定字符将 被截取 用tip的方式显示
///
/// 默认 30个字符
///
public new VmQueryItem WithTip(int showLength = 30)
{
TipLength = showLength;
return this;
}
public new VmQueryItem WithSort()
{
IsSort = true;
return this;
}
public new VmQueryItem WithClassName(string className)
{
ClassName = className;
return this;
}
public new VmQueryItem WithFormatter(string formatter)
{
Formatter = formatter;
return this;
}
public VmQueryItem WithHidden()
{
IsHidden = true;
return this;
}
public VmQueryItem WithDisabled()
{
IsDisabled = true;
return this;
}
public VmQueryItem WithFieldType(FType fieldType, EType expType = EType.Contains)
{
FieldType = fieldType;
ExpType = expType;
return this;
}
public VmQueryItem WithExpType(EType expType)
{
ExpType = expType;
return this;
}
public VmQueryItem WithSelectStr(string str, bool isSelectTree = false)
{
IsSelectTree = isSelectTree;
SelectStr = str;
return this;
}
public VmQueryItem WithSelectStr(Dictionary dic, bool isSelectTree = false)
{
IsSelectTree = isSelectTree;
if (dic.ContainsKey(FiledName)) SelectStr = dic[FiledName];
return this;
}
public VmQueryItem WithOnlyView()
{
IsOnlyView = true;
return this;
}
public VmQueryItem WithFieldHidden()
{
IsFieldHidden = true;
IsSearch = true;
return this;
}
public VmQueryItem WithSearch(bool isHidden = true, bool isDisabled = false, bool isReadonly = false, bool isOnlyView = false)
{
IsSearch = true;
IsHidden = isHidden;
IsDisabled = isDisabled;
IsReadonly = isReadonly;
IsOnlyView = isOnlyView;
return this;
}
}
public class VmQueryTreeSearch
{
public string Field { get; set; }
public int FType { get; set; }
public int EType { get; set; }
public string SelectUrl { get; set; }
public string SelectFieldName { get; set; }
}