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

namespace CarGallery
{
    public partial class AdminEditPost : System.Web.UI.Page
    {
        DatabaseRepository repo = new DatabaseRepository();

        protected void Page_Load(object sender, EventArgs e)
        {
            // aval etebar sanji mikonim aya taraf login shode ya na
            if (Session["username"] == null || Session["username"].ToString() == "")
                Response.Redirect("Login.aspx");

            // bad check mikonim bebinim asan taraf ro Edit click karde ya inke dasti vared shode ?!
            // vaqti ro edit click kone,ye session be esme post_id ba meqdare ID e oon posti ke ro editesh click karde 
            // ijad mishe , to in safhe check mishe asan session e post_id darim ya na ? age darim pore ya khali ?!

            if (Session["post_id"] == null || Session["post_id"].ToString() == "") // age session e post_id khali bood bargard be managePosts
                Response.Redirect("AdminManagePosts.aspx");

            if (!IsPostBack) // momkene be vaseteye zadane ye dokme safhe refresh beshe ( mese save ) 
                // pas age masalan save ro zad nabayad etela at dobare load beshe
                // pas migim age postback nabood etela at ro neshoon bede
            {

                // dar in mored ham ye tozihati qablan dadm
                // miad etela ate ye post ro load mikone va neshoon mide

                DataTable dt = repo.postGetSingle(Session["post_id"].ToString());

                txtTitle.Text = dt.Rows[0][1].ToString();
                txtDescription.Text = dt.Rows[0][2].ToString();
                bool comment = (bool)dt.Rows[0][7];
                bool slide = (bool)dt.Rows[0][8];

                if (slide)
                {
                    rbtnSlideTrue.Checked = true;
                    rbtnSlideFalse.Checked = false;
                }
                else
                {
                    rbtnSlideFalse.Checked = true;
                    rbtnSlideTrue.Checked = false;
                }

                if (comment)
                {
                    rbtnCommentsTrue.Checked = true;
                    rbtnCommentsFalse.Checked = false;
                }
                else
                {
                    rbtnCommentsFalse.Checked = true;
                    rbtnCommentsTrue.Checked = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            // age dokme save ro zad etela at ro bede be method e postUpdate ke to DatabaseRepository hastesh

            repo.postUpdate(Session["post_id"].ToString(), txtTitle.Text, txtDescription.Text, rbtnSlideTrue.Checked, rbtnCommentsTrue.Checked);
            Response.Redirect("AdminManagePosts.aspx"); // va bargard be ManagePosts
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminManagePosts.aspx"); // age cancel ro zad ham bargard be ManagePosts
        }
    }
}