<template>
    <section class="content">
        <div class="box box-primary">
            <section class="content-header">
                <h1 id="itemTitle">游戏表情</h1>
            </section>
            <section class="content">
                <div id="table"></div>
                <div id="toolbar">
                    <button id="addBtn" class="btn btn-default">
                        <i class="glyphicon glyphicon-plus"></i>增加
                    </button>&nbsp;&nbsp;&nbsp;
                    表情类型: <select name="type" id="searchType" class="input-m">
                        <option value="0">--全部--</option>
                        <option value="1">游戏</option>
                    </select>
                    <button id="btnSearch" class="btn btn-sm btn-primary">查询</button>
                </div>
            </section>
        </div>
    </section>
    <div class="modal fade" id="actModal" 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">x</span></button>
                    <h4 class="modal-title" id="addModalLabel">新增</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="actForm">
                        <input type="hidden" name="id" id="actId" value="0">
                        <div class="form-group">
                            <div class="form-group">
                                <label for="faceUrl" class="col-sm-3 control-label">游戏表情:</label>
                                <div class="col-sm-9">
                                    <img src="" id="faceImg" style="width:250px;height:90px;" alt="">
                                    <input type="file" id="uploadGameFile" name="uploadGameFile"
                                        accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
                                    <button class="btn btn-success" type="button" id="uploadGameBtn">上传</button>
                                    <input type="hidden" class="form-control validate[required]" name="faceUrl" id="faceUrl"
                                        style="width:400px">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="type" class="col-sm-3 control-label">类型:</label>
                                <select name="type" id="type" class="col-sm-2">
                                    <option value="1">游戏</option>
                                </select>
                            </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="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">x</span></button>
                    <h4 class="modal-title" id="editModalLabel">编辑</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="editForm">
                        <input type="hidden" name="id" id="e_id">
                        <div class="form-group">
                            <label for="faceUrl" class="col-sm-3 control-label">游戏图标:</label>
                            <div class="col-sm-9">
                                <img src="" id="e_faceImg" style="width:90px;height:90px;" alt="">
                                <input type="file" id="adduploadGameFile" name="uploadGameFile"
                                    accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
                                <button class="btn btn-success" type="button" id="addGameUploadBtn">上传</button>
                                <input type="hidden" id="e_faceUrl" name="faceUrl"
                                    class="form-control validate[required]" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="e_type" class="col-sm-3 control-label">类型:</label>
                            <select name="type" id="e_type" class="col-sm-2">
                                <option value="1">游戏</option>
                            </select>
                        </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="edit">保存</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="sureModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="sure">确定</button>
                </div>
            </div>
        </div>
    </div>
</template>

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

