<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 -->
                <section class="content">
                    <div id="table"></div>
                    <div id="toolbar">

                        <button id="addBtn" class="btn btn-sm btn-primary">增加</button>
                        <div class="col-sm-12">

                        </div>
                    </div>
                </section><!-- .content -->
            </div>
        </div>
    </section>
    <div class="modal fade" id="addModal" 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">新增</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="erbanNoInput" class="col-sm-2 control-label">平台号:</label>
                        <div class="col-sm-10">
                            <input type="text" id="erbanNoInput" name="erbanNo" class="form-control validate[required]">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="saveSuperAdmin">保存</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';
import { showLoading, hideLoading } from '@/utils/maintainer';

export default {
    name: "SuperAdminView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {

            $(function () {

                $('#table').bootstrapTable('destroy');
                initTable();

                $("#exportBtn").on('click', function () {
                    $("#searchForm").submit();
                })

                $("#table").on("click", '.opt-remove', function () {
                    var id = $(this).attr("data-id");
                    if (id == 'undefined') {
                        $("#tipMsg").text("id参数有误");
                        $("#tipModal").modal('show');
                        return;
                    }
                    if (confirm("你确认【删除】该超管吗?")) {
                        showLoading();
                        var params = "uids=" + id;
                        $.ajax({
                            type: 'post',
                            url: "/admin/super/del",
                            data: params,
                            dataType: "json",
                            success: function (json) {
                                hideLoading();
                                if (json.success == 'true') {
                                    $("#tipMsg").text("操作成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("操作失败");
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                });
                $("#addBtn").click(function () {
                    // 打开新增弹窗
                    $("#addModal").modal('show');
                    $("#erbanNoInput").val("");
                });

                $("#saveSuperAdmin").click(function () {
                    var erbanNo = $("#erbanNoInput").val();
                    if (erbanNo == '' || erbanNo == undefined || erbanNo == null) {
                        alert("请输入平台号！")
                    } else {
                        showLoading()
                        $.ajax({
                            type: "post",
                            url: "/admin/super/save",
                            data: 'erbanNo=' + erbanNo,
                            dataType: "json",
                            success: function (json) {
                                hideLoading();
                                if (json.success == 'true') {
                                    $("#addModal").modal('hide');
                                    $("#tipMsg").text("保存成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("保存失败，原因: " + json.msg);
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                });

            });
        }
    },

};


function initTable() {
    $('#table').bootstrapTable({
        columns: [
            { field: 'tmp', title: 'ID', align: 'center', width: '10%', checkbox: true },
            { field: 'uid', title: 'uid', align: 'center', width: '10%' },
            { field: 'erbanNo', title: '平台号', align: 'center', width: '10%' },
            { field: 'nick', title: '昵称', align: 'center', width: '10%' },
            {
                field: 'uid', title: '创建时间', align: 'center', width: '12%', formatter: function (val, row, index) {
                    var ret = '<span id="createTimeSpan_' + val + '" >-</span>';
                    return ret;

                }
            },
            {
                field: 'uid', title: '操作', align: 'center', width: '10%', formatter: function (val, row, index) {
                    var ret = '<button class="btn btn-sm btn-danger opt-remove" data-id=' + val + '>' +
                        '<i class="glyphicon glyphicon-remove"></i>删除</button>';
                    return ret;

                }
            }
        ],
        cache: false,
        striped: true,
        showRefresh: false,
        pagination: false,
        search: false,
        sidePagination: "server", //表示服务端请求
        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
        //设置为limit可以获取limit, offset, search, sort, order
        queryParamsType: "undefined",
        queryParams: function queryParams(params) {   //设置查询参数
            var param = {
                pageNumber: params.pageNumber,
                pageSize: params.pageSize,
            };
            return param;
        },
        toolbar: '#toolbar',
        url: '/admin/super/list',
        onLoadSuccess: function (data) {  //加载成功时执行
            setTimeout(function () {
                loadCreateTime(data);
            }, 100);
        },
        onLoadError: function () {  //加载失败时执行
            console.log("load fail");
        }
    });
}

/**
 * 加载创建时间
 * @param data
 */
function loadCreateTime(data) {
    var param = '';
    for (var i = 0; i < data.rows.length; i++) {
        var user = data.rows[i];
        param += "uids=" + user.uid + "&";
    }
    if (param != '') {
        $.ajax({
            type: "get",
            url: "/admin/super/get/time",
            data: param,
            dataType: "json",
            success: function (json) {
                for (var i = 0; i < json.rows.length; i++) {
                    $("#createTimeSpan_" + json.rows[i].uid).text(json.rows[i].date);
                }
            }
        });
    }

}
</script>

<style scoped></style>