<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
	$assignment = isset($assignment) && is_array($assignment) ? $assignment : array();
	$status_label = isset($assignment['status_label']) ? (string) $assignment['status_label'] : 'Pendiente';
	$status_class = isset($assignment['status_class']) ? (string) $assignment['status_class'] : 'warning';
	$items = !empty($assignment['items']) && is_array($assignment['items']) ? $assignment['items'] : array();
	$notes = !empty($assignment['notes']) && is_array($assignment['notes']) ? $assignment['notes'] : array();
	$flash_message = isset($flash_message) ? (string) $flash_message : '';
	$flash_error = isset($flash_error) ? (string) $flash_error : '';
	$compact_header = !empty($compact_header);
?>

<div class="task-modal-card">
	<div class="task-modal-head">
		<div>
			<?php if (empty($compact_header)) { ?>
				<h3><?php echo htmlspecialchars(isset($assignment['title']) ? (string) $assignment['title'] : 'Tarea', ENT_QUOTES, 'UTF-8'); ?></h3>
			<?php } ?>
			<div class="task-modal-meta">
				<span class="badge <?php echo htmlspecialchars($status_class, ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlspecialchars($status_label, ENT_QUOTES, 'UTF-8'); ?></span>
				<span>Asignada: <?php echo htmlspecialchars(isset($assignment['assigned_date_label']) ? $assignment['assigned_date_label'] : '-', ENT_QUOTES, 'UTF-8'); ?></span>
				<span>Vence: <?php echo htmlspecialchars(isset($assignment['due_date_label']) ? $assignment['due_date_label'] : '-', ENT_QUOTES, 'UTF-8'); ?></span>
			</div>
		</div>
		<div class="actions" style="margin-top:0;">
			<button class="btn btn-outline js-modal-edit-toggle" type="button">Editar</button>
		</div>
	</div>

	<?php if ($flash_message !== '') { ?>
		<div class="panel panel-message success" style="margin-bottom:0;">
			<?php echo htmlspecialchars($flash_message, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>
	<?php if ($flash_error !== '') { ?>
		<div class="panel panel-message error" style="margin-bottom:0;">
			<?php echo htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>

	<form class="task-modal-section js-modal-edit-form" method="post" action="<?php echo site_url('tareas/'.(int) (isset($assignment['id']) ? $assignment['id'] : 0)); ?>" style="display:none;">
		<input type="hidden" name="action_type" value="update_assignment" />
		<div class="task-modal-form-grid">
			<label class="task-field">
				<span>Titulo</span>
				<input type="text" name="title" value="<?php echo htmlspecialchars(isset($assignment['title']) ? (string) $assignment['title'] : '', ENT_QUOTES, 'UTF-8'); ?>" required />
			</label>
			<label class="task-field">
				<span>Vence</span>
				<input type="date" name="due_date" value="<?php echo htmlspecialchars(!empty($assignment['due_date']) ? (string) $assignment['due_date'] : '', ENT_QUOTES, 'UTF-8'); ?>" />
			</label>
		</div>
		<label class="task-field">
			<span>Detalle</span>
			<textarea name="description" rows="4"><?php echo htmlspecialchars(isset($assignment['description']) ? (string) $assignment['description'] : '', ENT_QUOTES, 'UTF-8'); ?></textarea>
		</label>
		<div class="actions" style="justify-content:flex-end;">
			<button class="btn btn-outline js-modal-edit-cancel" type="button">Cancelar</button>
			<button class="btn" type="submit">Guardar cambios</button>
		</div>
	</form>

	<div class="task-modal-section">
		<h4>Detalle</h4>
		<p class="note">
			<?php echo htmlspecialchars(!empty($assignment['description']) ? (string) $assignment['description'] : 'Sin detalle', ENT_QUOTES, 'UTF-8'); ?>
		</p>
	</div>

	<div class="task-modal-grid">
		<section class="task-modal-section">
			<h4>Subtareas</h4>
			<?php if (empty($items)) { ?>
				<div class="empty">Sin subtareas.</div>
			<?php } else { ?>
				<div class="task-modal-list">
					<?php foreach ($items as $item) { ?>
						<?php $completed = isset($item['status']) && $item['status'] === 'completed'; ?>
						<div class="task-modal-row">
							<span><?php echo htmlspecialchars(isset($item['name']) ? (string) $item['name'] : 'Subtarea', ENT_QUOTES, 'UTF-8'); ?></span>
							<span class="badge <?php echo $completed ? 'success' : 'warning'; ?>"><?php echo $completed ? 'Completada' : 'Pendiente'; ?></span>
						</div>
					<?php } ?>
				</div>
			<?php } ?>
		</section>

		<section class="task-modal-section">
			<h4>Notas</h4>
			<?php if (empty($notes)) { ?>
				<div class="empty">Sin notas registradas.</div>
			<?php } else { ?>
				<div class="task-modal-list">
					<?php foreach ($notes as $note) { ?>
						<div class="task-modal-note">
							<div><?php echo htmlspecialchars(isset($note['note_text']) ? (string) $note['note_text'] : '', ENT_QUOTES, 'UTF-8'); ?></div>
							<div class="note" style="margin-top:6px;">
								<?php echo htmlspecialchars(isset($note['note_type']) ? (string) $note['note_type'] : 'nota', ENT_QUOTES, 'UTF-8'); ?>
								<?php if (!empty($note['created_at'])) { ?>
									| <?php echo htmlspecialchars(date('d/m/Y h:i A', strtotime($note['created_at'])), ENT_QUOTES, 'UTF-8'); ?>
								<?php } ?>
							</div>
						</div>
					<?php } ?>
				</div>
			<?php } ?>
		</section>
	</div>
</div>
