<?php
//if ($permissions->hasPermission('addExpenses')) {
//    include("Views/Accounting/addExpenses.php");
//}
?>
<style>
    #tableWithSearch thead tr>th {
        background-color: #4c0870;
        /*width:16% !important;*/
        color: #fff;
    }

    #tableWithSearch thead tr th[class*='sorting_'] {
        color: #fff;
    }

    .bg-master-lighter {
        border: none;
        background-color: #fff;
        border-top: 1px solid #ddd;
    }

    .datepicker thead tr .dow {
        color: #4c0870 !important;
    }

    .datepicker table tr td.day:hover {
        background: #4c0870 !important;
        color: #fff;
    }

    .datepicker table tr td.active {
        background-color: #4c0870 !important;
    }

    .datepicker thead tr .datepicker-switch {
        color: #4c0870 !important;
        font-size: 13px;
    }

    .datepicker table tr td span.active {
        background-color: #4c0870 !important;
    }

    .datepicker table tr td span:hover {
        background-color: #4c0870 !important;
        color: #fff;
    }

    .datepicker thead tr .next:before,
    .datepicker thead tr .prev:before {
        color: #4c0870 !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }

    .datepicker thead tr .next:before,
    .datepicker thead tr .prev:before {
        color: #4c0870 !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }

    .complete,
    .btn-group .active {
        background-color: #ed9421 !important;
        color: #fff;
    }

    #selected_entry {
        height: 90px;
        color: black;
        margin-top: 20px;
        font-size: 16px;
        padding-top: 2%;
    }

    .select_table td {
        padding-left: 70px;
    }

    .actionButton {
        background-color: #4c0870;
    }
</style>
<script src="<?php echo THEME_PATH; ?>/plugins/moment/moment.min.js"></script>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
            <div class="col-md-12 form-group">
                <div class="col-md-3 <?php echo $permissions->hasPermission('Branches') ? '' : 'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" >
                </div>


                <div class="  col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <input id="From_To_Date" type="text" class="form-control" name="From_To_Date">
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('search_note') ?></label>
                    <input id="search_note" type="text" class="form-control" name="search_note">
                </div>
                <?php if ($permissions->hasPermission($is_actual ? ($is_pending ? ($is_cash ? 'add_act_pending_cash_expense' : 'add_act_pending_expense') : ($is_cash ? 'add_act_cash_expense' : 'addExpensesActual')) : ($is_pending ? ($is_cash ? 'add_pending_cash_expense' : 'add_pending_expense') : ($is_cash ? 'add_cash_expense' : 'addExpenses')))) {
                ?>
                    <div class="col-md-3 pull-right">
                        <div class="floatingContainer ">
                            <a href="ACC/add_expense<?php echo ($is_actual ? '/act' : '') . ($is_pending ? '/pen' : '') . ($is_cash ? '/cash' : '') ?>">
                                <div class="actionButton">
                                    <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('add') ?></span></p>
                                </div>
                            </a>
                        </div>
                    </div>
                <?php } ?>
            </div>

        </div>
        <div class="panel-body">
            <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch">
                <thead>
                    <tr>
                        <th class="text-center" style="width:5%"><input type="checkbox" class="check_all_td pull-left multi_delete" />#</th>
                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('vendor_name'); ?></th>
                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Date'); ?></th>
                        <th class="text-center" style="width:15%"><?php print CommonLanguage::getMessage('description'); ?></th>
                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('total_price'); ?></th>
                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('vat'); ?></th>
                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('net_total'); ?></th>
                        <th class="text-center no-print" style="width:10%"><?php print CommonLanguage::getMessage('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
                <tfoot>
                    <th> <?php if ($permissions->hasPermission($is_actual ? ($is_pending ? ($is_cash ? "delete_multi_act_pending_cash_expense" : "delete_multi_act_pending_expense") : ($is_cash ? "delete_multi_act_cash_expense" : "act_delete_multi_expense")) : ($is_pending ? ($is_cash ? "delete_multi_act_cash_expense" : "delete_multi_pending_expense") : ($is_cash ? "delete_multi_cash_expense" : "delete_multi_expense")))) { ?>
                            <button class="btn btn-complete m-b-10 full-width" id="delete_all"><?php echo CommonLanguage::getMessage('Delete'); ?></button>
                        <?php }
                            if ($permissions->hasPermission($is_actual ? ($is_pending ? ($is_cash ? "multi_act_pending_cash_expense_to_official" : 'multi_act_pending_expense_to_official') : ($is_cash ? "multi_act_cash_expense_to_official" : 'multi_expense_to_official')) : ($is_pending ? ($is_cash ? "multi_pending_cash_expense_to_act" : 'multi_pending_expense_to_actual') : ($is_cash ? "multi_cash_expense_to_act" : 'multi_expense_to_actual')))) { ?>
                            <button class="btn btn-complete full-width" id="change_actual"><?php echo CommonLanguage::getMessage($is_actual ? 'to_official' : 'to_actual'); ?></button>
                        <?php } ?>
                    </th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th class="text-center"><strong id="total"></strong></th>
                    <th></th>
                </tfoot>
            </table>
        </div>
    </div>
</div>
<div class="modal fade" id="ConfirmDelete" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header text-left">
                <h4 class="modal-title bold"><?php print CommonLanguage::getMessage('Confirm_Delete') ?></h4>
                <div id="delete_alert"></div>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 m-t-10">
                        <p><?php print CommonLanguage::getMessage('about_delete_record') ?>&nbsp;<span id="rec_nb" class="bold red-star"></span></p>
                        <p><span class="red-star"><?php echo CommonLanguage::getMessage('dep_check_lang') ?></span></p>
                        <p><?php print CommonLanguage::getMessage('Proceed_Delete') ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn btn-danger width-auto" id="rec_delete"><?php print CommonLanguage::getMessage('delete') ?></button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="change_actual_modal" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <div id="change_act_alert"></div>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage($is_actual ? 'to_official_alert' : 'to_actual_alert') ?></h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 m-t-10">
                        <span id="act_nb" class="bold red-star"></span>
                        <p><?php print CommonLanguage::getMessage('proceed') . ' ?' ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn btn-danger width-auto" id="change_act_btn"><?php print CommonLanguage::getMessage('proceed') ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var Is_Actual = <?php echo $is_actual ? 'true' : 'false' ?>;
    var startDate, endDate,
        from_date = <?php echo json_encode($from_date); ?>,
        to_date = <?php echo json_encode($to_date); ?>,
        is_pending = <?php echo json_encode($is_pending); ?>,
        is_cash = <?php echo json_encode($is_cash); ?>,
        title = <?php echo json_encode(CommonLanguage::getMessage('expense_list')); ?>,
        rec_to_delete = [],
        rec_nb_del = [],
        rec_to_change = [],
        rec_nb_act = [];
    var today = <?php echo json_encode($today) ?>;
    $(function() {
        var data_session = get_storage_element(page_link);
        if (from_date != '' && to_date != '') {
            window.startDate = from_date;
            window.endDate = to_date;
            $("input[name=From_To_Date]").val(from_date + ' - ' + to_date)
        } else
        if (data_session.Expense_List_Date_From != undefined && data_session.Expense_List_Date_To != undefined) {
            window.startDate = data_session.Expense_List_Date_From;
            window.endDate = data_session.Expense_List_Date_To;
            $("input[name=From_To_Date]").val(data_session.Expense_List_Date_From + ' - ' + data_session.Expense_List_Date_To)
        }
        ChangeTable();
        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        $('#search_branch_id').on('change', function () {
            ChangeTable();
        });

        $(document).on('keydown', function(e) {
            if (e.which == 13)
                ChangeTable();
        });
        $("#tableWithSearch").on("click", ".settle_exp", function() {
            $.ajax({
                url: "ACCAjax/settle_expense_ajax",
                data: {
                    exp_id: $(this).parents('.Action').attr("data-id")
                },
                method: 'post',
                complete: function(v1, v2) {
                    if (v2 == 'success') {
                        if (v1.responseText == 'OK') {
                            toastr.success('<?php echo CommonLanguage::getMessage('Submitted_Successfully') ?>')
                            ChangeTable()
                        } else {
                            toastr.error('<?php echo CommonLanguage::getMessage('Failed_Submit') ?>')
                        }
                    }
                }
            })
        })
        $("input[name=From_To_Date]").daterangepicker({}, function(start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                window.endDate = end.format("YYYY-MM-DD")

            ChangeTable();

        });
        $("#delete_all").on("click", function() {
            rec_to_delete = []
            rec_nb_del = []
            $("#tableWithSearch .del_td:checked").each(function() {
                rec_to_delete.push($(this).attr("data-id"));
                rec_nb_del.push($(this).attr("data-nb"));
            })
            if ($("#tableWithSearch .del_td:checked").length > 0) {
                $("#rec_nb").html(rec_nb_del.join())
                $("#ConfirmDelete").modal("show")
            }
        })
        $("#tableWithSearch").on("click", ".delete_exp", function() {
            $('#rec_delete').attr('disabled', false);
            rec_to_delete = [$(this).parents(".Action").attr("data-id")];
            $("#rec_nb").html($(this).parents("tr").find("td:eq(0)").text())
            $("#ConfirmDelete").modal("show");
        })
        $("#rec_delete").on("click", function() {
            $.ajax({
                url: "ACCAjax/delete_expense",
                method: "GET",
                data: {
                    ids: rec_to_delete
                },
                complete: function(v1, v2) {
                    if (v2 == 'success') {
                        if (v1.responseText == 'ok') {
                            $("#delete_alert").attr("class", "alert alert-success").html('<?php echo CommonLanguage::getMessage('Deleted_Successfully') ?>')
                            rec_to_delete = []
                            ChangeTable()
                            setTimeout(function() {
                                $("#ConfirmDelete").modal("hide")
                                $("#delete_alert").addClass("hidden")
                            }, 1500)
                        } else {
                            $("#delete_alert").attr("class", "alert alert-danger").html(v1.responseText)
                        }
                    }
                }
            });
            //            common_delete_modal[1] = common_delete_modal[1] + '<br><span class="red-star">' + dep_check_lang + '</span>'
            //            IncludeDeleteModal(common_delete_modal, {id:rec_to_delete}, function () {
            //                ChangeTable();
            //            }, "ACCAjax/delete_expense");
        })
        $("#tableWithSearch").on("click", "input.check_all_td", function(e) {
            var is_checked = $(this).is(":checked");
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                $(row[i]).find("input.del_td").prop("checked", is_checked);
            }
        });
        $("#tableWithSearch").on("click", ".change_actual", function() {
            rec_to_change = [$(this).parents(".Action").attr("data-id")];
            $("#act_nb").html($(this).parents("tr").find("td:eq(0)").text())
            $("#change_actual_modal").modal("show")
        })
        $("#change_actual").on("click", function() {
            rec_to_change = []
            rec_nb_act = []
            $("#tableWithSearch .del_td:checked").each(function() {
                rec_to_change.push($(this).attr("data-id"));
                rec_nb_act.push($(this).attr("data-nb"));
            })
            if ($("#tableWithSearch .del_td:checked").length > 0) {
                $("#act_nb").html(rec_nb_act.join())
                $("#change_actual_modal").modal("show")
            }
        })
        $("#change_act_btn").on("click", function() {
            $.ajax({
                url: "ACCAjax/change_expense_actual",
                method: "POST",
                data: {
                    id: rec_to_change,
                    is_actual: Is_Actual ? 0 : 1,
                    is_pending: is_pending ? 1 : 0
                },
                complete: function(v1, v2) {
                    if (v2 == 'success') {
                        if (v1.responseText == 'OK') {
                            $("#change_act_alert").attr("class", "alert alert-success").html('<?php echo CommonLanguage::getMessage('Submitted_Successfully') ?>')
                            rec_to_change = []
                            ChangeTable()
                            setTimeout(function() {
                                $("#change_actual_modal").modal("hide")
                                $("#change_act_alert").addClass("hidden")
                            }, 1500)
                        } else {
                            $("#change_act_alert").attr("class", "alert alert-danger").html(v1.responseText)
                        }
                    }
                }
            })
        })
    })

    function ChangeTable() {
        var Date = {
            Is_Actual: Is_Actual ? 1 : 0,
            Expense_List_Date_From: window.startDate ? window.startDate : today,
            Expense_List_Date_To: window.endDate ? window.endDate : today,
            is_pending: is_pending,
            is_cash: is_cash,
            search_note: $("#search_note").val(),
            search_branch_id: $("#search_branch_id").val()

        };

        set_storage_element(page_link, Date);
        $.ajax({
            url: "ACCAjax/get_total_expenses",
            data: Date,
            complete: function(v1, v2) {
                if (v2 == "success") {
                    RebuildDataTable(title, [0, 1, 2, 3, 4, 5, 6], "ACCAjax/ExpenseListRequest", Date);
                    $("#total").html(v1.responseText);
                }
            }
        })


    }
</script>