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 AdminManagePosts : System.Web.UI.Page
    {
        DatabaseRepository repo;
        string sess;

        void getInfo()
        {
            gridView1.DataSource = repo.postsManage(byte.Parse(drdlFilter.SelectedValue));
            gridView1.DataBind();
        }
        // chon to halat haye mokhtalefi list e post ha gerefte mishe
        // pas behtare ye method barash dar nazar begirim harvaqt niaz dashtim hamoom method ro seda bezanim
        // drdlFilter.SelectedValue ye meqdari miad shomare itemi ke entekhab kardi ro barmigardoone
        // masalan age 0 bashe yani ro All tanzim shode va to method e postsManage to databaserepository goftim age 0 bood
        // hame post ha ro bargardoon va .......... (to DatabaseRepository tozih dadam)


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

            sess = Convert.ToString(Session["username"]);
            // age karbar login karde bood .............................
            if (sess != "") 
            {
                if (!IsPostBack) // age page be vaseteye zadane ye dokme ya ... refresh nashode bood
                    getInfo(); // etela at ro neshoon bede

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

        protected void gridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // gridview chand ta event dare , yekish RowCommand hastesh
            // vaqti ke yeki az dokme hayi ke to GridView hast feshorde beshe
            // RowCommand amal mikone
            // miaym check mikonim bebinim kodoom dokme feshorde shode
            // har dokmeyi zade shode bashe ye CommandName mifreste

            if (e.CommandName == "Edit") // age dokmeye Edit zade shode bashe ke Command name esh ro Edit dar nazar gereftim ina ejra mishe
            {
                Session["post_id"] = e.CommandArgument; // miad Command Argument ke vasash tarif kardim ro migire va ye session barash misaze
                Response.Redirect("AdminEditPost.aspx"); // va mire be safheye EditPost , to Edit post , session e post_id ro check mikone
                // va dar soorati ke khali nabood meqdare post_id ke darvaqe ID e ye post hast ke ma dokme Edit ro roosh zadim ro mide be ye method
                // va post ro migire va namayesh mide baraye Edit
            }

                // hamintor mesle bala edame mide
            else if (e.CommandName == "Delete")
            {
                Session["post_id"] = e.CommandArgument;
                Response.Redirect("AdminDeletePost.aspx");
            }
            else if (e.CommandName == "View") // age command Name bood view
                Response.Redirect("~/ViewSinglePost.aspx?post_id=" + e.CommandArgument);
                // mire be safheye ViewSinglePost.aspx va ye parameter ham behesh mide ke ID e oon post hastesh

            else if (e.CommandName == "Slide") // age dokmeye Slide ro zade bood (hamoon check Box ha)
                repo.postsSlideAllow(e.CommandArgument.ToString()); // miad method e postsSlideAllow ro seda mizane va be meqdare voroodisham
                // id e posti ke roosh Slide ro click kardi mide

                // va hamintor inam vase comment
            else if (e.CommandName == "Comment")
                repo.postsCommentAllow(e.CommandArgument.ToString());

            // va dar akhar method e getInfo() ke bala tarif kardim ro call mikonim ta
            // baz etela at ro begire ke virayesh hayi ke emal shode namayesh dade beshe
            getInfo();

        }

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

        protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // inam qablan goftam vase raftan be safhe haye digas

            gridView1.PageIndex = e.NewPageIndex;
            getInfo();
        }

        protected void drdnPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {

            // age oon item ke vase tedade namayesh (page size) gozashtim oon bala , taqir kard
            // bia page size ro ham taqir bede

            gridView1.PageSize = int.Parse(drdnPageSize.SelectedValue);

            // va data ro begir dobare
            getInfo();
        }

        protected void btnBatchAction_Click(object sender, EventArgs e)
        {
            // ma payin e safhe ye taqirate dastei darim , mitoonim daste jami ye seri posta ro pak konim
            // ya slideshono fa al ya qeyre fa al konim , ya  commenteshoono fa al ya qeyre fa al konim

            // chon taqirat bayad yeki yeki anjam beshe ro har satr 
            // ye halqe mizarim az 0 ta tedade satr haye dataGrid

            for (int x = 0; x < gridView1.Rows.Count; x++)
            {
                // ina ro copy kardam tozihatesho yadam nis daqiqan :D
                // vali vase gereftane ye Object az Datagrid estefade mishe masalan migi
                // satre 10 , controli ke esmesh chkBatchAction hast ro bede va beriz to masalan in moteqayer

                CheckBox chkBatchAction = (CheckBox)(gridView1.Rows[x].FindControl("chkBatchAction")); // in checkBox hayi hast ke samte chap mibini
                // har kodoom az ina tik khorde bashe , taqirat roosh emal mishe

                HiddenField post_id = (HiddenField)gridView1.Rows[x].FindControl("hid"); // inam fek konam ID e record bashe yadam nis

                
                // checkBox e satr e feli ro gereftim oon bala , migim age tik khorde bood
                if (chkBatchAction.Checked)
                {
                    if (drdlBatchAction.SelectedValue == "0") // age oon menu e gozine avali entekhab shode bood yani 0 bood
                    {
                        repo.postsSlideAllow(post_id.Value, true); // bia slide e in satri ke hastimo fa al kon
                    }
                    else if (drdlBatchAction.SelectedValue == "1") // age 1 bood
                    {
                        repo.postsSlideAllow(post_id.Value, false); // slide ha ro qeyre fa al kon
                    }
                    else if (drdlBatchAction.SelectedValue == "2") // va ......
                    {
                        repo.postsCommentAllow(post_id.Value, true);
                    }
                    else if (drdlBatchAction.SelectedValue == "3")
                    {
                        repo.postsCommentAllow(post_id.Value, false);
                    }
                    else if (drdlBatchAction.SelectedValue == "4")
                    {
                        repo.postsDelete(post_id.Value);
                    }
                }
            }

            getInfo();
        }

        protected void drdlFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            getInfo(); // age filter taqir kard bia dobare etela at ro begir (masalan zadim post hayi ke commenteshoon fa ale)
        }

    }
}