Imports System.Data.Odbc

Public Class acarsreport
    Inherits System.Web.UI.Page

#Region " Codice generato da Progettazione Web Form "

    'Chiamata richiesta da Progettazione Web Form.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTA: la seguente dichiarazione è richiesta da Progettazione Web Form.
    'Non spostarla o rimuoverla.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: questa chiamata al metodo è richiesta da Progettazione Web Form.
        'Non modificarla nell'editor del codice.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim thePath As String 'path where save log
        thePath = MapPath("\public\logs")
        Dim logS() As String

        If Not IsPostBack Then
            'ricevo i dati del report
            Dim fuel, datareport, dest, origin, duration, pilot, filename, log, callsign
            Dim Ftime, acftreg, acfttype, distance, more, out, pax
            Dim res As DataTable

            more = Request.QueryString("more")

            'salvo tutti i dati

            Dim dt() As String
            dt = Split(Request.QueryString("date"), "-")
            Dim dt1() As String
            dt1 = Split(Request.QueryString("time"), ":")

            log = Request.QueryString("log")
            fuel = Request.QueryString("fuel")
            datareport = Request.QueryString("date")
            dest = Request.QueryString("dest")
            origin = Request.QueryString("origin")
            pilot = Request.QueryString("pilot")
            duration = Request.QueryString("duration")
            callsign = Request.QueryString("callsign")
            Ftime = Request.QueryString("time")
            acftreg = Request.QueryString("reg")
            acfttype = Request.QueryString("equipment")
            distance = Request.QueryString("distance")
            more = Request.QueryString("more")
            filename = Request.QueryString("date").Replace("/", "") & "_" & Request.QueryString("time").Replace(":", "") & "_" & callsign & ".txt"
            If isActivePilot(pilot) Then
                If more = 0 Then
                    'Save flight data
                    Dim cn As OdbcConnection
                    Dim i As Integer, z As Integer, j As Integer, tmpQ As Integer
                    Dim sqlStr As String

                    cn = GetConn()
                    sqlStr = "INSERT INTO reports (pilot_id,date,time,callsign,origin_id,destination_id,registration,equipment,duration,fuel,distance,fsacars_rep_url) " & _
                             "VALUES ('" & pilot & "','" & datareport & "','" & Ftime & "','" & callsign & "','" & origin & "','" & dest & "','" & acftreg & "','" & acfttype _
                             & "','" & duration & "'," & fuel & "," & distance & ",'" & filename & "')"

                    Dim dta As New OdbcCommand(sqlStr, cn)
                    Try
                        dta.ExecuteNonQuery()
                        out = "OK"

                        'save file
                        Dim sb As New System.IO.StreamWriter(thePath & "/" & filename, True)
                        logS = Split(log, "*")
                        For i = 0 To logS.Length - 1
                            sb.WriteLine(logS(i))
                        Next
                        sb.Close()

                    Catch ex As Exception

                        out = "NOTOK " & ex.Message

                    End Try
                    cn.Close()

                    Response.Write(out)
                    Exit Sub
                Else
                    'save file
                    Try
                        Dim sb As New System.IO.StreamWriter(thePath & "/" & filename, True)
                        logS = Split(log, "*")
                        Dim i As Integer
                        For i = 0 To logS.Length - 1
                            sb.WriteLine(logS(i))
                        Next
                        sb.Close()
                        out = "OK"
                    Catch ex As Exception
                        out = "NOTOK " & ex.Message
                    End Try

                    Response.Write(out)
                    Exit Sub
                End If
            Else
                Response.Write("PILOT " & pilot & " NOT FOUND")
                Exit Sub
            End If


        End If
    End Sub

    Private Function GetConn() As Odbc.OdbcConnection
        Dim conn As Odbc.OdbcConnection, constr As String
        Dim dbserver As String, dbuser As String, userpassword As String, dbname As String
        'Insert your db data
        dbserver = "localhost"
        dbuser = "user"
        userpassword = "userpwd"
        dbname = "nomedb"

        constr = "driver={MySQL ODBC 3.51 Driver};server=" & dbserver & ";uid=" & dbuser & ";pwd=" & userpassword & ";database=" & dbname 'MySql
        conn = New Odbc.OdbcConnection(constr)
        conn.Open()
        GetConn = conn

    End Function

    Public Function isActivePilot(ByVal pilot As String) As Boolean
        Dim cn As OdbcConnection
        Dim i As Integer, z As Integer, j As Integer, tmpQ As Integer
        Dim sqlStr As String
        Dim dt As New DataTable
        isActivePilot = False
        cn = GetConn()
        sqlStr = "select pilot_id from pilots where pilot_num='" & pilot & "'"
        Dim dta As New OdbcDataAdapter(sqlStr, cn)
        dta.Fill(dt)
        If dt.Rows.Count > 0 Then isActivePilot = True
        cn.Close()
    End Function


End Class

