' Copyright � 2014 Annpoint, s.r.o.
'   Use of this software is subject to license terms. 
'   http://www.daypilot.org/
'
'   If you have purchased a DayPilot Pro license, you are allowed to use this 
'   code under the conditions of DayPilot Pro License Agreement:
'
'   http://www.daypilot.org/files/LicenseAgreement.pdf
'
'   Otherwise, you are allowed to use it for evaluation purposes only under 
'   the conditions of DayPilot Pro Trial License Agreement:
'   
'   http://www.daypilot.org/files/LicenseAgreementTrial.pdf
'   
'

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports DayPilot.Utils
Imports DayPilot.Web.Ui
Imports DayPilot.Web.Ui.Data
Imports DayPilot.Web.Ui.Enums
Imports DayPilot.Web.Ui.Events
Imports DayPilot.Web.Ui.Events.Scheduler
Imports BeforeEventRenderEventArgs = DayPilot.Web.Ui.Events.Calendar.BeforeEventRenderEventArgs

Partial Public Class _Default
	Inherits System.Web.UI.Page

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
		If Not IsPostBack Then
			DayPilotCalendarWeek.DataSource = GetData(DayPilotCalendarWeek.StartDate, DayPilotCalendarWeek.EndDate.AddDays(1))
			DayPilotCalendarWeek.DataBind()
		End If

	End Sub

	Protected Sub DayPilotCalendarWeek_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
		Select Case e.Command
			Case "navigate"
				DayPilotCalendarWeek.StartDate = CDate(e.Data("day"))
				DayPilotCalendarWeek.DataSource = GetData(DayPilotCalendarWeek.StartDate, DayPilotCalendarWeek.EndDate.AddDays(1))
				DayPilotCalendarWeek.DataBind()
				DayPilotCalendarWeek.Update()
			Case "refresh"
				DayPilotCalendarWeek.DataSource = GetData(DayPilotCalendarWeek.StartDate, DayPilotCalendarWeek.EndDate.AddDays(1))
				DayPilotCalendarWeek.DataBind()
				DayPilotCalendarWeek.Update()
		End Select
	End Sub


	Private Function GetData(ByVal start As Date, ByVal [end] As Date) As DataTable
		Dim da As New SqlDataAdapter("SELECT * FROM [event] WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))", ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
		da.SelectCommand.Parameters.AddWithValue("start", start)
		da.SelectCommand.Parameters.AddWithValue("end", [end])

		Dim dt As New DataTable()
		da.Fill(dt)

		Return dt
	End Function

	Protected Sub DayPilotCalendarWeek_OnBeforeEventRender(ByVal sender As Object, ByVal e As BeforeEventRenderEventArgs)
		e.Areas.Add((New Area()).Top(0).Left(10).Right(10).Height(20).CssClass("event_action_move").Move().Visibility(AreaVisibility.Visible))
	End Sub

	Protected Sub DayPilotCalendarWeek_OnEventMove(ByVal sender As Object, ByVal e As EventMoveEventArgs)
		Dim exists As DataRow = DbGetEvent(e.Value)
		If exists Is Nothing Then
			DbInsertEvent(e.NewStart, e.NewEnd, e.Text)
		Else
			DbUpdateEvent(e.Value, e.NewStart, e.NewEnd)
		End If


		DayPilotCalendarWeek.DataSource = GetData(DayPilotCalendarWeek.StartDate, DayPilotCalendarWeek.EndDate.AddDays(1))
		DayPilotCalendarWeek.DataBind()
		DayPilotCalendarWeek.Update()
	End Sub



	Private Sub DbUpdateEvent(ByVal id As String, ByVal start As Date, ByVal [end] As Date)
		Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
			con.Open()
			Dim cmd As New SqlCommand("UPDATE [event] SET eventstart = @start, eventend = @end WHERE id = @id", con)
			cmd.Parameters.AddWithValue("id", id)
			cmd.Parameters.AddWithValue("start", start)
			cmd.Parameters.AddWithValue("end", [end])
			cmd.ExecuteNonQuery()
		End Using
	End Sub

	Private Function DbGetEvent(ByVal id As String) As DataRow
		Dim da As New SqlDataAdapter("SELECT * FROM [event] WHERE id = @id", ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
		da.SelectCommand.Parameters.AddWithValue("id", id)
		Dim dt As New DataTable()
		da.Fill(dt)

		If dt.Rows.Count > 0 Then
			Return dt.Rows(0)
		End If
		Return Nothing
	End Function

	Private Sub DbInsertEvent(ByVal start As Date, ByVal [end] As Date, ByVal name As String)
		Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
			con.Open()
			Dim cmd As New SqlCommand("INSERT INTO [event] (eventstart, eventend, name) VALUES(@start, @end, @name)", con)
			'cmd.Parameters.AddWithValue("id", id);
			cmd.Parameters.AddWithValue("start", start)
			cmd.Parameters.AddWithValue("end", [end])
			cmd.Parameters.AddWithValue("name", name)
			cmd.ExecuteNonQuery()
		End Using
	End Sub
End Class
