<?php
if (!defined('ABSPATH')) exit;

use BLR\Backline\Modules\Tasks\Module as TasksModule;
use BLR\Backline\Modules\Tasks\Pages as TasksPages;

$__blr_ba_data = isset($ctx['task_view']) && is_array($ctx['task_view']) ? $ctx['task_view'] : TasksPages::view_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
  ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);

if (!empty($not_found)) {
  echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('Task not found.', 'blr-backline') . '</div></div>';
  return;
}
?>
<div class="container-fluid p-0">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <div>
      <h1 class="h3 mb-1"><?php echo esc_html(get_the_title($post)); ?></h1>
      <div class="d-flex gap-2 flex-wrap">
        <?php echo wp_kses_post(TasksModule::badge($status)); ?>
        <?php echo wp_kses_post(TasksModule::badge($priority, 'priority')); ?>
      </div>
    </div>
    <div class="d-flex gap-2">
      <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'tasks/'); ?>"><?php echo esc_html__('Back', 'blr-backline'); ?></a>
      <a class="btn btn-primary" href="<?php echo esc_url($base . 'tasks/edit/' . (int) $id . '/'); ?>"><?php echo esc_html__('Edit', 'blr-backline'); ?></a>
      <?php /* translators: %s: task title used in the delete confirmation prompt. */ ?>
      <form method="post" action="<?php echo esc_url($base . 'tasks/delete/' . (int) $id . '/'); ?>" class="blr-ba-delete-form d-inline" data-item-label="<?php echo esc_attr(sprintf(__('task “%s”', 'blr-backline'), get_the_title($post))); ?>">
        <?php wp_nonce_field('blr_ba_delete_blr_task_' . (int) $id); ?>
        <button type="submit" class="btn btn-outline-danger"><?php echo esc_html__('Delete', 'blr-backline'); ?></button>
      </form>
    </div>
  </div>

  <div class="row g-3">
    <div class="col-12 col-lg-4">
      <div class="card">
        <div class="card-header"><h5 class="card-title mb-0"><?php echo esc_html__('Workflow', 'blr-backline'); ?></h5></div>
        <div class="card-body">
          <div class="mb-2"><strong><?php echo esc_html__('Status:', 'blr-backline'); ?></strong> <?php echo wp_kses_post(TasksModule::badge($status)); ?></div>
          <div class="mb-2"><strong><?php echo esc_html__('Priority:', 'blr-backline'); ?></strong> <?php echo wp_kses_post(TasksModule::badge($priority, 'priority')); ?></div>
          <div class="mb-2"><strong><?php echo esc_html__('Start:', 'blr-backline'); ?></strong> <?php echo esc_html($start_date ?: '—'); ?></div>
          <div class="mb-2"><strong><?php echo esc_html__('Due:', 'blr-backline'); ?></strong> <span class="<?php echo esc_attr(TasksModule::due_state_class($due_date, $status)); ?>"><?php echo esc_html($due_date ?: '—'); ?></span></div>
          <div class="mb-2"><strong><?php echo esc_html__('Assignee:', 'blr-backline'); ?></strong> <?php echo esc_html($assignee instanceof WP_User ? $assignee->display_name : __('Unassigned', 'blr-backline')); ?></div>
          <div><strong><?php echo esc_html__('Completed:', 'blr-backline'); ?></strong> <?php echo esc_html($completed_at ?: '—'); ?></div>
        </div>
      </div>
      <div class="card mt-3">
        <div class="card-header"><h5 class="card-title mb-0"><?php echo esc_html__('Linked Record', 'blr-backline'); ?></h5></div>
        <div class="card-body">
          <?php if ($related_post instanceof WP_Post): ?>
            <div class="fw-semibold"><?php echo esc_html(TasksModule::related_label((int) $related_post->ID)); ?></div>
          <?php else: ?>
            <div class="text-muted"><?php echo esc_html__('No related record.', 'blr-backline'); ?></div>
          <?php endif; ?>
        </div>
      </div>
    </div>
    <div class="col-12 col-lg-8">
      <div class="card">
        <div class="card-header"><h5 class="card-title mb-0"><?php echo esc_html__('Details', 'blr-backline'); ?></h5></div>
        <div class="card-body">
          <?php if (trim((string) $post->post_content) !== ''): ?>
            <?php echo wp_kses_post(wpautop($post->post_content)); ?>
          <?php else: ?>
            <div class="text-muted"><?php echo esc_html__('No details yet.', 'blr-backline'); ?></div>
          <?php endif; ?>
          <?php if (trim($checklist) !== ''): ?>
            <hr>
            <div class="text-muted small mb-2"><?php echo esc_html__('Checklist / Notes', 'blr-backline'); ?></div>
            <?php echo wp_kses_post(wpautop($checklist)); ?>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>
</div>
<p class="small text-muted mb-0"><?php printf(/* translators: 1: modified date, 2: last editor display name */ esc_html__('Last edited %1$s by %2$s', 'blr-backline'), esc_html(get_the_modified_date(get_option('date_format') . ' ' . get_option('time_format'))), esc_html(($r = wp_get_post_revisions(get_the_ID(), ['numberposts' => 1])) ? get_the_author_meta('display_name', (int) array_shift($r)->post_author) : get_the_author_meta('display_name', (int) get_post_field('post_author', get_the_ID())))); ?></p>