<?php
$project_cost = $this->project_model->calculate_project('project_cost', $project_details->project_id);
$all_task_info = total_rows('tbl_task', array('project_id' => $project_details->project_id));
$all_expense_info = get_result('tbl_transactions', array('project_id' => $project_details->project_id, 'type' => 'Expense'));
$total_expense = $this->db->select_sum('amount')->where(array('project_id' => $project_details->project_id, 'type' => 'Expense'))->get('tbl_transactions')->row();
$billable_expense = $this->db->select_sum('amount')->where(array('project_id' => $project_details->project_id, 'type' => 'Expense', 'billable' => 'Yes'))->get('tbl_transactions')->row();
$not_billable_expense = $this->db->select_sum('amount')->where(array('project_id' => $project_details->project_id, 'type' => 'Expense', 'billable' => 'No'))->get('tbl_transactions')->row();

$project_hours = $this->project_model->calculate_project('project_hours', $project_details->project_id);
$tasks_hours = 0;
$open_tasks = false;
if ($project_details->billing_type == 'tasks_hours' || $project_details->billing_type == 'tasks_and_project_hours' || $project_details->billing_type == 'tasks_sub_tasks_and_project_hours') {
    $tasks_hours = $this->project_model->total_project_hours($project_details->project_id, '', true);
    $open_tasks = true;
}
$comment_type = 'projects';
$can_edit = $this->project_model->can_action('tbl_project', 'edit', array('project_id' => $project_details->project_id));
if (!empty($project_details->client_id)) {
    $currency = $this->project_model->client_currency_symbol($project_details->client_id);
} else {
    $currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
}
$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 (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 = json_decode($project_details->project_settings);
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;
}

$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 : '-';

$total_task = (int) $all_task_info;
$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->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 = 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();
    }
}

$paid_expense = 0;
if (!empty($staff_finance)) {
    foreach ($all_expense_info as $v_expenses) {
        if ($v_expenses->invoices_id != 0) {
            $paid_expense += $this->invoice_model->calculate_to('paid_amount', $v_expenses->invoices_id);
        }
    }
}
?>

