<?php
$opts = !empty($task['options_json']) ? (json_decode($task['options_json'], true) ?: []) : [];
$optsCsv = implode(',', array_map('strval', $opts));
$photoReq = $task['photo_requirement'] ?? 'n_a';
$inputType = $task['input_type'] ?? 'checkbox';
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-pen me-2 text-primary"></i>Edit Task · <?= e($template['name'] ?? '') ?></h5>
    <a href="<?= url('/PmTemplates/show/' . (int) $template['template_id']) ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/PmTemplates/updateTask/' . (int) $task['template_task_id']) ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <div class="row g-2">
        <div class="col-md-8"><label class="form-label">Title *</label>
            <input type="text" class="form-control form-control-sm" name="title" required value="<?= e($task['title']) ?>">
        </div>
        <div class="col-md-2"><label class="form-label">Order</label>
            <input type="number" class="form-control form-control-sm" name="sort_order" value="<?= (int) $task['sort_order'] ?>">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="is_required" value="1" id="etReq" <?= !empty($task['is_required']) ? 'checked' : '' ?>>
                <label for="etReq" class="form-check-label">Required</label>
            </div>
        </div>

        <div class="col-md-3"><label class="form-label">Input type</label>
            <select name="input_type" class="form-select form-select-sm">
                <?php foreach (\App\Models\PmTemplate::TASK_INPUT_TYPES as $it): ?>
                    <option value="<?= e($it) ?>" <?= $inputType === $it ? 'selected' : '' ?>><?= e($it) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-6"><label class="form-label">Options (comma-separated, only when Input Type = options)</label>
            <input type="text" class="form-control form-control-sm" name="options_json" value="<?= e($optsCsv) ?>" placeholder="OK,HIGH,LOW">
        </div>
        <div class="col-md-3"><label class="form-label">Photo requirement</label>
            <select name="photo_requirement" class="form-select form-select-sm">
                <?php foreach (\App\Models\PmTemplate::TASK_PHOTO_REQUIREMENTS as $pr): ?>
                    <option value="<?= e($pr) ?>" <?= $photoReq === $pr ? 'selected' : '' ?>><?= e($pr === 'n_a' ? 'N / A' : $pr) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="col-12"><label class="form-label">Description (optional)</label>
            <textarea class="form-control form-control-sm" name="description" rows="2"><?= e($task['description'] ?? '') ?></textarea>
        </div>
    </div>
    <div class="mt-3 d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/PmTemplates/show/' . (int) $template['template_id']) ?>">Cancel</a>
    </div>
</form>
</div>

<form method="post" action="<?= url('/PmTemplates/deleteTask/' . (int) $task['template_task_id']) ?>"
      class="mt-3 rs-confirm-submit"
      data-confirm-title="Delete task?"
      data-confirm-body="This will remove the task from the template. Existing work-order tasks are not affected."
      data-confirm-ok="Delete">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete task</button>
</form>
