<?php
$relatedType = (string) ($relatedType ?? '');
$relatedId = (int) ($relatedId ?? 0);
$tasks = $tasks ?? [];
$titleClass = $titleClass ?? 'rec-section-title';
$heading = $heading ?? 'Tasks';
$hint = $hint ?? 'Things to do next on this record.';
$icon = $icon ?? 'bi-check2-square';
$ownerId = (int) ($ownerId ?? 0);
$empty = $empty ?? 'No tasks on this record yet.';
$boxId = $boxId ?? 'related-tasks';
if (!can_see('tasks') || $relatedType === '' || $relatedId < 1) {
    return;
}
?>
<div class="card-soft rec-card p-3 mb-3" id="<?= e($boxId) ?>">
  <?php \App\Core\View::partial('record_section_head', [
      'title' => $heading,
      'hint' => $hint,
      'icon' => $icon,
  ]); ?>
  <?php if (can_edit('tasks')): ?>
    <form class="record-composer is-task ajax-form mb-3" action="<?= e(url('tasks')) ?>" method="post" enctype="multipart/form-data">
      <?= csrf_field() ?>
      <input type="hidden" name="related_type" value="<?= e($relatedType) ?>">
      <input type="hidden" name="related_id" value="<?= $relatedId ?>">
      <?php if ($ownerId > 0): ?>
        <input type="hidden" name="owner_id" value="<?= $ownerId ?>">
      <?php endif; ?>
      <div class="record-composer-head">
        <span class="record-composer-icon"><i class="bi bi-check2-square"></i></span>
        <div>
          <div class="record-composer-title">Add a task</div>
          <div class="record-composer-sub">What needs to happen next on this record.</div>
        </div>
      </div>
      <div class="record-composer-grid">
        <div class="record-composer-span">
          <label class="form-label" for="task-title-<?= e($relatedType) ?>-<?= $relatedId ?>">Task *</label>
          <input class="form-control" id="task-title-<?= e($relatedType) ?>-<?= $relatedId ?>" name="title" required maxlength="180" placeholder="What needs to happen?">
        </div>
        <div>
          <label class="form-label" for="task-date-<?= e($relatedType) ?>-<?= $relatedId ?>">Date</label>
          <input class="form-control" id="task-date-<?= e($relatedType) ?>-<?= $relatedId ?>" type="date" name="due_date">
        </div>
        <div>
          <label class="form-label" for="task-time-<?= e($relatedType) ?>-<?= $relatedId ?>">Time</label>
          <input class="form-control" id="task-time-<?= e($relatedType) ?>-<?= $relatedId ?>" type="time" name="due_time">
        </div>
        <div>
          <label class="form-label" for="task-prio-<?= e($relatedType) ?>-<?= $relatedId ?>">Priority</label>
          <select class="form-select" id="task-prio-<?= e($relatedType) ?>-<?= $relatedId ?>" name="priority">
            <option value="normal">Normal</option>
            <option value="high">High</option>
            <option value="low">Low</option>
          </select>
        </div>
        <div class="record-composer-span">
          <label class="form-label">File</label>
          <label class="record-file">
            <i class="bi bi-paperclip"></i>
            <span class="record-file-copy">
              <span class="record-file-name">Choose a file</span>
              <span class="record-file-hint">PDF, Word, JPEG, or PNG · up to 8 MB</span>
            </span>
            <input type="file" name="file" accept=".pdf,.doc,.docx,.jpg,.jpeg,.png,application/pdf,image/jpeg,image/png,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
          </label>
        </div>
      </div>
      <div class="record-composer-actions">
        <button class="btn btn-primary" type="submit"><i class="bi bi-plus-lg"></i> Add task</button>
      </div>
    </form>
  <?php endif; ?>
  <?php if (!$tasks): ?>
    <p class="text-muted mb-0"><?= e($empty) ?></p>
  <?php else: foreach ($tasks as $tk):
      $tdue = due_label($tk['due_at'] ?? null, $tk['status'] ?? 'open');
      $st = (string) ($tk['status'] ?? 'open');
      $stLabel = $st === 'completed' ? 'Done' : ($st === 'cancelled' ? 'Cancelled' : 'Open');
  ?>
    <div class="account-task-row">
      <div class="min-w-0">
        <div class="fw-semibold"><?= e($tk['title']) ?></div>
        <div class="small text-muted"><?= e(trim((string) ($tk['owner_name'] ?? '')) ?: 'Unassigned') ?> · <?= e($stLabel) ?></div>
        <?= task_file_chip($tk['file_name'] ?? null, (int) $tk['id']) ?>
      </div>
      <?php if (!empty($tdue['text'])): ?>
        <span class="due-pill <?= e($tdue['class']) ?>"><?= e($tdue['text']) ?></span>
      <?php endif; ?>
    </div>
  <?php endforeach; endif; ?>
</div>
