extends vibelog.admin.layout

block vibelog.title
	- import std.datetime : Clock;
	- import core.time : usecs;
	- title = (info.post ? "Modify" : "Add new") ~ " blog post";
	script(type="text/javascript", src="#{info.settings.rootDir}scripts/jquery.js")
	script(type="text/javascript", src="#{info.settings.rootDir}scripts/vibelog-edit.js")
	script(type="text/javascript").
		window.rootDir = "#{info.settings.rootDir}";

block vibelog-localnav
	ul.admin-local-nav
		li
			a(href='#{info.post ? "../" : "./"}') Cancel

block vibelog-content
	- if (info.error.length)
		p.error= info.error

	- if (!info.post || info.post.author == info.loginUser.username || info.loginUser.isPostAdmin())
		form(action='#{info.post ? "./" : "make_post"}', method="POST")
			input(type="hidden", name="id", value="#{info.post ? info.post.id.toString() : null}")
			table.blind
				col.caption
				tr
					td
						label(for="isPublic") Public
					td
						- if( info.post && info.post.isPublic )
							input#isPublic.checkbox(type="checkbox", name="isPublic", value="yes", checked)
						- else
							input#isPublic.checkbox(type="checkbox", name="isPublic", value="yes")
				tr
					td
						label(for="commentsAllowed") Comments allowed
					td
						- if( !info.post || info.post.commentsAllowed )
							input#commentsAllowed.checkbox(type="checkbox", name="commentsAllowed", value="yes", checked)
						- else
							input#commentsAllowed.checkbox(type="checkbox", name="commentsAllowed", value="yes")
				tr
					- if (info.loginUser.isPostAdmin())
						td
							label(for="author") Author
						td
							select#author(name="author", size="1")
								- foreach (usr; info.users)
									- if (usr.username == (info.post ? info.post.author : info.loginUser.username))
										option(value="#{usr.username}", selected) #{usr.name} (#{usr.username})
									- else
										option(value="#{usr.username}") #{usr.name} (#{usr.username})
					- else
						td Author
						td #{info.loginUser.name} (#{info.loginUser.username})
							input(type="hidden", name="author", value="#{info.loginUser.username}")
				tr
					td
						label(for="category") Category
					td
						select#category(name="category", size="1")
							- foreach (cat; info.globalConfig.categories)
								- if (info.loginUser.isPostAdmin() || info.loginUser.mayPostInCategory(cat))
									- if (info.post && cat == info.post.category)
										option(value="#{cat}", selected)= cat
									- else
										option(value="#{cat}")= cat
				tr
					td
						label(for="date") Date
					td
						input#date(type="text", name="date", value="#{info.post ? info.post.date.toSimpleString() : Clock.currTime().toSimpleString()}")
				tr
					td
						label(for="slug") Post slug
					td
						input#slug(type="text", name="slug", value="#{info.post ? info.post.slug : null}")
				tr
					td
						label(for="headerImage-field") Header image
					td
						input#headerImage-field(type="text", name="headerImage", value="#{info.post ? info.post.headerImage : null}")
				tr
					td(colspan=2)
						hr
				tr
					td
						label(for="header-field") Heading
					td
						input#header-field(type="text", name="header", value="#{info.post ? info.post.header : null}")
				tr
					td
						label(for="summary-title-field") Summary title
					td
						input#summary-title-field(type="text", name="summaryTitle", value="#{info.post ? info.post.summaryTitle : null}", maxlength=70)
				tr
					td
						label(for="summary-field") Summary
					td
						input#summary-field(type="text", name="summary", value="#{info.post ? info.post.summary : null}", maxlength=240)
				tr
					td
						label(for="subHeader") Sub-Heading
					td
						textarea#subHeader(cols="80", rows="5", name="subHeader")= info.post ? info.post.subHeader : null
				tr
					td Article text
						p
							input#preview-checkbox.checkbox(type="checkbox", onchange="previewUpdate();", style="width: auto;", autocomplete="off")
							label(for="preview-checkbox") Preview
						p
							label(for="filters-field") Filters
							- import std.array;
							input#filters-field(type="text", name="filters", onchange="previewUpdate();", style="width: auto;", autocomplete="off", value='#{info.post ? info.post.filters.join(" ") : "markdown"}')
					td
						#message-area
							textarea#message(name="content", cols="80", rows="40")= info.post ? info.post.content : null
							#message-preview(style="display: none;")
			input(type="submit", value='#{info.post ? "Apply changes" : "Create post!"}')

		- if (info.post)
			h2 Files

			- if (info.files.length)
				table
					tr
						th Name
						th Action
					- foreach (f; info.files)
						tr
							td= f
							td
								form(action="files/#{f}/delete", method="POST")
									button(type="submit") Delete
			- else
				p No files uploaded for this post.

			h3 Upload files
			form(action="files/", method="POST", enctype="multipart/form-data")
				input(type="file", name="files", multiple)
				button(type="submit") Upload

	- else
		p.error You are not the author of this post and/or are not authorized to change it.

		p Public: #{info.post.isPublic ? "yes" : "no"}
		p Author: #{info.post.author}
		p Heading: #{info.post.header}
		p Sub-Heading: #{info.post.subHeader}
		p Content:
		pre= info.post.content