export default {
    name: "GameFaceView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                $('#table').bootstrapTable('destroy');
                initTable();
                // 查询刷新
                $('#btnSearch').on('click', function () {
                    $('#table').bootstrapTable('destroy');
                    initTable();
                });

                $('#addBtn').on('click', function () {
                    cleanModal();
                    $("#actForm")[0].reset();
                    $('#actModal').modal('show');
                });
                $('#table').on('click', '.opt-edit', function () {
                    var id = $(this).attr("data-id-1");
                    var faceUrl = $(this).attr("data-id-2");
                    console.log(id);
                    console.log(faceUrl);
                    cleanModal();

                    $('#e_id').val(id);
                    $('#e_faceUrl').val(faceUrl);
                    $('#editModal').modal('show');
                });
                $('#uploadGameBtn').on('click', function () {
                    var options = {
                        type: 'post',
                        url: '/admin/gameFace/headimg.action',
                        dataType: 'json',
                        success: function (json) {
                            if (json.path) {
                                $('#faceUrl').val(json.path);
                                $('#faceImg').attr('src', json.path);
                                console.log(json.path);
                            } else {
                                $('#tipMsg').text(json.msg);
                                $('#tipModal').modal('show');
                            }
                        }
                    }
                    $('#actForm').ajaxSubmit(options);
                });
                $('#addGameUploadBtn').on('click', function () {
                    var options = {
                        type: 'post',
                        url: '/admin/gameFace/headimg.action',
                        dataType: 'json',
                        success: function (json) {
                            if (json.path) {
                                $('#e_faceUrl').val(json.path);
                                $('#e_faceImg').attr('src', json.path);
                                console.log(json.path);
                            } else {
                                $('#tipMsg').text(json.msg);
                                $('#tipModal').modal('show');
                            }
                        }
                    }
                    $('#editForm').ajaxSubmit(options);
                });
                $('#save').on('click', function () {
                    if ($('#actForm').validationEngine('validate')) {
                        $.ajax({
                            type: "post",
                            url: "/admin/gameFace/add.action",
                            data: $('#actForm').serialize(),
                            dataType: 'json',
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#actModal").modal('hide');
                                    $("#tipMsg").text(json.msg);
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("保存失败，错误码：" + json.message);
                                    $("#tipModal").modal('show');
                                }
                            }
                        })
                    }
                });
                $('#edit').on('click', function () {
                    if ($('#editForm').validationEngine('validate')) {
                        $.ajax({
                            type: "post",
                            url: "/admin/gameFace/update.action",
                            data: $('#editForm').serialize(),
                            dataType: 'json',
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#editModal").modal('hide');
                                    $("#tipMsg").text(json.msg);
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("修改失败，错误码：" + json.message);
                                    $("#tipModal").modal('show');
                                }
                            }
                        })
                    }
                });
                $("#table").on("click", '.opt-del', function () {
                    var id = $(this).attr("data-id");
                    if (id == 'undefined') {
                        $("#tipMsg").text("id参数有误");
                        $("#tipModal").modal('show');
                        return;
                    }
                    if (confirm("你确认删除吗?")) {
                        $.ajax({
                            type: 'post',
                            url: '/admin/gameFace/delete.action',
                            data: { 'id': id },
                            dataType: 'json',
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#tipMsg").text("删除成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("删除失败，错误码：" + json.message);
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                });

                function cleanModal() {
                    $('#actForm').find('input[type=text],input[type=hidden],input[type=file]').each(function () {
                        $(this).val('');
                    })
                    $('#actForm').find('img').attr('src', '');
                }
                function initTable() {
                    $('#table').bootstrapTable({
                        columns: [
                            { field: 'id', title: 'id', align: 'center', valign: 'middle', width: '10%' },
                            {
                                field: 'faceUrl',
                                title: '游戏图标',
                                align: 'center',
                                width: '10%',
                                formatter: function (val, row, index) {
                                    return "<img src='" + val + "' width='60' height='50'>";
                                }
                            },
                            {
                                field: 'type',
                                title: '类型',
                                align: 'center',
                                width: '10%',
                                valign: 'middle',
                                formatter: function (val, row, index) {
                                    console.log(val);
                                    switch (val) {
                                        case 1:
                                            return '游戏';
        
                                    }
                                }
                            },
                            {
                                field: 'updateTime',
                                title: '创建时间',
                                align: 'center',
                                width: '10%',
                                valign: 'middle',
                                formatter: function (val, row, index) {
                                    if (val) {
                                        var date = new Date(val);
                                        return date.format("yyyy-MM-dd hh:mm:ss");
                                    } else {
                                        return '-';
                                    }
                                }
                            },
                            {
                                field: 'createTime',
                                title: '创建时间',
                                align: 'center',
                                width: '10%',
                                valign: 'middle',
                                formatter: function (val, row, index) {
                                    if (val) {
                                        var date = new Date(val);
                                        return date.format("yyyy-MM-dd hh:mm:ss");
                                    } else {
                                        return '-';
                                    }
                                }
                            },
                            {
                                field: 'tmp',
                                title: '操作',
                                align: 'center',
                                width: '10%',
                                valign: 'middle',
                                formatter: function (val, row, index) {
                                    var id = row.id;
                                    var faceUrl = row.faceUrl;
                                    return '<button class="btn btn-sm btn-success opt-edit" data-id-1 ="' + id + ' "data-id-2="' + faceUrl + '">' + '编辑</button>' + "&nbsp;&nbsp;&nbsp;" +
                                        "<button class='btn btn-sm btn-danger opt-del' data-id=" + id + ">删除</button>";

                                }
                            }
                        ],
                        cache: false,
                        striped: true,
                        showRefresh: false,
                        pageSize: 20,
                        pagination: true,
                        pageList: [10, 20, 30, 50],
                        sidePagination: "server", //表示服务端请求
                        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                        //设置为limit可以获取limit, offset, search, sort, order
                        queryParamsType: "undefined",
                        queryParams: function queryParams(params) {   //设置查询参数
                            var param = {
                                pageSize: params.pageSize,
                                pageNumber: params.pageNumber,
                                type: parseInt($("#searchType").val()),
                            };
                            console.log(param);
                            return param;
                        },
                        uniqueId: 'type',
                        toolbar: '#toolbar',
                        url: '/admin/gameFace/list.action',
                        onLoadSuccess: function () {  //加载成功时执行
                            console.log("load success");
                        },
                        onLoadError: function () {  //加载失败时执行
                            console.log("load fail");
                        }
                    })
                }
            })
        }
    },

};
</script>

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

#imgMask img {
    width: 100%;
    height: 100%;
    vertical-align: top;
}
</style>