<div class="space-y-6">
    <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
        <div class="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
            <div class="space-y-2">
                <div class="text-xs uppercase tracking-wide text-slate-500"><?= lang('project') ?></div>
                <div class="flex flex-wrap items-center gap-3">
                    <h2 class="text-2xl font-semibold text-slate-900 dark:text-slate-100">
                        <?= html_escape($project_details->project_name) ?>
                    </h2>
                    <?php if (!empty($project_details->project_no)) { ?>
                        <span class="rounded-full border border-slate-200 dark:border-slate-700 px-2.5 py-1 text-xs text-slate-500">
                            <?= lang('project_no') ?>: <?= html_escape($project_details->project_no) ?>
                        </span>
                    <?php } ?>
                    <?php if (!empty($overdueDays)) { ?>
                        <span class="rounded-full bg-rose-50 text-rose-700 border border-rose-200 px-2.5 py-1 text-xs">
                            <?= lang('overdue') ?> <?= html_escape($overdueDays) ?>
                        </span>
                    <?php } ?>
                </div>
                <div class="flex flex-wrap items-center gap-2 text-sm text-slate-500">
                    <span><?= lang('client') ?>: <strong class="text-slate-700 dark:text-slate-200"><?= html_escape($name) ?></strong></span>
                    <span class="text-slate-300">â€¢</span>
                    <span><?= lang('categories') ?>: <strong class="text-slate-700 dark:text-slate-200"><?= html_escape($pc_name) ?></strong></span>
                </div>
                <div class="flex flex-wrap items-center gap-2">
                    <?= get_tags($project_details->tags, true) ?>
                </div>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <span class="inline-flex items-center rounded-full border px-2.5 py-1 text-xs font-medium <?= $status_badge ?>">
                    <?= lang($status_key) ?>
                </span>
                <?php if (!empty($can_edit) && !empty($edited)) { ?>
                    <details class="relative">
                        <summary class="list-none cursor-pointer rounded-lg border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-1.5 text-xs font-medium text-slate-700 dark:text-slate-200 hover:border-slate-300">
                            <?= lang('change') ?>
                        </summary>
                        <div class="absolute right-0 z-20 mt-2 w-44 overflow-hidden rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 shadow-lg">
                            <a class="block px-4 py-2 text-sm hover:bg-slate-50 dark:hover:bg-slate-800" href="<?= base_url() ?>admin/projects/change_status/<?= $project_details->project_id ?>/started"><?= lang('started') ?></a>
                            <a class="block px-4 py-2 text-sm hover:bg-slate-50 dark:hover:bg-slate-800" href="<?= base_url() ?>admin/projects/change_status/<?= $project_details->project_id ?>/in_progress"><?= lang('in_progress') ?></a>
                            <a class="block px-4 py-2 text-sm hover:bg-slate-50 dark:hover:bg-slate-800" href="<?= base_url() ?>admin/projects/change_status/<?= $project_details->project_id ?>/on_hold"><?= lang('on_hold') ?></a>
                            <a class="block px-4 py-2 text-sm hover:bg-slate-50 dark:hover:bg-slate-800" href="<?= base_url() ?>admin/projects/change_status/<?= $project_details->project_id ?>/cancel"><?= lang('cancel') ?></a>
                            <a class="block px-4 py-2 text-sm hover:bg-slate-50 dark:hover:bg-slate-800" href="<?= base_url() ?>admin/projects/change_status/<?= $project_details->project_id ?>/completed"><?= lang('completed') ?></a>
                        </div>
                    </details>
                    <a class="inline-flex items-center rounded-lg border border-slate-200 dark:border-slate-700 px-3 py-1.5 text-xs font-medium text-slate-700 dark:text-slate-200 hover:border-slate-300" href="<?= base_url() ?>admin/projects/create/<?= $project_details->project_id ?>">
                        <?= lang('edit') ?>
                    </a>
                <?php } ?>
            </div>
        </div>
    </div>

    <div class="grid gap-4 md:grid-cols-4">
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-5 shadow-sm">
            <p class="text-xs uppercase text-slate-500"><?= lang('tasks') ?></p>
            <p class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= $total_task ?></p>
            <p class="text-sm text-slate-500"><?= $completed ?> <?= lang('completed') ?></p>
        </div>
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-5 shadow-sm">
            <p class="text-xs uppercase text-slate-500"><?= lang('completed') ?></p>
            <p class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100"><?= round($project_progress, 2) ?>%</p>
            <div class="mt-2 h-2 w-full rounded-full bg-slate-100 dark:bg-slate-800">
                <div class="h-2 rounded-full bg-emerald-500" style="width: <?= round($project_progress, 2) ?>%"></div>
            </div>
        </div>
        <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-5 shadow-sm">
            <p class="text-xs uppercase text-slate-500"><?= $lang ?></p>
            <p class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100">
                <?= $TotalGone ?> / <?= $totalDays ?>
            </p>
            <p class="text-sm text-slate-500"><?= $lang ?> (<?= round($tprogress, 2) ?>%)</p>
        </div>
        <?php if (!empty($staff_finance)) { ?>
            <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-5 shadow-sm">
                <p class="text-xs uppercase text-slate-500"><?= lang('total_bill') ?></p>
                <p class="mt-2 text-2xl font-semibold text-slate-900 dark:text-slate-100">
                    <?= display_money($project_cost, $currency->symbol) ?>
                </p>
                <p class="text-sm text-slate-500"><?= lang('billing_type') ?>: <?= lang($project_details->billing_type) ?></p>
            </div>
        <?php } ?>
    </div>

    <div class="grid gap-6 lg:grid-cols-3">
        <div class="space-y-6 lg:col-span-2">
            <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
                <div class="mb-4 flex items-center justify-between">
                    <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('overview') ?></h3>
                    <?php if (!empty($project_details->demo_url)) { ?>
                        <a class="text-xs text-primary hover:underline" href="<?= $project_details->demo_url ?>" target="_blank">
                            <?= lang('demo_url') ?>
                        </a>
                    <?php } ?>
                </div>
                <div class="grid gap-4 md:grid-cols-2">
                    <div class="rounded-xl border border-slate-200 dark:border-slate-800 p-4">
                        <p class="text-xs uppercase text-slate-500"><?= lang('start_date') ?></p>
                        <p class="mt-1 text-sm font-semibold text-slate-900 dark:text-slate-100"><?= display_date($project_details->start_date) ?></p>
                    </div>
                    <div class="rounded-xl border border-slate-200 dark:border-slate-800 p-4">
                        <p class="text-xs uppercase text-slate-500"><?= lang('end_date') ?></p>
                        <p class="mt-1 text-sm font-semibold text-slate-900 dark:text-slate-100"><?= display_date($project_details->end_date) ?></p>
                        <?php if (!empty($overdueDays)) { ?>
                            <p class="text-xs text-rose-600"><?= lang('overdue') ?> <?= html_escape($overdueDays) ?></p>
                        <?php } ?>
                    </div>
                    <div class="rounded-xl border border-slate-200 dark:border-slate-800 p-4">
                        <p class="text-xs uppercase text-slate-500"><?= lang('billing_type') ?></p>
                        <p class="mt-1 text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang($project_details->billing_type) ?></p>
                        <?php if (!empty($project_details->estimate_hours)) { ?>
                            <p class="text-xs text-slate-500"><?= lang('estimate_hours') ?>: <?= $project_details->estimate_hours ?></p>
                        <?php } ?>
                    </div>
                    <div class="rounded-xl border border-slate-200 dark:border-slate-800 p-4">
                        <p class="text-xs uppercase text-slate-500"><?= lang('timer_status') ?></p>
                        <div class="mt-1 flex items-center gap-2">
                            <span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-medium <?= $timer_on ? 'bg-emerald-50 text-emerald-700' : 'bg-rose-50 text-rose-700' ?>">
                                <?= $timer_on ? lang('on') : lang('off') ?>
                            </span>
                            <?php if (!empty($can_edit) && !empty($edited)) { ?>
                                <?php if ($timer_on) { ?>
                                    <a class="text-xs text-rose-600 hover:underline" href="<?= base_url() ?>admin/projects/tasks_timer/off/<?= $project_details->project_id ?>">
                                        <?= lang('stop_timer') ?>
                                    </a>
                                <?php } else { ?>
                                    <a class="text-xs text-emerald-600 hover:underline" href="<?= base_url() ?>admin/projects/tasks_timer/on/<?= $project_details->project_id ?>">
                                        <?= lang('start_timer') ?>
                                    </a>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
                </div>

                <?php $show_custom_fields = custom_form_label(4, $project_details->project_id); ?>
                <?php if (!empty($show_custom_fields)) { ?>
                    <div class="mt-6 grid gap-4 md:grid-cols-2">
                        <?php foreach ($show_custom_fields as $c_label => $v_fields) { ?>
                            <?php if (!empty($v_fields)) { ?>
                                <div class="rounded-xl border border-slate-200 dark:border-slate-800 p-4">
                                    <p class="text-xs uppercase text-slate-500"><?= $c_label ?></p>
                                    <p class="mt-1 text-sm text-slate-900 dark:text-slate-100"><?= $v_fields ?></p>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </div>
                <?php } ?>
            </div>

            <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
                <div class="mb-4 flex items-center justify-between">
                    <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('participants') ?></h3>
                    <?php if (!empty($can_edit) && !empty($edited)) { ?>
                        <?= btn_modal('admin/projects/update_users/' . $project_details->project_id, lang('add_more'), lang('participants'), 'lg', 'plus', array('class' => 'inline-flex items-center rounded-lg border border-slate-200 dark:border-slate-700 px-3 py-1.5 text-xs font-medium text-slate-700 dark:text-slate-200 hover:border-slate-300')) ?>
                    <?php } ?>
                </div>
                <?php if ($project_details->permission == 'all') { ?>
                    <p class="text-sm text-slate-600 dark:text-slate-300"><?= lang('everyone') ?></p>
                <?php } else { ?>
                    <div class="flex flex-wrap items-center gap-2">
                        <?php foreach ($participants as $user) { ?>
                            <?php $ring = ($user->role_id == 1) ? 'ring-rose-500' : 'ring-emerald-500'; ?>
                            <span class="relative inline-flex items-center" data-tooltip="<?= html_escape($user->fullname) ?>">
                                <img src="<?= base_url() . $user->avatar ?>" class="h-9 w-9 rounded-full ring-2 ring-white dark:ring-slate-900 <?= $ring ?>" alt="<?= html_escape($user->fullname) ?>">
                            </span>
                        <?php } ?>
                        <?php if (empty($participants)) { ?>
                            <span class="text-sm text-slate-500"><?= lang('no_data_found') ?></span>
                        <?php } ?>
                    </div>
                <?php } ?>
            </div>

            <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
                <h3 class="mb-4 text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('description') ?></h3>
                <div class="prose max-w-none text-sm text-slate-700 dark:text-slate-200">
                    <?= !empty($project_details->description) ? $project_details->description : '-' ?>
                </div>
            </div>
        </div>

        <div class="space-y-6">
            <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
                <h3 class="mb-4 text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('progress') ?></h3>
                <div class="space-y-4">
                    <div>
                        <div class="flex items-center justify-between text-sm text-slate-600 dark:text-slate-300">
                            <span><?= $TotalGone ?> / <?= $totalDays ?> <?= $lang ?></span>
                            <span><?= round($tprogress, 2) ?>%</span>
                        </div>
                        <div class="mt-2 h-2 w-full rounded-full bg-slate-100 dark:bg-slate-800">
                            <div class="h-2 rounded-full bg-amber-500" style="width: <?= round($tprogress, 2) ?>%"></div>
                        </div>
                    </div>
                    <div>
                        <div class="flex items-center justify-between text-sm text-slate-600 dark:text-slate-300">
                            <span><?= $completed ?> / <?= $total_task ?> <?= lang('open') ?> <?= lang('tasks') ?></span>
                            <span><?= round($task_progress, 2) ?>%</span>
                        </div>
                        <div class="mt-2 h-2 w-full rounded-full bg-slate-100 dark:bg-slate-800">
                            <div class="h-2 rounded-full bg-indigo-500" style="width: <?= round($task_progress, 2) ?>%"></div>
                        </div>
                    </div>
                    <div>
                        <div class="flex items-center justify-between text-sm text-slate-600 dark:text-slate-300">
                            <span><?= lang('completed') ?></span>
                            <span><?= round($project_progress, 2) ?>%</span>
                        </div>
                        <div class="mt-2 h-2 w-full rounded-full bg-slate-100 dark:bg-slate-800">
                            <div class="h-2 rounded-full bg-emerald-500" style="width: <?= round($project_progress, 2) ?>%"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
                <h3 class="mb-4 text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('hours') ?></h3>
                <div class="space-y-4 text-sm text-slate-700 dark:text-slate-200">
                    <div>
                        <p class="text-xs uppercase text-slate-500"><?= lang('project_hours') ?></p>
                        <?= $this->project_model->get_time_spent_result($project_hours); ?>
                    </div>
                    <?php if (!empty($tasks_hours)) { ?>
                        <div>
                            <p class="text-xs uppercase text-slate-500"><?= lang('task_hours') ?></p>
                            <?= $this->project_model->get_time_spent_result($tasks_hours); ?>
                        </div>
                    <?php } ?>
                    <?php if ($project_details->billing_type == 'tasks_and_project_hours') {
                        $total_hours = $project_hours + $tasks_hours; ?>
                        <div class="text-sm text-slate-600 dark:text-slate-300">
                            <?= lang('total') ?>: <?= $this->project_model->get_spent_time($total_hours); ?>
                        </div>
                    <?php } ?>
                </div>
                <?php if (!empty($staff_finance)) { ?>
                    <div class="mt-4 rounded-xl border border-slate-200 dark:border-slate-800 p-4 text-sm text-slate-600 dark:text-slate-300">
                        <p class="font-semibold text-slate-900 dark:text-slate-100"><?= lang('total_bill') ?></p>
                        <p class="mt-1 text-lg font-semibold"><?= display_money($project_cost, $currency->symbol) ?></p>
                    </div>
                <?php } ?>
            </div>

            <?php if (!empty($staff_finance)) { ?>
                <div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 p-6 shadow-sm">
                    <h3 class="mb-4 text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('expense') ?></h3>
                    <div class="space-y-2 text-sm text-slate-600 dark:text-slate-300">
                        <div class="flex items-center justify-between"><span><?= lang('total') ?></span><span><?= display_money($total_expense->amount, $currency->symbol) ?></span></div>
                        <div class="flex items-center justify-between"><span><?= lang('billable') ?></span><span><?= display_money($billable_expense->amount, $currency->symbol) ?></span></div>
                        <div class="flex items-center justify-between"><span><?= lang('not_billable') ?></span><span><?= display_money($not_billable_expense->amount, $currency->symbol) ?></span></div>
                        <div class="flex items-center justify-between"><span><?= lang('billed') ?></span><span><?= display_money($paid_expense, $currency->symbol) ?></span></div>
                        <div class="flex items-center justify-between"><span><?= lang('unbilled') ?></span><span><?= display_money($billable_expense->amount - $paid_expense, $currency->symbol) ?></span></div>
                    </div>
                </div>
            <?php } ?>
        </div>
    </div>
</div>
