<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>
                <div id="toolbar">
                    <div class="col-sm-12">
                        <label for="erbanNo" class="col-sm-2 control-label">用户ID:</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" name="erbanNo" id="erbanNo" placeholder="">
                        </div>
                        <label for="bubbleId" class="col-sm-2 control-label">装扮ID:</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" name="bubbleId" id="bubbleId" placeholder="">
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <button id="btnSearch" class="btn btn-default">
                            <i class="glyphicon glyphicon-search"></i>查询
                        </button>
                    </div>
                </div>
            </div>
            <!-- .content -->
            <div id="table"></div>
        </div>
    </section>
</template>

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

export default {
    name: "ChatbubbleRecordView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable({
                    columns: [
                        { field: 'id', title: 'id', visible: false, align: 'center', width: '5%' },
                        { field: 'bubbleId', title: '装扮ID', align: 'center', width: '5%' },
                        { 
                            field: 'bubbleName', 
                            title: '装扮名称', 
                            align: 'center', 
                            width: '5%',
                            formatter: function (val) {
                                let value = val;
                                if (val && val.startsWith('{') && val.endsWith('}')) {
                                    value = JSON.parse(val).zh;
                                }
                                return value;
                            } 
                        },
                        { field: 'erbanNo', title: '获得用户ID', align: 'center', width: '5%' },
                        { field: 'nick', title: '用户昵称', align: 'center', width: '5%' },
                        {
                            field: 'comeFrom', title: '获得来源', align: 'center', width: '5%',
                            formatter: function (val) {
                                if (val === 1) {
                                    return '获得贵族身份';
                                } else if (val === 2) {
                                    return '后台手动发放';
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'createTime', title: '获得时间', align: 'center', width: '5%',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format("yyyy-MM-dd hh:mm:00");
                                } else {
                                    return '-';
                                }
                            }
                        },
                        { field: 'days', title: '有效期(天)', align: 'center', width: '5%' },
                        { field: 'remake', title: '备注', align: 'center', width: '5%' },
                        {
                            field: 'status', title: '状态', align: 'center', width: '5%',
                            formatter: function (val) {
                                if (val === 1) {
                                    return '正常';
                                } else if (val === 2) {
                                    return '已撤回';
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'updateTime', title: '撤回时间', align: 'center', width: '5%',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format("yyyy-MM-dd hh:mm:00");
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'opt',
                            title: '操作',
                            align: 'middle',
                            width: '5%',
                            formatter: function (val, row, index) {
                                let isRecoveryDisabled = false;
                                let text = '撤回';
                                if (row.comeFrom === 1 || row.status === 2) {
                                    isRecoveryDisabled = true;
                                }
                                if (row.status === 2) {
                                    text = '已撤回';
                                }
                                return '<button id="btnEdit" name="btnEdit" class="btn btn-sm btn-success opt-edit" data-id=' + row.id + " " + `${isRecoveryDisabled ? ' disabled ' : ''}` + '>' +
                                    '<i class="glyphicon glyphicon-edit"></i>' + `${text}` + '</button>&nbsp;&nbsp;';
                            }
                        }
                    ],
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 10,
                    pagination: true,
                    pageList: [10, 20, 50, 100],
                    uniqueId: "id",
                    search: false,
                    sidePagination: "server", //表示服务端请求
                    //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                    //设置为limit可以获取limit, offset, search, sort, order
                    queryParamsType: "-",
                    queryParams: function queryParams(params) {   //设置查询参数
                        var param = {
                            page: params.pageNumber,
                            pageSize: params.pageSize,
                            erbanNo: $('#erbanNo').val(),
                            bubbleId: $('#bubbleId').val()
                        };
                        return param;
                    },
                    ajax: function (request) {                    //使用ajax请求
                        $.ajax({
                            type: "GET",
                            url: '/admin/chatbubble/listRecordByPage',
                            contentType: 'application/json;charset=utf-8',
                            dataType: 'json',
                            data: request.data,
                            success: function (res) {
                                console.log(res);
                                request.success({
                                    rows: res.rows,
                                    total: res.total
                                });
                                // $('#table-request').bootstrapTable('load', res.data.rows);
                            },
                            error: function (error) {
                                console.log(error);
                            }
                        })
                    },
                    toolbar: '#toolbar',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });

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

                // 撤回
                $("#table").on("click", '#btnEdit', function () {
                    var id = $(this).attr("data-id");
                    $.ajax({
                        type: "get",
                        url: "/admin/chatbubble/recoveryChatBubble",
                        data: { recordId: id },
                        dataType: "json",
                        success: function (json) {
                            if (json.code === 200) {
                                $("#tipMsg").text("操作成功");
                                $("#tipModal").modal('show');
                            } else {
                                $("#tipMsg").text("操作失败, " + json.message);
                                $("#tipModal").modal('show');
                            }
                            TableHelper.doRefresh('#table');
                        }
                    });
                });

            });
        }
    },

};
</script>

<style scoped>
#imgMask {
    position: absolute;
    top: 0;
    left: 0;
    width: 450px;
    height: 450px;
    padding: 4px;
    background: #fff;
    z-index: 999;
    display: none;
}

#imgMask img {
    width: 100%;
    height: 100%;
    vertical-align: top;
}

.control-span {
    padding-top: 7px;
    margin-bottom: 0;
    font-weight: 400;
    display: block;
}

.ul-li {
    margin-bottom: 20px !important;
}

.ul-i {
    margin-right: 10px !important;
}

.i-font {
    color: #00acd6;
}

.ul-li label {
    color: #c9cccf;
}

.fixed-table-body {
    height: auto !important;
}</style>