<?php
$started = 0;
$in_progress = 0;
$cancel = 0;
$completed = 0;
$project_time = 0;
$project_count = !empty($all_project) ? count($all_project) : 0;
if (!empty($all_project)) {
    foreach ($all_project as $v_project) {
        if ($v_project->project_status == 'started') {
            $started++;
        }
        if ($v_project->project_status == 'in_progress') {
            $in_progress++;
        }
        if ($v_project->project_status == 'completed') {
            $completed++;
        }
        if ($v_project->project_status == 'cancel') {
            $cancel++;
        }
    }
}
$project_info = $this->report_model->get_permission('tbl_project');
if (!empty($project_info)) {
    foreach ($project_info as $v_projects) {
        $project_time += $this->report_model->task_spent_time_by_id($v_projects->project_id, true);
    }
}

$summary_cards = array(
    array(
        'label' => lang('project'),
        'value' => $project_count,
        'meta' => 'Projects included in the current portfolio view',
        'icon' => 'folder-kanban',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('in_progress'),
        'value' => $in_progress,
        'meta' => 'Projects actively moving through delivery',
        'icon' => 'loader-circle',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('completed'),
        'value' => $completed,
        'meta' => 'Projects fully delivered and closed',
        'icon' => 'check-circle-2',
        'tone' => 'text-emerald-600 dark:text-emerald-300 bg-emerald-500/10',
    ),
    array(
        'label' => lang('time_spent'),
        'value' => $this->report_model->get_time_spent_result($project_time),
        'meta' => 'Logged execution time across all projects',
        'icon' => 'clock-3',
        'tone' => 'text-violet-600 dark:text-violet-300 bg-violet-500/10',
    ),
);

$status_categories = array(
    lang('started'),
    lang('in_progress'),
    lang('completed'),
    lang('cancel'),
);
$status_series = array(array(
    'name' => lang('project'),
    'categories' => $status_categories,
    'data' => array($started, $in_progress, $completed, $cancel),
));

$assignment_categories = array();
$assignment_started = array();
$assignment_in_progress = array();
$assignment_cancel = array();
if (!empty($user_project) && !empty($assign_user)) {
    foreach ($user_project as $user => $v_project_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_started[] = !empty($v_project_user['started']) ? count((array)$v_project_user['started']) : 0;
            $assignment_in_progress[] = !empty($v_project_user['in_progress']) ? count((array)$v_project_user['in_progress']) : 0;
            $assignment_cancel[] = !empty($v_project_user['cancel']) ? count((array)$v_project_user['cancel']) : 0;
            break;
        }
    }
}
if (empty($assignment_categories)) {
    $assignment_categories = array(lang('no_data_found'));
    $assignment_started = array(0);
    $assignment_in_progress = array(0);
    $assignment_cancel = array(0);
}
$assignment_series = array(
    array('name' => lang('started'), 'categories' => $assignment_categories, 'data' => $assignment_started),
    array('name' => lang('in_progress'), 'categories' => $assignment_categories, 'data' => $assignment_in_progress),
    array('name' => lang('cancel'), 'categories' => $assignment_categories, 'data' => $assignment_cancel),
);
?>
<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 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('project') . ' ' . lang('status') ?></h3>
                <p class="text-sm text-muted-foreground">Status mix across the current project portfolio.</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('project_assignment') ?></h3>
                <p class="text-sm text-muted-foreground">Project ownership split by team member and delivery state.</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>
