Imports System.Data.SqlClient
Imports System.IO

Public Class Vehicles
    Inherits System.Web.UI.Page

    Property contractId As Integer

    Private Sub Page_Load() Handles Me.Load
        Try

            contractId = Logic.Conversion.ToInteger(Request.Params("cid"))
            Master.CurrentClient.VerifyContractAccess(contractId)

            Dim perms = Master.CurrentClient.Permissions
            If Not perms.Contains("View Vehicles Page") Then Throw New HttpException(403, "Forbidden")
            Dim showSchedules = perms.Contains("Edit Maintenance Schedules")
            Dim showRecords = perms.Contains("Edit Vehicle Service Items")

            phTabs1.Visible = showSchedules Or showRecords
            phTabs2.Visible = showSchedules Or showRecords
            phTabs3.Visible = showSchedules Or showRecords
            phTabs4.Visible = showSchedules Or showRecords
            phTabs5.Visible = showSchedules Or showRecords
            phSchedulesButton.Visible = showSchedules
            phSchedulesTab.Visible = showSchedules
            phRecordsButton.Visible = showRecords
            phRecordsTab.Visible = showRecords

            dsVehicles.ConnectionString = Logic.Database.ConnectionString
            dsVehicles.SelectParameters("contractId").DefaultValue = contractId
            dsRecords.ConnectionString = Logic.Database.ConnectionString
            dsRecords.SelectParameters("contractId").DefaultValue = contractId
            dsSchedules.ConnectionString = Logic.Database.ConnectionString
            dsSchedules.SelectParameters("contractId").DefaultValue = contractId

            If Not Page.IsPostBack Then
                Master.SetParentPage("Vehicles.aspx?cid=" & contractId)
            End If

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


    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
        ' Do NOT call MyBase.VerifyRenderingInServerForm
    End Sub

    Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
        Response.Clear()
        Response.Buffer = True
        Response.ClearContent()
        Response.ClearHeaders()
        Response.Charset = ""
        Dim FileName As String = "VehicleList_" & contractId & ".xls"
        Dim strwritter As New StringWriter
        Dim HTMLTextWrtter As New HtmlTextWriter(strwritter)
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName)
        gvVehicles.GridLines = GridLines.Both
        gvVehicles.HeaderStyle.Font.Bold = True
        gvVehicles.RenderControl(HTMLTextWrtter)
        Response.Write(strwritter.ToString())
        Response.End()
    End Sub


    Private Sub btnAddVehicle_Click() Handles btnAddVehicle.Click
        Response.Redirect("EditVehicle.aspx?cid=" & contractId)
    End Sub

    Private Sub btnAddRecord_Click() Handles btnAddRecord.Click
        Response.Redirect("EditServiceItem.aspx?cid=" & contractId)
    End Sub

    Private Sub btnAddSchedule_Click() Handles btnAddSchedule.Click
        Response.Redirect("EditSchedule.aspx?cid=" & contractId)
    End Sub

    Private Sub gvVehicles_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvVehicles.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            Dim url = String.Format("EditVehicle.aspx?id={0}&cid={1}", data("id"), data("contractId"))
            e.Row.Attributes.Add("data-href", url)
        End If
    End Sub

    Private Sub gvSchedules_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvSchedules.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            Dim url = String.Format("EditSchedule.aspx?id={0}&cid={1}", data("id"), data("contractId"))
            e.Row.Attributes.Add("data-href", url)
        End If
    End Sub

    Private Sub gvRecords_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvRecords.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As DataRowView = e.Row.DataItem
            Dim url = String.Format("EditServiceItem.aspx?id={0}&cid={1}", data("id"), data("contractId"))
            e.Row.Attributes.Add("data-href", url)
        End If
    End Sub

End Class