Imports Telerik.Web.UI

Public Class Projekty_seznam
    Inherits System.Web.UI.UserControl

    Public sAdresaUpravit As String = gadrProjekt_upravit
    Public sAdresaOdstranit As String = gadrProjekt_odstranit
    Public sAdresaSave As String = gadrProjekty_seznam

    Dim sStrankaID As String = "200"
    Dim sKategorieID As String
    Public sTable As String = "[projekty].[list]"

    Public bShowLocalization As Boolean = False

    Private sFotoCestaRelativni As String
    Private sFotoCestaAbsolutni As String

    ''' <summary>
    ''' Seznam zaznamu.
    ''' Verze: 1.0
    ''' Tabulka musi obsahovat pole - id, sort
    ''' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        cmdNovy.Attributes.Add("onclick", "ShowPopInfo('Probíhá načítání stránky...');")
        cmdSave.Attributes.Add("onclick", "ShowPopInfo('Probíhá ukládání stránky...');")

        Dim l As New PCSolution.Localize

        NacistData()
        NacistKategorie()

        LoadLink(sStrankaID, 0, txtTitle.Text, txtLink.Text, txtHorniMenuText.Text, txtDescription.Text)

    End Sub

#Region "  NACIST DATA  "

    Sub NacistData()

        Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT * FROM [projekty].[zaznam] WHERE id = " & sStrankaID)
        Dim cRow As DataRow = DS.Rows(0)

        txtNadpis.Text = cRow("nadpis" & _Language)
        txtText.Text = cRow("text" & _Language)

        '# Lokalizace
        If _Language <> "cz" Then
            lblNadpis.Text = cRow("nadpiscz")
            lblText.Text = cRow("textcz")
        Else
            ajax.ResponseScripts.Add("$('.label-localize').css('display','none');")
        End If
        '@

    End Sub

#End Region

#Region "  NACIST KATEGORIE  "

    Sub NacistKategorie()

        If Request.QueryString("id") <> "" Or Request.QueryString("id") <> "0" Then
            sKategorieID = Request.QueryString("id")
        End If

        Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT * FROM [projekty].[kategorie] ORDER BY rok DESC")
        For Each cRow As DataRow In DS.Rows

            ddKategorie.Items.Add(New RadComboBoxItem(cRow("nadpis" & _Language), cRow("id")))
            If sKategorieID = "" Then sKategorieID = cRow("id")

        Next

        If sKategorieID <> "" Then
            ddKategorie.SelectedValue = sKategorieID
        End If

        If DS.Rows.Count = 0 Then

            ddKategorie.Visible = False
            phTable.Visible = False
            cmdNovy.Visible = False
            phNoItem.Controls.Add(New LiteralControl("<div class=""noitem"">Není nalezená žádná kategorie.<br/><a style=""font-size:14px;text-decoration:underline;"" href=""" & gadrProjekty_kategorie_upravit & """>Přidat kategorii</a></div>"))

        Else

            NacistProjekty()
            Dim pSort As New PCSolution.SortItems
            pSort.InsertSorting(sTable)

        End If

    End Sub


    Private Sub ddKategorie_SelectedIndexChanged(sender As Object, e As RadComboBoxSelectedIndexChangedEventArgs) Handles ddKategorie.SelectedIndexChanged

        Response.Redirect("/id-" & e.Value & gadrProjekty_seznam)

    End Sub

#End Region

#Region "  NACIST FOTO  "

    Sub NacistProjekty()

        Dim s As String = ""

        Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT * FROM " & sTable & " WHERE rokid = " & sKategorieID & " ORDER BY sort")
        For Each cRow As DataRow In DS.Rows

            s &= "<tr id=""sort" & cRow("id") & """>"
            s &= "<td class=""item-edit first""><a href=""/zaznamid-" & sKategorieID & "-" & cRow("id") & sAdresaUpravit & """>Upravit</a></td>"
            s &= "<td class=""item-edit""><a href=""#"" onclick=""ConfirmDelete('Odstranit referenci', 'Opravdu chcete odstranit referenci?','/zaznamid-" & sKategorieID & "-" & cRow("id") & sAdresaOdstranit & "','','');return false;"">Odstranit</a></td>"

            s &= "<td class=""pointer"">"
            If cRow("zobrazit" & _Language) = "False" Then s &= "<span class=""label label-red""> </span>&nbsp;"

            If cRow("nadpis" & _Language) = "" Then cRow("nadpis" & _Language) = "<span class=""lokalizace-empty"">" & cRow("nadpiscz") & "</span>"
            If cRow("kratkytext" & _Language) = "" Then  cRow("kratkytext" & _Language) = "<span class=""lokalizace-empty"">" & cRow("kratkytextcz") & "</span>"

            s &= cRow("nadpis" & _Language) & "</td>"
            s &= "<td class=""pointer"">" & cRow("kratkytext" & _Language) & "</td>"
            s &= "</tr>"

        Next

        ph.Controls.Add(New LiteralControl(s)) : s = ""

        If DS.Rows.Count = 0 Then
            phTable.Visible = False
            phNoItem.Controls.Add(New LiteralControl("<div class=""noitem"">Není nalezená žádná reference.</div>"))
        End If

    End Sub

#End Region

#Region "  ULOZIT  "

    Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click


        DB.ExecuteCommand("UPDATE [projekty].[zaznam] SET " &
                           "nadpis" & _Language & " = '" & _PrevestTextNaBezpecny(txtNadpis.Text) & "', " &
                           "text" & _Language & " = '" & _PrevestTextNaBezpecny(txtText.Text) & "' " &
                           "WHERE id = " & sStrankaID)

        '# Ulozit informace o prohlizeci
        Dim sLink As String = SaveLink(sStrankaID, 0, txtTitle.Text, txtLink.Text, txtHorniMenuText.Text, txtDescription.Text)

        _NotifyText = "Stránka je uložená."
        Response.Redirect(sAdresaSave)

    End Sub

#End Region

#Region "  NOVY PROJEKT  "

    Private Sub cmdNovy_Click(sender As Object, e As EventArgs) Handles cmdNovy.Click

        Response.Redirect("/zaznamid-" & sKategorieID & "-0" & sAdresaUpravit)

    End Sub

#End Region

End Class