<?php
if (!empty($project_details->client_id)) {
    $currency = $this->items_model->client_currency_symbol($project_details->client_id);
} else {
    $currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
}

$project_settings = json_decode($project_details->project_settings);
$project_cost = $this->items_model->calculate_project('project_cost', $project_details->project_id);
$project_hours = $this->items_model->calculate_project('project_hours', $project_details->project_id);
$all_expense_info = get_result('tbl_transactions', array('project_id' => $project_details->project_id, 'type' => 'Expense'));
$project_finance_summary = isset($project_finance_summary) ? $project_finance_summary : array('total_amount' => 0, 'billable_amount' => 0, 'not_billable_amount' => 0);

$total_task = (int) total_rows('tbl_task', array('project_id' => $project_details->project_id));
$completed = (int) total_rows('tbl_task', array('project_id' => $project_details->project_id, 'task_status' => 'completed'));
$task_progress = $total_task > 0 ? min(100, ($completed / $total_task) * 100) : 0;
$project_progress = (float) $this->items_model->get_project_progress($project_details->project_id);
$project_progress = min(100, max(0, $project_progress));

$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 (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 = is_array($project_settings) ? $project_settings : array();
$staff_finance = in_array('show_finance_overview', $project_settings);

$client_info = $this->db->where('client_id', $project_details->client_id)->get('tbl_client')->row();
$name = !empty($client_info) ? $client_info->name : '-';
$p_category = $this->db->where('customer_group_id', $project_details->category_id)->get('tbl_customer_group')->row();
$pc_name = !empty($p_category) ? $p_category->customer_group : '-';

$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 dark:bg-emerald-900/30 dark:text-emerald-200 dark:border-emerald-800',
    'in_progress' => 'bg-blue-50 text-blue-700 border-blue-200 dark:bg-blue-900/30 dark:text-blue-200 dark:border-blue-800',
    'cancel' => 'bg-rose-50 text-rose-700 border-rose-200 dark:bg-rose-900/30 dark:text-rose-200 dark:border-rose-800',
    'on_hold' => 'bg-amber-50 text-amber-700 border-amber-200 dark:bg-amber-900/30 dark:text-amber-200 dark:border-amber-800',
    'started' => 'bg-indigo-50 text-indigo-700 border-indigo-200 dark:bg-indigo-900/30 dark:text-indigo-200 dark:border-indigo-800',
);
$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 dark:bg-slate-800 dark:text-slate-200 dark:border-slate-700';

$timer_on = timer_status('projects', $project_details->project_id, 'on');

$participants = array();
if ($project_details->permission != 'all') {
    $permission = json_decode($project_details->permission);
    $permission_ids = array_keys((array) $permission);
    if (!empty($permission_ids)) {
        $participants = $this->db
            ->select('tbl_users.user_id, tbl_users.role_id, tbl_account_details.fullname, tbl_account_details.avatar')
            ->from('tbl_users')
            ->join('tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id', 'left')
            ->where_in('tbl_users.user_id', $permission_ids)
            ->get()->result();
    }
}

$health_score = round(($project_progress + $task_progress) / 2);
$budget_spent = !empty($project_finance_summary['total_amount']) ? (float) $project_finance_summary['total_amount'] : 0;
$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 : null;
if (empty($project_details_style)) {
    $project_details_style = config_item('project_details_style');
}
if (empty($project_details_style)) {
    $project_details_style = 'style_one';
}
if (is_numeric($project_details_style)) {
    $style_map = array('1' => 'style_one', '2' => 'style_two', '3' => 'style_three');
    $project_details_style = isset($style_map[$project_details_style]) ? $style_map[$project_details_style] : 'style_one';
}

$style_view_map = array(
    'style_one' => 'client/projects/project_details/styles/details_style_one',
    'style_two' => 'client/projects/project_details/styles/details_style_two',
    'style_three' => 'client/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());
