@use validator::ValidationErrors;
@use plume_models::blogs::Blog;
@use plume_models::instance::Instance;
@use plume_models::medias::Media;
@use crate::template_utils::*;
@use crate::templates::base;
@use crate::templates::partials::image_select;
@use crate::routes::blogs;
@use crate::routes::blogs::EditForm;
@use crate::routes::medias;

@(ctx: BaseContext, blog: &Blog, medias: Vec<Media>, form: &EditForm, errors: ValidationErrors)

@:base(ctx, i18n!(ctx.1, "Edit \"{}\""; &blog.title), {}, {
	<a href="@uri!(blogs::details: name = &blog.fqn, page = _)">@blog.title</a>
}, {
	<h1>@i18n!(ctx.1, "Edit \"{}\""; &blog.title)</h1>
    <form method="post" action="@uri!(blogs::update: name = &blog.fqn)">
        <!-- Rocket hack to use various HTTP methods -->
        <input type=hidden name="_method" value="put">

        @(Input::new("title", i18n!(ctx.1, "Title"))
            .default(&form.title)
            .error(&errors)
            .set_prop("minlenght", 1)
            .html(ctx.1))

        <label for="summary">@i18n!(ctx.1, "Description")<small>@i18n!(ctx.1, "Markdown syntax is supported")</small></label>
        <textarea id="summary" name="summary" rows="20">@form.summary</textarea>

        <p>
            @i18n!(ctx.1, "You can upload images to your gallery, to use them as blog icons, or banners.")
            <a href="@uri!(medias::new)">@i18n!(ctx.1, "Upload images")</a>
        </p>

        @:image_select(ctx, "icon", i18n!(ctx.1, "Blog icon"), true, medias.clone(), form.icon)
        @:image_select(ctx, "banner", i18n!(ctx.1, "Blog banner"), true, medias, form.banner)

        @if let Ok(themes) = Instance::list_themes() {
            <label for="css">@i18n!(ctx.1, "Custom theme")</label>
            <select name="theme" id="theme">
                <option value="" @if form.theme.is_none() { selected }>@i18n!(ctx.1, "Default theme")</option>
                @for theme in themes {
                    <option value="@theme" @if Some(theme.clone()) == form.theme { selected }>@theme</option>
                }
            </select>
        } else {
            <p class="error">@i18n!(ctx.1, "Error while loading theme selector.")</p>
        }

        <input type="submit" value="@i18n!(ctx.1, "Update blog")"/>
    </form>

    <h2>@i18n!(ctx.1, "Danger zone")</h2>
    <p>@i18n!(ctx.1, "Be very careful, any action taken here can't be reversed.")</p>
    <form method="post" action="@uri!(blogs::delete: name = &blog.fqn)" onsubmit="return confirm('@i18n!(ctx.1, "Are you sure that you want to permanently delete this blog?")')">
        <input type="submit" class="inline-block button destructive" value="@i18n!(ctx.1, "Permanently delete this blog")">
    </form>
})