<#assign title="结算">
<#include "/admin/head.ftl">
<section class="vbox">
    <section class="scrollable wrapper">
        <div class="panel panel-default">
            <div class="panel-body">
                <form id="theform" name="theform" onsubmit="return false;">
                    <div class="row">
                        <div class="col-xs-12 col-sm-6 col-md-3 form-group">
                            <div class="input-group">
                                <label class="input-group-addon" for="saleName">销售</label>
                                <input type="text" id="saleName" name="saleName" class="form-control"></input>
                            </div>
                        </div>

                        <div class="col-xs-12 col-sm-6 col-md-3 form-group">
                            <div class="input-group">
                                <label class="input-group-addon" for="orderStartAt">开始时间</label>
                                <input type="date" id="orderStartAt" name="orderStartAt" class="form-control"></input>
                            </div>
                        </div>

                        <div class="col-xs-12 col-sm-6 col-md-3 form-group">
                            <div class="input-group">
                                <label class="input-group-addon" for="orderEndAt">结束时间</label>
                                <input type="date" id="orderEndAt" name="orderEndAt" class="form-control"></input>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 col-sm-6 col-md-3 form-group">
                            <button id="searchOrder" type="button" class="btn btn-success btn-s-xs">查询</button>
                            <button id="commit" type="button" class="btn btn-primary btn-s-xs">结算</button>
                        </div>
                    </div>
                </form>

                <table id="thetable"></table>


            </div>
        </div>

    </section>
</section>
<#include "/admin/foot.ftl">

<script>
    initTable();
    var orderStartAt = '';
    var orderEndAt = '';
    $('#searchOrder').click(function () {
        orderStartAt = $('#orderStartAt').val();
        orderEndAt = $('#orderEndAt').val();
        initTable();
    });

    $('#commit').click(function () {

        var checkedbox= $("#thetable").bootstrapTable('getSelections');
        if (checkedbox.length <= 0)
            return;
        var idList = [];
        checkedbox.forEach(function (item) {
            idList.push(item["saleId"]);
        });

        $.ajax({
            url: '/cashier/order_settle.json?orderStartAt='+orderStartAt+'&orderEndAt='+orderEndAt,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(idList),
            type: 'post',
            success: function (data) {
                if (data.success) {
                    layer.msg('结算成功');
                    initTable();

                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg("网络异常");
            }
        });


    });

    function responseHandler(root) {
        return { "total": root.pagination.count, "rows": root.data };
    }


    function initTable() {
        $('#thetable').bootstrapTable('destroy').bootstrapTable({
            url: '/cashier/settle.json',
            method: 'get',
            queryParams: function (params) {
                $('#theform').serializeArray().forEach(function fn(v) {
                    params[v.name] = v.value;
                });
                return params;
            },
            responseHandler: responseHandler,
            idField: 'saleId',
            exportDataType: 'all',
            exportTypes: ['txt', 'excel'],
            dataShowExport: true,
            search: false,
            sortStable: false,
            sidePagination: 'server',
            pagination: true,
            columns: [
                {
                    checkbox:true
                }, {
                    field: 'placeName',
                    title: '场地'
                }, {
                    field: 'saleName',
                    title: '姓名'
                }, {
                    field: 'orderCount',
                    title: '订单数量'
                }, {
                    field: 'commission',
                    title: '佣金金额'
                }, {
                    field: 'orderStartAt',
                    title: '起始时间',
                    formatter: numdate
                }, {
                    field: 'orderEndAt',
                    title: '截止时间',
                    formatter: numdate
                }
            ]
        });
    }

</script>
