@inject HttpClient _hc
@layout Shared.BlogLayout
@page "/BlogDetails/{ItemID:int}"
@using global::Shared.DTOs
@using moadiran.Layout
@inject NavigationManager Navigation
<PageTitle>@Item?.PageTitle</PageTitle>



<HeadContent>
    @if (!string.IsNullOrEmpty(description))
    {
        <meta name="description" content="@description" />
    }
    @if (!string.IsNullOrEmpty(keywords))
    {
        <meta name="keywords" content="@keywords">
    }

    <meta property="og:type" content="article" />
    <meta property="og:title" content="@Item?.PageTitle" />
    <meta property="og:description" content="@description" />
    <meta property="og:image" content="https://moadiran.irimg/blog/@Item?.Photo" />
    <meta property="og:url" content="https://moadiran.irBlogDetails/@ItemID" />
    <meta property="og:site_name" content="Moadiran" />

    <meta name="author" content="مهدی ربیع نژاد">
    <meta name="owner" content="مهدی ربیع نژاد">
</HeadContent>

<main>
    <!-- breadcrumb-area-start -->
    <div style="border-radius: .8em;" class="breadcrumb__area breadcrumb-ptb-4 p-relative blue-bg-2">
        <div class="container">
            <div class="row align-items-end">
                <div class="col-xl-9 col-lg-9 col-md-9">
                    <div class="blog-details-banner z-index-2">
                        <div class="blog-details-title-box">
                            <div class="tp-blog-meta d-flex align-items-center">
                                <div class="tp-blog-category category-color-1"><span>@Item?.Title.Split(':')[0]</span></div><!--!-->
                                <div class="tp-blog-date"><span>@Item?.Date</span></div>
                            </div>
                            <h1 class="blog-details-banner-title">@Item?.Title.Split(':')[1]</h1>
                        </div>
                        <div class="tp-blog-author-info-box d-flex align-items-center">
                            <div class="tp-blog-avata">
                                <img class="w-px-40 h-auto rounded-circle" src="img/avata/blog-avata-Publisher.png" alt="Publisher">
                            </div>
                            <div class="tp-blog-author-info">
                                <h5>مهدی ربیع نژاد</h5>
                                <span>مدیر سایت</span>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <!-- breadcrumb-area-end -->
    <!-- breadcrumb-banner-start -->
    <div class="blog-details-img-area mb-80">
        <div class="container">
            <div class="row">
                <div class="col-xl-12">
                    <div class="blog-details-big-img z-index-2">
                        <img src="img/blog/@Item?.Photo" width="400" height="400" alt="@Item?.Title">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- breadcrumb-banner-end -->
    <!-- postbox area start -->
    <div class="postbox__area pt-100 pb-100">
        <div class="container">
            <div class="row">
                <div class="col-xxl-8 col-xl-8 col-lg-8">
                    <div class="postbox__details-wrapper pr-20">
                        <article>
                            <div class="postbox__details-title-box pb-30">
                                @((MarkupString)Item?.Text)
                            </div>
                        </article>
                    </div>
                </div>
                <div class="col-xxl-4 col-xl-4 col-lg-4">
                    <div class="sidebar__wrapper">
                        <div class="sidebar__widget mb-40">
                            <div class="sidebar__widge-title-box">
                                <h3 class="sidebar__widget-title">جستوجو</h3>
                            </div>
                            <div class="sidebar__widget-content">
                                <div class="sidebar__search">
                                    <form action="#">
                                        <div class="sidebar__search-input-2">

                                            <InputText @bind-Value="strSearch" type="text" class="inputText" placeholder="کلمه ای را بنویسید ..." />

                                            <button @onclick="SearchClick">
                                                <svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                    <path d="M8.01371 15.2219C11.9525 15.2219 15.1456 12.0382 15.1456 8.11096C15.1456 4.18368 11.9525 1 8.01371 1C4.07488 1 0.881836 4.18368 0.881836 8.11096C0.881836 12.0382 4.07488 15.2219 8.01371 15.2219Z" stroke="#5F6168" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                                    <path d="M16.9287 16.9996L13.0508 13.1331" stroke="#5F6168" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                                </svg>
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <div class="sidebar__widget mb-40">
                            <div class="sidebar__widge-title-box">
                                <h3 class="sidebar__widget-title">پست های اخیر</h3>
                            </div>
                            <div class="sidebar__widget-content">
                                <div class="sidebar__post rc__post">
                                    @foreach (var item in collectionitem)
                                    {
                                        <ItemBlog___v2 Item="item" />
                                    }
                                </div>
                            </div>
                        </div>
                        @if (Item != null && !string.IsNullOrEmpty(Item.Tags))
                        {
                            <div class="sidebar__widget mb-40">
                                <div class="sidebar__widge-title-box">
                                    <h3 class="sidebar__widget-title">برچسب ها</h3>
                                </div>
                                <div class="sidebar__widget-content">
                                    <div class="tagcloud">
                                        @foreach (var item in Item.Tags.Split('-'))
                                        {
                                            string P = "/ListBlog/" + item;
                                            string itemtitle = "درباره " + item;
                                            <NavLink rel="tag" title="@itemtitle" href="@P">@item</NavLink>
                                        }
                                    </div>
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

@code {
    //meta tags
    public string? description { get; set; }
    public string? keywords { get; set; }
    //---------------------
    public string strSearch { get; set; }
    [Parameter]
    public int? ItemID { get; set; }

    public List<BlogDto> collectionitem { get; set; } = new List<BlogDto>();
    public BlogDtoFull? Item { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        if (ItemID != null)
        {
            Item = await GetItems();
            if (Item == null)
                Navigation.NavigateTo("/");

            else await GenerateTags();
        }

        else
            Navigation.NavigateTo("/");
        await base.OnParametersSetAsync();
    }
    protected override async Task OnInitializedAsync()
    {

        var request = await _hc.GetFromJsonAsync<PagingDto<BlogDto>>($"Base/LastBlog?PageIndex=1&PageSize=3");
        if (request != null)
            collectionitem = request.list;

        await base.OnInitializedAsync();
    }
}
@functions {
    public async Task SearchClick()
    {
        if (!string.IsNullOrEmpty(strSearch))
            Navigation.NavigateTo("/SearchBlog/" + strSearch);
    }
    public async Task<BlogDtoFull?> GetItems()
    {
        var response = await _hc.GetAsync($"Base/GetBlogByID/{ItemID}");

        if (!response.IsSuccessStatusCode)
            return null;


        return await response.Content.ReadFromJsonAsync<BlogDtoFull>();
    }
    public async Task GenerateTags()
    {
        keywords = Item.keywordsTag;
        description = Item.DescriptionTag;

    }
}
