<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** PORTFOLIO FORM — Expects: $project, $categories, $talent, $mode. */
$p = $project;
$gallery = decode_json($p['gallery']);
$sel_talent = array_filter(explode(',', (string) $p['talent_ids']));
?>
<div class="page-head">
	<div><h2><?= $mode === 'edit' ? 'Edit' : 'Add' ?> Project</h2></div>
	<a href="<?= base_url('admin/portfolio') ?>" class="btn btn-ghost">← Back</a>
</div>

<form action="<?= base_url('admin/portfolio/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>Project Details</h3></div>
				<div class="card-body">
					<div class="form-grid cols-2">
						<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>Brand / Client</label>
							<input type="text" name="brand" value="<?= html_escape($p['brand']) ?>"></div>
						<div class="fld"><label>Year</label>
							<input type="text" name="year" value="<?= html_escape($p['year']) ?>"></div>
						<div class="fld fld-full"><label>Excerpt</label>
							<textarea name="excerpt" placeholder="Short summary shown on portfolio cards"><?= html_escape($p['excerpt']) ?></textarea></div>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>Case Study</h3></div>
				<div class="card-body">
					<div class="form-grid">
						<div class="fld"><label>The Brief</label><textarea name="cs_brief"><?= html_escape($p['cs_brief']) ?></textarea></div>
						<div class="fld"><label>The Concept</label><textarea name="cs_concept"><?= html_escape($p['cs_concept']) ?></textarea></div>
						<div class="fld"><label>The Execution</label><textarea name="cs_execution"><?= html_escape($p['cs_execution']) ?></textarea></div>
						<div class="fld"><label>The Results</label><textarea name="cs_results"><?= html_escape($p['cs_results']) ?></textarea></div>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>Gallery</h3></div>
				<div class="card-body">
					<?php if ($gallery): ?>
						<div style="display:flex;flex-wrap:wrap;gap:10px;margin-bottom:12px;">
							<?php foreach ($gallery as $g): ?>
								<label style="position:relative;cursor:pointer;">
									<img src="<?= upload_url($g) ?>" class="thumb-lg">
									<span style="display:flex;align-items:center;gap:5px;font-size:.76rem;margin-top:4px;">
										<input type="checkbox" name="keep_gallery[]" value="<?= html_escape($g) ?>" checked> Keep
									</span>
								</label>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
					<div class="fld"><label>Add Gallery Images</label>
						<?php $this->load->view('admin/partials/_upload_field', array('name' => 'gallery', 'multiple' => TRUE, 'accept' => 'image/*', 'label' => 'Choose images')); ?>
						<p class="hint">Select multiple images to add to this project's gallery.</p>
					</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 &amp; Video</h3></div>
				<div class="card-body">
					<div class="fld"><label>Cover Image</label>
						<?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',
							'multiple' => FALSE
						)); ?>
					</div>
					<div class="fld"><label>Video Type</label>
						<select name="video_type">
							<?php foreach (array('none'=>'No Video','youtube'=>'YouTube','file'=>'Uploaded File') as $v=>$l): ?>
								<option value="<?= $v ?>" <?= $p['video_type'] === $v ? 'selected' : '' ?>><?= $l ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="fld"><label>YouTube URL / ID</label>
						<input type="text" name="youtube_id" value="<?= html_escape($p['youtube_id']) ?>"></div>
					<div class="fld"><label>Video File</label>
						<?php $this->load->view('admin/partials/_upload_field', array(
							'name' => 'video_file', 
							'accept' => 'video/mp4,video/webm', 
							'icon' => 'film', 
							'label' => 'Choose video',
							'multiple' => FALSE
						)); ?>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>Settings</h3></div>
				<div class="card-body">
					<div class="fld"><label>Category</label>
						<select name="category" required>
							<option value="">Select…</option>
							<?php foreach ($categories as $k=>$v): ?>
								<option value="<?= $k ?>" <?= $p['category'] === $k ? 'selected' : '' ?>><?= html_escape($v) ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="fld"><label>Sort Order</label><input type="number" name="sort_order" value="<?= (int) $p['sort_order'] ?>"></div>
					<div class="fld"><label class="check"><input type="checkbox" name="is_featured" value="1" <?= $p['is_featured'] ? 'checked' : '' ?>><span>Featured on homepage</span></label></div>
					<div class="fld"><label class="check"><input type="checkbox" name="is_active" value="1" <?= $p['is_active'] ? 'checked' : '' ?>><span>Active</span></label></div>
				</div>
			</div>

			<?php if ($talent): ?>
			<div class="card">
				<div class="card-head"><h3>Featured Talent</h3></div>
				<div class="card-body">
					<?php foreach ($talent as $t): ?>
						<label class="check" style="margin-bottom:8px;">
							<input type="checkbox" name="talent_ids[]" value="<?= $t['id'] ?>" <?= in_array($t['id'], $sel_talent) ? 'checked' : '' ?>>
							<span><?= html_escape($t['name']) ?></span>
						</label>
					<?php endforeach; ?>
				</div>
			</div>
			<?php endif; ?>
		</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 Project</button>
		<a href="<?= base_url('admin/portfolio') ?>" class="btn btn-ghost">Cancel</a>
	</div></div></div>
</form>