<#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;" style="padding-bottom: 15px;">
                    <div id="toolbar">
                        <button id="addPlace" type="button" class="btn btn-success btn-s-xs">添加场地</button>
                    </div>
                </form>
                <table id="thetable"></table>
            </div>
        </div>

    </section>
</section>

<!-- Modal -->
<div class="modal fade" id="placeModal" 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="placeName" class="col-sm-2 control-label">场地名称</label>

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

                        <div class="form-group">
                            <label for="placeAddress" class="col-sm-2 control-label">场地地址</label>

                            <div class="col-sm-10">
                                <input type="text" id="placeAddress" name="placeAddress" class="form-control"
                                       autocomplete="off"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="placeDesc" class="col-sm-2 control-label">备注</label>

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

                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="savePlace();">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

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

    initTable();

    $('#addPlace').click(function () {
        $('#placeModal').find('h4').html('添加场地');
        $('#placeModal').find('form').formClear();
        $('#placeModal').modal('toggle');
    });

    function responseHandler(root) {
        return { "rows": root.data };
    }

    function placeDelete(placeId) {
        $.ajax({
            url: '/admin/place_delete.json',
            data: { placeId: placeId },
            type: 'post',
            success: function (data) {
                if (data.success) {
                    layer.msg('删除成功');
                    initTable();

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

    function savePlace() {
        var data = $('#placeModal').find('form').formGet();
        if (!data.placeName) {
            layer.msg('场地名不能为空');
            return;
        }

        var url;
        if ($('#placeModal').find('h4').html() === '编辑场地') {
            url = '/admin/place_edit.json';
        } else {
            //添加
            url = '/admin/place_addnew.json';
        }

        $.ajax({
            url: url,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            type: 'post',
            success: function (data) {
                if (data.success) {
                    layer.msg("保存成功");
                    initTable();
                    $('#placeModal').modal('toggle');

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

    function placeEdit(row) {
        $('#placeModal').find('form').formSet(row);
        $('#placeModal').find('h4').html('编辑场地');
        $('#placeModal').modal('toggle');
    }

    function userResetPwd(row) {

    }

    function placeDisable(row) {
        var data = row;
        if (row.beActive === 'Y' || row.beActive === 'N') {
            if (row.beActive === 'Y') {
                data.beActive = 'N';
            } else if (row.beActive === 'N') {
                data.beActive = 'Y';
            }

            $.ajax({
                url: '/admin/place_edit.json',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                type: 'post',
                success: function (data) {
                    if (data.success) {
                        if (row.beActive === 'Y') {
                            layer.msg('已启用当前场地');
                        } else if (row.beActive === 'N') {
                            layer.msg('已禁用当前场地');
                        }
                        initTable();

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

    function initTable() {
        $('#thetable').bootstrapTable('destroy').bootstrapTable({
            url: '/admin/place.json',
            method: 'get',
            queryParams: function (params) {
                $('#theform').serializeArray().forEach(function fn(v) {
                    params[v.name] = v.value;
                });
                return params;
            },
            responseHandler: responseHandler,
            idField: 'placeId',
            exportDataType: 'all',
            exportTypes: ['txt', 'excel'],
            dataShowExport: true,
            search: false,
            sortStable: false,
            sidePagination: 'server',
            columns: [
                {
                    field: 'placeId',
                    title: '操作',
                    width: '120px',
                    formatter: function (v, row) {
                        var ts = [
                            '<button class="btn btn-default btn-sm" onclick=\'placeEdit(' + JSON.stringify(row) + ')\'>编辑</button>',
                            '<button class="btn btn-danger btn-sm" onclick="placeDelete(\'' + row.placeId + '\')">删除</button>'
                        ];
                        return ts.join('\n');
                    }
                }, {
                    field: 'placeName',
                    title: '名称'
                }, {
                    field: 'placeAddress',
                    title: '地址'
                }, {
                    field: 'placeDesc',
                    title: '备注'
                }, {
                    field: 'beActive',
                    title: '状态',
                    formatter: function (v, row) {
                        if (row.beActive === 'Y') {
                            return '<button class="btn btn-default btn-sm" onclick=\'placeDisable(' + JSON.stringify(row) + ')\'>启用中</button>';
                        }
                        return '<button class="btn btn-dark btn-sm" onclick=\'placeDisable(' + JSON.stringify(row) + ')\'>已禁用</button>';
                    }
                }
            ]
        });
    }


</script>