<?php
$not_started = 0;
$in_progress = 0;
$completed = 0;
$deferred = 0;
$waiting_for_someone = 0;
$task_time = 0;
$tasks_count = !empty($all_tasks) ? count($all_tasks) : 0;
if (!empty($all_tasks)) {
    foreach ($all_tasks as $v_tasks) {
        if ($v_tasks->task_status == 'not_started') {
            $not_started++;
        }
        if ($v_tasks->task_status == 'in_progress') {
            $in_progress++;
        }
        if ($v_tasks->task_status == 'completed') {
            $completed++;
        }
        if ($v_tasks->task_status == 'deferred') {
            $deferred++;
        }
        if ($v_tasks->task_status == 'waiting_for_someone') {
            $waiting_for_someone++;
        }
    }
}
$tasks_info = $this->report_model->get_permission('tbl_task');
if (!empty($tasks_info)) {
    foreach ($tasks_info as $v_tasks) {
        $task_time += $this->report_model->task_spent_time_by_id($v_tasks->task_id);
    }
}

$summary_cards = array(
    array(
        'label' => lang('task'),
        'value' => $tasks_count,
        'meta' => 'Operational tasks in the current report scope',
        'icon' => 'list-todo',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('completed'),
        'value' => $completed,
        'meta' => 'Tasks closed and delivered successfully',
        'icon' => 'check-circle-2',
        'tone' => 'text-emerald-600 dark:text-emerald-300 bg-emerald-500/10',
    ),
    array(
        'label' => lang('in_progress'),
        'value' => $in_progress,
        'meta' => 'Tasks currently under active work',
        'icon' => 'loader-circle',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('time_spent'),
        'value' => $this->report_model->get_time_spent_result($task_time),
        'meta' => 'Logged effort across all task records',
        'icon' => 'clock-3',
        'tone' => 'text-violet-600 dark:text-violet-300 bg-violet-500/10',
    ),
);

$status_categories = array(
    lang('not_started'),
    lang('in_progress'),
    lang('completed'),
    lang('deferred'),
    lang('waiting_for_someone'),
);
$status_series = array(array(
    'name' => lang('task'),
    'categories' => $status_categories,
    'data' => array($not_started, $in_progress, $completed, $deferred, $waiting_for_someone),
));

$assignment_categories = array();
$assignment_not_started = array();
$assignment_in_progress = array();
$assignment_deferred = array();
if (!empty($user_tasks) && !empty($assign_user)) {
    foreach ($user_tasks as $user => $v_task_user) {
        if ($user == 'all') {
            continue;
        }
        foreach ($assign_user as $v_user) {
            if ((string)$v_user->user_id !== (string)$user) {
                continue;
            }
            $assignment_categories[] = !empty($v_user->username) ? $v_user->username : fullname($v_user->user_id);
            $assignment_not_started[] = !empty($v_task_user['not_started']) ? count((array)$v_task_user['not_started']) : 0;
            $assignment_in_progress[] = !empty($v_task_user['in_progress']) ? count((array)$v_task_user['in_progress']) : 0;
            $assignment_deferred[] = !empty($v_task_user['deferred']) ? count((array)$v_task_user['deferred']) : 0;
            break;
        }
    }
}
if (empty($assignment_categories)) {
    $assignment_categories = array(lang('no_data_found'));
    $assignment_not_started = array(0);
    $assignment_in_progress = array(0);
    $assignment_deferred = array(0);
}
$assignment_series = array(
    array('name' => lang('not_started'), 'categories' => $assignment_categories, 'data' => $assignment_not_started),
    array('name' => lang('in_progress'), 'categories' => $assignment_categories, 'data' => $assignment_in_progress),
    array('name' => lang('deferred'), 'categories' => $assignment_categories, 'data' => $assignment_deferred),
);
?>
<div class="space-y-6 sales-report-page">
    <div class="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4 sales-report-cardgrid">
        <?php foreach ($summary_cards as $card): ?>
            <div class="rounded-xl border border-border bg-card p-4 shadow-sm sales-report-card">
                <div class="flex items-start justify-between gap-3">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= $card['label'] ?></p>
                        <p class="mt-2 text-xl font-bold text-foreground"><?= $card['value'] ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= $card['meta'] ?></p>
                    </div>
                    <span class="inline-flex h-10 w-10 shrink-0 items-center justify-center rounded-xl <?= $card['tone'] ?>">
                        <i data-lucide="<?= $card['icon'] ?>" class="h-5 w-5"></i>
                    </span>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <section class="grid grid-cols-1 gap-6 xl:grid-cols-12">
        <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4 xl:col-span-4">
            <div class="mb-4 border-b border-border pb-4">
                <h3 class="text-lg font-semibold text-foreground"><?= lang('task') . ' ' . lang('status') ?></h3>
                <p class="text-sm text-muted-foreground">Distribution of task status across active and completed work items.</p>
            </div>
            <div class="sales-report-chart" data-chart="bar" data-height="320" data-height-sm="240" data-series='<?= json_encode($status_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
        </div>
        <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4 xl:col-span-8">
            <div class="mb-4 border-b border-border pb-4">
                <h3 class="text-lg font-semibold text-foreground"><?= lang('tasks_r_assignment') ?></h3>
                <p class="text-sm text-muted-foreground">Assignment load split by not started, in progress, and deferred work.</p>
            </div>
            <div class="sales-report-chart" data-chart="bar" data-height="320" data-height-sm="240" data-series='<?= json_encode($assignment_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
        </div>
    </section>
</div>
