<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** BLOG FORM — Expects: $post, $mode. */
$p = $post;
$pub = $p['publish_at'] ? date('Y-m-d\TH:i', strtotime($p['publish_at'])) : '';
?>
<div class="page-head">
	<div><h2><?= $mode === 'edit' ? 'Edit' : 'New' ?> Article</h2></div>
	<a href="<?= base_url('admin/blog') ?>" class="btn btn-ghost">← Back</a>
</div>

<form action="<?= base_url('admin/blog/save') ?>" method="post" enctype="multipart/form-data">
	<?= form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()) ?>
	<input type="hidden" name="id" value="<?= (int) $p['id'] ?>">

	<div class="grid grid-2-1">
		<div>
			<div class="card">
				<div class="card-head"><h3>Article</h3></div>
				<div class="card-body">
					<div class="form-grid">
						<div class="fld"><label>Title <span class="req">*</span></label>
							<input type="text" name="title" value="<?= html_escape($p['title']) ?>" required data-slug-source></div>
						<div class="fld"><label>Slug</label>
							<input type="text" name="slug" value="<?= html_escape($p['slug']) ?>" data-slug-target placeholder="auto"></div>
						<div class="fld"><label>Excerpt</label>
							<textarea name="excerpt" placeholder="Short summary shown on the journal index"><?= html_escape($p['excerpt']) ?></textarea></div>
						<div class="fld"><label>Body <span class="req">*</span></label>
							<textarea name="body" style="min-height:340px;font-family:monospace;font-size:.86rem;"><?= html_escape($p['body']) ?></textarea>
							<p class="hint">HTML is allowed. Use &lt;p&gt;, &lt;h3&gt;, &lt;ul&gt;, &lt;blockquote&gt;, &lt;img&gt; etc.</p>
						</div>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>SEO</h3></div>
				<div class="card-body">
					<div class="form-grid">
						<div class="fld"><label>Meta Title</label><input type="text" name="meta_title" value="<?= html_escape($p['meta_title']) ?>"></div>
						<div class="fld"><label>Meta Description</label><textarea name="meta_desc"><?= html_escape($p['meta_desc']) ?></textarea></div>
					</div>
				</div>
			</div>
		</div>

		<div>
			<div class="card">
				<div class="card-head"><h3>Cover Image</h3></div>
				<div class="card-body">
					<?php if ($p['cover_image']): ?><img src="<?= upload_url($p['cover_image']) ?>" class="thumb-lg" id="imgPrev" style="margin-bottom:8px;"><?php else: ?><img src="" class="thumb-lg" id="imgPrev" style="display:none;margin-bottom:8px;"><?php endif; ?>
					<?php $this->load->view('admin/partials/_upload_field', array(
						'name' => 'cover_image',
						'accept' => 'image/*',
						'icon' => 'image',
						'label' => 'Choose image',
						'preview' => '#imgPrev',
					)); ?>
				</div>
			</div>
			<div class="card">
				<div class="card-head"><h3>Publishing</h3></div>
				<div class="card-body">
					<div class="fld"><label>Status</label>
						<select name="status">
							<?php foreach (array('draft'=>'Draft','published'=>'Published','archived'=>'Archived') as $v=>$l): ?>
								<option value="<?= $v ?>" <?= $p['status'] === $v ? 'selected' : '' ?>><?= $l ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="fld"><label>Category</label>
						<input type="text" name="category" value="<?= html_escape($p['category']) ?>" placeholder="e.g. Insights"></div>
					<div class="fld"><label>Publish Date / Time</label>
						<input type="datetime-local" name="publish_at" value="<?= $pub ?>">
						<p class="hint">Leave blank to publish immediately.</p>
					</div>
				</div>
			</div>
		</div>
	</div>

	<div class="card"><div class="card-body"><div class="form-actions">
		<button type="submit" class="btn btn-primary"><?= bloom_icon('check', 16) ?> Save Article</button>
		<a href="<?= base_url('admin/blog') ?>" class="btn btn-ghost">Cancel</a>
	</div></div></div>
</form>
