using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils.ORM;
using PageAdmin.Utils;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.E.Controllers
{

    [NotCheckMenuItemPermissions]
    public class PreviewController:BaseController
    {
        DataBaseContext _DbContext;

        public PreviewController()
        {
            _DbContext = DbHelper.DbContext();
        }

        [HttpGet]
        public ActionResult Site()
        {
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            string url= WebUrlHelper.GetSiteUrlPrefix(id);;
            Response.Redirect(url);
            return null;
        }
        [HttpGet]
        public ActionResult Column()
        {
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            int columnId = id;
            dynamic data = _DbContext.QueryFirst("pa_column", "*", "id=" + columnId);
            int siteId = data.SiteId;
            string path = data.Path;
            string siteUrlPrefix = WebUrlHelper.GetSiteUrlPrefix(siteId);
            string url = siteUrlPrefix + path;
            Response.Redirect(url);
            return null;
        }

        [HttpGet]
        public ActionResult Info()
        {
            string table = StringHelper.Format<string>(Request.QueryString["table"]);
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            InfoTableBLL infoTableBll = new InfoTableBLL();
            if (infoTableBll.IsExists(table,true))
            {
                dynamic data = _DbContext.QueryFirst(table, "*", "id=" + id);
                string url = WebUrlHelper.GetInfoUrl(table,id);
                Response.Redirect(url);
                return null;
            }
            else
            {
                return Content("无效的参数!");
            }
        }
    }
}
