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

<div class="row">
    <div class="col-md-12">
        <?php
        $filter_config = [
            [
                'type' => 'text',
                'name' => 'q',
                'label' => lang('search'),
                'placeholder' => lang('search_by') . ' ' . __('item') . ',' . __('vendor') . ',' . __('invoice'),
                'column' => 'col-md-3'
            ],
            [
                'type' => 'dropdown',
                'name' => 'vendor',
                'label' => lang('vendor'),
                'data' => $vendors,
                'value' => 'id',
                'text' => 'name',
                'placeholder' => lang('all_vendors'),
                'column' => 'col-md-2'
            ],
            [
                'type' => 'dropdown',
                'name' => 'category',
                'label' => lang('category'),
                'data' => $categories,
                'value' => 'id',
                'text' => 'category',
                'placeholder' => lang('all_categories'),
                'column' => 'col-md-2'
            ],
            [
                'type' => 'dropdown',
                'name' => 'item',
                'label' => lang('item'),
                'data' => $purchases_items,
                'value' => 'id',
                'text' => 'name',
                'placeholder' => lang('all_items'),
                'column' => 'col-md-2'
            ]
        ];
        $this->load->view('backend/common/filter_page', ['filter_config' => $filter_config]);
        ?>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title flex-1"><?= lang('purchases') ?></h3>
                <button type="button" class="btn btn-secondary btn-sm" data-toggle="modal" data-target="#addModal"><i class="fa fa-plus"></i> <?= lang('add_new') ?></button>
            </div>
            <div class="card-body">
                <div class="table-responsive responsiveTable">
                    <table class="table table-bordered mb-0">
                        <thead>
                            <tr>
                                <td>#</td>
                                <th><?= lang('vendor/supplier') ?></th>
                                <th><?= lang('items') ?></th>
                                <th><?= lang('category') ?></th>
                                <th><?= lang('unit') ?></th>
                                <th><?= lang('unit_price') ?></th>
                                <th><?= lang('quantity') ?></th>
                                <th><?= lang('in_stock') ?></th>
                                <th><?= lang('payment_status') ?></th>
                                <th><?= lang('status') ?></th>
                                <th class="text-right"><?= lang('action') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($purchases as $key => $row): ?>
                                <tr>
                                    <td><?= $key + 1 ?></td>
                                    <td data-label="<?= lang('vendor/supplier'); ?>">
                                        <p class="mb-0"> <?= isset($row['vendor_name']) ? $row['vendor_name'] : '' ?></p>
                                        <p class="activeColor mt-5"><?= full_time($row['created_at']); ?></p>
                                    </td>
                                    <td data-label="<?= lang('items'); ?>"><?= isset($row['item_name']) ? $row['item_name'] : '' ?></td>
                                    <td data-label="<?= lang('category'); ?>"><?= isset($row['category_name']) ? $row['category_name'] : '' ?></td>

                                    <td data-label="<?= lang('unit'); ?>"><?= $row['unit'] ?></td>
                                    <td data-label="<?= lang('unit_price'); ?>">$ <?= $row['unit_price'] ?></td>
                                    <td data-label="<?= lang('quantity'); ?>"><?= $row['quantity'] ?></td>
                                    <td data-label="<?= lang('stock'); ?>"><?= $row['stock'] ?></td>
                                    <td data-label="<?= lang('payment_status'); ?>"><span class="<?= strtolower($row['payment_status']) == 'paid' ? 'text-success' : 'text-danger' ?>"><?= $row['payment_status'] ?></span></td>
                                    <td data-label="<?= lang('status'); ?>">
                                        <?php if ($row['stock'] > $row['item_min_stock']): ?>
                                            <span class="stock_status label-success"><?= lang('in_stock') ?></span>
                                        <?php elseif ($row['stock'] == 0): ?>
                                            <span class="stock_status label-danger"><?= lang('out_of_stock') ?></span>
                                        <?php else: ?>
                                            <span class="stock_status label-warning"><?= lang('low_stock') ?></span>
                                        <?php endif; ?>
                                    </td>
                                    <td data-label="<?= lang('action'); ?>" class="text-right">
                                        <a href="#view_<?= $row['id']; ?>" data-toggle="modal" class="btn btn-flat btn-secondary btn-sm"> <i class="fa fa-eye"></i> <?= lang('details') ?></a>

                                        <a href="#edit_<?= $row['id']; ?>" data-toggle="modal" class="text-info btn-sm"> <i class="fa fa-edit"></i> <?= !empty(lang('edit')) ? lang('edit') : "edit"; ?></a>

                                        <a href="<?= base_url('delete-item/' . html_escape($row['id']) . '/vendor_inventory_purchases'); ?>" class="text-danger action_btn" data-msg="<?= !empty(lang('want_to_delete')) ? lang('want_to_delete') : "Want to Delete?"; ?>"><i class="fa fa-trash"></i> <?= !empty(lang('delete')) ? lang('delete') : "Delete"; ?></a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>



