Imports System.Data
Imports LYB.VB
Imports LYB.VB.CLASSES

Partial Class group2_Repertoar
    Inherits System.Web.UI.Page

    Private member As Member
    Private data As New DataUtils
    Private _lastRowCount As Integer
    Private utils As New DataUtils
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim btnLedning As LinkButton = Master.FindControl("btnLedning")
        btnLedning.Style.Add("color", "yellow")
        Dim btnMedlemmar As LinkButton = Master.FindControl("btnMedlemmar")
        btnMedlemmar.Style.Add("color", "yellow")
        Dim btnBildarkiv As LinkButton = Master.FindControl("btnBildarkiv")
        btnBildarkiv.Style.Add("color", "yellow")
        Dim btnKalender As LinkButton = Master.FindControl("btnKalender")
        btnKalender.Style.Add("color", "yellow")

        Dim btnRepertoar As LinkButton = Master.FindControl("btnRepertoar")
        btnRepertoar.Style.Add("color", "white")

        If Not Me.IsPostBack Then
            MultiView1.ActiveViewIndex = 0
            btnAktuellRepertoar.Style.Add("color", "white")

            Dim webPages As DataView = data.GetWebPagesData(2)
            lblLastChanged.Text = Resources.Resource.PageLastChanged & " " & data.FindWebPageData(webPages, "Repertoar")
            Dim ev As System.EventArgs = Nothing
            btnAktuellRepertoar_Click(btnAktuellRepertoar, ev)
            Session("searchString") = "%"
            lblCurrentPageTop.Text = "Sida 1"
            lblCurrentPageBottom.Text = "Sida 1"
        Else
            Dim a As Integer = 0
        End If
        Dim searchStringtextBox As TextBox = Me.MultiView1.FindControl("txtSearchString")
        Dim searchString As String = searchStringtextBox.Text
        If Not IsNothing(searchString) Then
            If searchString <> "" And searchString <> "%" Then
                Session("searchString") = searchString
            Else
                Session("searchString") = "%"
            End If
        Else
            Session("searchString") = "%"
        End If
        Dim searchRangeList As DropDownList = Me.MultiView1.FindControl("lstSearchRange")
        Dim searchRange As String = searchRangeList.Text

        SearchNoteLibrary(Session("searchString"), searchRange)

    End Sub

    Protected Sub btnAktuellRepertoar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAktuellRepertoar.Click
        MultiView1.ActiveViewIndex = 0
        btnAktuellRepertoar.Style.Add("color", "white")
        btnNextConcert.Style.Add("color", "yellow")
        btnNotbibliotek.Style.Add("color", "yellow")
        DisplayList("AR", "repertoarTable")
    End Sub

    Protected Sub btnNextConcert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextConcert.Click
        MultiView1.ActiveViewIndex = 1
        btnAktuellRepertoar.Style.Add("color", "yellow")
        btnNextConcert.Style.Add("color", "white")
        btnNotbibliotek.Style.Add("color", "yellow")
        DisplayList("NC", "nextConcertTable")
    End Sub

    Protected Sub btnNotbibliotek_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNotbibliotek.Click
        MultiView1.ActiveViewIndex = 2
        btnAktuellRepertoar.Style.Add("color", "yellow")
        btnNextConcert.Style.Add("color", "yellow")
        btnNotbibliotek.Style.Add("color", "white")
    End Sub

    Private Sub DisplayList(ByVal listType As String, ByVal table As String)
        member = CType(Session("member"), Member)
        Dim defaultFilePath As String = utils.GetDefaultDocumentURL()
        Dim display As New Display
        Dim data As New DataUtils
        Dim listTable As Table = Me.MultiView1.FindControl(table)
        listTable.CellSpacing = 0
        listTable.CellPadding = 5
        Dim tbrowHeading As New TableRow
        tbrowHeading.Cells.Add(display.MakeTableHeaderCell("Musikstycke", member.FontFamily, member.FontSize, 2))
        tbrowHeading.Cells.Add(display.MakeTableHeaderCell("Kompositör / Arr.", member.FontFamily, member.FontSize))
        'tbrowHeading.Cells.Add(display.MakeTableHeaderCell("Piano"))
        listTable.Rows.Add(tbrowHeading)
        Dim dw As DataView = data.GetDocuments(member.CurrentLoginGroup, listType)
        If Not IsNothing(dw) Then
            Dim savedCompositionName As String = ""
            For Each row As DataRowView In dw
                Dim tbrow As New TableRow
                'If Not row.Item("HeadingID") Is System.DBNull.Value Then
                '    Dim tbrowEmpty As New TableRow
                '    tbrowEmpty.Cells.Add(display.MakeTableCell("&nbsp;", member.FontFamily, member.FontSize, 3))
                '    listTable.Rows.Add(tbrowEmpty)
                '    tbrow.Cells.Add(display.MakeTableCell(row.Item("HeadingText").ToString, member.FontFamily, row.Item("HeadingFontSize").ToString, "bold", 3))
                'Else
                If Not row.Item("CompositionID") Is System.DBNull.Value Then
                    If row.Item("CompositionName").ToString = savedCompositionName Then
                        ' show only document icon
                        row.Item("CompositionID") = System.DBNull.Value
                        row.Item("ComposerID") = System.DBNull.Value
                    End If
                    savedCompositionName = row.Item("CompositionName").ToString
                End If
                If Not row.Item("CompositionID") Is System.DBNull.Value Then
                    tbrow.Cells.Add(display.MakeTableCell(row.Item("CompositionName").ToString, member.FontFamily, member.FontSize))
                Else
                    tbrow.Cells.Add(display.MakeTableCell("&nbsp;", member.FontFamily, member.FontSize))
                End If
                Dim documentLink As String
                Dim documentLinkAddition As String = ""
                If Not row.Item("FileName") Is System.DBNull.Value Then
                    Select Case LCase(row.Item("type").ToString)
                        Case "pdf"
                            documentLinkAddition = "pdf/"
                        Case "mp3"
                            documentLinkAddition = "mp3/"
                        Case "doc"
                            documentLinkAddition = "word/"
                        Case "wma"
                            documentLinkAddition = "wma/"
                        Case "wmv"
                            documentLinkAddition = "videos/"
                        Case "nwc"
                            documentLinkAddition = "noteworthy/"
                    End Select
                    documentLink = "<a href='" & defaultFilePath & documentLinkAddition
                    If Not row.Item("URL") Is System.DBNull.Value Then
                        If row.Item("URL").ToString().StartsWith("http://") Then
                            documentLink = "<a href='" & row.Item("URL").ToString()
                        End If
                    End If
                    documentLink = documentLink & row.Item("filename").ToString & "." & row.Item("type").ToString
                    documentLink = documentLink & "' target='_blank'><img src='../images/" & LCase(row.Item("type").ToString) & "_ico.gif' border='no' /></a>"
                Else
                    documentLink = "&nbsp;"
                End If
                tbrow.Cells.Add(display.MakeTableCell(documentLink, member.FontFamily, member.FontSize))
                Dim celltext As String
                If Not row.Item("ComposerID") Is System.DBNull.Value Then
                    If Not row.Item("CommentBeforeComposer") Is System.DBNull.Value Then
                        celltext = "&nbsp;&nbsp;&nbsp;" & row.Item("CommentBeforeComposer").ToString & "&nbsp;" & row.Item("ComposerName").ToString
                    Else
                        celltext = "&nbsp;&nbsp;&nbsp;" & row.Item("ComposerName").ToString
                    End If
                    If Not row.Item("CommentAfterComposer") Is System.DBNull.Value Then
                        celltext = celltext & ",&nbsp;" & row.Item("CommentAfterComposer").ToString
                    End If
                ElseIf Not row.Item("CommentBeforeComposer") Is System.DBNull.Value Then
                    celltext = "&nbsp;&nbsp;&nbsp;" & row.Item("CommentBeforeComposer").ToString
                Else
                    celltext = "&nbsp;"
                End If
                tbrow.Cells.Add(display.MakeTableCell(celltext, member.FontFamily, member.FontSize))
                If Session("emailAddress") = "leif.bjorkman@comhem.se" Then
                    celltext = row.Item("sortorder").ToString
                    tbrow.Cells.Add(display.MakeTableCell(celltext, member.FontFamily, member.FontSize))
                End If
                'End If
                listTable.Rows.Add(tbrow)
            Next
        End If

    End Sub

    Protected Sub grdNoteLibrary_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdNoteLibrary.PageIndexChanged
        ApplyPageNumbering()
    End Sub

    Protected Sub grdNoteLibrary_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdNoteLibrary.RowCreated
        If Session("browser") = "IE" _
            Or Session("browser") = "AppleMAC-Safari" Then
            If e.Row.RowType = DataControlRowType.Header Then
                Dim sortDirection As New Label
                sortDirection.EnableTheming = False
                sortDirection.Font.Name = "webdings"
                sortDirection.Font.Size = FontUnit.Small
                Dim sortedColumn As String = CType(Session("sortedColumn"), String)
                Dim sortedDirection As WebControls.SortDirection = CType(Session("sortDirection"), WebControls.SortDirection)
                Dim cellIndex As Integer = 0
                Select Case sortedColumn
                    Case "Musikstycke"
                        cellIndex = 0
                    Case "Kompositör / Arr"
                        cellIndex = 2
                    Case "Nyckelord"
                        cellIndex = 4
                    Case Else
                        cellIndex = 0
                End Select
                Select Case sortedDirection
                    Case WebControls.SortDirection.Ascending
                        sortDirection.Text = "5"
                    Case WebControls.SortDirection.Descending
                        sortDirection.Text = "6"
                    Case Else
                        sortDirection.Text = ""
                End Select
                e.Row.Cells(cellIndex).Controls.Add(sortDirection)
            End If
        End If
    End Sub

    Protected Sub grdNoteLibrary_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdNoteLibrary.RowDataBound
        Try
            member = CType(Session("member"), Member)
            Dim defaultFilePath As String = utils.GetDefaultDocumentURL()
            If Not e.Row.RowType = DataControlRowType.Header Then
                Dim row As GridViewRow = e.Row
                Dim documentName As String = row.Cells(5).Text.ToString()
                Dim documentType As String = row.Cells(6).Text.ToString()
                Dim URL As String = row.Cells(7).Text.ToString()
                Dim documentLink As String
                Dim documentLinkAddition As String = ""
                Select Case LCase(documentType)
                    Case "pdf"
                        documentLinkAddition = "pdf/"
                    Case "mp3"
                        documentLinkAddition = "mp3/"
                    Case "doc"
                        documentLinkAddition = "word/"
                    Case "wma"
                        documentLinkAddition = "wma/"
                    Case "wmv"
                        documentLinkAddition = "videos/"
                    Case "nwc"
                        documentLinkAddition = "noteworthy/"
                End Select
                If URL <> "&nbsp;" Then
                    documentLink = "<a href='" & URL
                Else
                    documentLink = "<a href='" & defaultFilePath & documentLinkAddition
                End If
                documentLink = documentLink & documentName & "." & documentType
                documentLink = documentLink & "' target='_blank'><img src='../images/" & documentType & "_ico.gif' height='16' width='16' border='no' /></a>"
                row.Cells(5).Text = documentLink
                row.Cells(6).Text = ""
                row.Cells(7).Text = ""
                row.Cells(0).Style.Add("width", "30em")
                row.Cells(1).Style.Add("width", "30em")
                row.Cells(2).Style.Add("width", "30em")
                row.Cells(3).Style.Add("width", "30em")
                If member.FontSize <> "" Then
                    row.Cells(0).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(1).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(2).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(3).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(4).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(5).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(6).Style.Add("font-size", member.FontSize & "em")
                    row.Cells(7).Style.Add("font-size", member.FontSize & "em")
                End If
            End If
        Catch ex As System.Exception
            Dim er As String = ex.Message
            Dim b As Integer = 0
        End Try
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    End Sub
    Protected Sub SearchNoteLibrary(ByVal searchString As String, ByVal searchRange As String)
        Dim searchStringLocal As String = ""
        If searchString <> "" And searchString <> "%" Then
            Select Case searchRange
                Case "TitleAndComposerAndKeyword"
                    searchStringLocal = "WHERE (Composition.CompositionName LIKE '%" & searchString & "%' " & _
                                        "OR Composer.ComposerName LIKE '%" & searchString & "%' " & _
                                        "OR Composition.Keywords LIKE '%" & searchString & "%') " & _
                                        "AND Composition.ComposerID <> 0 "
                Case "Title"  ' Title
                    searchStringLocal = "WHERE Composition.CompositionName LIKE '%" & searchString & "%' " & _
                                        "AND Composition.ComposerID <> 0 "
                Case "Composer"  ' Composer
                    searchStringLocal = "WHERE Composer.ComposerName LIKE '%" & searchString & "%' " & _
                                        "AND Composition.ComposerID <> 0 "
                Case "Keyword"  ' Keyword
                    searchStringLocal = "WHERE Composition.Keywords LIKE '%" & searchString & "%' " & _
                                        "AND Composition.ComposerID <> 0 "
                Case "TitleAndKeyword"  ' Title and Keyword
                    searchStringLocal = "WHERE (Composition.CompositionName LIKE '%" & searchString & "%' " & _
                                        "OR Composition.Keywords LIKE '%" & searchString & "%') " & _
                                        "AND Composition.ComposerID <> 0 "
            End Select
        Else
            searchStringLocal = "WHERE Composition.ComposerID <> 0 "
        End If
        SqlDataSource1.SelectCommand = _
