using PageAdmin.BLL;
using PageAdmin.Common;
using PageAdmin.Utils.ORM;
using PageAdmin.Utils;
using PageAdmin.Web.Filter;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.E.Controllers
{
    //信息常用api
    public class InfoDataController : BaseController
    {
        private DataBaseContext _DbContext;

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

        /// <summary>
        /// 添加点击
        /// </summary>
        /// <param name="table"></param>
        /// <param name="id"></param>
        [AjaxOnly]
        [HttpPost]
        public string AddClicks(FormCollection form)
        {
           int clicks = 0;
            string table = form["table"];
            int columnId=StringHelper.Format<int>(form["columnId"]);
            if(columnId>0)
            {
                ColumnBLL columnBLL = new ColumnBLL();
                table = columnBLL.GetTable(columnId);
            }

            int id = StringHelper.Format<int>(form["id"]);
            if (!string.IsNullOrEmpty(table) && id > 0)
            {
                InfoTableBLL infoTableBll = new InfoTableBLL();
                if (infoTableBll.IsExists(table, true))
                {
                     string sql = "update [" + table + "] set clicks=clicks+1 where id=@id";
                    _DbContext.SetDbParamter("id", id);
                    _DbContext.Execute(sql);
                    _DbContext.SetDbParamter("id", id);
                    clicks = (int)(_DbContext.GetField(table, "clicks", "id =@id") ?? 0);
                }
            }
            return clicks.ToString();
        }

        /// <summary>
        /// 返回点击数
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpGet]
        public string GetClicks()
        {
            int clicks = 0;
            string table = Request.QueryString["table"];
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            if (!string.IsNullOrEmpty(table) && id > 0)
            {
                InfoTableBLL infoTableBll = new InfoTableBLL();
                if (infoTableBll.IsExists(table, true))
                {
                    ; _DbContext.SetDbParamter("id", id);
                    clicks = (int)(_DbContext.GetField(table, "clicks", "id =@id") ?? 0);
                }
            }
            return clicks.ToString();
        }


        /// <summary>
        /// 匿名发布
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [CheckTable(TableType =TableTypeEnum.CustomInfoTable)]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [AjaxOnly]
        public JsonResult Add(FormCollection form)
        {
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.CurrentUser = ViewBag.CurrentUser;
            mvcContext.From = FromEnum.Guest;
            if (mvcContext.CurrentUser!=null)
            {
                if(mvcContext.CurrentUser.Uid>0)
                {
                    mvcContext.From = FromEnum.Member;
                }
            }
            mvcContext.Table = ViewBag.Table;
            mvcContext.IncludeCustomField = true;
            mvcContext.IncludeAttachments = true;
            mvcContext.TableType = TableTypeEnum.CustomInfoTable;
            mvcContext.Data = form;
            AddBLL addBll = new AddBLL();
            var tipsInfo = addBll.Add(mvcContext);
            int newId = mvcContext.DetailId;
            new InfoUrlSetBLL(mvcContext.Table).Set(mvcContext, newId); //设置Url映射表
            return base.Json(tipsInfo);
        }


    }


}