Imports System.Data.SqlClient
Imports System.Threading

Public Class EditSchedule
    Inherits System.Web.UI.Page

    Property contractId As Integer
    Property scheduleId As Integer

    Private Sub Page_Load() Handles Me.Load
        Try

            scheduleId = Logic.Conversion.ToInteger(Request.Params("id"))
            contractId = Logic.Conversion.ToInteger(Request.Params("cid"))

            ' Load contract id
            If scheduleId > 0 Then
                Using connection As New SqlConnection(Logic.Database.connectionString)
                    connection.Open()
                    Dim cmd As New SqlCommand("SELECT contractId FROM MaintenanceSchedules WHERE id=@scheduleId", connection)
                    cmd.Parameters.AddWithValue("scheduleId", scheduleId)
                    contractId = cmd.ExecuteScalar()
                End Using
            End If

            ' Validate params
            If contractId = 0 Then
                Throw New Exception(String.Format("Invalid schedule id ({0}), or contract id ({1}).", scheduleId, contractId))
            End If

            ' Ensure we have permission
            Master.CurrentClient.VerifyContractAccess(contractId)
            Dim perms = Master.CurrentClient.Permissions
            If Not perms.Contains("Edit Maintenance Schedules") Then Throw New HttpException(403, "Forbidden")

            ' Fill in data
            If Not Page.IsPostBack Then
                Master.SetParentPage("Vehicles.aspx?cid=" & contractId)
                _FillData(scheduleId)
            End If

        Catch ex As ThreadAbortException
            Throw
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub btnCancel_Click() Handles btnCancel.Click
        Response.Redirect("Vehicles.aspx?cid=" & contractId)
    End Sub

    Private Sub btnSave_Click() Handles btnSave.Click
        If Not Page.IsValid Then
            Exit Sub
        End If
        Try
            Dim schedule = If(scheduleId, New Logic.MaintenanceSchedule(scheduleId), New Logic.MaintenanceSchedule)
            _GetData(schedule)
            schedule.Save("[Web: " & Master.CurrentClient.username & "]")
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
        Response.Redirect("Vehicles.aspx?cid=" & contractId)
    End Sub

    Private Sub _FillData(itemId As Integer)
        With If(scheduleId, New Logic.MaintenanceSchedule(scheduleId), New Logic.MaintenanceSchedule)
            txtLabel.Text = .label
            txtDescription.Text = .description
            chkMileage.Checked = .mileageSchedule.HasValue
            txtMileage.Text = If(.mileageSchedule.HasValue, .mileageSchedule.Value, "")
            chkDays.Checked = .daySchedule.HasValue
            txtDays.Text = If(.daySchedule.HasValue, .daySchedule.Value, "")
        End With
    End Sub

    Private Sub _GetData(schedule As Logic.MaintenanceSchedule)
        With schedule
            .contractId = contractId
            .label = txtLabel.Text
            .description = txtDescription.Text
            .mileageSchedule = Logic.Conversion.ToInteger(txtMileage.Text)
            .daySchedule = Logic.Conversion.ToInteger(txtDays.Text)
            If Not chkMileage.Checked Then .mileageSchedule = Nothing
            If Not chkDays.Checked Then .daySchedule = Nothing
        End With
    End Sub

End Class