@model boilerplate.Controllers.PanelController.AddNewsViewModel
@{
    ViewBag.Title = Model.News.News_ID == 0 ? "افزودن خبر جدید" : "ویرایش خبر";
    Layout = "~/Views/_PanelSideBar.cshtml";
}

@section Heads {
    <!-- Add CSS for Select2 library -->
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.ckeditor.com/4.16.2/standard/ckeditor.js"></script>
}

<div class="card card-custom">
    <div class="card-header">
        <h3 class="card-label">@ViewBag.Title</h3>
    </div>
    <div class="card-body">
        @using (Html.BeginForm("AddNews", "Panel", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(model => model.News.News_ID)
            @Html.HiddenFor(model => model.News.News_IMG)

            if (!ViewData.ModelState.IsValid)
            {
                <div class="alert alert-danger">
                    @Html.ValidationSummary(false)
                </div>
            }

            <div class="form-group">
                <label>عنوان خبر</label>
                @Html.EditorFor(model => model.News.News_Title, new { htmlAttributes = new { @class = "form-control" } })
            </div>

            <div class="form-group">
                <label>دسته‌بندی‌ها</label>
                <select name="selectedCategories" class="form-control select2-categories" multiple="multiple">
                    @{
                        var selectedCategories = (Model.News.News_Category ?? "").Split(',');
                    }
                    @foreach (var category in Model.AllCategories)
                    {
                        <option value="@category.Item_Title" @(selectedCategories.Contains(category.Item_Title) ? "selected" : "")>@category.Item_Title</option>
                    }
                </select>
            </div>

            <div class="form-group">
                <label>تگ‌ها</label>
                <select name="selectedTags" class="form-control select2-tags" multiple="multiple">
                    @{
                        var selectedTags = (Model.News.News_Tag ?? "").Split(',');
                    }
                    @foreach (var tag in Model.AllTags)
                    {
                        <option value="@tag.Item_Title" @(selectedTags.Contains(tag.Item_Title) ? "selected" : "")>@tag.Item_Title</option>
                    }
                </select>
            </div>

            <div class="form-group">
                <label>خلاصه خبر</label>
                @Html.TextAreaFor(model => model.News.News_Summary, new { @class = "form-control", rows = 3 })
            </div>

            <div class="form-group">
                <label>توضیحات کامل</label>
                @Html.TextAreaFor(model => model.News.News_Description, new { @class = "form-control", id = "newsDescriptionEditor" })
            </div>

            <div class="form-group">
                <label>تصویر شاخص خبر</label>
                <input type="file" name="imageFile" class="form-control-file" accept="image/*" />
                @if (!string.IsNullOrEmpty(Model.News.News_IMG))
                {
                    <div class="mt-2">
                        <span>تصویر فعلی:</span>
                        <img src="~/Media/News/@Model.News.News_IMG" style="width: 150px; height: auto;" />
                    </div>
                }
            </div>

            <div class="card-footer text-right">
                <button type="submit" class="btn btn-primary mr-2">ذخیره</button>
                <a href="@Url.Action("NewsList", "Panel")" class="btn btn-secondary">انصراف</a>
            </div>
        }
    </div>
</div>

@section Script {
    <!-- کتابخانه Select2 (فرض بر این است که jQuery در لایوت اصلی شما لود شده است) -->
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
        $(document).ready(function () {
            // --- FIXED: Separate initialization for Categories and Tags ---

            // Initialize Select2 for CATEGORIES (without the ability to add new ones)
            $('.select2-categories').select2({
                placeholder: "یک یا چند دسته بندی انتخاب کنید",
                dir: "rtl"
            });

            // Initialize Select2 for TAGS (NOW also without the ability to add new ones)
            $('.select2-tags').select2({
                placeholder: "یک یا چند تگ انتخاب کنید",
                dir: "rtl"
                // The 'tags: true' option has been removed
            });

            // Initialize CKEditor
            CKEDITOR.replace('newsDescriptionEditor', { language: 'fa' });
        });
    </script>
}
