using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CarGallery
{
    public partial class AdminManageComments : System.Web.UI.Page
    {
        DatabaseRepository repo;
        string sess;

        void getInfo()
        {
            gridView1.DataSource = repo.commentsManage(int.Parse(drdlFilter.SelectedValue),txtPostID.Text);
            gridView1.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            repo = new DatabaseRepository();

            sess = Convert.ToString(Session["username"]);

            if (sess != "")
            {
                if (!IsPostBack)
                    getInfo();

            }
            else
                Response.Redirect("~/Login.aspx");
        }

        protected void gridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                Session["comment_id"] = e.CommandArgument;
                Response.Redirect("AdminDeleteComment.aspx");
            }
            
            else if (e.CommandName == "Confirm")
                repo.commentConfirm(1, e.CommandArgument.ToString(), Session["username"].ToString());


            gridView1.DataSource = null;
            gridView1.DataBind();
            getInfo();

        }

        protected void gridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridView1.EditIndex = e.NewEditIndex;
        }

        protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView1.PageIndex = e.NewPageIndex;
            getInfo();
        }

        protected void drdnPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridView1.PageSize = int.Parse(drdnPageSize.SelectedValue);
            getInfo();
        }

        protected void btnBatchAction_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < gridView1.Rows.Count; x++)
            {
                CheckBox chkBatchAction = (CheckBox)(gridView1.Rows[x].FindControl("chkBatchAction"));
                HiddenField id = (HiddenField)gridView1.Rows[x].FindControl("hid");

                if (chkBatchAction.Checked)
                {
                    if (drdlBatchAction.SelectedValue == "0")
                    {
                        repo.commentConfirm(2, id.Value,Session["username"].ToString());
                    }
                    else if (drdlBatchAction.SelectedValue == "1")
                    {
                        repo.commentConfirm(3, id.Value, Session["username"].ToString());
                    }
                    else if (drdlBatchAction.SelectedValue == "2")
                    {
                        repo.commentDelete(id.Value);
                    }
                }
            }

            getInfo();
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            getInfo();
        }

    }
}