<?php

$total_in_stock = 0;
$total_out_of_stock = 0;
$total_low_stock = 0;

foreach ($purchases_items as $row):
    $p_item = $this->inventory_m->purchases_item_select_by_item_id($row['id']);

    if (!empty($p_item) && $p_item['stock'] > $row['min_stock']) {
        $total_in_stock++;
    }

    if (!empty($p_item) && $p_item['stock'] == 0) {
        $total_out_of_stock++;
    } elseif (!empty($p_item) && $p_item['stock'] <= $row['min_stock']) {
        $total_low_stock++;
    }

endforeach;

?>
<?php include 'inc/inventory_menu.php'; ?>

<div class="row">
    <div class="col-md-12">
        <?php
        $filter_config = [
            [
                'type' => 'text',
                'name' => 'q',
                'label' => __('search'),
                'placeholder' => __('search_by') . ' ' . __('item'),
                'column' => 'col-md-3'
            ],
            [
                'type' => 'dropdown',
                'name' => 'category',
                'label' => __('category'),
                'data' => $category_list,
                'value' => 'id',
                'text' => 'category',
                'placeholder' => __('all_categories'),
                'column' => 'col-md-2'
            ],
            [
                'type' => 'dropdown',
                'name' => 'status',
                'label' => __('status'),
                'data' => [
                    [
                        'id' => 'in_stock',
                        'status' => __('in_stock')
                    ],
                    [
                        'id' => 'low_stock',
                        'status' => __('low_stock')
                    ],
                    [
                        'id' => 'out_of_stock',
                        'status' => __('out_of_stock')
                    ]
                ],
                'value' => 'id',
                'text' => 'status',
                'placeholder' => __('status'),
                'column' => 'col-md-2'
            ]
        ];
        __filter($filter_config);
        ?>
    </div>
</div>

<!----------------------------------------------
        Top Search Filter Area End
---------------------------------------------->


<div class="row mt-30">
    <div class="col-md-3 col-sm-6">
        <div class="card items_info_box">
            <div class="card-body">
                <div class="items_info_content">
                    <span class="icon"><i class="icofont-brand-aliexpress"></i></span>
                    <h4 class="count_number"><?= $total_items; ?></h4>
                    <p><?= __('total_items') ?></p>
                </div>
            </div>
        </div>
    </div><!-- col -->
    <div class="col-md-3 col-sm-6">
        <div class="card items_info_box">
            <div class="card-body">
                <div class="items_info_content">
                    <span class="icon fz-30 bg_teal"><i class="icofont-stack-overflow"></i></span>
                    <h4 class="count_number"><?= $total_in_stock; ?></h4>
                    <p><?= __('in_stock') ?></p>
                </div>
            </div>
        </div>
    </div><!-- col -->
    <div class="col-md-3 col-sm-6">
        <div class="card items_info_box">
            <div class="card-body">
                <div class="items_info_content">
                    <span class="icon fz-30 bg_orange"><i class="icofont-battery-low"></i></span>
                    <h4 class="count_number"><?= $total_low_stock; ?></h4>
                    <p><?= __('low_stock') ?></p>
                </div>
            </div>
        </div>
    </div><!-- col -->
    <div class="col-md-3 col-sm-6">
        <div class="card items_info_box">
            <div class="card-body">
                <div class="items_info_content">
                    <span class="icon bg_danger"><i class="fa-solid fa-box-open"></i></span>
                    <h4 class="count_number"><?= $total_out_of_stock; ?></h4>
                    <p><?= __('out_of_stock') ?></p>
                </div>
            </div>
        </div>
    </div><!-- col -->
</div>

<div class="row mt-30">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header p-17">
                <h3 class="card-title"><i class="fa-solid fa-cart-flatbed"></i>&nbsp; <?= __('inventory_items') ?> &nbsp;&nbsp;<span class="badge badge-primary"><?= $total_items ?> <?= __('items') ?></span></h3>
            </div>
            <div class="card-body">
                <?php $this->load->view('backend/inventory/request_table'); ?>
            </div>
        </div>
    </div>
</div>

<div class="row mt-40">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header px-17 py-12">
                <h3 class="card-title"><i class="fa-solid fa-triangle-exclamation text-danger"></i>&nbsp;<?= __('stock_alert') ?></h3>
            </div>
            <div class="card-body">
                <?php foreach ($recent_activities as $row): ?>

                    <?php if ($row['p_item_stock'] == 0): ?>
                        <div class="callout alert_callout callout-danger">
                            <h4><?= $row['item_name'] ?> - <sm> <?= __('out_of_stock') ?></sm>
                            </h4>
                        </div>
                    <?php elseif ($row['p_item_stock'] <= $row['item_min_stock']): ?>
                        <div class="callout alert_callout bg-warning-soft">
                            <h4><?= $row['item_name'] ?> - <sm> <?= __('only'); ?> <?= '<b class="text-info">'  . $row['p_item_stock'] . $row['unit'] . '</b>' ?> <?= __('remaining') ?></sm>
                            </h4>
                        </div>
                    <?php endif; ?>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header px-17 py-12">
                <h3 class="card-title"><i class="fa-solid fa-check-to-slot"></i>&nbsp;<?= __('recent_activities') ?></h3>
            </div>
            <div class="card-body">
                <?php foreach ($recent_activities as $row): ?>
                    <div class="callout alert_callout activities_alert">
                        <h5><?= __('send_request') ?> <?= get_time_ago($row['created_at']) ?></h5>
                        <h4 class="fz-17"><span class="activeColor"><?= $row['quantity'] . ' ' . $row['unit'] ?></span> &nbsp; <?= $row['item_name'] ?></h4>
                    </div>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>


<script>
    $(document).on('change', '.request_select', function(s) {
        s.preventDefault();
        var url = $(this).val();

        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: {
                'csrf_test_name': csrf_value
            },
            success: function(json) {
                console.log(json);
                if (json.success) {
                    location.reload();
                }
            }
        })
    })
</script>