<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">
                    <form id="searchForm" action="/admin/family/member/export" method="get" target="_blank">
                        <div class="col-sm-12">
                            <label for="familyId" class="col-sm-1 control-label">家族ID:</label>
                            <div class="col-sm-2"><input type="text" class="form-control" name="familyId" id="familyId">
                            </div>

                            <label for="familyNick" class="col-sm-1 control-label">家族昵称:</label>
                            <div class="col-sm-2"><input type="text" class="form-control" name="familyNick" id="familyNick">
                            </div>

                            <label for="memberId" class="col-sm-1 control-label">用户ID:</label>
                            <div class="col-sm-2"><input type="text" class="form-control" name="memberId" id="memberId">
                            </div>

                            <label for="memberId" class="col-sm-1 control-label">用户昵称:</label>
                            <div class="col-sm-2"><input type="text" class="form-control" name="memberNick" id="memberNick">
                            </div>
                        </div>
                    </form>

                    <div class="col-sm-12">
                        <button id="btnSearch" class="btn btn-default">
                            <i class="glyphicon glyphicon-search"></i>搜索
                        </button>
                        <button id="btnExport" class="btn btn-default ">
                            <i class="glyphicon glyphicon-export"></i>导出
                        </button>
                        <button id="btnBatchDel" class="btn btn-default ">
                            <i class="glyphicon glyphicon-wrench"></i>批量删除
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </section>
</template>

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

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

                //批量删除
                $("#btnBatchDel").on('click', function () {
                    var rows = $("#table").bootstrapTable("getSelections");
                    if (rows.length == 0) {
                        alert("请先选择要删除的记录");
                        return;
                    }
                    console.log("rows: " + rows.length);
                    var idArr = [];
                    var familyIdArr = [];
                    for (var i = 0; i < rows.length; i++) {
                        if (rows[i]['position'] == '10') {
                            alert(rows[i]['name'] + "是" + rows[i]['familyName'] + "家族的族长，不允许删除");
                            return;
                        }
                        idArr.push(rows[i]['uid']);
                        familyIdArr.push(rows[i]['familyId']);
                    }
                    console.log(idArr);
                    console.log(familyIdArr);
                    if (confirm("你确认批量删除该记录吗?" +
                        "\r\n删除后再也不能找回，请谨慎操作！")) {
                        $.ajax({
                            type: 'post',
                            url: "/admin/family/member/del",
                            data: {
                                'ids': JSON.stringify(idArr),
                                'familyIds': JSON.stringify(familyIdArr)
                            },
                            dataType: "json",
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#tipMsg").text("删除成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("删除失败");
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                })
            });

            function initTable() {
                //初始化表格数据
                $('#table').bootstrapTable({
                    columns: [
                        { field: 'tmp', valign: 'middle', title: 'uid', align: 'center', checkbox: true, width: '5%' },
                        //{field: 'seq', title: '编号', align: 'center', width: '5%',valign: 'middle'},
                        { field: 'familyId', title: '家族ID', align: 'center', width: '5%', valign: 'middle' },
                        { field: 'familyName', title: '家族昵称', align: 'center', width: '5%', valign: 'middle' },
                        { field: 'erbanNo', title: '用户ID', align: 'center', width: '5%', valign: 'middle' },
                        { field: 'name', title: '用户昵称', align: 'center', width: '5%', valign: 'middle' },
                        {
                            field: 'position',
                            title: '职位',
                            align: 'center',
                            width: '5%',
                            valign: 'middle',
                            formatter: function (val, row, index) {
                                if (val == '10') {

                                    return "族长";
                                } else if (val == '0') {
                                    return '成员';
                                } else {
                                    return '-';
                                }
                            }

                        },
                        { field: 'money', title: '家族币余额', align: 'center', width: '5%', valign: 'middle' },
                        {
                            field: 'joinTime',
                            title: '加入时间',
                            align: 'center',
                            width: '5%',
                            valign: 'middle',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format("yyyy-MM-dd hh:mm:00");
                                } else {
                                    return '-';
                                }
                            }
                        }
                    ],
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 20,
                    pagination: true,
                    pageList: [20, 50, 100, 200, 300, 500],
                    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,
                            familyId: $('#familyId').val(),
                            familyNick: $('#familyNick').val(),
                            memberId: $('#memberId').val(),
                            memberNick: $('#memberNick').val(),
                        };
                        return param;
                    },
                    toolbar: '#toolbar',
                    url: '/admin/family/member/list',
                    onLoadSuccess: function () {  //加载成功时执行
                        //复选框居中
                        $(".bs-checkbox").css({ 'text-align': 'center', 'vertical-align': 'middle' });
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });
            }

            /**导出功能*/
            $("#btnExport").on("click", function () {
                $("#searchForm").submit();
            })
        }
    },

};
</script>

<style scoped></style>