<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** TALENT FORM — Expects: $talent, $categories, $mode. */

// Safety: Ensure $talent exists and is an array
if (!isset($talent) || !is_array($talent)) {
    $talent = array();
}

$t = $talent;

// Safely decode JSON fields
$gallery = isset($t['gallery']) ? decode_json($t['gallery']) : array();
$stats = isset($t['stats']) ? decode_json($t['stats']) : array();
$socials = isset($t['socials']) ? decode_json($t['socials']) : array();

// Ensure mode exists
$mode = $mode ?? 'create';
?>
<div class="page-head">
	<div><h2><?= $mode === 'edit' ? 'Edit' : 'Add' ?> Talent</h2></div>
	<a href="<?= base_url('admin/talent') ?>" class="btn btn-ghost">← Back</a>
</div>

<form action="<?= base_url('admin/talent/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) ($t['id'] ?? 0) ?>">

	<div class="grid grid-2-1">
		<div>
			<div class="card">
				<div class="card-head"><h3>Profile</h3></div>
				<div class="card-body">
					<div class="form-grid cols-2">
						<div class="fld"><label>Name <span class="req">*</span></label>
							<input type="text" name="name" value="<?= html_escape($t['name'] ?? '') ?>" required data-slug-source></div>
						<div class="fld"><label>Slug</label>
							<input type="text" name="slug" value="<?= html_escape($t['slug'] ?? '') ?>" data-slug-target placeholder="auto"></div>
						<div class="fld fld-full"><label>Tagline</label>
							<input type="text" name="tagline" value="<?= html_escape($t['tagline'] ?? '') ?>" placeholder="e.g. Editorial &amp; runway model"></div>
						<div class="fld fld-full"><label>Biography</label>
							<textarea name="bio" style="min-height:150px;"><?= html_escape($t['bio'] ?? '') ?></textarea></div>
						<div class="fld"><label>Location</label>
							<input type="text" name="location" value="<?= html_escape($t['location'] ?? '') ?>"></div>
						<div class="fld"><label>Availability</label>
							<select name="availability">
								<?php foreach (array('available'=>'Available','booked'=>'Booked','unavailable'=>'Unavailable') as $v=>$l): ?>
									<option value="<?= $v ?>" <?= ($t['availability'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option>
								<?php endforeach; ?>
							</select>
						</div>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>Stats &amp; Socials</h3></div>
				<div class="card-body">
					<div class="form-grid cols-2">
						<div class="fld"><label>Followers</label>
							<input type="text" name="stat_followers" value="<?= html_escape($stats['followers'] ?? '') ?>" placeholder="e.g. 24K"></div>
						<div class="fld"><label>Height</label>
							<input type="text" name="stat_height" value="<?= html_escape($stats['height'] ?? '') ?>" placeholder="e.g. 5'9&quot;"></div>
						<div class="fld"><label>Based In</label>
							<input type="text" name="stat_based" value="<?= html_escape($stats['based'] ?? '') ?>"></div>
						<div class="fld"><label>Instagram URL</label>
							<input type="text" name="social_instagram" value="<?= html_escape($socials['instagram'] ?? '') ?>"></div>
						<div class="fld"><label>TikTok URL</label>
							<input type="text" name="social_tiktok" value="<?= html_escape($socials['tiktok'] ?? '') ?>"></div>
						<div class="fld"><label>YouTube URL</label>
							<input type="text" name="social_youtube" value="<?= html_escape($socials['youtube'] ?? '') ?>"></div>
					</div>
				</div>
			</div>

			<div class="card">
				<div class="card-head"><h3>Portfolio Gallery</h3></div>
				<div class="card-body">
					<?php if (!empty($gallery)): ?>
						<div style="display:flex;flex-wrap:wrap;gap:10px;margin-bottom:12px;">
							<?php foreach ($gallery as $g): ?>
								<label style="cursor:pointer;">
									<img src="<?= upload_url($g) ?>" class="thumb-lg">
									<span style="display:flex;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 Images</label>
						<?php $this->load->view('admin/partials/_upload_field', array(
							'name' => 'gallery', 
							'multiple' => TRUE, 
							'accept' => 'image/*', 
							'label' => 'Choose images'
						)); ?>
					</div>
				</div>
			</div>
		</div>

		<div>
			<div class="card">
				<div class="card-head"><h3>Photo</h3></div>
				<div class="card-body">
					<?php if (!empty($t['photo'])): ?>
						<img src="<?= upload_url($t['photo']) ?>" 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' => 'photo', 
						'accept' => 'image/*', 
						'icon' => 'camera', 
						'label' => 'Choose photo', 
						'preview' => '#imgPrev',
						'multiple' => FALSE
					)); ?>
				</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 ?>" <?= ($t['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) ($t['sort_order'] ?? 0) ?>">
					</div>
					<div class="fld">
						<label class="check">
							<input type="checkbox" name="is_featured" value="1" <?= isset($t['is_featured']) && $t['is_featured'] ? 'checked' : '' ?>>
							<span>Featured on homepage</span>
						</label>
					</div>
					<div class="fld">
						<label class="check">
							<input type="checkbox" name="is_active" value="1" <?= isset($t['is_active']) && $t['is_active'] ? 'checked' : '' ?>>
							<span>Active</span>
						</label>
					</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 Talent</button>
				<a href="<?= base_url('admin/talent') ?>" class="btn btn-ghost">Cancel</a>
			</div>
		</div>
	</div>
</form>