using System;
using System.Collections.Generic;
using System.Globalization;
namespace HRM.BO
{
///
/// What <app-virtual-grid> sends to a page endpoint: the page's own filters plus the grid's columns,
/// scroll position, search text and sort. The same class is used by every page, so no page needs a
/// request class of its own. See HRM.DA/DA/VirtualPaging/README.md.
///
public class VirtualPageRequest
{
/// The page's filters, e.g. { fromDate: "2025-01-01", toDate: "2025-12-31" }. Read them with GetDate / GetInt / GetString.
public Dictionary Filters { get; set; } = new Dictionary();
/// The grid's columns. Only these are selected, searched and sortable.
public List Columns { get; set; } = new List();
public int Skip { get; set; }
/// Rows wanted. 0 = all rows (used by Export to Excel).
public int Take { get; set; } = 100;
public string Search { get; set; }
public string SortField { get; set; }
/// "asc" or "desc".
public string SortDir { get; set; }
///
/// True when the totals must be counted (Preview, new search). Scrolling and sorting do not change
/// the totals, so they send false and the COUNT is skipped.
///
public bool CountTotal { get; set; }
///
/// True for Export to Excel: every row matching the search, in the grid's sort, comes back as an .xlsx
/// file (VirtualPageResult.ExcelFile) instead of JSON.
///
public bool ExportExcel { get; set; }
/// A filter as text, or null when it was not sent.
public string GetString(string name)
{
string value;
if (Filters != null && Filters.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value))
return value.Trim();
return null;
}
/// A "yyyy-MM-dd" filter. Throws a message fit for the user when it is missing or invalid.
public DateTime GetDate(string name, string label = null)
{
DateTime value;
if (!DateTime.TryParseExact(GetString(name), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
return value;
}
///
/// A comma-separated list of whole numbers (e.g. selected employee IDs "12,15,40"), without duplicates.
/// Throws a message fit for the user when it is missing, empty or holds anything but numbers.
///
public List GetIntList(string name, string label = null)
{
var values = new List();
var seen = new HashSet();
foreach (string part in (GetString(name) ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
int value;
if (!int.TryParse(part.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
if (seen.Add(value))
values.Add(value);
}
if (values.Count == 0)
throw new ArgumentException("Please select " + (label ?? name) + ".");
return values;
}
/// A whole-number filter. Throws a message fit for the user when it is missing or invalid.
public int GetInt(string name, string label = null)
{
int value;
if (!int.TryParse(GetString(name), NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
throw new ArgumentException("Please select a valid " + (label ?? name) + ".");
return value;
}
}
/// One grid column: Field = a column alias of the page's query; Type = how it is shown.
public class VirtualPageColumn
{
public string Field { get; set; }
/// "text" (default), "number", "money", "date" or "datetime".
public string Type { get; set; }
/// Header text in the Excel file.
public string Title { get; set; }
/// Column width in the grid (px); the Excel column gets a matching width.
public int Width { get; set; }
}
/// One window of rows for the grid.
public class VirtualPageResult
{
/// Rows matching the search (all rows when there is no search). Null when not counted (CountTotal = false).
public int? Total { get; set; }
/// All rows, ignoring the search. Null when not counted.
public int? TotalWithoutSearch { get; set; }
/// The rows of this window, holding only the requested columns.
public List> Data { get; set; } = new List>();
/// The .xlsx file, set only for ExportExcel requests. The controller returns it as a file.
public byte[] ExcelFile { get; set; }
public const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
}