using
System;
using
System.Collections.Generic;
using
System.Collections;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
DHTMLX.Scheduler;
using
DHTMLX.Scheduler.Controls;
using
DHTMLX.Scheduler.Data;
using
DHTMLX.Common;
using
Rental.Models;
namespace
Rental.Controllers
{
public
class
HomeController : Controller
{
public
ActionResult Index(FormState state)
{
var
scheduler =
new
DHXScheduler(
this
);
scheduler.Extensions.Add(SchedulerExtensions.Extension.Collision);
scheduler.Extensions.Add(SchedulerExtensions.Extension.Minical);
scheduler.BeforeInit.Add(
"def_template();"
);
scheduler.Config.time_step = 60;
scheduler.XY.bar_height = 76;
scheduler.InitialValues.Add(
"text"
,
""
);
if
(_ParseDate(state.DateFrom, state.TimeFrom) !=
default
(DateTime))
{
scheduler.InitialDate = _ParseDate(state.DateFrom, state.TimeFrom);
}
var
context =
new
RentalDataContext();
var
cars = _SelectCars(context, state);
if
(cars.Count() == 0)
{
ViewData[
"Message"
] =
"Nothing was found on your request"
;
cars = _SelectCars(context);
}
var
printableList = cars.Select(c =>
new
{ key = c.id, label = c.Brand, price = c.Price, link = c.Photo });
_ConfigureLightbox(scheduler, printableList);
_ConfigureViews(scheduler, printableList);
_UpdateViewData(scheduler, context, state);
scheduler.PreventCache();
scheduler.LoadData =
true
;
scheduler.EnableDataprocessor =
true
;
var
model =
new
ViewModel();
model.Scheduler = scheduler;
model.CategoryCount = cars.Count();
return
View(model);
}
protected
void
_UpdateViewData(DHXScheduler scheduler, RentalDataContext context, FormState state)
{
ViewData[
"Price"
] = _CreatePriceSelect(scheduler, state.Price);
ViewData[
"Type"
] = _CreateTypeSelect(context.Types, state.Type);
ViewData[
"DateFrom"
] = state.DateFrom;
ViewData[
"DateTo"
] = state.DateTo;
ViewData[
"TimeFrom"
] = _CreateTimeSelect(scheduler, state.TimeFrom);
ViewData[
"TimeTo"
] = _CreateTimeSelect(scheduler, state.TimeTo);
ViewData[
"DateFilter"
] = state.DateFilter;
}
private
List<SelectListItem> _CreateTypeSelect(IEnumerable<Models.Type> types,
string
selected)
{
var
typesList =
new
List<SelectListItem>()
{
new
SelectListItem(){Value =
""
, Text =
"Any"
}
};
foreach
(
var
type
in
types)
{
var
item =
new
SelectListItem() { Value = type.id.ToString(), Text =
string
.Format(
"{0}: {1} cars"
, type.title, type.Cars.Count) };
if
(item.Value == selected)
item.Selected =
true
;
typesList.Add(item);
}
return
typesList;
}
private
List<SelectListItem> _CreatePriceSelect(DHXScheduler scheduler,
string
selected)
{
var
priceRanges =
new
string
[] {
"50-80"
,
"80-120"
,
"120-150"
};
var
prices =
new
List<SelectListItem>(){
new
SelectListItem(){Value =
""
, Text =
"Any"
}
};
foreach
(
var
pr
in
priceRanges)
{
var
item =
new
SelectListItem() { Value = pr, Text =
string
.Format(
"${0}"
, pr) };
if
(pr == selected)
item.Selected =
true
;
prices.Add(item);
}
return
prices;
}
private
List<SelectListItem> _CreateTimeSelect(DHXScheduler scheduler,
string
selected)
{
var
opts =
new
List<SelectListItem>();
for
(
var
i = scheduler.Config.first_hour; i < scheduler.Config.last_hour; i++)
{
var
value =
string
.Format(
"{0}:00"
, i < 10 ?
"0"
+ i.ToString() : i.ToString());
var
item =
new
SelectListItem() { Text = value, Value = value };
if
(value == selected)
item.Selected =
true
;
opts.Add(item);
}
return
opts;
}
protected
IQueryable<Car> _SelectCars(RentalDataContext context)
{
return
_SelectCars(context,
null
);
}
private
DateTime _ParseDate(
string
date,
string
time)
{
var
datestr =
string
.Format(
"{0} {1}"
, date, time);
DateTime result =
new
DateTime();
DateTime.TryParse(datestr, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None,
out
result);
return
result;
}
protected
IQueryable<Car> _SelectCars(RentalDataContext context, FormState state)
{
IQueryable<Car> cars =
from
car
in
context.Cars
select
car;
if
(state ==
null
)
return
cars;
string
_type = state.Type;
string
_price = state.Price;
var
_from =
default
(DateTime);
var
_to =
default
(DateTime);
if
(!
string
.IsNullOrEmpty(state.DateFrom))
{
_from = _ParseDate(state.DateFrom, state.TimeFrom);
_to = _ParseDate(state.DateTo, state.TimeTo);
if
(_from.CompareTo(
default
(DateTime)) != 0 && _to.CompareTo(
default
(DateTime)) == 0)
{
_to = _from.AddHours(1);
}
}
if
(!
string
.IsNullOrEmpty(_type))
{
int
type = context.Types.First().id;
if
(!
string
.IsNullOrEmpty(_type))
{
int
.TryParse(_type,
out
type);
}
cars = cars.Where(c => c.TypeId == type);
}
if
(!
string
.IsNullOrEmpty(_price))
{
var
price = _price.Split(
'-'
);
int
low =
int
.Parse(price[0]);
int
top =
int
.Parse(price[1]);
cars = cars.Where(c => c.Price <= top && c.Price >= low);
}
if
(state.DateFilter && _from !=
default
(DateTime) && _to !=
default
(DateTime))
{
cars =
from
car
in
cars
where
car.Orders.Count == 0
|| car.Orders.Where(o => o.end_date > _from && o.start_date < _to).Count() == 0
select
car;
}
return
cars;
}
protected
void
_ConfigureViews(DHXScheduler scheduler, IEnumerable cars)
{
var
units =
new
TimelineView(
"Orders"
,
"car_id"
);
units.X_Step = 2;
units.X_Length = 12;
units.X_Size = 12;
units.Dx = 149;
units.Dy = 76;
units.EventDy = units.Dy - 5;
units.AddOptions(cars);
units.RenderMode = TimelineView.RenderModes.Bar;
scheduler.Views.Clear();
scheduler.Views.Add(units);
scheduler.InitialView = scheduler.Views[0].Name;
}
protected
void
_ConfigureLightbox(DHXScheduler scheduler, IEnumerable cars)
{
scheduler.Lightbox.Add(
new
LightboxText(
"text"
,
"Contact details"
) { Height = 42, Focus =
true
});
scheduler.Lightbox.Add(
new
LightboxText(
"description"
,
"Note"
) { Height = 63 });
var
select
=
new
LightboxSelect(
"car_id"
,
"Car Brand"
);
scheduler.Lightbox.Add(
select
);
scheduler.Lightbox.Add(
new
LightboxText(
"pick_location"
,
"Pick up location"
) { Height = 21 });
scheduler.Lightbox.Add(
new
LightboxText(
"drop_location"
,
"Drop off location"
) { Height = 21 });
select
.AddOptions(cars);
scheduler.Lightbox.Add(
new
LightboxTime(
"time"
,
"Time period"
));
}
public
ContentResult Data()
{
return
new
SchedulerAjaxData((
new
RentalDataContext()).Orders);
}
public
ContentResult Save(
int
? id, FormCollection actionValues)
{
var
action =
new
DataAction(actionValues);
RentalDataContext data =
new
RentalDataContext();
try
{
var
changedEvent = (Order)DHXEventsHelper.Bind(
typeof
(Order), actionValues);
switch
(action.Type)
{
case
DataActionTypes.Insert:
data.Orders.InsertOnSubmit(changedEvent);
break
;
case
DataActionTypes.Delete:
changedEvent = data.Orders.SingleOrDefault(ev => ev.id == action.SourceId);
data.Orders.DeleteOnSubmit(changedEvent);
break
;
default
:
var
eventToUpdate = data.Orders.SingleOrDefault(ev => ev.id == action.SourceId);
DHXEventsHelper.Update(eventToUpdate, changedEvent,
new
List<
string
>() {
"id"
});
break
;
}
data.SubmitChanges();
action.TargetId = changedEvent.id;
}
catch
{
action.Type = DataActionTypes.Error;
}
return
(
new
AjaxSaveResponse(action));
}
}
}