using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digioz;

public partial class controls_admin_NewsPublishing : System.Web.UI.UserControl
{    
    public bool Edit
    {
        set { ViewState["Edit"] = value; }
        get
        {
            if (ViewState["Edit"] == null)
                return false;
            else
                return (bool)ViewState["Edit"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
            if (MultiView1.ActiveViewIndex == 1)
            {
                PopulateGridView();
            }
       
            FreeTextBox1.Visible = true;
            txtNewsSubject.Visible = true;
            lblNewsDescription.Visible = true;
            lblNewsSubject.Visible = true;
            btnPublish.Visible = true;
            lblOutput.Text = " ";       
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
        if (MultiView1.ActiveViewIndex == 1)
        {
            PopulateGridView();
        }

        if (MultiView1.ActiveViewIndex == 0 && Edit == true)
        {
            txtNewsSubject.Text = "";
            FreeTextBox1.Text = "";
            Edit = false;
            btnPublish.Text = "Publish";
        }
    }

    protected void btnPublish_Click(object sender, EventArgs e)
    {
        string coSaveStatus = "";
        coSaveStatus = NewsManagement.SaveNews(Session["username"].ToString(), Server.UrlEncode(FreeTextBox1.Text), txtNewsSubject.Text, FreeTextBox1.Text, ActiveEditArticle.Value, Edit);
        if (Edit == true)
        {
            FreeTextBox1.Visible = false;
            txtNewsSubject.Visible = false;
            lblNewsDescription.Visible = false;
            lblNewsSubject.Visible = false;
            btnPublish.Visible = false;
            if ((coSaveStatus == "allok:") || (coSaveStatus == ""))
            {
                lblOutput.Text = "The article has been successfully updated to the database!";
                lblOutput.ForeColor = System.Drawing.Color.Green;

                //clear controls
                txtNewsSubject.Text = "";
                FreeTextBox1.Text = "";
                txtNewsSubject.Focus();

            }
            else
            {
                lblOutput.Text = "The article has NOT been updated to the database. Please retry this operation later. The error reported is : " + coSaveStatus.Substring(5);
                lblOutput.ForeColor = System.Drawing.Color.Red;
            }
            lblOutput.Font.Bold = true;
            Edit = false;
            FreeTextBox1.Text = string.Empty;
            txtNewsSubject.Text = string.Empty;
        }
        else
        {
            if ((coSaveStatus == "allok:") || (coSaveStatus == ""))
            {
                lblOutput.Text = "Operation succeeded !";
                lblOutput.ForeColor = System.Drawing.Color.Green;

                //clear controls
                txtNewsSubject.Text = "";
                FreeTextBox1.Text = "";
                txtNewsSubject.Focus();
            }
            else
            {
                lblOutput.Text = "Operation failed. Please retry later. The error reported is : " + coSaveStatus.Substring(5);
                lblOutput.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
	
    protected void GridViewDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF'");
            // when mouse leaves the row, change the bg color to its original value   
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
        }
    }

    protected void GridViewDisplay_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        this.GridViewDisplay.PageIndex = e.NewPageIndex;
        this.GridViewDisplay.DataBind();
    }

    protected void GridViewDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this article  ?" + "')");
        }
    }
    protected void GridViewDisplay_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int _articleId = Convert.ToInt32(GridViewDisplay.DataKeys[e.RowIndex].Value);
        
        //delete article
        ActiveEditArticle.Value = _articleId.ToString();
        NewsManagement.DeleteNews(_articleId);
        PopulateGridView();
    }
 
    protected void PopulateGridView()
    {
        NewsList loNewsList = NewsManagement.GetNewsList();
        GridViewDisplay.DataSource = loNewsList;
        GridViewDisplay.DataBind();   
    }

    protected void GridViewDisplay_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btnChange")
        {
            int _newsId;
            _newsId = Convert.ToInt32(e.CommandArgument) ;
            MultiView1.ActiveViewIndex = 0;
            ActiveEditArticle.Value = _newsId.ToString();

            string[] lsNewsData = NewsManagement.GetNews(_newsId);
            txtNewsSubject.Text = lsNewsData[0];
            FreeTextBox1.Text = Server.UrlDecode(lsNewsData[1]);
            btnPublish.Text = "Update ";
            Edit = true;
        }
    }
}
