<#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 class="col-xs-12 col-sm-6 col-md-3 form-group">
                            <div class="input-group">
                                <label class="input-group-addon" for="roomName">包厢</label>
                                <input type="text" id="roomName" name="roomName" 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="customerName">客户</label>
                                <input type="text" id="customerName" name="customerName" 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>
                        </div>
                    </div>
                </form>
                <table id="thetable"></table>
            </div>
        </div>

    </section>
</section>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel"></h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal">
                    <div class="panel-body">
                        <input type="hidden" name="orderId"/>
                        <input type="hidden" name="roomId"/>
                        <input type="hidden" name="placeName"/>
                        <input type="hidden" name="placeId"/>
                        <input type="hidden" name="roomName"/>
                        <input type="hidden" name="customerName"/>
                        <input type="hidden" name="customerNumber"/>
                        <input type="hidden" name="saleName1"/>
                        <input type="hidden" name="saleId1"/>
                        <input type="hidden" name="saleName2"/>
                        <input type="hidden" name="saleId2"/>
                        <input type="hidden" name="orderCreateAt"/>
                        <input type="hidden" name="orderPayAt"/>
                        <input type="hidden" name="bePaid"/>
                        <input type="hidden" name="orderCommission1"/>
                        <input type="hidden" name="orderCommission2"/>
                        <input type="hidden" name="beActive"/>

                        <div class="form-group">
                            <label for="orderPayAmount" class="col-sm-2 control-label">买单金额</label>

                            <div class="col-sm-10">
                                <input type="number" id="orderPayAmount" name="orderPayAmount" class="form-control"
                                       autocomplete="off"/>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="payOrder();">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

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

<script>
    initTable();
    $('#searchOrder').click(function () {
        initTable();
    });

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

    function customerAccounts(row) {
        $('#myModal').find('form').formSet(row);

        $('#myModal').find('h4').html('买单');
        $('#myModal').modal('toggle');
    }

    function discardOrder(orderId) {
        $.ajax({
            url: '/cashier/order_discard.json',
            data: { orderId: orderId },
            type: 'post',
            success: function (data) {
                if (data.success) {
                    layer.msg("作废成功");
                    initTable();
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg("网络异常");
            }
        });
    }

    function payOrder() {
        var data = $('#myModal').find('form').formGet();
        var orderId = data.orderId;
        var payAmount = data.orderPayAmount;

        $.ajax({
            url: '/cashier/pay_order.json',
            data: { orderId: orderId, payAmount: payAmount },
            type: 'post',
            success: function (data) {
                if (data.success) {
                    layer.msg("结算成功");
                    initTable();
                    $('#myModal').modal('toggle');

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

    function initTable() {
        $('#thetable').bootstrapTable('destroy').bootstrapTable({
            url: '/cashier/customer_accounts.json',
            method: 'get',
            queryParams: function (params) {
                $('#theform').serializeArray().forEach(function fn(v) {
                    params[v.name] = v.value;
                });
                return params;
            },
            responseHandler: responseHandler,
            idField: 'order_id',
            exportDataType: 'all',
            exportTypes: ['txt', 'excel'],
            dataShowExport: true,
            search: false,
            sortStable: false,
            sidePagination: 'server',
            pagination: true,
            columns: [
                {
                    field: 'orderId',
                    title: '操作',
                    width: '120px',
                    formatter: function (v, row) {
                        var ts = [];
                        if (!row.paid) {
                            ts = [
                                '<button class="btn btn-success btn-sm" onclick=\'customerAccounts(' + JSON.stringify(row) + ')\'>结账</button>',
                                '<button class="btn btn-dark btn-sm" onclick="discardOrder(\'' + row.orderId + '\')">作废</button>'
                            ];
                        }
                        return ts.join('\n');
                    }
                }, {
                    field: 'placeName',
                    title: '场地'
                }, {
                    field: 'roomName',
                    title: '包厢'
                }, {
                    field: 'customerName',
                    title: '客户名称',
                }, {
                    field: 'customerNumber',
                    title: '消费人数'
                }, {
                    field: 'arrivalAt',
                    title: '预定时间',
                    formatter: numdate
                }
            ]
        });
    }

    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };

</script>
