using VberZero.Tools.StringModel;
namespace VberAdmin.Web.Models.Input;
public class VmInputDate : VmInputBase
{
public VmInputDate(string name, string displayName) : base(name, displayName, VmInputType.Date)
{
DefaultClass = "form-control form-control-sm form-control-solid ps-12 flatpickr-input";
DateType = VmDateType.Date;
//IsNoCalendar = false;
Format = "Y-m-d";
IsOnlyTime = false;
}
public VmInputDate(string name, string displayName, VmDateType dateType) : this(name, displayName)
{
DateType = dateType;
}
public VmDateType DateType { get; set; }
public bool IsOnlyTime { get; set; }
public bool EnableSeconds { get; set; }
public int ShowMonths { get; set; } = 1;
public int HourIncrement { get; set; } = 1;
public int MinuteIncrement { get; set; } = 5;
public string DefaultDate { get; protected set; }
public string DefaultHour { get; protected set; }
public string DefaultMinute { get; protected set; }
public string MinDate { get; protected set; }
public string MaxDate { get; protected set; }
public string MinTime { get; protected set; }
public string MaxTime { get; protected set; }
public string Format { get; protected set; }
///
/// 禁用的日期
/// 数组 单个日期: ["2025-01-10", "22025-01-11"] ||
/// 日期范围 [{ from: "2025-01-05", to: "2025-01-25" },{...}]
/// 函数 [function(date) { return (date.getDay() === 0 || date.getDay() === 6);} ] 返回true禁用
///
public string Disable { get; protected set; }
///
/// 启用日期(其余禁用)
///
public string Enable { get; protected set; }
public string ControlDataStr
=> $"data-date-show-months=\"{ShowMonths}\"" +
$"data-date-hour-increment=\"{HourIncrement}\"" +
$"data-date-minute-increment=\"{MinuteIncrement}\"" +
$"{(EnableSeconds ? "data-date-enable-seconds=\"true\"" : "")} " +
$"{(IsOnlyTime ? "data-date-no-Calendar=\"true\"" : "")} " +
$"{(DefaultDate.Empty() ? "" : $"data-date-default-date=\"{DefaultDate}\"")} " +
$"{(DefaultHour.Empty() ? "" : $"data-date-default-hour=\"{DefaultHour}\"")} " +
$"{(DefaultMinute.Empty() ? "" : $"data-date-default-minute=\"{DefaultMinute}\"")} " +
$"{(MinDate.Empty() ? "" : $"data-date-min-date=\"{MinDate}\"")} " +
$"{(MaxDate.Empty() ? "" : $"data-date-max-date=\"{MaxDate}\"")} " +
$"{(MinTime.Empty() ? "" : $"data-date-min-time=\"{MinTime}\"")} " +
$"{(MaxTime.Empty() ? "" : $"data-date-max-time=\"{MaxTime}\"")} " +
$"{(Disable.Empty() ? "" : $"data-date-disable=\"{Disable}\"")} " +
$"{(Enable.Empty() ? "" : $"data-date-enable=\"{Enable}\"")} " +
$"{(Format.Empty() ? "" : $"data-date-format=\"{Format}\"")} ";
public override string Other
{
get
{
string str;
switch (DateType)
{
case VmDateType.DateTime:
str = "date-time";
break;
case VmDateType.DateRange:
str = "r_date";
break;
case VmDateType.DateTimeRange:
str = "r_date-time";
break;
default:
str = "date";
break;
}
return $"{base.Other ?? ""} data-vb-date=\"{str}\" {ControlDataStr}";
}
set => _other = value;
}
public VmInputDate WithFormat(string format)
{
Format = format;
return this;
}
public VmInputDate WithDefaultDate(string date)
{
DefaultDate = date;
return this;
}
public VmInputDate WithMinDate(string date)
{
MinDate = date;
return this;
}
public VmInputDate WithMaxDate(string date)
{
MaxDate = date;
return this;
}
public VmInputDate WithShowMonths(int months)
{
ShowMonths = months < 1 ? 1 : months;
return this;
}
///
/// 禁用的日期
///
/// 数组 eg:
/// 单个日期: ["2025-01-10", "22025-01-11"] ;
/// 日期范围 [{ from: "2025-01-05", to: "2025-01-25" },{...}];
/// 函数 [function(date) { return (date.getDay() === 0 || date.getDay() === 6);} ] 返回true禁用
///
///
public VmInputDate WithDisable(string disable)
{
Disable = disable;
return this;
}
///
/// 禁用的日期
///
/// 数组 eg:
/// 单个日期: ["2025-01-10", "22025-01-11"] ;
/// 日期范围 [{ from: "2025-01-05", to: "2025-01-25" },{...}];
/// 函数 [function(date) { return (date.getDay() === 0 || date.getDay() === 6);} ] 返回true启用
///
///
public VmInputDate WithEnable(string enable)
{
Enable = enable;
return this;
}
}
public class VmInputDateTime : VmInputDate
{
public VmInputDateTime(string name, string displayName) : base(name, displayName, VmDateType.DateTime)
{
Format = "Y-m-d H:i";
}
public VmInputDateTime WithOnlyTime()
{
IsOnlyTime = true;
Format = "H:i";
return this;
}
public VmInputDateTime WithIncrement(int hour = 1, int minute = 5)
{
HourIncrement = hour;
MinuteIncrement = minute;
return this;
}
public VmInputDateTime WithSeconds()
{
EnableSeconds = true;
return this;
}
public VmInputDateTime WithDefaultTime(string hour, string minute)
{
DefaultHour = hour;
DefaultMinute = minute;
return this;
}
public VmInputDateTime WithMinTime(string time)
{
MinTime = time;
return this;
}
public VmInputDateTime WithMaxTime(string time)
{
MaxTime = time;
return this;
}
}
public class VmInputDateRange : VmInputDate
{
public VmInputDateRange(string name, string displayName) : base(name, displayName, VmDateType.DateRange)
{
DefaultClass += " vb-date-range";
}
///
/// 起始时间间隔
///
/// 单位:天
///
///
public VmInputDateRange WithInterval(int interval, DateTime? value = null)
{
var date = value ?? DateTime.Today;
DefaultDate = $"[{date.AddDays(0 - interval):yyyy-MM-dd},{date:yyyy-MM-dd}]";
return this;
}
}
public class VmInputDateTimeRange : VmInputDate
{
public VmInputDateTimeRange(string name, string displayName) : base(name, displayName, VmDateType.DateTimeRange)
{
DefaultClass += " vb-date-range";
Format = "Y-m-d H:i";
}
public VmInputDateTimeRange WithIncrement(int hour = 1, int minute = 5)
{
HourIncrement = hour;
MinuteIncrement = minute;
return this;
}
public VmInputDateTimeRange WithSeconds()
{
EnableSeconds = true;
return this;
}
public VmInputDateTimeRange WithDefaultTime(string hour, string minute)
{
DefaultHour = hour;
DefaultMinute = minute;
return this;
}
public VmInputDateTimeRange WithMinTime(string time)
{
MinTime = time;
return this;
}
public VmInputDateTimeRange WithMaxTime(string time)
{
MaxTime = time;
return this;
}
public VmInputDateTimeRange WithOnlyTime()
{
Format = "H:i";
IsOnlyTime = true;
return this;
}
///
/// 起始时间间隔
///
/// 单位:天
///
///
public VmInputDateTimeRange WithInterval(int interval, DateTime? value = null)
{
var date = value ?? DateTime.Today;
DefaultDate = $"[{date.AddDays(0 - interval):yyyy-MM-dd},{date:yyyy-MM-dd}]";
return this;
}
}