<!----------------------------------------------
            Modal Area
---------------------------------------------->

<!-- Purchase  Edit Modal -->
<?php foreach ($purchases as $row): ?>
    <div class="modal fade" id="edit_<?= $row['id'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <form action="<?= base_url('admin/inventory/add_purchases') ?>" class="form-submit" method="post">
                <!-- csrf token -->
                <?= csrf(); ?>

                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">×</button>
                        <h4 class="modal-title"><?= lang('add_new_purchase') ?></h4>
                    </div>
                    <div class="modal-body">

                        <div class="row">
                            <div class="form-group col-md-6">
                                <label for=""><?= lang('vendor/supplier') ?><span class="error">*</span></label>
                                <select name="vendor_id" class="form-control" required>
                                    <?php foreach ($vendors as $vendor): ?>
                                        <option <?= ($row['vendor_id'] == $vendor['id']) ? 'selected' : ''  ?> value="<?= $vendor['id'] ?>"><?= $vendor['name'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>

                            <div class="form-group col-md-6">
                                <label for=""><?= lang('category') ?> <span class="error">*</span></label>
                                <select name="cat_id" class="form-control purchase_category" required>
                                    <option value="0">Select Category</option>
                                    <?php foreach ($categories as $cat): ?>
                                        <?php if ($cat['status'] == '1'): ?>
                                            <option <?= ($row['cat_id'] == $cat['id']) ? 'selected' : ''  ?> value="<?= $cat['id'] ?>"><?= $cat['category'] ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div><!-- row -->

                        <div class="row">
                            <div class="form-group col-md-6">
                                <label for=""><?= lang('item') ?> <span class="error">*</span></label>
                                <select name="item_id" class="form-control purchase_item" required>
                                    <?php include VIEWPATH . 'backend/inventory/ajax_load_items.php' ?>
                                </select>
                            </div>

                            <div class="form-group col-md-6">
                                <label for=""><?= lang('stock') ?> <span class="error">*</span></label>
                                <input type="text" name="stock" class="form-control only_number" placeholder="<?= lang('stock') ?>" value="<?= $row['stock'] ?>" required>
                            </div>
                        </div><!-- row -->

                        <div class="row">
                            <div class="form-group col-md-6">
                                <label for=""><?= lang('unit_price') ?> <span class="error">*</span></label>
                                <input type="text" name="unit_price" class="form-control only_number" placeholder="0.00" value="<?= $row['unit_price'] ?>" required>
                            </div>

                            <div class="form-group col-md-6">
                                <label for=""><?= lang('quantity') ?> <span class="error">*</span></label>
                                <input type="text" name="quantity" class="form-control only_number" value="<?= $row['quantity'] ?>" required>
                            </div>
                        </div><!-- row -->

                        <div class="row">
                            <div class="form-group col-md-6">
                                <label for="name"><?= lang('purchase_date') ?><span class="error">*</span></label>
                                <input type="date" name="purchases_date" class="form-control" value="<?= $row['purchases_date'] ?>" required>
                            </div>

                            <div class="form-group col-md-6">
                                <label for="name"><?= lang('invoice_number') ?><span class="error">*</span></label>
                                <input type="text" name="invoice_number" class="form-control only_number" placeholder="<?= lang('invoice_number') ?>" value="<?= $row['invoice_number'] ?>" required>
                            </div>
                        </div><!-- row -->

                        <div class="row">
                            <div class="form-group col-md-6">
                                <label for=""><?= lang('unit') ?> <span class="error">*</span></label>
                                <select name="unit" class="form-control" required>
                                    <option <?= $row['unit'] == lang('pieces') ? 'selected' : '' ?> value="<?= lang('pieces') ?>"><?= lang('pieces') ?></option>
                                    <option <?= $row['unit'] == lang('boxs') ? 'selected' : '' ?> value="<?= lang('boxs') ?>"><?= lang('boxs') ?></option>
                                    <option <?= $row['unit'] == lang('packet') ? 'selected' : '' ?> value="<?= lang('packet') ?>"><?= lang('packet') ?></option>
                                    <option <?= $row['unit'] == lang('bottle') ? 'selected' : '' ?> value="<?= lang('bottle') ?>"><?= lang('bottle') ?></option>
                                    <option <?= $row['unit'] == lang('kg') ? 'selected' : '' ?> value="<?= lang('kg') ?>"><?= lang('kg') ?></option>
                                    <option <?= $row['unit'] == lang('g') ? 'selected' : '' ?> value="<?= lang('g') ?>"><?= lang('g') ?></option>
                                    <option <?= $row['unit'] == lang('liter') ? 'selected' : '' ?> value="<?= lang('liter') ?>"><?= lang('liter') ?></option>
                                </select>
                            </div>

                            <div class="form-group col-md-3">
                                <label for=""><?= lang('payment_method') ?><span class="error">*</span></label>
                                <select name="payment_method" class="form-control" required>

                                    <option <?= $row['payment_method'] == lang('payment_with_cash') ? 'selected' : '' ?> value="<?= lang('payment_with_cash') ?>"><?= lang('payment_with_cash') ?></option>

                                    <option <?= $row['payment_method'] == lang('bank_transfer') ? 'selected' : '' ?> value="<?= lang('bank_transfer') ?>"><?= lang('bank_transfer') ?></option>

                                    <option <?= $row['payment_method'] == lang('cash_on_delivery') ? 'selected' : '' ?> value="<?= lang('cash_on_delivery') ?>"><?= lang('cash_on_delivery') ?></option>

                                </select>
                            </div>

                            <div class="form-group col-md-3">
                                <label for=""><?= lang('payment_status') ?><span class="error">*</span></label>
                                <select name="payment_status" class="form-control" required>

                                    <option <?= $row['payment_status'] == 'paid' ? 'selected' : '' ?> value="paid"><?= lang('paid') ?></option>

                                    <option <?= $row['payment_status'] == 'unpaid' ? 'selected' : '' ?> value="unpaid"><?= lang('unpaid') ?></option>

                                </select>
                            </div>

                        </div>

                    </div>
                    <input type="hidden" name="id" value="<?= $row['id'] ?>">
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal"><?= lang('close') ?></button>
                        <button type="submit" class="btn btn-primary"><?= lang('save_change') ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
<?php endforeach; ?>


<!-- Purchase  Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <form action="<?= base_url('admin/inventory/add_purchases') ?>" class="form-submit" method="post">
            <!-- csrf token -->
            <?= csrf(); ?>

            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h4 class="modal-title"><?= lang('add_new_purchase') ?></h4>
                </div>
                <div class="modal-body">

                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for=""><?= lang('vendor/supplier') ?><span class="error">*</span></label>
                            <select name="vendor_id" class="form-control" required>
                                <?php foreach ($vendors as $vendor): ?>
                                    <option value="<?= $vendor['id'] ?>"><?= $vendor['name'] ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group col-md-6">
                            <label for=""><?= lang('category') ?> <span class="error">*</span></label>
                            <select name="cat_id" class="form-control purchase_category" required>
                                <option value="0">Select Category</option>
                                <?php foreach ($categories as $row): ?>
                                    <?php if ($row['status'] == '1'): ?>
                                        <option value="<?= $row['id'] ?>"><?= $row['category'] ?></option>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div><!-- row -->

                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for=""><?= lang('item') ?> <span class="error">*</span></label>
                            <select name="item_id" class="form-control purchase_item" required>
                                <option value="0"><?= lang('not_found') ?></option>
                                <?php include VIEWPATH . 'backend/inventory/ajax_load_items.php' ?>
                            </select>
                        </div>

                        <div class="form-group col-md-6">
                            <label for=""><?= lang('stock') ?> <span class="error">*</span></label>
                            <input type="text" name="stock" class="form-control only_number" placeholder="<?= lang('stock') ?>" required>
                        </div>
                    </div><!-- row -->

                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for=""><?= lang('unit_price') ?> <span class="error">*</span></label>
                            <input type="text" name="unit_price" class="form-control only_number" placeholder="0.00" required>
                        </div>

                        <div class="form-group col-md-6">
                            <label for=""><?= lang('quantity') ?> <span class="error">*</span></label>
                            <input type="text" name="quantity" class="form-control only_number" value="1" required>
                        </div>
                    </div><!-- row -->

                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="name"><?= lang('purchase_date') ?><span class="error">*</span></label>
                            <input type="date" name="purchases_date" class="form-control" required>
                        </div>

                        <div class="form-group col-md-6">
                            <label for="name"><?= lang('invoice_number') ?><span class="error">*</span></label>
                            <input type="text" name="invoice_number" class="form-control only_number" placeholder="<?= lang('invoice_number') ?>" required>
                        </div>
                    </div><!-- row -->

                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for=""><?= lang('unit') ?> <span class="error">*</span></label>
                            <select name="unit" class="form-control" required>
                                <option value="<?= lang('pieces') ?>"><?= lang('pieces') ?></option>
                                <option value="<?= lang('boxs') ?>"><?= lang('boxs') ?></option>
                                <option value="<?= lang('packet') ?>"><?= lang('packet') ?></option>
                                <option value="<?= lang('bottle') ?>"><?= lang('bottle') ?></option>
                                <option value="<?= lang('kg') ?>"><?= lang('kg') ?></option>
                                <option value="<?= lang('g') ?>"><?= lang('g') ?></option>
                                <option value="<?= lang('liter') ?>"><?= lang('liter') ?></option>
                            </select>
                        </div>

                        <div class="form-group col-md-3">
                            <label for=""><?= lang('payment_method') ?><span class="error">*</span></label>
                            <select name="payment_method" class="form-control" required>
                                <option value="<?= lang('payment_with_cash') ?>"><?= lang('payment_with_cash') ?></option>
                                <option value="<?= lang('bank_transfer') ?>"><?= lang('bank_transfer') ?></option>
                                <option value="<?= lang('cash_on_delivery') ?>"><?= lang('cash_on_delivery') ?></option>
                            </select>
                        </div>

                        <div class="form-group col-md-3">
                            <label for=""><?= lang('payment_status') ?><span class="error">*</span></label>
                            <select name="payment_status" class="form-control" required>
                                <option value="<?= lang('paid') ?>"><?= lang('paid') ?></option>
                                <option value="<?= lang('unpaid') ?>"><?= lang('unpaid') ?></option>
                            </select>
                        </div>

                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal"><?= lang('close') ?></button>
                    <button type="submit" class="btn btn-primary"><?= lang('save_change') ?></button>
                </div>
            </div>
        </form>
    </div>
</div>


<!-- View Details Modal -->
<?php foreach ($purchases as $row): ?>
    <div class="modal fade" id="view_<?= $row['id'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h4 class="modal-title"><?= lang('details') ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="purchases_items_details col-md-6">
                            <ul class="item__details">
                                <li>
                                    <span><?= lang('vendor/supplier') ?> :</span>
                                    <span><?= isset($row['vendor_name']) ? $row['vendor_name'] : '' ?></span>
                                </li>
                                <li>
                                    <span><?= lang('items') ?> :</span>
                                    <span><?= isset($row['item_name']) ? $row['item_name'] : '' ?></span>
                                </li>
                                <li>
                                    <span> <?= lang('category') ?> :</span>
                                    <span><?= isset($row['category_name']) ? $row['category_name'] : '' ?></span>
                                </li>
                                <li>
                                    <span><?= lang('stock') ?> :</span>
                                    <span><?= $row['stock'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('unit') ?> :</span>
                                    <span><?= $row['unit'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('unit_price') ?> :</span>
                                    <span>$ <?= $row['unit_price'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('total_price') ?> : </span>
                                    <span> $<?= $row['total_price'] ?></span>
                                </li>
                            </ul>
                        </div>
                        <div class="purchases_items_details col-md-6">
                            <ul class="item__details">
                                <li>
                                    <span><?= lang('quantity') ?> :</span>
                                    <span><?= $row['quantity'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('invoice_number') ?> :</span>
                                    <span><?= $row['invoice_number'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('payment_method') ?> : </span>
                                    <span><?= $row['payment_method'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('payment_status') ?> :</span>
                                    <span><?= $row['payment_status'] ?></span>
                                </li>
                                <li>
                                    <span><?= lang('status') ?> :</span>
                                    <?php if ($row['stock'] > $row['item_min_stock']): ?>
                                        <span class="stock_status label-success"><?= lang('in_stock') ?></span>
                                    <?php elseif ($row['stock'] == 0): ?>
                                        <span class="stock_status label-danger"><?= lang('out_of_stock') ?></span>
                                    <?php else: ?>
                                        <span class="stock_status label-warning"><?= lang('low_stock') ?></span>
                                    <?php endif; ?>
                                </li>
                                <li>
                                    <span><?= lang('purchase_date') ?> :</span>
                                    <span><?= $row['purchases_date'] ?></span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>




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

        $.ajax({
            url: base_url + 'admin/inventory/get_inventory_items/' + id,
            type: 'GET',
            dataType: 'json',
            success: function(json) {
                if (json.success) {
                    $('.purchase_item').html(json.load_data);
                };
            }
        })
    })
</script>