@model IEnumerable<boilerplate.Models.News>

@{
    ViewBag.Title = "آخرین اخبار";
    
}
<style>
    .badge-info {
        color: black;
        border: 1px solid lightgray;
    }
    body {
        padding-top:0px !important;
    }
</style>
<div class="container mt-5">
    <h2 class="text-center mb-4">@ViewBag.Title</h2>
    <hr />
    <div class="row">
        @if (!Model.Any())
        {
            <div class="col-12">
                <p class="alert alert-warning text-center">در حال حاضر خبری برای نمایش وجود ندارد.</p>
            </div>
        }
        else
        {
            foreach (var item in Model)
            {
                <div class="col-md-4 mb-4">
                    <div class="card h-100 shadow-sm">
                        <img src="@Url.Content("~/Media/News/" + item.News_IMG)" class="card-img-top" alt="@item.News_Title" style="height: 200px; object-fit: cover;"
                             onerror="this.onerror=null;this.src='@Url.Content("~/Media/default-image.png")';">
                        <div class="card-body d-flex flex-column">
                            <h5 class="card-title">@item.News_Title</h5>

                            <p class="card-text text-muted">@item.News_Summary</p>

                            @* --- New section for Category and Tags --- *@
                            <div class="mt-3">
                                @if (!string.IsNullOrEmpty(item.News_Category))
                                {
                                    <div class="mb-2">
                                        <strong style="font-size: 0.9rem;">دسته بندی: </strong>
                                        @* --- FIXED: Loop through categories to create separate badges --- *@
                                        @{
                                            var categories = item.News_Category.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                            foreach (var category in categories)
                                            {
                                                <span class="badge badge-info p-2 mr-1">@category.Trim()</span>
                                            }
                                        }
                                    </div>
                                }

                                @if (!string.IsNullOrEmpty(item.News_Tag))
                                {
                                    <div>
                                        <strong style="font-size: 0.9rem;">تگ ها: </strong>
                                        @{
                                            var tags = item.News_Tag.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                            foreach (var tag in tags)
                                            {
                                                <span class="badge badge-light p-2 mr-1" style="color: #333; border: 1px solid #ccc;">@tag.Trim()</span>
                                            }
                                        }
                                    </div>
                                }
                            </div>

                            @*<div class="mt-auto pt-3">
                                <a href="@Url.Action("NewsDetails", "Home", new { id = item.News_ID })" class="btn btn-primary">ادامه مطلب</a>
                            </div>*@
                        </div>
                        <div class="card-footer text-muted small">
                            @(item.News_Date > DateTime.MinValue ? new boilerplate.Models.Functions().ConvertToPersianDate(item.News_Date) : "")
                        </div>
                    </div>
                </div>
            }
        }
    </div>
</div>
