Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Class ArticleMetaEntry
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If
        If Not IsPostBack Then

            If Session("AdminID") = Nothing Then
                Response.Redirect("AdminLogin.aspx")
            End If
            tblmeta.Visible = False
            ActiveControls(False)
            fillgrid()
            gvAdmin.SelectedIndex = -1
            btnPublish.Attributes.Add("onclick", "return confirm('Are You Sure ! Want to Publish Article ? ');")
        End If

    End Sub

    Protected Sub fillgrid()
        Try
            Dim dt As DataTable
            dt = ReturnDataTable("SELECT     Article_Master.Article_Kid, Article_Master.Article_Title, Article_Master.Article_Keyword, Category_Master.Category_Name, Article_Master.Article_Status,Convert(NVARCHAR,Article_Master.Article_ApproveDate,106) AS Article_ApproveDate FROM         Article_Master INNER JOIN   Category_Master ON Article_Master.Article_CategoryId = Category_Master.Category_Kid WHERE     (Article_Master.Article_IsDeleted = '0') ORDER BY Article_Master.Article_PublishDate DESC")

            gvAdmin.DataSource = dt
            gvAdmin.DataBind()

            gvAdmin.SelectedIndex = -1
            dt.Clear()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub UpdateData(ByVal strMode As String)
        Try
            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@Article_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(gvAdmin.SelectedDataKey.Value)
            sqlcoll.Add("@Article_MetaTitle", Data.SqlDbType.NVarChar, 1000).Value = Trim(txtMetaTitle.Text)
            sqlcoll.Add("@Article_MetaKeywords", Data.SqlDbType.NVarChar, 2000).Value = Trim(txtMetakeywords.Text)
            sqlcoll.Add("@Article_MetaDescription", Data.SqlDbType.NVarChar, 2000).Value = Trim(txtMetaDescription.Text)
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.Char, 10).Value = strMode.Trim
            sqlcoll.Add("@InsertFrom", Data.SqlDbType.NVarChar, 50).Value = "ArticleMetaEntry"
            ExecQuery("ArticleContent_Proc", sqlcoll)
            sqlcoll.Clear()
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub gvAdmin_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAdmin.PageIndexChanging
        gvAdmin.PageIndex = e.NewPageIndex
        fillgrid()
        gvAdmin.SelectedIndex = -1
    End Sub

    'Protected Sub gvAdmin_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvAdmin.RowDataBound
    '    Dim dk As String
    '    Try
    '        If e.Row.RowType = DataControlRowType.DataRow Then
    '            dk = e.Row.Cells(4).Text
    '            e.Row.Cells(4).Text = Format$(Convert.ToDateTime(dk), "dd-MMM-yyyy hh:mm:ss tt")
    '        End If
    '    Catch ex As Exception
    '    End Try

    'End Sub

    Protected Sub gvAdmin_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvAdmin.SelectedIndexChanged
        DisplayData(gvAdmin.SelectedDataKey.Value)
    End Sub

    Protected Sub DisplayData(ByVal key As String)
        Try
            tblmeta.Visible = True
            DetailsView1.Visible = True
            Clearfields()

            Dim str As String = "SELECT     Article_Master.Article_Title, Article_Master.Article_Keyword, Article_Master.Article_Body, Category_Master.Category_Name, Article_Master.Article_RegDate,Article_Master.Article_MetaTitle,Article_Master.Article_MetaKeywords,Article_Master.Article_MetaDescription FROM         Article_Master INNER JOIN Category_Master ON Article_Master.Article_CategoryId = Category_Master.Category_Kid WHERE     Article_Master.Article_IsDeleted = '0' AND Article_Master.Article_Kid = '" & RemoveLiterals(key) & "'"
            Dim dt As DataTable = ReturnDataTable(str)
            DetailsView1.DataSource = dt
            DetailsView1.DataBind()
            CType(DetailsView1.Rows(0).Cells(1).FindControl("JJDiv1"), HtmlControls.HtmlGenericControl).InnerHtml = dt.Rows(0).Item("Article_Body").ToString

            Dim dk1 As String = dt.Rows(0).Item("Article_Keyword").ToString
            Dim arr As String() = Split(dk1, "-")
            Dim i As Integer
            Dim catnm As String = ""
            For i = 0 To arr.Length - 1
                catnm += ReturnValue("select Tag_Name from Tag_Master where Tag_Kid='" & RemoveLiterals(arr(i)) & "' and Tag_IsDeleted='0'") & ", "
            Next
            DetailsView1.Rows(3).Cells(1).Text = catnm.Remove(catnm.Length - 2, 2)
            DetailsView1.Rows(4).Cells(1).Text = gvAdmin.SelectedRow.Cells(4).Text

            If dt.Rows(0).Item("Article_MetaTitle").ToString <> Nothing Then
                txtMetaTitle.Text = dt.Rows(0).Item("Article_MetaTitle")
            End If
            If dt.Rows(0).Item("Article_MetaDescription").ToString <> Nothing Then
                txtMetaDescription.Text = dt.Rows(0).Item("Article_MetaDescription")
            End If

            If dt.Rows(0).Item("Article_MetaKeywords").ToString <> Nothing Then
                txtMetakeywords.Text = dt.Rows(0).Item("Article_MetaKeywords")
            End If

            If txtMetaTitle.Text <> Nothing And txtMetaDescription.Text <> Nothing And txtMetakeywords.Text <> Nothing Then
                btnSave.Visible = False
                btnPublish.Visible = True
                btnEditMetaContent.Visible = True
                btnPublishLater.Visible = True
                ActiveControls(False)
            Else
                btnSave.Visible = True
                btnPublish.Visible = False
                btnEditMetaContent.Visible = False
                btnPublishLater.Visible = False
                ActiveControls(True)
            End If

        Catch ex As Exception
        End Try
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtMetaDescription.Text = "" Or txtMetakeywords.Text = "" Or txtMetaTitle.Text = "" Then
            CreateMessageAlert(Me, "Please Enter Details", "StrKeyVal")
            Exit Sub
        End If
        UpdateData("Update")
        CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")
        ActiveControls(False)
        btnEditMetaContent.Visible = True
        btnPublish.Visible = True
        btnPublishLater.Visible = True
        btnSave.Visible = False
    End Sub

    Protected Sub Clearfields()
        txtMetaDescription.Text = ""
        txtMetakeywords.Text = ""
        txtMetaTitle.Text = ""
    End Sub
    Protected Sub ActiveControls(ByVal Action As Boolean)
        txtMetaDescription.Enabled = Action
        txtMetakeywords.Enabled = Action
        txtMetaTitle.Enabled = Action
    End Sub

    Protected Sub btnEditMetaContent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditMetaContent.Click
        '' DisplayData(gvAdmin.SelectedDataKey.Value)
        ActiveControls(True)
        btnEditMetaContent.Visible = False
        btnSave.Visible = True
        btnPublish.Visible = False
        btnPublishLater.Visible = False
    End Sub

    Protected Sub btnPublishLater_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPublishLater.Click
        Clearfields()
        tblmeta.Visible = False
        DetailsView1.DataSource = Nothing
        DetailsView1.DataBind()
    End Sub

   
    Protected Sub btnPublish_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPublish.Click
        publishArticle("Update")
        CreateMessageAlert(Me, "Article Published Successfully...", "StrKeyVal")
        tblmeta.Visible = False
        fillgrid()
        DetailsView1.DataSource = Nothing
        DetailsView1.DataBind()

    End Sub

    Protected Sub publishArticle(ByVal strMode As String)
        Try
            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@Article_Kid", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(gvAdmin.SelectedDataKey.Value)
            sqlcoll.Add("@Article_Status", SqlDbType.NVarChar, 20).Value = "Publish"
            sqlcoll.Add("@Article_PublishDate", Data.SqlDbType.DateTime).Value = DateTime.Now
            sqlcoll.Add("@Article_PublishedBy", Data.SqlDbType.NVarChar, 10).Value = RemoveLiterals(Session("g_User"))
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.Char, 10).Value = strmode.Trim
            sqlcoll.Add("@InsertFrom", Data.SqlDbType.NVarChar, 50).Value = "ArticlePublish"
            ExecQuery("ArticleContent_Proc", sqlcoll)
            sqlcoll.Clear()


        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnApprove_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        UpdateData("Update")
        DetailsView1.Visible = False
        fillgrid()
        gvAdmin.SelectedIndex = -1
        CreateMessageAlert(Me, "Article Approved Successfully...", "StrKeyVal")
    End Sub



    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        gvAdmin.SelectedIndex = -1
        DetailsView1.Visible = False
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        UpdateData("Delete")
        DetailsView1.Visible = False
        fillgrid()
        gvAdmin.SelectedIndex = -1
        CreateMessageAlert(Me, "Article Deleted Successfully...", "StrKeyVal")
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("ArticleContentSubmission.aspx?MetaKid=" & EncodeURL(RemoveLiterals(gvAdmin.SelectedDataKey.Value.ToString)) & "")
    End Sub

End Class