<?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('Automatic_invoice'); ?></div class="fs-1">
                        <div class="new_invoice_btn">
                            <button type="button" class="shadow-lg add_invoice_button" data-bs-toggle="modal" data-bs-target="#createPaymentModal" title="<?php echo $this->lang->line('new_invoice'); ?>">
                                <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_invoice'); ?>
                                </span>
                            </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-customer="-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-customer="-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-customer="-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-customer="-1" data-bs-target="#DeleteInvoice">
                                    <i class="pt-1 me-1 fas fa-eye text-success"></i>
                                </button>
                        </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('invoice_id'); ?></th>
                                <th><?php echo $this->lang->line('days'); ?></th>
                                <th><?php echo $this->lang->line('expire'); ?></th>
                                <th><?php echo $this->lang->line('counter'); ?></th>
                                <th><?php echo $this->lang->line('created_Invoice'); ?></th>
                                <th><?php echo $this->lang->line('last_update'); ?></th>
                                <th><?php echo $this->lang->line('date_creation'); ?></th>
                                </thead>
                                <tbody>
                                <?php foreach ($autoInvoice as $auto) { ?>
                                <?php if ($auto->invoice_type == "customer_invoice") { ?>
                                    <tr>
                                        <td><?php echo $auto->id; ?></td>
                                        <td><a href="/admin/invoices/view/<?php echo $auto->invoice_id; ?>" target="_blank"><?php echo $auto->invoice_id; ?></a></td>
                                        <td><?php echo $auto->days; ?></td>
                                        <td><?php echo $auto->expire; ?></td>
                                        <td><?php echo $auto->counter; ?></td>
                                        <td>
                                            <?php
                                            if (!empty($auto->created_Invoice)) {
                                                foreach (explode(",", $auto->created_Invoice) as $invoice) {
                                                    echo '<a href=/admin/invoices/view/' . $invoice . ' target="_blank" class="btn-link me-2">' . $invoice . '</a>';
                                                }
                                            };
                                            ?>
                                        </td>
                                        <td><?php echo $auto->lastUpdate; ?></td>
                                        <td><?php echo $auto->dateCreation; ?></td>
                                    </tr>
                                <?php } ?>
                            <?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="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 type="text/javascript">
    <?php
            $url = '';
            if( $this->session->userdata('site_lang') == 'hebrew' ){
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
            }else if( $this->session->userdata('site_lang') == 'arabic' ){
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Arabic.json';
            }else if( $this->session->userdata('site_lang') == 'english' ){
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json';
            }else{
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
            }
        ?>
        $(document).ready(function() {
            var oTbl1 = $('#dt_basic2').DataTable({
            "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'dt-buttons col-sm-6 col-xs-12 hidden-xs'l>r>" +
                "t" +
                "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
            "buttons": ['excelHtml5'],
            "processing": false,
            "bStateSave": false,
            "dom": 'Blfrtip',
            "order": [
                [0, "asc"]
            ],
            "pagingType": "full_numbers",
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "<?php echo $this->lang->line('Everything'); ?>"]
            ],
            "pageLength": 10,
            "sPaginationType": "full_numbers",
            "iDisplayLength": 10,
            "language": {
                    "url": "<?php echo $url; ?>"
                },
            orderCellsTop: true,
            fixedHeader: true
        });
        oTbl1.buttons().container().appendTo('.dataTables_filter');
        $('input[aria-controls="dt_basic2"]').attr('placeholder', '<?php echo $this->lang->line('search1'); ?>...');
        $('#dt_basic2_info').css('float', 'left').parent().css('float', 'left');
        $('.dataTables_paginate ').parent().css('float', 'right');
        $('.dataTables_paginate ').css('text-align', 'right');
        var row_input = '<tr><td></td>';
        row_input += '<td><input type="text" name="category_name" class="category_name form-control" value="" autocomplete="OFF" /></td>';
        row_input += '<td><input type="text" name="description" class="description form-control" value="" autocomplete="OFF" /></td>';
        row_input += '<td><a href="#0" class="add_new btn btn-success btn-circle"><i class="fa fa-check"></i></a></td></tr>';
        $(document).on('click', '.add_new', function() {
            var elem = $(this).parent().parent();
            var data = {};
            data.text = elem.find('.category_name').val();
            data.desc = elem.find('.description').val();
            $.post(root + 'admin/customers/addCategory', data, function(d) {
                if (d.result == true) {
                    elem.remove();
                    var edit_btn = '<a href="#0" class="edit_this btn btn-warning btn-circle" rel="' + d.id + '"><i class="fa fa-edit"></i></a>';
                    var del_btn = '<a href="#0" class="delete_this btn btn-danger btn-circle" rel="' + d.id + '"><i class="fa fa-trash-o"></i></a>';
                    var n_row_input = '<tr><td>' + d.id + '</td>';
                    n_row_input += '<td><input type="text" name="category_name" class="category_name form-control" value="' + data.text + '" autocomplete="OFF" /></td>';
                    n_row_input += '<td><input type="text" name="description" class="description form-control" value="' + data.desc + '" autocomplete="OFF" /></td>';
                    n_row_input += '<td>' + edit_btn + del_btn + '</td></tr>';
                    $('#dt_basic2 tbody').append(n_row_input);
                    var e = getResult(type = 'success');
                } else {
                    var e = getResult(type = 'failed');
                }
                $('#dt_basic2 tbody').append(row_input);
            }, 'json');
        });
        });

        $(document).on('click', '#dt_basic2 tbody tr', function() {
            let elem = $(this);
            let rowAttr = elem.attr('rel-customer');
            $('tr').css('background-color', "white");
            elem.css('background-color', "#e3edff")
            $('#deleteInvoiceButton').css('border-color', "#e9200c");
            $('#viewInvoiceButton').css('border-color', "#27b980");

            $('#deleteInvoiceButton').attr('rel-customer', rowAttr);
            $('#viewInvoiceButton').attr('rel-customer', rowAttr);
            $('#send_between').attr('rel-customer', rowAttr);

        });
        $('#savePaymentDetails').on('click', function() {

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

        $.post(root + 'admin/invoices/addPayment', 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>