<?php include 'inc/inventory_menu.php'; ?>
<div class="row">
    <div class="col-lg-10">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title flex-1"><?= lang('item_list') ?></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>
                                <th>#</th>
                                <th><?= lang('items') ?></th>
                                <th><?= lang('category') ?></th>
                                <th><?= lang('unit') ?></th>
                                <th><?= lang('min_stock') ?></th>
                                <th><?= lang('status') ?></th>
                                <th><?= lang('action') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($purchases_items as $key => $row): ?>
                                <tr>
                                    <td><?= $key + 1; ?></td>
                                    <td data-label="<?= lang('name'); ?>"><?= $row['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('min_stock'); ?>"><?= $row['min_stock'] ?></td>
                                    <td data-label="<?= lang('status'); ?>">
                                        <?php if (is_access('change-status') == 1) : ?>
                                            <a href="javascript:;" data-id="<?= html_escape($row['id']); ?>" data-status="<?= html_escape($row['status']); ?>" data-table="vendor_inventory_purchases_items" class="label <?= $row['status'] == 1 ? 'label-success' : 'label-danger' ?> change_status"> <i class="fa <?= $row['status'] == 1 ? 'fa-check' : 'fa-close' ?>"></i>&nbsp; <?= $row['status'] == 1 ? (!empty(lang('live')) ? lang('live') : "Live") : (!empty(lang('hide')) ? lang('hide') : "Hide"); ?></a>
                                        <?php endif; ?>
                                    </td>
                                    <td data-label="<?= lang('action'); ?>">
                                        <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_items'); ?>" 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><!-- Table -->
                </div>
            </div><!-- card-body -->
        </div>
    </div>
</div>


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

<!-- Purchase Items Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <form action="<?= base_url('admin/inventory/add_purchases_items') ?>" 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') ?></h4>
                </div>
                <div class="modal-body">

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

                    <div class="form-group">
                        <label for=""><?= lang('product') ?> <?= lang('name') ?> <span class="error">*</span></label>
                        <input type="text" name="name" class="form-control" placeholder="<?= lang('name') ?>" required>
                    </div>

                    <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-6">
                            <label for=""><?= lang('min_stock') ?> <span class="error">*</span></label>
                            <input type="text" name="min_stock" class="form-control" placeholder="<?= lang('min_stock') ?>" value="5" required>
                        </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>


<!-- Purchase Item Edit Modal -->
<?php foreach ($purchases_items as $row): ?>
    <div class="modal fade" id="edit_<?= $row['id'] ?>" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <form action="<?= base_url('admin/inventory/add_purchases_items') ?>" 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('edit') ?></h4>
                    </div>
                    <div class="modal-body">

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

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

                        <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-6">
                                <label for=""><?= lang('min_stock') ?> <span class="error">*</span></label>
                                <input type="text" name="min_stock" class="form-control" placeholder="<?= lang('min_stock') ?>" value="<?= $row['min_stock'] ?>" required>
                            </div>
                        </div>

                    </div>
                    <input type="hidden" name="item_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; ?>