"SELECT Composition.CompositionName Musikstycke, " & _
"Composition.CommentBeforeComposer [  ], " & _
"Composer.ComposerName [Kompositör / Arr], " & _
"Composition.CommentAfterComposer [   ], " & _
"Composition.Keywords Nyckelord, " & _
"DocumentPath.FileName Fil, " & _
"DocumentPath.Type [ ], " & _
"DocumentPath.URL [    ] " & _
"FROM Composition_DocumentPath_List cdl " & _
"INNER JOIN List l ON l.ListID = cdl.ListID " & _
"INNER JOIN Composition ON cdl.CompositionID = Composition.CompositionID " & _
"INNER JOIN Composer ON Composition.ComposerID = Composer.ComposerID " & _
"INNER JOIN DocumentPath ON cdl.DocumentPathID = DocumentPath.DocumentPathID " & _
searchStringLocal & _
"AND l.ListType = 'NL' " & _
"ORDER BY Composition.CompositionName"
        If searchStringLocal <> "" And searchStringLocal <> "&" Then
            lblCurrentPageTop.Text = "Sida 1"
            lblCurrentPageBottom.Text = "Sida 1"
            grdNoteLibrary.DataBind()
        Else
            grdNoteLibrary.PageSize = 10
            grdNoteLibrary.DataBind()
        End If
        ApplyPageNumbering()
    End Sub

    Protected Sub grdNoteLibrary_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdNoteLibrary.Sorting
        Session("sortedColumn") = e.SortExpression
        Session("sortDirection") = e.SortDirection
    End Sub
    Protected Sub ApplyPageNumbering()
        lblCurrentPageTop.Text = "Sida " & (grdNoteLibrary.PageIndex + 1).ToString & " (" & (grdNoteLibrary.PageCount).ToString & ")"
        lblCurrentPageBottom.Text = "Sida " & (grdNoteLibrary.PageIndex + 1).ToString & " (" & (grdNoteLibrary.PageCount).ToString & ")"

    End Sub
End Class
