<?php
use BLR\Backline\Core\Access;
use BLR\Backline\Modules\Tasks\Module as TasksModule;
use BLR\Backline\Modules\Tasks\Pages as TasksPages;

if (!defined('ABSPATH')) {
  exit;
}

$__blr_ba_data = isset($ctx['task_form']) && is_array($ctx['task_form']) ? $ctx['task_form'] : TasksPages::form_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 ($is_edit && (!$post || $post->post_type !== 'blr_task')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('Task not found.', 'blr-backline') . '</div></div>'; return; }
if ($is_edit && !Access::can_edit_post($post_id, 'blr_task')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('You do not have permission to edit this task.', 'blr-backline') . '</div></div>'; return; }
if (!$is_edit && !Access::can_create_post_type('blr_task')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('You do not have permission to create tasks.', 'blr-backline') . '</div></div>'; return; }

$getm = static function (string $key) use ($post_id, $failed_post, $posted_values) {
  return TasksPages::task_meta_value($post_id, $key, $failed_post, $posted_values);
};
?>
<div class="container-fluid p-0">
  <div class="d-flex align-items-center justify-content-between mb-3">
    <h1 class="h3 mb-0"><?php echo $is_edit ? esc_html__('Edit Task', 'blr-backline') : esc_html__('New Task', 'blr-backline'); ?></h1>
    <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'tasks/'); ?>"><?php echo esc_html__('Back', 'blr-backline'); ?></a>
  </div>

  <?php if ($saved_notice): ?><div class="alert alert-success"><?php echo esc_html__('Saved.', 'blr-backline'); ?></div><?php endif; ?>
  <?php foreach ($errors as $e): ?><div class="alert alert-danger"><?php echo esc_html($e); ?></div><?php endforeach; ?>

  <div class="card">
    <div class="card-body">
      <form method="post">
        <?php wp_nonce_field('blr_ba_save_task'); ?>
        <div class="row">
          <div class="col-12 col-lg-8">
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Task', 'blr-backline'); ?></label>
              <input class="form-control" type="text" name="title" value="<?php echo esc_attr($title_v); ?>" required>
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Details', 'blr-backline'); ?></label>
              <textarea class="form-control" name="content" rows="8"><?php echo esc_textarea($content_v); ?></textarea>
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Checklist / Notes', 'blr-backline'); ?></label>
              <textarea class="form-control" name="blr_task_checklist" rows="5" placeholder="<?php echo esc_attr__('- Draft artwork&#10;- Get approval&#10;- Upload masters', 'blr-backline'); ?>"><?php echo esc_textarea((string) $getm('blr_task_checklist')); ?></textarea>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Status', 'blr-backline'); ?></label>
              <select class="form-select" name="blr_task_status">
                <?php foreach (TasksModule::statuses() as $status): ?>
                  <option value="<?php echo esc_attr($status); ?>" <?php selected((string) $getm('blr_task_status'), $status); ?>><?php echo esc_html(TasksModule::status_label($status)); ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Priority', 'blr-backline'); ?></label>
              <select class="form-select" name="blr_task_priority">
                <?php foreach (TasksModule::priorities() as $priority): ?>
                  <option value="<?php echo esc_attr($priority); ?>" <?php selected((string) $getm('blr_task_priority'), $priority); ?>><?php echo esc_html(TasksModule::priority_label($priority)); ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Start Date', 'blr-backline'); ?></label>
              <input class="form-control" type="date" name="blr_task_start_date" value="<?php echo esc_attr((string) $getm('blr_task_start_date')); ?>">
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Due Date', 'blr-backline'); ?></label>
              <input class="form-control" type="date" name="blr_task_due_date" value="<?php echo esc_attr((string) $getm('blr_task_due_date')); ?>">
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Assignee', 'blr-backline'); ?></label>
              <select class="form-select" name="blr_task_assignee_user_id">
                <option value="0"><?php echo esc_html__('Unassigned', 'blr-backline'); ?></option>
                <?php foreach ($users as $user): ?>
                  <option value="<?php echo (int) $user->ID; ?>" <?php selected((int) $getm('blr_task_assignee_user_id'), (int) $user->ID); ?>><?php echo esc_html($user->display_name); ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Related Record', 'blr-backline'); ?></label>
              <select class="form-select" name="blr_task_related_post_id">
                <option value="0"><?php echo esc_html__('None', 'blr-backline'); ?></option>
                <?php foreach ($related_options as $related_id => $label): ?>
                  <option value="<?php echo (int) $related_id; ?>" <?php selected((int) $getm('blr_task_related_post_id'), (int) $related_id); ?>><?php echo esc_html($label); ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <?php if ((string) $getm('blr_task_completed_at') !== ''): ?>
              <div class="alert alert-light border"><?php echo esc_html__('Completed at:', 'blr-backline'); ?> <strong><?php echo esc_html((string) $getm('blr_task_completed_at')); ?></strong></div>
            <?php endif; ?>
          </div>
        </div>
        <div class="d-flex gap-2"><button class="btn btn-primary" type="submit"><?php echo esc_html__('Save', 'blr-backline'); ?></button><a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'tasks/'); ?>"><?php echo esc_html__('Cancel', 'blr-backline'); ?></a></div>
      </form>
    </div>
  </div>
</div>
