<#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="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="saleName">销售</label>-->
                                <#--<input type="text" id="saleName" name="saleName" 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="searchRoom" type="button" class="btn btn-success btn-s-xs">查询</button>-->
                        <#--</div>-->
                    <#--</div>-->
                <#--</form>-->
                <table id="thetable"></table>
            </div>
        </div>

    </section>
</section>

<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="roomId"/>

                        <div class="form-group">
                            <label for="saleName" class="col-sm-2 control-label">销售名称</label>

                            <div class="col-sm-10">
                                <input type="text" id="saleName" name="saleName" class="form-control"
                                       autocomplete="off" readonly="readonly"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="salePhone" class="col-sm-2 control-label">销售电话</label>

                            <div class="col-sm-10">
                                <input type="tel" id="salePhone" name="salePhone" class="form-control"
                                       autocomplete="off" readonly="readonly"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="arrivalAt" class="col-sm-2 control-label">预定时间</label>

                            <div class="col-sm-10">
                                <input type="text" id="arrivalAt" name="arrivalAt" class="form-control"
                                       readonly="readonly"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="customerNumber" class="col-sm-2 control-label">客户人数</label>

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

                        <div class="form-group">
                            <label for="customerTel" class="col-sm-2 control-label">客户电话</label>

                            <div class="col-sm-10">
                                <input type="tel" id="customerTel" name="customerTel" class="form-control"
                                       autocomplete="off" readonly="readonly"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="paid" class="col-sm-2 control-label">结账状态</label>

                            <div class="col-sm-10">
                                <input type="text" id="paid" name="paid" class="form-control"
                                       autocomplete="off" readonly="readonly"/>
                            </div>
                        </div>

                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<#include "/admin/foot.ftl">

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

    function roomEnable(v){
        // $('#myModal').find('h4').html('包厢清场');
        // $('#myModal').find('h3').html(v.roomName);
        // $('#myModal').find('form').formSet(v);
        // $('#myModal').modal('toggle');

        // var data = $('#myModal').find('form').formGet();
        $.ajax({
            url: '/worker/room_enable.json?roomId='+v.roomId,
            type: 'get',
            success: function (data) {
                if (data.success) {

                    layer.msg('包厢已清场');
                    initTable();

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


    function roomUse(v) {
        // var data = $('#myModal').find('form').formGet();
        $.ajax({
            url: '/worker/room_use.json?roomId='+v.roomId,
            type: 'get',
            success: function (data) {
                if (data.success) {

                    layer.msg('包厢已开始使用');
                    initTable();

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

    function orderInfo(v) {
        $.ajax({
            url: '/worker/room_order.json?roomId='+v.roomId,
            type: 'get',
            success: function (data) {
                if (data.success) {
                    data.data.arrivalAt = new Date(data.data.arrivalAt);
                    data.data.arrivalAt = data.data.arrivalAt.toLocaleString();
                    data.data.paid = data.data.paid==1?'已结账':'未结账';
                    $('#myModal').find('h4').html('订单信息');
                    $('#myModal').find('form').formSet(data.data);
                    $('#myModal').modal('toggle');

                } 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: '/worker/room.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: 'roomId',
                    title: '房间编号'
                }, {
                    field: 'roomName',
                    title: '房间名称'
                }, {
                    field: 'beUse',
                    title: '状态',
                    formatter: function (v, row) {
                        if (v==='N') {
                            return '可预定';
                        } else if (v==='U') {
                            return '使用中';
                        }
                        return '已预定';
                    }
                }, {
                    field: 'beUse',
                    title: '操作',
                    formatter: function (v, row) {
                        var ts = [];
                        if (row.beUse === 'Y' || row.beUse === 'U') {
                            if (row.beUse === 'Y') {
                                ts.push('<button class="btn btn-success btn-sm" onclick=\'roomUse(' + JSON.stringify(row) + ')\'>到场</button>');
                            }
                            ts.push('<button class="btn btn-danger btn-sm" onclick=\'roomEnable(' + JSON.stringify(row) + ')\'>清场</button>');
                            ts.push('<button class="btn btn-default btn-sm" onclick=\'orderInfo(' + JSON.stringify(row) + ')\'>订单详情</button>');
                        }
                        return ts.join('\n');
                    }
                }
            ]
        });
    }


</script>