Imports Telerik.Web.UI

Public Class Sluzby_Upravit
    Inherits System.Web.UI.UserControl

    Public cTitle As Admin.Title
    Public sStrankaID As String = "300"
    Public sID As String  'ID zaznamu

    Public sAdresaSave As String = gadrSluzby_upravit
    Public sAdresaStorno As String = gadrSluzby

    Public sTable As String = "[sluzby].[list]"

    Public bShowLocalization As Boolean = True

    Private sFotoCestaRelativni As String = _PathFotoRelative & "/Sluzby"
    Private sFotoCestaAbsolutni As String = _PathFoto & "\Sluzby"
    Public bPovolitVerzeFotografii As Boolean = True

    ''' <summary>
    ''' Seznam zaznamu - upravit zaznam
    ''' Verze: 1.0
    ''' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        cmdSave.Attributes.Add("onclick", "ShowPopInfo('" & gstrProbihaUkladaniZaznamu & "');")

        Dim l As New PCSolution.Localize
        l.RadAsyncUpload(cmdFoto1)
        cmdFoto1.OnClientFilesUploaded = "fUploaded"
        cmdFoto1.AllowedFileExtensions = New String(1) {".jpg", ".png"}
        cmdFoto1.Width = Unit.Pixel(50)

        cTitle._Nadpis("Služby")

        sID = Request.QueryString("id")

        lblBC.Text = "<a href=""" & gadrSluzby & """>Služby</a>"

        If sID = "" Then
            lblFoto1.Visible = False
            cmdFoto1Delete.Visible = False
            chkZobrazit.Checked = True
        End If
        '@

        If sID = "0" Then sID = ""

        If sID <> "" Then
            sID = Request.QueryString("id")
            lblCaption.Text = "Služby"
            cTitle._Podnadpis("upravit")
        Else
            lblCaption.Text = "Služby"
            cTitle._Podnadpis("nový článek")
            lblBC.Text &= "➞ Nový článek"
        End If

        If IsPostBack = False Then
            Me.NacistData()
        End If

        txtNadpis.Focus()

    End Sub

#Region "  NACIST DATA  "

    Sub NacistData()

        If sID = "" Then
            ajax.ResponseScripts.Add("$('.label-localize').css('display','none');")
            Exit Sub
        End If

        Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT * FROM " & sTable & " WHERE id = '" & sID & "'")
        If DS.Rows.Count = 0 Then Exit Sub

        Dim cRow As DataRow = DS.Rows(0)

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

        chkZobrazit.Checked = cRow("zobrazit" & _Language)

        If txtNadpis.Text <> "" Then
            lblBC.Text &= "➞ " & txtNadpis.Text
        Else
            lblBC.Text &= "➞ " & cRow("nadpiscz")
        End If

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

        If IO.File.Exists(sFotoCestaAbsolutni & "\" & sID & "\Pozadi\v" & cRow("foto1verze") & "\Nahled1\" & cRow("foto1nazev") & cRow("foto1pripona")) Then
            lblFoto1.Text = "<img src=""" & sFotoCestaRelativni & "/" & sID & "/Pozadi/v" & cRow("foto1verze") & "/Nahled2/" & cRow("foto1nazev") & cRow("foto1pripona") & """ alt=""" & cRow("foto1nazev") & """ />"
            cmdFoto1.Visible = False
            lblFoto1.Visible = True
            cmdFoto1Delete.Visible = True
        Else
            lblFoto1.Visible = False
            cmdFoto1Delete.Visible = False
        End If

    End Sub

#End Region

#Region "  NAHLED FOTOGRAFIE  "

    Private Sub cmdFoto1_FileUploaded(sender As Object, e As FileUploadedEventArgs) Handles cmdFoto1.FileUploaded

        Me.UlozitZaznam()

        For n As Int16 = 0 To cmdFoto1.UploadedFiles.Count - 1

            Dim pDoc As New PCSolution.Document

            Dim sFileName As String = pDoc.OdstranitDiakritiku(IO.Path.GetFileNameWithoutExtension(cmdFoto1.UploadedFiles(n).FileName), "-")
            Dim sExt As String = IO.Path.GetExtension(cmdFoto1.UploadedFiles(n).FileName)

            '# Pokud je potreba vytvaret verze souboru, vytvorit novou cilovou slozku pro verzi
            If bPovolitVerzeFotografii = True Then
                Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT foto1verze FROM " & sTable & " WHERE id = '" & sID & "'")
                If DS.Rows.Count > 0 Then
                    Dim cRow As DataRow = DS.Rows(0)
                    If cRow("foto1verze") = 0 Then
                        cRow("foto1verze") = 1
                    Else
                        cRow("foto1verze") += 1
                    End If
                    sFotoCestaAbsolutni &= "\" & sID & "\Pozadi\v" & cRow("foto1verze")
                    sFotoCestaRelativni &= "/" & sID & "/Pozadi/v" & cRow("foto1verze")
                    DB.ExecuteCommand("UPDATE " & sTable & " SET foto1verze = " & cRow("foto1verze") & " WHERE id = '" & sID & "'")
                End If
            End If
            '@

            IO.Directory.CreateDirectory(sFotoCestaAbsolutni) 'Vytvorit slozku

            '# Nahrat fotografii a zmensit na pozadovany rozmer
            cmdFoto1.UploadedFiles(n).SaveAs(sFotoCestaAbsolutni & "\original" & sExt)

            Dim fil As New PCSolution.Images.Images
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled1", "original" & sExt, sFileName, 1920, 0, 80, sExt)
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled2", "original" & sExt, sFileName, 250, 0, 80, sExt)
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled3", "original" & sExt, sFileName, 600, 0, 80, sExt)

            DB.ExecuteCommand("UPDATE " & sTable & " SET foto1nazev = '" & sFileName & "', foto1pripona = '" & sExt & "' WHERE id = '" & sID & "'")

        Next

        Response.Redirect("/id-" & sID & sAdresaSave)

    End Sub

    Private Sub cmdFoto1Delete_Click(sender As Object, e As EventArgs) Handles cmdFoto1Delete.Click

        Me.UlozitZaznam()

        Try
            Dim p As New PCSolution.DirectoryDelete
            p.Delete(sFotoCestaAbsolutni & "\" & sID & "\Pozadi")
        Catch ex As Exception
        End Try

        DB.ExecuteCommand("UPDATE " & sTable & " SET foto1nazev = '', foto1pripona = '' WHERE id = '" & sID & "'")

        Response.Redirect("/id-" & sID & sAdresaSave)

    End Sub

#End Region

#Region "  ULOZIT  "

    Sub UlozitZaznam()

        If sID = "" Then '# Vytvorit novy zaznam

            Dim iSort As Int16 = 1
            Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT max(sort) FROM " & sTable)
            Try
                iSort = DS.Rows(0)(0) + 1
            Catch ex As Exception
            End Try
            DB.ExecuteCommand("INSERT INTO " & sTable & " (sort) VALUES (" & iSort & ")")

            DS = DB.ExecuteSelectCommand("SELECT max(id) FROM " & sTable)
            sID = DS.Rows(0)(0)

        End If

        DB.ExecuteCommand("UPDATE " & sTable & " SET " &
                           "nadpis" & _Language & " = '" & _PrevestTextNaBezpecny(txtNadpis.Text) & "', " &
                           "text" & _Language & " = '" & _PrevestTextNaBezpecny(txtPopis.Text) & "', " &
                           "zobrazit" & _Language & " = '" & chkZobrazit.Checked & "' " &
                           "WHERE id = " & sID)

    End Sub

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

        Me.UlozitZaznam()
        Response.Redirect(sAdresaStorno)

    End Sub

    Private Sub cmdStorno_Click(sender As Object, e As EventArgs) Handles cmdStorno.Click

        Response.Redirect(sAdresaStorno)

    End Sub

#End Region

End Class