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

<?php
$total_taken = 0;
$total_quota = 0;
$leave_report = leave_report($profile_info->user_id);
?>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-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('leave') . ' ' . lang('details_of') . ' ' . $profile_info->fullname ?></h4>
        </div>
        <div class="p-5">
            <table class="table w-full">
                <tbody>
                <?php
                if (!empty($leave_report['leave_category'])) {
                    foreach ($leave_report['leave_category'] as $lkey => $v_l_report) {
                        $total_quota += $leave_report['leave_quota'][$lkey];
                        $total_taken += $leave_report['leave_taken'][$lkey];
                        ?>
                        <tr class="border-b border-border">
                            <td class="px-3 py-2 font-semibold text-foreground"> <?= $leave_report['leave_category'][$lkey] ?></td>
                            <td class="px-3 py-2 text-right text-foreground">
                                <?= $leave_report['leave_taken'][$lkey] ?>/<?= $leave_report['leave_quota'][$lkey]; ?>
                            </td>
                        </tr>
                    <?php }
                }
                ?>

                <tr>
                    <td class="px-3 py-2 font-semibold text-foreground"> <?= lang('total') ?></td>
                    <td class="px-3 py-2 text-right text-foreground"> <?= $total_taken; ?> /<?= $total_quota; ?></td>
                </tr>
                </tbody>
            </table>
        </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('leave_report') ?></h4>
        </div>
        <div class="p-5">
            <div id="panelChart5">
                <div class="chart-pie-my flot-chart"></div>
            </div>
        </div>
    </div>
</div>

<?php
$all_category = $this->db->get('tbl_leave_category')->result();
$color = array('37bc9b', '7266ba', 'f05050', 'ff902b', '7266ba', 'f532e5', '5d9cec', '7cd600', '91ca00', 'ff7400', '1cc200', 'bb9000', '40c400');
foreach ($all_category as $key => $v_category) {
    if (!empty($my_leave_report['leave_taken'][$key])) {
        $a = $my_leave_report['leave_taken'][$key];
    }
}
if (!empty($a)) {
    ?>
    <script type="text/javascript">
        // CHART PIE
        // -----------------------------------
        (function (window, document, $, undefined) {

            $(function () {

                var data = [
                    <?php
                    if(!empty($all_category)){
                    foreach ($all_category as $key => $v_category) {
                    if (!empty($my_leave_report['leave_taken'][$key])) {
                    $result = $my_leave_report['leave_taken'][$key];
                    ?>
                    {
                        "label": "<?= $v_category->leave_category . ' ( <small>' . lang('quota') . ': ' . $my_leave_report['leave_quota'][$key] . ' ' . lang('taken') . ': ' . $result . '</small>)'?>",
                        "color": "#<?=$color[$key] ?>",
                        "data": <?= $result?>
                    },
                    <?php }
                    }
                    }?>
                ];

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

            });

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

    </script>
<?php } ?>
