using Falcon.Extend;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace FAuth.Extensions.TagHelpers
{
    /// <summary>
    /// 实现ajax调用的属性
    /// </summary>
    [HtmlTargetElement("a")]
    [HtmlTargetElement("form")]
    public class FAjaxTagHelper:TagHelper
    {
        /// <summary>
        /// 是否允许FAjax，默认True，一般不需要设置
        /// </summary>
        [HtmlAttributeName("fajax-enable")]
        public bool Enable { get; set; } = true;
        /// <summary>
        /// 更新元素选择器
        /// </summary>
        [HtmlAttributeName("fajax-updata")]
        public string Updata { get; set; } = "";
        /// <summary>
        /// 更新模式。默认为空，可选BEFORE、AFTER或REPLACE-WITH
        /// </summary>
        [HtmlAttributeName("fajax-model")]
        public string Model { get; set; } = "";
        /// <summary>
        /// 开始ajax调用之前执行
        /// </summary>
        [HtmlAttributeName("fajax-begin")]
        public string OnBegin { get; set; } = "";
        /// <summary>
        /// 调用ajax之后执行
        /// </summary>
        [HtmlAttributeName("fajax-complete")]
        public string OnComplete { get; set; } = "";
        /// <summary>
        /// 调用成功后执行
        /// </summary>
        [HtmlAttributeName("fajax-success")]
        public string OnSuccess { get; set; } = "";
        /// <summary>
        /// 调用失败执行
        /// </summary>
        [HtmlAttributeName("fajax-failure")]
        public string OnFailure { get; set; } = "";

        public override void Process(TagHelperContext context,TagHelperOutput output) {
            if(this.Updata.IsNotNullOrEmpty()) {
                output.Attributes.SetAttribute("data-ajax-update",this.Updata);
                output.Attributes.SetAttribute("data-ajax","true");
            }
            if(this.Model.IsNotNullOrEmpty()) {
                output.Attributes.SetAttribute("data-ajax-mode",this.Model);
                output.Attributes.SetAttribute("data-ajax","true");
            }
            if(this.OnBegin.IsNotNullOrEmpty()) {
                output.Attributes.SetAttribute("data-ajax-begin",this.OnBegin);
                output.Attributes.SetAttribute("data-ajax","true");
            }
            if(this.OnComplete.IsNotNullOrEmpty()) {
                output.Attributes.SetAttribute("data-ajax-complete",this.OnComplete);
                output.Attributes.SetAttribute("data-ajax","true");
            }
            if(this.OnSuccess.IsNotNullOrEmpty()) {
                output.Attributes.SetAttribute("data-ajax-success",this.OnSuccess);
                output.Attributes.SetAttribute("data-ajax","true");
            }
            if(this.OnFailure.IsNotNullOrEmpty()) {
                output.Attributes.SetAttribute("data-ajax-failure",this.OnFailure);
                output.Attributes.SetAttribute("data-ajax","true");
            }
            if(!this.Enable) {
                output.Attributes.RemoveAll("data-ajax");
            }
            //base.Process(context,output);
        }
    }
}
