<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<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('total') . ' ' . lang('task') . ' ' . lang('time_spent') ?></h4>
        </div>
        <div class="p-5">
            <?php
            $tasks_info = $this->user_model->my_permission('tbl_task', $profile_info->user_id);
            $task_time = 0;
            $task_time = $this->user_model->my_spent_time($profile_info->user_id);
            if (!empty($tasks_info)) {
                foreach ($tasks_info as $v_u_tasks) {
                }
            }
            echo $this->user_model->get_time_spent_result($task_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('task') . ' ' . lang('report') ?></h4>
        </div>
        <div class="p-5">
            <div class="chart-pie flot-chart"></div>
        </div>
    </div>
</div>
<?php

$not_started = 0;
$in_progress = 0;
$completed = 0;
$deferred = 0;
$waiting_for_someone = 0;

if (!empty($tasks_info)) : foreach ($tasks_info as $v_tasks) :
    if ($v_tasks->task_status == 'not_started') {
        $not_started += count(array($v_tasks->task_status));
    }
    if ($v_tasks->task_status == 'in_progress') {
        $in_progress += count(array($v_tasks->task_status));
    }
    if ($v_tasks->task_status == 'completed') {
        $completed += count(array($v_tasks->task_status));
    }
    if ($v_tasks->task_status == 'deferred') {
        $deferred += count(array($v_tasks->task_status));
    }
    if ($v_tasks->task_status == 'waiting_for_someone') {
        $waiting_for_someone += count(array($v_tasks->task_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($not_started) || !empty($in_progress) || !empty($completed) || !empty($deferred) || !empty($waiting_for_someone)) { ?>
    <script type="text/javascript">
        $(document).ready(function () {
            // CHART PIE
            // -----------------------------------
            (function (window, document, $, undefined) {

                $(function () {

                    var data = [{
                        "label": "<?= lang('not_started')?>",
                        "color": "#23b7e5",
                        "data": <?= $not_started?>
                    }, {
                        "label": "<?= lang('in_progress')?>",
                        "color": "#ff902b",
                        "data": <?= $in_progress?>
                    }, {
                        "label": "<?= lang('completed')?>",
                        "color": "#27c24c",
                        "data": <?= $completed?>
                    }, {
                        "label": "<?= lang('deferred')?>",
                        "color": "#f05050",
                        "data": <?= $deferred?>
                    }, {
                        "label": "<?= lang('waiting_for_someone')?>",
                        "color": "#ff902b",
                        "data": <?= $waiting_for_someone?>
                    },];

                    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 = $('.chart-pie');
                    if (chart.length)
                        $.plot(chart, data, options);

                });

            })(window, document, window.jQuery);

        });

    </script>
<?php } ?>
