<?php
$project_overview = isset($project_overview) && is_array($project_overview) ? $project_overview : array();
$project_cost = isset($project_overview['project_cost']) ? (float) $project_overview['project_cost'] : 0;
$project_hours = isset($project_overview['project_hours']) ? $project_overview['project_hours'] : 0;
$tasks_hours = isset($project_overview['tasks_hours']) ? $project_overview['tasks_hours'] : 0;
$open_tasks = in_array($project_details->billing_type, array('tasks_hours', 'tasks_and_project_hours', 'tasks_sub_tasks_and_project_hours'), true);
$total_task = isset($project_overview['total_task']) ? (int) $project_overview['total_task'] : 0;
$completed = isset($project_overview['completed_task']) ? (int) $project_overview['completed_task'] : 0;
$total_expense_amount = isset($project_overview['total_expense']) ? (float) $project_overview['total_expense'] : 0;
$billable_expense_amount = isset($project_overview['billable_expense']) ? (float) $project_overview['billable_expense'] : 0;
$not_billable_expense_amount = isset($project_overview['not_billable_expense']) ? (float) $project_overview['not_billable_expense'] : 0;
$attachments_count = isset($project_overview['attachments_count']) ? (int) $project_overview['attachments_count'] : 0;
$comments_count = isset($project_overview['comments_count']) ? (int) $project_overview['comments_count'] : 0;
$comment_type = 'projects';
$can_edit = $this->project_model->can_action('tbl_project', 'edit', array('project_id' => $project_details->project_id));
$currency = isset($project_currency) ? $project_currency : null;
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$this->load->helper('date');
$totalDays = round((human_to_unix($project_details->end_date . ' 00:00') - human_to_unix($project_details->start_date . ' 00:00')) / 3600 / 24);
$TotalGone = $totalDays;
$tprogress = 100;
if ($totalDays > 0 && human_to_unix($project_details->start_date . ' 00:00') < time() && human_to_unix($project_details->end_date . ' 00:00') > time()) {
	$TotalGone = round((human_to_unix($project_details->end_date . ' 00:00') - time()) / 3600 / 24);
	$tprogress = $TotalGone / $totalDays * 100;
}
if (human_to_unix($project_details->end_date . ' 00:00') < time()) {
	$TotalGone = 0;
	$tprogress = 0;
}
if (safe_strtotime(date('Y-m-d')) > safe_strtotime($project_details->end_date . '00:00')) {
	$lang = lang('days_gone');
} else {
	$lang = lang('days_left');
}
$project_settings = !empty($project_details->project_settings)
	? normalize_project_settings($project_details->project_settings)
	: array();
if (empty(admin())) {
	if (!empty($project_settings) && in_array('show_staff_finance_overview', $project_settings)) {
		$staff_finance = true;
	} else {
		$staff_finance = false;
	}
} else {
	$staff_finance = true;
}

$name = !empty($project_details->client_name) ? $project_details->client_name : '-';
$pc_name = !empty($project_details->category_name) ? $project_details->category_name : '-';

$task_progress = $total_task > 0 ? min(100, ($completed / $total_task) * 100) : 0;
$project_progress = (float) $this->project_model->get_project_progress($project_details->project_id);
$project_progress = min(100, max(0, $project_progress));

$overdueDays = null;
$is_overdue = $project_details->project_status != 'completed' && $totalDays < 0;
if ($is_overdue) {
	$overdueDays = $totalDays . ' ' . lang('days_gone');
}

$status_map = array(
	'completed' => 'bg-emerald-50 text-emerald-700 border-emerald-200',
	'in_progress' => 'bg-blue-50 text-blue-700 border-blue-200',
	'cancel' => 'bg-rose-50 text-rose-700 border-rose-200',
	'on_hold' => 'bg-amber-50 text-amber-700 border-amber-200',
	'started' => 'bg-indigo-50 text-indigo-700 border-indigo-200',
);
$status_key = $project_details->project_status ?: 'unknown';
$status_badge = isset($status_map[$status_key]) ? $status_map[$status_key] : 'bg-slate-100 text-slate-700 border-slate-200';

$timer_on = !empty($project_timer_on);

$participants = isset($project_participants) ? $project_participants : array();

$completion_label = $project_progress >= 80 ? 'On Track' : ($project_progress >= 50 ? 'At Risk' : 'Needs Attention');
$completion_tone = $project_progress >= 80 ? 'bg-emerald-50 text-emerald-700 border-emerald-200' : ($project_progress >= 50 ? 'bg-amber-50 text-amber-700 border-amber-200' : 'bg-rose-50 text-rose-700 border-rose-200');
$health_score = round(($project_progress + $task_progress) / 2);
$budget_spent = $total_expense_amount;
$budget_total = (float) $project_cost;
$budget_percent = $budget_total > 0 ? min(100, ($budget_spent / $budget_total) * 100) : 0;

$project_details_style = isset($project_details_style) ? $project_details_style : 'style_one';

$style_view_map = array(
	'style_one' => 'admin/projects/project_details/styles/details_style_one',
	'style_two' => 'admin/projects/project_details/styles/details_style_two',
	'style_three' => 'admin/projects/project_details/styles/details_style_three',
);
$style_view = isset($style_view_map[$project_details_style]) ? $style_view_map[$project_details_style] : $style_view_map['style_one'];
$this->load->view($style_view, get_defined_vars());
