<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title">
                <h4 class="bold"><?php echo CommonLanguage::getMessage('coupon_type_balance'); ?> </h4>
            </div>
            <div class="row">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                    </div>
                </div> 
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('bone_type') ?></label>
                    <input id="coupon_type" type="hidden" class="full-width" name="coupon_type" >
                </div> 
                <div class="col-md-3">
                    <div><label><?php print CommonLanguage::getMessage('with_details'); ?></label></div>
                    <input type="checkbox" id="with_details" name="with_details"/>
                </div>
                <div class="col-md-3">
                    <div><label><?php print CommonLanguage::getMessage('with_previous'); ?></label></div>
                    <input type="checkbox" id="with_previous" name="with_previous" checked/>
                </div>

            </div>
        </div>
        <div class="panel-body">
            <div class="row ">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('product'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('quantity'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>

                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('coupon_type_balance')); ?>,
            is_actual = <?php echo json_encode($is_actual); ?>,
            today = <?php echo json_encode($today) ?>;
    $(function () {
        build_select2($("#coupon_type"), "xhr/CommonSearch?col_id=bone_type_id&col_name=bone_type_name&table=tbl_bone_type")

        ChangeTable();
        $("input[name=From_To_Date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD")
            ChangeTable();
        });
        $('#coupon_type').on('change', function () {
            ChangeTable();
        });
        $('#with_details,#with_previous').on('click', function () {
            ChangeTable();
        });



    })
    function ChangeTable() {
        var Date = {
            is_actual: is_actual ? 1 : 0,
            coupon_type: $('#coupon_type').val(),
            with_details: $('#with_details').is(":checked") ? 1 : 0,
            with_previous: $('#with_previous').is(":checked") ? 1 : 0,
            Date_From: window.startDate ? window.startDate : today,
            Date_To: window.endDate ? window.endDate : today};
//                            RebuildDataTable(title, [0, 1, 2], "InventoryAjax/coupon_type_balance_ajax", Date);

        $.ajax({
            url: "InventoryAjax/get_coupon_type_balance",
            data: Date,
            complete: function (v1, v2) {
                if (v2 == "success") {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseText)
                    RebuildDataTable(title, [], "", {}, '', '', {bSort: false})
                }
            }
        })
    }
</script>