<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$expense_info = $this->user_model->get_expense_info($id);
$paid_total = 0;
$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$currency_symbol = (!empty($currency) && isset($currency->symbol)) ? $currency->symbol : false;
?>
<div class="space-y-4">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-4">
            <h4 class="text-sm font-semibold text-foreground"><?= lang('paid') . ' ' . lang('total') ?></h4>
        </div>
        <div class="p-5">
            <div class="overflow-x-auto overflow-y-visible rounded-xl border border-border bg-card dark:border-slate-700 dark:bg-slate-900">
                <table class="w-full border-collapse text-sm">
                    <thead class="bg-muted/50 text-xs uppercase tracking-[0.14em] text-muted-foreground dark:bg-slate-800/80 dark:text-slate-400">
                        <tr>
                            <th class="border-b border-border px-4 py-3 text-left font-semibold dark:border-slate-700"><?= lang('name') . '/' . lang('title')  ?></th>
                            <th class="border-b border-border px-4 py-3 text-left font-semibold dark:border-slate-700"><?= lang('type') ?></th>
                            <th class="border-b border-border px-4 py-3 text-left font-semibold dark:border-slate-700"><?= lang('total') . ' ' . lang('amount') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (!empty($expense_info)) {
                            foreach ($expense_info as $v_expense) {
                                if ($v_expense->module == 'vip_table' || $v_expense->module == 'list_stock') {
                                    continue;
                                }
                                $total_amount = 0;
                                if ($v_expense->module == 'expense') {
                                    $paid_total += $v_expense->expense_total_amount;
                                    $total_amount = $v_expense->expense_total_amount;
                                } else {
                                    $paid_total += $v_expense->total_amount;
                                    $total_amount = $v_expense->total_amount;
                                }
                                ?>
                                <tr class="border-t border-border dark:border-slate-700">
                                    <td class="px-4 py-3 text-foreground dark:text-slate-100">
                                        <?= $v_expense->name  ?> ( <a class="text-primary hover:underline" href="<?= base_url('admin/projects/project_details/'.$v_expense->project_id.'/'.$v_expense->module)?>"><?= $v_expense->project_name ?></a> )
                                    </td>
                                    <td class="px-4 py-3 text-foreground dark:text-slate-100"><?= lang($v_expense->module) ?></td>
                                    <td class="px-4 py-3 font-medium text-foreground dark:text-slate-100"><?= display_money($total_amount) ?></td>
                                </tr>
                        <?php
                            }
                        } else {
                        ?>
                            <tr>
                                <td colspan="3" class="px-4 py-8 text-center text-sm text-muted-foreground dark:text-slate-400"><?= lang('no_data_found') ?></td>
                            </tr>
                        <?php
                        }
                        ?>
                    </tbody>
                    <tfoot>
                        <tr class="border-t border-border bg-indigo-50 dark:border-slate-700 dark:bg-indigo-950/30">
                            <td colspan="2" class="px-4 py-3 text-right font-semibold text-indigo-950 dark:text-indigo-200"><?= lang('total')  ?></td>
                            <td class="px-4 py-3 font-semibold text-indigo-950 dark:text-indigo-200"><?= display_money($paid_total, $currency_symbol) ?></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>

    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-4">
            <h4 class="text-sm font-semibold text-foreground"><?= lang('total') . ' ' . lang('project') . ' ' . lang('time_spent') ?></h4>
        </div>
        <div class="p-5">
            <?php
            $project_info = $this->user_model->my_permission('tbl_project', $profile_info->user_id);
            $project_time = 0;
            $project_time = $this->user_model->my_spent_time($profile_info->user_id, true);
            if (!empty($project_info)) {
                foreach ($project_info as $v_projects) {
                }
            }
            echo $this->user_model->get_time_spent_result($project_time)
            ?>
        </div>
    </div>

    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-4">
            <h4 class="text-sm font-semibold text-foreground"><?= lang('project') . ' ' . lang('report') ?></h4>
        </div>
        <div class="p-5">
            <div class="project-chart-pie flot-chart"></div>
        </div>
    </div>
</div>

<?php

$started = 0;
$in_progress = 0;
$cancel = 0;
$completed = 0;
if (!empty($project_info)):
    foreach ($project_info as $v_project) :
        if ($v_project->project_status == 'started') {
            $started += count(array($v_project->project_status));
        }
        if ($v_project->project_status == 'in_progress') {
            $in_progress += count(array($v_project->project_status));
        }
        if ($v_project->project_status == 'completed') {
            $completed += count(array($v_project->project_status));
        }
        if ($v_project->project_status == 'cancel') {
            $cancel += count(array($v_project->project_status));
        }
    endforeach;
endif;
?>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.js"></script>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.tooltip.min.js"></script>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.resize.js"></script>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.pie.js"></script>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.time.js"></script>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.categories.js"></script>
<script src="<?= base_url() ?>assets/plugins/Flot/jquery.flot.spline.min.js"></script>
<?php if (!empty($started) || !empty($in_progress) || !empty($completed) || !empty($cancel)) { ?>
<script type="text/javascript">
$(document).ready(function() {
    // CHART PIE
    // -----------------------------------
    (function(window, document, $, undefined) {

        $(function() {

            var data = [{
                "label": "<?= lang('started')?>",
                "color": "#ff902b",
                "data": <?= $started?>
            }, {
                "label": "<?= lang('in_progress')?>",
                "color": "#5d9cec",
                "data": <?= $in_progress?>
            }, {
                "label": "<?= lang('completed')?>",
                "color": "#23b7e5",
                "data": <?= $completed?>
            }, {
                "label": "<?= lang('cancel')?>",
                "color": "#7266ba",
                "data": <?= $cancel?>
            }];

            var options = {
                series: {
                    pie: {
                        show: true,
                        innerRadius: 0,
                        label: {
                            show: true,
                            radius: 0.8,
                            formatter: function(label, series) {
                                return '<div class="flot-pie-label">' +
                                    Math.round(series.percent) +
                                    '%</div>';
                            },
                            background: {
                                opacity: 0.8,
                                color: '#222'
                            }
                        }
                    }
                }
            };

            var chart = $('.project-chart-pie');
            if (chart.length)
                $.plot(chart, data, options);

        });

    })(window, document, window.jQuery);
    // CHART BAR STACKED
    // -----------------------------------
});
</script>
<?php } ?>
