<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-2 " >
                    <label><?php echo CommonLanguage::getMessage('to_date'); ?></label>
                    <input id="date" type="text" class="form-control" name="date" value="<?php echo $today; ?>" >
                </div> 
                <div class="col-md-2  <?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '':'hidden'; ?>" >
                    <label><?php echo CommonLanguage::getMessage('currency') ?></label>
                    <select id="currency_id" class="full-width" data-init-plugin="select2">
                        <option value="1"><?php echo smc_session()['local_currency_name'] . ' - ' . smc_session()['local_currency_symbol'] ?></option>
                        <option value="2"><?php echo smc_session()['second_currency']['name'] . ' - ' . smc_session()['second_currency']['symbol'] ?></option>
                    </select>
                </div>
                <div class="col-md-2 <?php echo!$is_customer ? 'hidden' : '' ?>" >
                    <label><?php print CommonLanguage::getMessage('customers_salesman'); ?></label>
                    <input  id="cust_salesman"  class="full-width" type="hidden" />

                </div>
                <div class="col-md-2 <?php echo!$is_customer ? 'hidden' : '' ?>" >
                    <label><?php print CommonLanguage::getMessage('trans_salesman'); ?></label>
                    <input  id="salesman_id"  class="full-width" type="hidden" />

                </div>
                <div class="col-md-2 <?php echo!$is_customer ? 'hidden' : '' ?>">
                    <label><?php print CommonLanguage::getMessage('customer_group'); ?></label>
                    <input type="hidden"   name="customer_group_id" id="customer_group_id" class="full-width"/>
                </div>
<!--                <div class="col-md-2">
                    <label><?php print CommonLanguage::getMessage('acc_due_inv'); ?></label>
                    <input type="checkbox"   name="acc_due_inv" id="acc_due_inv" checked/>
                </div>-->
<!--                <div class="col-md-2" style="display: grid;">
                    <label><?php print CommonLanguage::getMessage('negative_balance'); ?>?</label>
                    <input type="checkbox"   name="negative_balance" id="negative_balance" />
                </div>-->
                <div class="col-md-2 " style="    ">
                    <button id="Generate_Report" type="button" class="btn btn-complete " data-toggle="dropdown" aria-expanded="false">
                        <?php print CommonLanguage::getMessage('generate_report'); ?></button>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered accounting" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('customer_name') ?></th>
                                    <?php if ($is_customer) { ?>
                                        <th class="v-align-middle text-center " > <?php print CommonLanguage::getMessage('salesman'); ?> </th>
                                        <th class="v-align-middle text-center " > <?php print CommonLanguage::getMessage('customer_group'); ?> </th>

                                    <?php } ?>
                                    <th class="v-align-middle text-center " > <?php print CommonLanguage::getMessage('limit_days'); ?> </th>
                                    <th class="v-align-middle text-center " > <?php print CommonLanguage::getMessage('limit_balance'); ?> </th>
                                    <th class="v-align-middle text-center " > <?php print CommonLanguage::getMessage('credit'); ?> </th>
                                    <th class="v-align-middle text-center " > <1 </th>
                                    <th class="v-align-middle text-center " > <15 </th>
                                    <th class="v-align-middle text-center " > <30 </th>
                                    <th class="v-align-middle text-center " ><60 </th>
                                    <th class="v-align-middle text-center " ><90 </th>     
                                    <th class="v-align-middle text-center " ><120 </th>
                                    <th class="v-align-middle text-center " > <?php print CommonLanguage::getMessage('More'); ?></th>
                                    <th class="v-align-middle text-center " style="width:16%" > <?php print CommonLanguage::getMessage('balance'); ?></th>
                                    <th class="v-align-middle text-center " style="width:16%" > <?php print CommonLanguage::getMessage('pending'); ?></th>
                                    <th class="v-align-middle text-center " style="width:16%" > <?php print CommonLanguage::getMessage('overall_balance'); ?></th>

                                </tr>
                            </thead>
                            <tbody></tbody>
                            <tfoot> 
                            <th><?php echo CommonLanguage::getMessage('Total') ?></th>
                            <?php if ($is_customer) { ?>
                                <th></th><th></th>
                                    <?php } ?><th></th><th></th>
                            <th> <div class="text-center"><span id="total_credit"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_0"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_15"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_30"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_60"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_90"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_120"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_more"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_balance"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_pending"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            <th> <div class="text-center"><span id="total_overall_balance"></span>&nbsp;<span class="currency_symbol"></span></div></th>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode($title); ?>,
            is_customer = <?php echo json_encode($is_customer) ?>,
            local_symbol = <?php echo json_encode(smc_session()['local_currency_symbol']) ?>,
            second_symbol = <?php echo json_encode(smc_session()['second_currency']['symbol']) ?>,
            is_actual = <?php echo json_encode($is_actual) ?>;

    $(function () {
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($("#salesman_id,#cust_salesman"), "xhr/employeeSearch")
        new Switchery($("#acc_due_inv").get(0), {color: $.Pages.getColor("complete")})
        new Switchery($("#negative_balance").get(0), {color: $.Pages.getColor("complete")})
        ChangeTable()

        $("#date").datepicker({autoclose: true})
        $("#Generate_Report").on("click", function () {
            ChangeTable()
        })

    });
    function ChangeTable() {

        var data = {
            date: $("#date").val(),
            is_actual: is_actual,
            is_customer: is_customer,
            currency_id: $("#currency_id").val(),
            salesman_id: $("#salesman_id").val(),
            cust_salesman: $("#cust_salesman").val(),
            customer_group_id: $("#customer_group_id").val(),
//            acc_due_inv: $("#acc_due_inv").is(":checked") ? 1 : 0,
            acc_due_inv: 0,
//            negative_balance: $("#negative_balance").is(":checked") ? 1 : 0,
            negative_balance:  1 ,
        };

        $.ajax({
            url: "ACCAjax/aging_report_bydate_ajax",
            data: data,
            dataType: 'JSON',
            complete: function (v1, v2) {
                if (v2 == 'success') {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseJSON['html'])
                    $("#total_credit").html(v1.responseJSON['totals']['credit'])
                    $("#total_0").html(v1.responseJSON['totals'][0])
                    $("#total_15").html(v1.responseJSON['totals'][15])
                    $("#total_30").html(v1.responseJSON['totals'][30])
                    $("#total_60").html(v1.responseJSON['totals'][60])
                    $("#total_90").html(v1.responseJSON['totals'][90])
                    $("#total_120").html(v1.responseJSON['totals'][120])
                    $("#total_more").html(v1.responseJSON['totals']['more'])
                    $("#total_balance").html(v1.responseJSON['totals']['balance'])
                    $("#total_pending").html(v1.responseJSON['totals']['pending'])
                    $("#total_overall_balance").html(v1.responseJSON['totals']['overall_balance'])
                    $(".currency_symbol").html($("#currency_id").val() == 1 ? local_symbol : second_symbol)
                    //                    console.log(v1.responseJSON['totals'])
                    RebuildDataTable(title, [], "", {}, '', '');
                }
            }
        });
    }
</script>