<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('daily_log'); ?></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="#newModal" title="<?php echo $this->lang->line('new_daily_log'); ?>">
                            <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_daily_log'); ?>
                            </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 view_invoice p-2  getPaymentsData" id="view_log" rel-log="-1" style="width: 50px;">
                            <i class="pt-1 me-1 fas fa-edit text-warning"></i>
                        </button>
                    </div>
                </div>
                <div class="collapse w-100" id="date_search" style="margin-left: 18px;">
                    <div class="date-searchWrapper">
                        <div class="card p-3 w-25" style="border-radius: 0 0 20px 20px; float: left;">

                            <form method="POST" action="<?php echo site_url(); ?>admin/customers/customersOnline">
                                <div>
                                    <div class="form-group">
                                        <input type="text" class="form-control datepicker" name="from_date" id="from_date" value="" autocomplete="OFF" placeholder="<?php echo $this->lang->line('from_date'); ?>">
                                    </div>
                                </div>
                                <div>
                                    <div class="form-group">
                                        <input type="text" class="form-control datepicker" name="to_date" id="to_date" value="" autocomplete="OFF" placeholder="<?php echo $this->lang->line('to_date'); ?>">
                                    </div>
                                </div>
                                <div>
                                    <button type="submit" class="btn btn-success p-2">
                                        <?php echo $this->lang->line('search'); ?>
                                    </button>
                                </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><?php echo $this->lang->line('number_shortcut'); ?></th>
                                <th><?php echo $this->lang->line('date'); ?></th>
                                <th><?php echo $this->lang->line('debit'); ?></th>
                                <th><?php echo $this->lang->line('balance_credit'); ?></th>
                                <th><?php echo $this->lang->line('difference'); ?></th>
                                <th><?php echo $this->lang->line('notes'); ?></th>
                            </thead>
                            <?php foreach ($dailyLog as $log) { ?>
                                <tbody>
                                    <tr rel-log="<?php echo $log->id; ?>">
                                        <td><?php echo $log->id; ?></td>
                                        <td><?php echo date("d/m/Y", strtotime($log->dateCreation)); ?></td>
                                        <td><?php echo $log->sumCredit ?></td>
                                        <td><?php echo $log->sumDebit ?></td>
                                        <td><?php echo $log->sumTotal ?></td>
                                        <td><?php echo $log->note ?></td>
                                    </tr>
                                </tbody>
                            <?php }; ?>
                            <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 class="modal" tabindex="-1" id="viewModal" role="dialog" title="<?php echo $this->lang->line('daily_log'); ?>">
                <div class="modal-dialog modal-xl" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title"><?php echo $this->lang->line('daily_log'); ?></h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-lg-12">
                                    <form action="<?php echo site_url(); ?>admin/dailyLog/updateLog" method="POST" id="updateLog" autocomplete="OFF">
                                        <input type="hidden" value="" id="viewLogId" name="logId" />
                                        <div class="row">
                                            <div class="col col-lg-12 col-md-12 col-sm-12">
                                                <h2>
                                                    <?php echo $this->lang->line('daily_log'); ?>
                                                </h2>
                                            </div>
                                            <div class="col col-lg-12 col-md-12 col-sm-12" id="log_table_view">
                                                <table class="table" id="log_table_view">
                                                    <thead>
                                                        <th>##</th>
                                                        <th><?php echo $this->lang->line('debit'); ?></th>
                                                        <th><?php echo $this->lang->line('balance_credit'); ?></th>
                                                        <th><?php echo $this->lang->line('notes'); ?></th>
                                                    </thead>
                                                    <tbody></tbody>
                                                    <tfoot>
                                                        <tr id="btns_group">
                                                            <td colspan="6" style="text-align:center;">
                                                                <a href="#0" class="btn btn-success btn-circle add_item_view">
                                                                    <i class="fa fa-plus"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                <input type="text" value="" name="notes" placeholder="<?php echo $this->lang->line('note_for_all'); ?>" class="form-control">
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" id="view_total_credit" name="total" class="form-control" readonly="readonly" />
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" id="view_total_debit" name="total" class="form-control" readonly="readonly" />
                                                            </td>
                                                            <td></td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </div>
                                            <div class="col col-lg-12 col-lg-12 col-sm-12">
                                                <div class="row">
                                                    <div class="col-lg-5"></div>
                                                    <div class="col-lg-5"></div>
                                                    <div class="col-lg-2">
                                                        <button type="button" class="btn btn-success btn-block" disabled="disabled" id="save_logView">
                                                            <?php echo $this->lang->line('confirm'); ?>
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-bs-dismiss="modal"><?php echo $this->lang->line('close'); ?></button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal" id="newModal" role="dialog" title="<?php echo $this->lang->line('new_daily_log'); ?>">
                <div class="modal-dialog modal-xl">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-bs-dismiss="modal">&times;</button>
                            <h4 class="modal-title"><?php echo $this->lang->line('new_daily_log'); ?></h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-lg-12">
                                    <form action="<?php echo site_url(); ?>admin/dailyLog/createLog" method="POST" id="createNewLog" autocomplete="OFF">
                                        <div class="row">
                                            <div class="col col-lg-12 col-md-12 col-sm-12">
                                                <h2><?php echo $this->lang->line('new_daily_log'); ?></h2>
                                            </div>
                                            <div class="col col-lg-12 col-md-12 col-sm-12" id="invoice_details">
                                                <table class="table" id="log_table">
                                                    <thead>
                                                        <th>##</th>
                                                        <th><?php echo $this->lang->line('debit'); ?></th>
                                                        <th><?php echo $this->lang->line('balance_credit'); ?></th>
                                                        <th><?php echo $this->lang->line('notes'); ?></th>
                                                    </thead>
                                                    <tbody>
                                                        <tr>
                                                            <td>
                                                                <select class="id_item select2" name="account[1]">
                                                                    <?php foreach ($all_accounts as $accountKey => $accountArray) { ?>
                                                                            <?php foreach ($accountArray as $current_account) { ?>
                                                                                <?php
                                                                                if ($accountKey == 'suppliers')  $current_account->name = '300' . $current_account->id . ' - ' . $current_account->name;
                                                                                if ($accountKey == 'users')      $current_account->name = '400' . $current_account->id . ' - ' . $current_account->name;
                                                                                if ($accountKey == 'accounts')   $current_account->name = '200' . $current_account->id . ' - ' . $current_account->name;
                                                                                ?>
                                                                                <option value="<?php echo $current_account->id; ?>"><?php echo $current_account->name; ?></option>
                                                                            <?php }; ?>
                                                                    <?php }; ?>
                                                                </select>
                                                            </td>
                                                            <td><input name="total_credit[]" class="total_credit form-control"></td>
                                                            <td><input name="total_debit[]" class="total_debit form-control"></td>
                                                            <td><input name="note[]" class="form-control"></td>
                                                        </tr>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr id="btns_group">
                                                            <td colspan="6" style="text-align:center;">
                                                                <a href="#0" class="btn btn-success btn-circle add_item">
                                                                    <i class="fa fa-plus"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                <input type="text" value="" name="notes" placeholder="<?php echo $this->lang->line('note_for_all'); ?>" class="form-control">
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" id="total_credit" name="total" class="form-control" readonly="readonly" />
                                                            </td>
                                                            <td>
                                                                <input type="text" value="" id="total_debit" name="total" class="form-control" readonly="readonly" />
                                                            </td>
                                                            <td></td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </div>
                                            <div class="col col-lg-12 col-lg-12 col-sm-12">
                                                <div class="row">
                                                    <div class="col-lg-5"></div>
                                                    <div class="col-lg-5"></div>
                                                    <div class="col-lg-2">
                                                        <button type="button" class="btn btn-success btn-block" disabled="disabled" id="save_log">
                                                            <?php echo $this->lang->line('confirm'); ?>
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-bs-dismiss="modal"><?php echo $this->lang->line('close'); ?></button>
                        </div>
                    </div>
                </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() {
        $('#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": [],
            "processing": false,
            "bStateSave": false,
            "dom": 'Blfrtip',
            "pagingType": "full_numbers",
            "lengthMenu": [
                [10, 25, 50, -1],
                [10, 25, 50, "הכל"]
            ],
            "pageLength": 20,
            "sPaginationType": "full_numbers",
            "iDisplayLength": 10,
            "sScrollY": 500,
            "bScrollCollapse": true,
            "scrollX": true,
            "language": {
                "url": "<?php echo $url; ?>"
            },
            orderCellsTop: true,
            fixedHeader: false,
            searching: false
        });
        $(document).on('click', '#dt_basic2 tbody tr', function() {
            let elem = $(this);
            let rowAttr = elem.attr('rel-log');
            $('tr').css('background-color', "white");
            elem.css('background-color', "#e3edff")
            $('#deleteInvoiceButton').css('border-color', "#e9200c");
            $('#viewInvoiceButton').css('border-color', "#27b980");

            $('#deleteInvoiceButton').attr('rel-log', rowAttr);
            $('#viewInvoiceButton').attr('rel-log', rowAttr);
            $('#view_log').attr('rel-log', rowAttr);
        });
        $(document).on('change', '.total_credit', function() {
            var elem = $(this);
            var row = elem.parent().parent();
            row.find('.total_debit').val("");
            row.find('.total_debit').attr("readonly", true);
            getSumTd();
        });
        $(document).on('change', '.total_debit', function() {
            var elem = $(this);
            var row = elem.parent().parent();
            row.find('.total_credit').val("");
            row.find('.total_credit').attr("readonly", true);
            getSumTd();
        });
        $(document).on('change', '.total_credit_view', function() {
            var elem = $(this);
            var row = elem.parent().parent();
            row.find('.total_debit_view').val("");
            row.find('.total_debit_view').attr("readonly", true);
            getSumTdView();
        });
        $(document).on('change', '.total_debit_view', function() {
            var elem = $(this);
            var row = elem.parent().parent();
            row.find('.total_credit_view').val("");
            row.find('.total_credit_view').attr("readonly", true);
            getSumTdView();
        });
        $(document).on('click', '.add_item', function() {
            var $row = $('#log_table tbody tr:first').clone();
            $row.find('input').val("");
            $row.find('input').removeAttr("readonly");
            $('#log_table tbody').append($row);
            getSumTd();
        });
        $(document).on('click', '.add_item_view', function() {
            var $row = $('#log_table_view tbody tr:first').clone();
            $row.find('input').val("");
            $row.find('input').removeAttr("readonly");
            $row.find('input.total_credit_view').attr("name", "total_credit[]");
            $row.find('input.total_debit_view').attr("name", "total_debit[]");
            $row.find('input.view_note').attr("name", "note[]");
            $row.find('select.id_item').attr("name", "account[]");
            $('#log_table_view tbody').append($row);
            getSumTdView();
        });
        $('#save_log').on('click', function() {
            $('#createNewLog').submit();
        });
        $('#save_logView').on('click', function() {
            $('#updateLog').submit();
        });
        $(document).on('click', '.remove_this_item', function() {});
        $(document).on('dblclick', '#dt_basic2 tr', function() {
            let elem = $(this);
            if (elem.attr("rel-log") === "-1") {
                bootbox.alert("Please Select from the table below to continue");
            } else {
                $('#log_table_view tbody').html("");
                var data = {};
                data.id = $(this).attr("rel-log");
                $.post(root + 'admin/dailyLog/getPaymentsData', data, function(d) {
                    if (d.result) {
                        $('#viewLogId').val(data.id);
                        var sumDebit = 0;
                        var sumCredit = 0;
                        $(d.data).each(function(index, value) {
                            var $row = '<tr><td>';
                            $row += '<select class="id_item" name="account[' + value.id + ']">';
                            $row += '<option>2003</option>';
                            $row += '<option>nn</option>';
                            $row += '</select>';
                            $row += '</td>';
                            if (Number(value.total) > 0) {
                                sumCredit += Number(value.total);
                                $row += '<td><input value="' + Number(value.total) + '" name="total_credit[' + value.id + ']" class="total_credit_view form-control"></td>';
                                $row += '<td><input readonly="readonly" name="total_debit[' + value.id + ']" class="total_debit_view form-control"></td>';
                            } else {
                                sumDebit += Number(value.total) * -1;
                                $row += '<td><input readonly="readonly" name="total_credit[' + value.id + ']" class="total_credit_view form-control"></td>';
                                $row += '<td><input value="' + Number(value.total) * -1 + '" name="total_debit[' + value.id + ']" class="total_debit_view form-control"></td>';
                            }
                            $row += '<td><input value="' + value.notes + '" name="note[' + value.id + ']" class="view_note form-control"></td>';
                            $row += '</tr>';
                            $('#log_table_view tbody').append($row);
                        });
                        if (sumCredit == sumDebit) {
                            $('#save_logView').attr('disabled', false);
                        }
                        $('#view_total_credit').val(sumCredit);
                        $('#view_total_debit').val(sumDebit);
                    }
                }, 'json');
                $('#viewModal').modal('show');
            }
        });
        $(document).on('click', '.getPaymentsData', function() {
            let elem = $(this);
            if (elem.attr("rel-log") === "-1") {
                bootbox.alert("Please Select from the table below to continue");
            } else {
                $('#log_table_view tbody').html("");
                var data = {};
                data.id = $(this).attr("rel-log");
                $.post(root + 'admin/dailyLog/getPaymentsData', data, function(d) {
                    if (d.result) {
                        $('#viewLogId').val(data.id);
                        var sumDebit = 0;
                        var sumCredit = 0;
                        $(d.data).each(function(index, value) {
                            var $row = '<tr><td>';
                            $row += '<select class="id_item" name="account[' + value.id + ']">';
                            $row += '<option>2003</option>';
                            $row += '<option>nn</option>';
                            $row += '</select>';
                            $row += '</td>';
                            if (Number(value.total) > 0) {
                                sumCredit += Number(value.total);
                                $row += '<td><input value="' + Number(value.total) + '" name="total_credit[' + value.id + ']" class="total_credit_view form-control"></td>';
                                $row += '<td><input readonly="readonly" name="total_debit[' + value.id + ']" class="total_debit_view form-control"></td>';
                            } else {
                                sumDebit += Number(value.total) * -1;
                                $row += '<td><input readonly="readonly" name="total_credit[' + value.id + ']" class="total_credit_view form-control"></td>';
                                $row += '<td><input value="' + Number(value.total) * -1 + '" name="total_debit[' + value.id + ']" class="total_debit_view form-control"></td>';
                            }
                            $row += '<td><input value="' + value.notes + '" name="note[' + value.id + ']" class="view_note form-control"></td>';
                            $row += '</tr>';
                            $('#log_table_view tbody').append($row);
                        });
                        if (sumCredit == sumDebit) {
                            $('#save_logView').attr('disabled', false);
                        }
                        $('#view_total_credit').val(sumCredit);
                        $('#view_total_debit').val(sumDebit);
                    }
                }, 'json');
                $('#viewModal').modal('show');
            }
        });
    });

    function getSumTd() {
        var sumDebit = 0;
        var sumCredit = 0;
        $('.total_debit').each(function(index) {
            sumDebit += Number($(this).val());
        });
        $('.total_credit').each(function(index) {
            sumCredit += Number($(this).val());
        });
        $('#total_credit').val(sumCredit);
        $('#total_debit').val(sumDebit);
        if (sumDebit > 0 && sumCredit > 0 && sumDebit == sumCredit) {
            $('#save_log').attr('disabled', false);
        } else {
            $('#save_log').attr('disabled', true);
        }
    }

    function getSumTdView() {
        var sumDebit = 0;
        var sumCredit = 0;
        $('#log_table_view .total_credit_view').each(function(index) {
            sumCredit += Number($(this).val());
        });
        $('#log_table_view .total_debit_view').each(function(index) {
            sumDebit += Number($(this).val());
        });
        $('#view_total_credit').val(sumCredit);
        $('#view_total_debit').val(sumDebit);
        if (sumDebit > 0 && sumCredit > 0 && sumDebit == sumCredit) {
            $('#save_logView').attr('disabled', false);
        } else {
            $('#save_logView').attr('disabled', true);
        }
    }
</script>
<style>
    table#dt_basic2 {
        width: 100% !important;
    }
</style>