<?php
$project_expense_data = isset($project_expense_data) && is_array($project_expense_data) ? $project_expense_data : array();
$all_expense_info = isset($project_expense_data['rows']) ? $project_expense_data['rows'] : array();
$total_expense = isset($project_expense_data['total_expense']) ? (float) $project_expense_data['total_expense'] : 0;
$billable_expense = isset($project_expense_data['billable_expense']) ? (float) $project_expense_data['billable_expense'] : 0;
$not_billable_expense = isset($project_expense_data['not_billable_expense']) ? (float) $project_expense_data['not_billable_expense'] : 0;
$expense_total_records = isset($project_expense_data['total_records']) ? (int) $project_expense_data['total_records'] : 0;
$expense_category = isset($project_expense_data['categories']) ? $project_expense_data['categories'] : array();
$currency = isset($project_expense_data['currency']) ? $project_expense_data['currency'] : null;
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$type = isset($expense_filter_type) ? $expense_filter_type : $this->uri->segment(6);
$category_id = isset($expense_category_id) ? $expense_category_id : $this->uri->segment(7);
?>

<div class="space-y-5">
    <div class="flex flex-wrap items-center justify-between gap-3">
        <div>
            <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('expense') ?></h3>
            <p class="text-sm text-slate-500"><?= lang('expense') ?> overview</p>
        </div>
        <div class="flex items-center gap-2">
            <details class="relative">
                <summary class="list-none flex cursor-pointer rounded-lg border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-800 px-3 py-2 text-xs font-medium text-slate-600 dark:text-slate-300">
                    <i data-lucide="filter" class="h-4 w-4 mr-1"></i><?= lang('filter_by') ?>
                </summary>
                <div class="absolute right-0 z-20 mt-2 w-56 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 <?= empty($type) ? 'font-semibold' : '' ?>" href="<?= base_url() ?>admin/projects/project_details/<?= $project_details->project_id ?>/expense"><?= lang('all') ?></a>
                    <?php if (count(array($expense_category)) > 0) { ?>
                        <div class="border-t border-slate-100 dark:border-slate-800"></div>
                        <?php foreach ($expense_category as $v_category) { ?>
                            <?php $is_active = (!empty($category_id) && $type == 'category' && $category_id == $v_category->expense_category_id); ?>
                            <a class="block px-4 py-2 text-sm hover:bg-slate-50 dark:hover:bg-slate-800 <?= $is_active ? 'font-semibold' : '' ?>" href="<?= base_url() ?>admin/projects/project_details/<?= $project_details->project_id ?>/expense/category/<?= $v_category->expense_category_id; ?>">
                                <?= $v_category->expense_category; ?>
                            </a>
                        <?php } ?>
                    <?php } ?>
                </div>
            </details>
            <?php if (!empty($edited)) { ?>
                <a href="<?= base_url() ?>admin/transactions/create_expense/project_expense/<?= $project_details->project_id ?>"
                   class="inline-flex items-center gap-2 rounded-lg bg-slate-900 px-3 py-2 text-xs font-semibold text-white hover:bg-slate-800 dark:hover:bg-slate-700">
                    <i data-lucide="plus" class="h-4 w-4"></i><?= lang('new_expense') ?>
                </a>
            <?php } ?>
        </div>
    </div>

    <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('total_expense') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= display_money($total_expense, $currency->symbol) ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('all') ?></div>
        </div>
        <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('billable') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= display_money($billable_expense, $currency->symbol) ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('yes') ?></div>
        </div>
        <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('not_billable') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= display_money($not_billable_expense, $currency->symbol) ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('no') ?></div>
        </div>
        <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
            <div class="text-xs font-semibold text-slate-500 dark:text-slate-400"><?= lang('expense') ?></div>
            <div class="text-2xl font-bold mt-2 text-slate-900 dark:text-slate-100"><?= $expense_total_records ?></div>
            <div class="text-xs text-slate-400 dark:text-slate-500 mt-1"><?= lang('records') ?></div>
        </div>
    </div>

    <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
        <div class="overflow-x-auto overflow-y-visible">
            <table id="manage_expense" class="min-w-full text-sm">
                <thead class="bg-slate-50 text-slate-500 dark:bg-slate-800 dark:text-slate-300">
                    <tr>
                        <th class="px-4 py-3 text-left"><?= lang('name') . '/' . lang('title') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('date') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('categories') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('amount') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('attachment') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('action') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php
                    if (!empty($all_expense_info)) :
                        foreach ($all_expense_info as $v_expense) :
                                $category = !empty($v_expense->expense_category) ? $v_expense->expense_category : lang('undefined_category');
                                $can_edit = $this->items_model->can_action('tbl_transactions', 'edit', array('transactions_id' => $v_expense->transactions_id));
                                $can_delete = $this->items_model->can_action('tbl_transactions', 'delete', array('transactions_id' => $v_expense->transactions_id));
                                $e_edited = can_action('31', 'edited');
                                $e_deleted = can_action('31', 'deleted');
                    ?>
                                <tr id="table-expense-<?= $v_expense->transactions_id ?>">
                                    <td class="px-4 py-3">
                                        <a class="font-medium text-primary hover:underline dark:text-sky-300" href="<?= base_url() ?>admin/transactions/view_expense/<?= $v_expense->transactions_id ?>">
                                            <?= (!empty($v_expense->name) ? $v_expense->name : '-') ?>
                                        </a>
                                    </td>
                                    <td class="px-4 py-3"><?= display_date($v_expense->date); ?></td>
                                    <td class="px-4 py-3"><?= $category ?></td>
                                    <td class="px-4 py-3"><?= display_money($v_expense->amount, $currency->symbol) ?></td>
                                    <td class="px-4 py-3">
                                        <?php
                                        $attachement_info = !empty($v_expense->attachement) ? json_decode($v_expense->attachement) : array();
                                        if (!empty($attachement_info)) { ?>
                                            <a class="font-medium text-primary hover:underline dark:text-sky-300" href="<?= base_url() ?>admin/transactions/download/<?= $v_expense->transactions_id ?>"><?= lang('download') ?></a>
                                        <?php } ?>
                                    </td>
                                    <td class="px-4 py-3">
                                        <a class="btn btn-info btn-xs inline-flex h-7 w-8 items-center justify-center rounded-lg border-0 p-0 shadow-sm transition hover:opacity-90 focus:outline-none focus:ring-2 focus:ring-sky-400 focus:ring-offset-1" href="<?= base_url() ?>admin/transactions/view_details/<?= $v_expense->transactions_id ?>/details">
                                            <span data-lucide="list" class="h-4 w-4"></span>
                                        </a>
                                        <?php if (!empty($can_edit) && !empty($e_edited)) { ?>
                                            <?= btn_edit('admin/transactions/create_expense/' . $v_expense->transactions_id) ?>
                                        <?php }
                                        if (!empty($can_delete) && !empty($e_deleted)) {
                                            echo ajax_anchor(base_url("admin/transactions/delete_expense/" . $v_expense->transactions_id), "", array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table-expense-" . $v_expense->transactions_id));
                                        } ?>
                                    </td>
                                </tr>
                    <?php
                        endforeach;
                    endif;
                    ?>
                    <?php if (empty($all_expense_info)) { ?>
                        <tr>
                            <td colspan="6" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>


