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

namespace PageAdmin.Web.Areas.E.Controllers
{


    /// <summary>
    /// 检测重复
    /// </summary>
    public class CheckRepeateController : Controller
    {
        /// <summary>
        /// 自定义表单的检测重复
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult CheckCustomField(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo();
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            int fieldId = StringHelper.Format<int>(Request.QueryString["FieldId"]);
            string validateName= StringHelper.Format<string>(form["_validateName"]);
            string fieldValue = StringHelper.Format<string>(form[validateName]);
            FieldBLL fieldBLL = new FieldBLL();
            dynamic fieldData = fieldBLL.GetData(fieldId);
            if (fieldData == null)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "无效的fieldId参数";
                return base.Json(tipsInfo);
            }

            CheckExistsBLL checkExistsBLL = new CheckExistsBLL();
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.Table = fieldData.TableName;
            mvcContext.DbContext = DbHelper.DbContext();
            Type dataType= StringHelper.TransformValueType(fieldData.ValueType);
            object value = StringHelper.Format(fieldValue, dataType);
            tipsInfo = checkExistsBLL.CheckExists(mvcContext, id, fieldData.Remark, fieldData.Name, value);
            return base.Json(tipsInfo);
        }


        /// <summary>
        /// 检测注册手机是否
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult CheckRegMobile(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo();
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            string validateName = StringHelper.Format<string>(form["_validateName"]);
            string fieldValue = StringHelper.Format<string>(form[validateName]);
            if (fieldValue == null)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "无效的_validateName参数";
                return base.Json(tipsInfo);
            }
            CheckExistsBLL checkExistsBLL = new CheckExistsBLL();
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.Table = "pa_member";
            mvcContext.DbContext = DbHelper.DbContext();
            tipsInfo = checkExistsBLL.CheckExists(mvcContext, id, "手机", "mobile", fieldValue);
            return base.Json(tipsInfo);
        }


        /// <summary>
        /// 检测注册邮箱是否重复
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult CheckRegEmail(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo();
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            string validateName = StringHelper.Format<string>(form["_validateName"]);
            string fieldValue = StringHelper.Format<string>(form[validateName]);
            if (fieldValue == null)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "无效的_validateName参数";
                return base.Json(tipsInfo);
            }
            CheckExistsBLL checkExistsBLL = new CheckExistsBLL();
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.Table = "pa_member";
            mvcContext.DbContext = DbHelper.DbContext();
            tipsInfo = checkExistsBLL.CheckExists(mvcContext, id, "邮箱", "email", fieldValue);
            return base.Json(tipsInfo);
        }

        /// <summary>
        /// 检测用户名是否重复
        /// </summary>
        /// <returns></returns>
        [AjaxOnly]
        [HttpPost]
        public JsonResult CheckUserName(FormCollection form)
        {
            TipsInfo tipsInfo = new TipsInfo();
            int id = StringHelper.Format<int>(Request.QueryString["id"]);
            string validateName = StringHelper.Format<string>(form["_validateName"]);
            string fieldValue = StringHelper.Format<string>(form[validateName]);
            if (fieldValue == null)
            {
                tipsInfo.State = 0;
                tipsInfo.Msg = "无效的_validateName参数";
                return base.Json(tipsInfo);
            }
            CheckExistsBLL checkExistsBLL = new CheckExistsBLL();
            MvcControllerContext mvcContext = new MvcControllerContext();
            mvcContext.Table = "pa_member";
            mvcContext.DbContext = DbHelper.DbContext();
            tipsInfo = checkExistsBLL.CheckExists(mvcContext, id,"用户名","username", fieldValue);
            return base.Json(tipsInfo);
        }

    }
}