<template>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1 id="itemTitle"></h1>
                </section>
                <!-- .content -->
                <div id="table"></div>
                <div id="toolbar">
                    <div class="col-sm-12">
                        <button id="btnAdd" class="btn btn-default">
                            <i class="glyphicon glyphicon-plus-sign"></i>新增
                        </button>
                        <button id="btnSearch" class="btn btn-default">
                            <i class="glyphicon glyphicon-search"></i>查询
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <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="modalLabel">H5活动通用飘屏配置</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <input type="hidden" name="id" id="id" />
                        <div class="form-group">
                            <label for="fromErbanNo" class="col-sm-3 control-label">Peko号（非必填）：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="fromErbanNo">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="type" class="col-sm-3 control-label">表示形式：</label>
                            <div class="col-sm-9">
                                <select name="type" id="type" class="form-control validate[required]">
                                    <option value="0">房间静态飘屏</option>
                                    <option value="1">全服动态飘屏</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="floatingContent" class="col-sm-3 control-label">发送内容：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="floatingContent">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="save">保存</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="tipModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">提示信息</h4>
                </div>
                <div class="modal-body" id="tipMsg"></div>
            </div>
        </div>
    </div>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';

export default {
    name: "FloatingScreenView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable({
                    columns: [
                        { field: 'fromErbanNo', title: 'Peko号', align: 'center', width: '5%' },
                        { field: 'content', title: '发送内容', align: 'center', width: '5%' },
                        {
                            field: 'type',
                            title: '表示形式',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                var value = '';
                                if (val == 0) {
                                    value = '房间静态飘屏';
                                } else if (val == 1) {
                                    value = '全服动态飘屏';
                                }
                                return value;
                            }
                        },
                        { field: 'createTime', title: '创建时间', align: 'center', width: '5%' },
                    ],
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 20,
                    pagination: true,
                    pageList: [20, 50, 100],
                    search: false,
                    sidePagination: "server", //表示服务端请求
                    //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                    //设置为limit可以获取limit, offset, search, sort, order
                    queryParamsType: "undefined",
                    queryParams: function queryParams(params) {   //设置查询参数
                        var param = {
                            page: params.pageNumber,
                            pageSize: params.pageSize,
                        };
                        return param;
                    },
                    toolbar: '#toolbar',
                    url: '/admin/floating/screen/page',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });

                $('#btnAdd').on('click', function () {
                    $("#fromErbanNo").val('');
                    $("#type").val('');
                    $("#floatingContent").val('');
                    $("#editModal").modal('show');
                });

                // 查询刷新
                $('#btnSearch').on('click', function () {
                    TableHelper.doRefresh('#table');
                });

                $("#save").click(function () {
                    const msg = '确定要保存吗？';
                    if (confirm(msg)) {
                        var data = {
                            fromErbanNo: $('#fromErbanNo').val(),
                            type: $('#type').val(),
                            content: $('#floatingContent').val(),
                        };
                        $.ajax({
                            type: "post",
                            url: "/admin/floating/screen/save",
                            data: data,
                            dataType: "json",
                            success: function (json) {
                                if (json.success == 'true' || json.code == 200) {
                                    $("#tipMsg").text("保存成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                    $("#editModal").modal('hide');
                                } else {
                                    $("#tipMsg").text("保存失败." + json.message);
                                    $("#tipModal").modal('show');
                                    $("#editModal").modal('hide');
                                }
                            }
                        });
                    }
                });
            });
        }
    },

};
</script>

<style scoped></style>