<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('31', 'created');
$edited = can_action('31', 'edited');
$deleted = can_action('31', 'deleted');
?>

<div class="erp-page">
    <div id="transactions_state_report_div"></div>

    <?php ob_start(); ?>
    <div class="table-responsive p-4">
        <table class="table table-striped DataTables bulk_table" id="DataTables" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <?php if (!empty($deleted)) { ?>
                    <th data-orderable="false">
                        <div class="checkbox c-checkbox">
                            <label class="needsclick">
                                <input id="select_all" type="checkbox">
                            </label>
                        </div>
                    </th>
                    <?php } ?>
                    <th><?= lang('expense') . ' ' . lang('prefix'); ?></th>
                    <th><?= lang('name') . '/' . lang('title'); ?></th>
                    <th><?= lang('date'); ?></th>
                    <th><?= lang('account_name'); ?></th>
                    <th class="col-currency"><?= lang('amount'); ?></th>
                    <th><?= lang('tags'); ?></th>
                    <th><?= lang('status'); ?></th>
                    <?php $show_custom_fields = custom_form_table(2, null);
				if (!empty($show_custom_fields)) {
					foreach ($show_custom_fields as $c_label => $v_fields) {
						if (!empty($c_label)) { ?>
                    <th><?= $c_label; ?></th>
                    <?php }
					}
				}
				?>
                    <th><?= lang('attachment'); ?></th>
                    <th class="col-options no-sort text-right"><?= lang('action'); ?></th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
        <?php
	$table_markup = ob_get_clean();
	echo $this->load->view('admin/common/erp/table_shell', array(
		'table' => $table_markup,
	), true);
	?>
    </div>
</div>

<script>
list = base_url + "admin/transactions/expenseList";
window.erpDatatablePreload = window.erpDatatablePreload || {};
window.erpDatatablePreload.DataTables = window.erpDatatablePreload.DataTables || {};
<?php if (!empty($datatable_preload)) { ?>
window.erpDatatablePreload.DataTables["list"] = <?= json_encode($datatable_preload, JSON_UNESCAPED_SLASHES) ?>;
<?php } ?>
bulk_url = base_url + "admin/transactions/bulk_delete_expense";

function reloadExpenses(filter, searchType) {
    var url = list;
    if (filter) {
        url += "/" + filter;
        if (searchType) {
            url += "/" + searchType;
        }
    }
    if (typeof table_url !== "undefined") {
        table_url(url);
    } else if (typeof fire_datatable === "function") {
        fire_datatable(url);
    }
}

$(function() {
    window.expenseAccountFilter = function(id) {
        reloadExpenses(id, 'by_account');
    };
    window.expenseCategoryFilter = function(id) {
        reloadExpenses(id, 'by_category');
    };
    $(document).on('click', '[data-expense-filter-action]', function() {
        var filter = $(this).data('expense-filter') || '';
        var searchType = $(this).data('expense-search') || '';
        reloadExpenses(filter, searchType);
    });

    ins_data(base_url + 'admin/transactions/transactions_state_report');
});
</script>
