<?php
$edited = can_action('56', 'edited');
$all_task_info = get_result('tbl_task', array('opportunities_id' => $opportunity_details->opportunities_id));
?>

<div class="space-y-4 pt-4">
    <div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/50">
        <div class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-200 px-5 py-4 dark:border-slate-700">
            <div>
                <h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('task') ?></h3>
                <p class="text-xs text-slate-500 dark:text-slate-400">
                    <?= count((array) $all_task_info) ?> <?= lang('task') ?>
                </p>
            </div>
            <?php if (!empty($edited)) { ?>
                <a href="<?= base_url() ?>admin/tasks/create/opportunities/<?= $opportunity_details->opportunities_id ?>"
                   class="inline-flex items-center gap-2 rounded-lg bg-slate-900 px-3 py-2 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
                    <i data-lucide="plus" class="h-4 w-4"></i>
                    <?= lang('new_task') ?>
                </a>
            <?php } ?>
        </div>

        <div class="overflow-x-auto">
            <table id="table-milestones" class="min-w-full text-sm">
                <thead class="bg-slate-200 text-slate-500 dark:bg-slate-800/60 dark:text-slate-300">
                    <tr>
                        <th class="px-4 py-3 text-left"></th>
                        <th class="px-4 py-3 text-left"><?= lang('task_name') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('due_date') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('progress') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('changes/view') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php if (!empty($all_task_info)) : foreach ($all_task_info as $v_task) : ?>
                            <?php
                            $due_date = $v_task->due_date;
                            $due_time = safe_strtotime($due_date);
                            $current_time = safe_strtotime(date('Y-m-d'));
                            $is_overdue = ($current_time > $due_time && (int) $v_task->task_progress < 100);
                            if ($v_task->task_status == 'completed') {
                                $status_class = 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300';
                            } elseif ($v_task->task_status == 'not_started') {
                                $status_class = 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300';
                            } elseif ($v_task->task_status == 'deferred') {
                                $status_class = 'bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300';
                            } else {
                                $status_class = 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
                            }
                            ?>
                            <tr id="table-tasks-<?= $v_task->task_id ?>" class="align-middle">
                                <td class="px-4 py-3">
                                    <label class="inline-flex items-center">
                                        <input type="checkbox" data-id="<?= $v_task->task_id ?>" class="h-4 w-4 rounded border-slate-300 text-primary" <?= ((int) $v_task->task_progress >= 100) ? 'checked' : '' ?>>
                                    </label>
                                </td>
                                <td class="px-4 py-3">
                                    <a href="<?= base_url() ?>admin/tasks/details/<?= $v_task->task_id ?>"
                                       class="font-medium text-blue-600 hover:underline dark:text-blue-400 <?= ((int) $v_task->task_progress >= 100) ? 'line-through opacity-70' : '' ?>">
                                        <?= html_escape($v_task->task_name) ?>
                                    </a>
                                </td>
                                <td class="px-4 py-3 text-slate-700 dark:text-slate-200">
                                    <?= display_date($due_date) ?>
                                    <?php if ($is_overdue) { ?>
                                        <span class="ml-2 inline-flex items-center rounded-full bg-rose-100 px-2 py-0.5 text-xs font-semibold text-rose-700 dark:bg-rose-900/40 dark:text-rose-300">
                                            <?= lang('overdue') ?>
                                        </span>
                                    <?php } ?>
                                </td>
                                <td class="px-4 py-3">
                                    <span class="inline-flex rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_class ?>">
                                        <?= lang($v_task->task_status) ?>
                                    </span>
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center gap-2">
                                        <div class="h-2 w-24 rounded-full bg-slate-100 dark:bg-slate-700">
                                            <div class="h-2 rounded-full <?= ((int) $v_task->task_progress >= 100) ? 'bg-emerald-500' : 'bg-rose-400' ?>" style="width: <?= (int) $v_task->task_progress ?>%"></div>
                                        </div>
                                        <span class="text-xs text-slate-500 dark:text-slate-400"><?= (int) $v_task->task_progress ?>%</span>
                                    </div>
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex flex-wrap items-center gap-0">
                                        <?= ajax_anchor(
                                            base_url("admin/tasks/delete_task/" . $v_task->task_id),
                                            "",
                                            array(
                                                "class" => "inline-flex items-center justify-center rounded-lg border border-rose-200 bg-rose-50 p-2 text-rose-600 hover:bg-rose-100 dark:border-rose-900/50 dark:bg-rose-900/20 dark:text-rose-300 dark:hover:bg-rose-900/40",
                                                "title" => lang('delete'),
                                                "data-fade-out-on-success" => "#table-tasks-" . $v_task->task_id
                                            )
                                        ); ?>
                                        <?= btn_edit('admin/tasks/create/' . $v_task->task_id) ?>
                                        <?php if (timer_status('tasks', $v_task->task_id, 'on')) { ?>
                                            <a class="ml-1 inline-flex h-8 min-w-[92px] items-center justify-center rounded-lg border border-rose-200 bg-rose-600 px-3 text-xs font-semibold text-white shadow-sm transition hover:bg-rose-500"
                                               href="<?= base_url() ?>admin/tasks/tasks_timer/off/<?= $v_task->task_id ?>">
                                                <?= lang('stop_timer') ?>
                                            </a>
                                        <?php } else { ?>
                                            <a class="ml-1 inline-flex h-8 min-w-[92px] items-center justify-center rounded-lg border border-emerald-200 bg-emerald-600 px-3 text-xs font-semibold text-white shadow-sm transition hover:bg-emerald-500"
                                               href="<?= base_url() ?>admin/tasks/tasks_timer/on/<?= $v_task->task_id ?>">
                                                <?= lang('start_timer') ?>
                                            </a>
                                        <?php } ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else : ?>
                        <tr>
                            <td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500 dark:text-slate-400">
                                <?= lang('no_data_found') ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
