<?php
if (
    !isset($permissions['finance']['invoices'])
    || isset($permissions['finance']['invoices'])
    && $permissions['finance']['invoices'] == 0
) redirect('admin/dashboard');

?>
<script src="https://cdn.lordicon.com/libs/mssddfmo/lord-icon-2.1.0.js"></script>
<div id="main p-0" role="main">
    <!-- <div class="header-bg shadow-sm p-0" style="background: url(https://images.pexels.com/photos/1475033/pexels-photo-1475033.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)"> -->
        <!-- <div class="header-bg shadow-lg p-0" style="background: url(https://images.pexels.com/photos/1487834/pexels-photo-1487834.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)"></div> -->
        <div id="content" style=" padding-top: 0px; ">
            <div class="container">
                <div class="row mt-3 justify-content-center">
                    <div class="header_invoice_wrapper shadow">
                        <div class="fs-1 w-25 text-white d-flex align-items-center"><?php echo $this->lang->line('all_products'); ?></div class="fs-1">
                        <div class="new_invoice_btn">
                            <button type="button" class="shadow-lg add_invoice_button" href="<?php echo site_url(); ?>admin/products/productForm/" title="<?php echo $this->lang->line('new_product'); ?>">
                                <lord-icon src="https://cdn.lordicon.com/mecwbjnp.json" trigger="loop" delay="1000" colors="primary:#356359,secondary:#00bcd4" style="width:32px;height:32px">
                                </lord-icon>
                                <span class="pt-1 text-dark">
                                    <?php echo $this->lang->line('new_product'); ?>
                                </span>
                            </button>
                            <button type="button" class="shadow-lg excel_exporter cancel_invoice p-2" style="width: 50px;" data-bs-toggle="collapse" href="#date_search" role="button" aria-expanded="false" aria-controls="date_search">
                                <i class="fa fa-search "></i> </button>
                            <button type="button" class="shadow-lg excel_exporter" data-bs-toggle="modal" data-bs-target="#ExcelExportModal">
                                <i class="far fa-file-excel "></i>
                                <!-- <lord-icon src="https://cdn.lordicon.com/nocovwne.json" trigger="loop" delay="1000" colors="primary:#356359,secondary:#00bcd4" style="width:32px;height:32px">
                                </lord-icon> -->
                                <!-- <span class="pt-1 text-dark">
                                    Excel
                                </span> -->
                            </button>
                            <button type="button" class="shadow-lg excel_exporter cancel_invoice p-2" style="width: 50px;" data-bs-toggle="modal" id="deleteInvoiceButton" rel-invoice="-1" data-bs-target="#DeleteInvoice">
                                <i class="fas fa-file-pdf text-warning"></i>
                            </button>
                            <button type="button" class="shadow-lg excel_exporter cancel_invoice p-2" style="width: 50px;" data-bs-toggle="modal" id="deleteInvoiceButton" rel-invoice="-1" data-bs-target="#DeleteInvoice">
                                <i class="fas fa-copy text-primary"></i>
                            </button>
                            <button type="button" class="shadow-lg excel_exporter cancel_invoice p-2" style="width: 50px;" data-bs-toggle="modal" id="deleteInvoiceButton" rel-invoice="-1" data-bs-target="#DeleteInvoice">
                                <i class="pt-1 me-1 fas fa-trash-alt text-danger"></i>
                            </button>
                            <button type="button" class="shadow-lg excel_exporter view_invoice p-2" style="width: 50px;" data-bs-toggle="modal" id="viewInvoiceButton" rel-invoice="-1" data-bs-target="#DeleteInvoice">
                                <i class="pt-1 me-1 fas fa-eye text-success"></i>
                            </button>
                        </div>
                    </div>
                    <div class="collapse w-100" id="date_search">
                        <div class="date-searchWrapper" style="margin-left: 10px;">
                            <div class="p-3 w-50 bg-white mx-auto" style="border-radius: 0 0 20px 20px; float: left;">
                                <form class="w-75 mx-auto" method="POST">
                                    <div class="form-group">
                                        <div class="">
                                            <label><?php echo $this->lang->line('customer'); ?></label>
                                            <select multiple style="width:100%" class="select2" name="customerId[]">
                                                <?php $suppliersIds = array();
                                                if (isset($_POST['suppliersIds'])) $suppliersIds = $_POST['suppliersIds'];
                                                foreach ($suppliers as $supplier) : ?>
                                                    <option value="<?php print $supplier->supplier_id; ?>" <?php if (in_array($supplier->supplier_id, $suppliersIds)) print 'selected="selected"'; ?>><?php print $supplier->name; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        <div class="d-flex justify-content-between">
                                            <div class="form-group">
                                                <div class="input">
                                                    <input class="form-control text-center" name="firstPayDate" id="firstPayDate" type="text" placeholder="<?php echo $this->lang->line('from_date'); ?>" value="<?php if (isset($_POST['firstPayDate'])) print $_POST['firstPayDate']; ?>" autocomplete="off" style="width: 100%;" />
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="input">
                                                    <input class="form-control text-center" name="firstPayToDate" id="firstPayToDate" type="text" placeholder="<?php echo $this->lang->line('to_date'); ?>" value="<?php if (isset($_POST['firstPayToDate'])) print $_POST['firstPayToDate']; ?>" autocomplete="off" style="width: 100%;" />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="button-group" style="margin-top: 24px;">
                                        <div class="form-group">
                                            <button class="btn btn-danger p-1" type="submit"><i class="fa fa-search"></i><?php echo $this->lang->line('search'); ?></button>
                                            <button class="btn btn-default p-1" type="button" onclick="clearAll();"><i class="fa fa-eraser"></i><?php echo $this->lang->line('clean_all'); ?></button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <hr class="bg-white mt-5" style="border-radius: 30px; padding: 2px;" />
                        <div class=" table-responsive card card-body">
                            <table class="table table-sm" id="dt_basic2">
                                <thead>
                                <th>#</th>
                                    <th><?php echo $this->lang->line('name'); ?></th>
                                    <th><?php echo $this->lang->line('sku'); ?></th>
                                    <th><?php echo $this->lang->line('price'); ?></th>
                                    <th><?php echo $this->lang->line('quantity'); ?></th>
                                    <th><?php echo $this->lang->line('quantity'); ?></th>
                                    <th><?php echo $this->lang->line('warehouse'); ?></th>
                                    <th><?php echo $this->lang->line('date_creation'); ?></th>
                                    <th><?php echo $this->lang->line('date'); ?> <?php echo $this->lang->line('update'); ?></th>
                                    <th><?php echo $this->lang->line('status'); ?></th>
                                </thead>
                                <tbody>
                                <?php foreach ($all_traffic as $traffic) { ?>
                                    <tr>
                                        <td><?php echo $traffic->id; ?></td>
                                        <td><?php echo $traffic->name; ?></td>
                                        <td><?php echo $traffic->code; ?></td>
                                        <td><?php echo $traffic->price; ?></td>
                                        <?php if ($traffic->is_in == 1) { ?>
                                            <td><?php echo $traffic->unit; ?></td>
                                            <td></td>
                                        <?php } else { ?>
                                            <td></td>
                                            <td><?php echo $traffic->unit; ?></td>
                                        <?php } ?>
                                        <td><?php echo $traffic->warehouse; ?></td>
                                        <td><?php echo date('d/m/Y H:i:s', strtotime($traffic->dateCreation)); ?></td>
                                        <td><?php echo date('d/m/Y H:i:s', strtotime($traffic->lastUpdate)); ?></td>
                                        <td>
                                            <?php if ($traffic->is_Exit == 0) { ?>
                                                <span style=" color: Tomato; font-size: 25px; ">
                                                    <i class="fas fa-clipboard-list"></i>
                                                </span>
                                            <?php } else if($traffic->is_Exit == 1) { ?>
                                                <span style=" color: ForestGreen; font-size: 25px;">
                                                    <i class="fas fa-clipboard-check"></i>
                                                </span>
                                            <?php } else { ?>
                                            <span style="font-size: 25px; color: Dodgerblue;">
                                                    <i class="fas fa-clipboard-check"></i>
                                            </span>
                                            <?php } ?>
                                        </td>
                                    </tr>
                                <?php }; ?>
                                </tbody>
                                <tfoot>
                                    <!-- <tr>
                                        <td colspan="2"></td>
                                        <td><strong><?php echo number_format($balance, 2); ?></strong></td>
                                        <td colspan="3"></td>
                                    </tr> -->
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="<?php echo site_url(); ?>assets/js/customerInvoice.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.5.2/bootbox.min.js" integrity="sha512-RdSPYh1WA6BF0RhpisYJVYkOyTzK4HwofJ3Q7ivt/jkpW6Vc8AurL1R+4AUcvn9IwEKAPm/fk7qFZW3OuiUDeg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css">
    <script>
        $(document).ready(function() {
            $('#savePaymentDetails').on('click', function() {

                var data = $('#payment_data').serializeArray();

                $.post(root + 'admin/invoices/addPaymentSupplier', data, function(d) {

                    if (d.result == true) {
                        alert("<?php echo $this->lang->line('Done'); ?>!");
                        location.reload();
                    } else {
                        alert("<?php echo $this->lang->line('Error'); ?>!");
                    }

                }, 'json');
            });

            $('a.convert_box').on('click', function() {

                var payment_id = $(this).attr('rel-id');
                $('#payment_id').val(payment_id);
                $('#convert_box').modal('show');

            });

            $('#convert_box_data').on('click', function() {

                var data = {};
                data.payment_id = $('#payment_id').val();
                data.new_box = $('#new_payment_box').val();
                data.new_payment = $('#new_payment_type').val();

                $.post(root + 'admin/invoices/supplierconvertPayment', data, function(d) {

                    if (d.result == true) {
                        alert("<?php echo $this->lang->line('Done'); ?>!");
                        location.reload();
                    } else {
                        alert("<?php echo $this->lang->line('Error'); ?>!");
                    }

                }, 'json');
            });



        });
    </script>