<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">
                        <label for="erbanNo" class="col-sm-1 control-label">Peko号:</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" name="searchValue" id="searchValue">
                        </div>
                    </div>
                    <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">充值黑名单配置</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <input type="hidden" name="id" id="id" />
                        <div class="form-group">
                            <label for="erbanNo" class="col-sm-3 control-label">Peko号：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="erbanNo">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="limitType" class="col-sm-3 control-label">限制内容：</label>
                            <div class="col-sm-9" id="limitType">
                                <input type="checkbox" name="limitType" value="1" />iOS充值<br>
                                <input type="checkbox" name="limitType" value="2" />Google充值<br>
                                <input type="checkbox" name="limitType" value="4" />H5充值<br>
                            </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: "ChargeUserLimitView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable({
                    columns: [
                        { field: 'erbanNo', title: 'Peko号', align: 'center', width: '5%' },
                        { field: 'nick', title: '昵称', align: 'center', width: '5%' },
                        {
                            field: 'limitContent',
                            title: '限制内容',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                var value = '';
                                var isIos = row.isIos;
                                var isGoogle = row.isGoogle;
                                var isH5 = row.isH5;
                                if (isIos) {
                                    value += 'iOS充值<br/>';
                                }
                                if (isGoogle) {
                                    value += 'Google充值<br/>';
                                }
                                if (isH5) {
                                    value += 'H5充值<br/>';
                                }
                                return value;
                            }
                        },
                        { field: 'limitTime', title: '限制时间', align: 'center', width: '5%' },
                        {
                            field: 'id',
                            title: '操作',
                            align: 'center',
                            width: '5%',
                            valign: 'middle',
                            formatter: function (val, row, index) {
                                return '<button class="btn btn-sm btn-default opt-edit" data-index="' + index + '">编辑</button>' + '<button class="btn btn-sm btn-default opt-del" data-index="' + index + '">删除</button>';
                            }
                        }
                    ],
                    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,
                            erbanNo: $('#searchValue').val()
                        };
                        return param;
                    },
                    toolbar: '#toolbar',
                    url: '/admin/charge/user/limit/page',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });

                $('#table').on('click', '.opt-edit', function () {
                    const currentData = $('#table').bootstrapTable('getData')[$(this).data('index')];
                    var id = currentData.id;
                    var erbanNo = currentData.erbanNo;
                    var isIos = currentData.isIos;
                    var isGoogle = currentData.isGoogle;
                    var isH5 = currentData.isH5;
                    $("#erbanNo").val(erbanNo);
                    $("#id").val(id);
                    if (isIos) {
                        $("input:checkbox[value='1']").prop('checked', true);
                    } else {
                        $("input:checkbox[value='1']").prop('checked', false);
                    }
                    if (isGoogle) {
                        $("input:checkbox[value='2']").prop('checked', true);
                    } else {
                        $("input:checkbox[value='2']").prop('checked', false);
                    }
                    if (isH5) {
                        $("input:checkbox[value='4']").prop('checked', true);
                    } else {
                        $("input:checkbox[value='4']").prop('checked', false);
                    }
                    $("#editModal").modal('show');
                });

                $('#table').on('click', '.opt-del', function () {
                    const currentData = $('#table').bootstrapTable('getData')[$(this).data('index')];
                    var id = currentData.id;
                    const msg = '确定要删除吗？';
                    if (confirm(msg)) {
                        $.ajax({
                            type: "get",
                            url: "/admin/charge/user/limit/del?id=" + id,
                            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');
                                }
                            }
                        });
                    }
                });

                $('#btnAdd').on('click', function () {
                    $("#erbanNo").val('');
                    $("#id").val('');
                    $("input:checkbox[value='1']").prop('checked', false);
                    $("input:checkbox[value='2']").prop('checked', false);
                    $("input:checkbox[value='4']").prop('checked', false);
                    $("#editModal").modal('show');
                });

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

                $("#save").click(function () {
                    const msg = '确定要保存吗？';
                    if (confirm(msg)) {
                        var id = $('#id').val();
                        const limitTypeArray = $("input:checkbox[name='limitType']:checked").serializeArray();
                        if (!limitTypeArray) {
                            $("#tipMsg").text("限制内容不能为空");
                            $("#tipModal").modal('show');
                            return;
                        }
                        var limitType = 0;
                        for (let i = 0, len = limitTypeArray.length; i < len; i++) {
                            console.log(limitTypeArray[i]);
                            limitType |= limitTypeArray[i].value;
                        }
                        var data = {
                            erbanNo: $('#erbanNo').val(),
                            limitType: limitType
                        };
                        if (id) {
                            data.id = id;
                        }
                        $.ajax({
                            type: "post",
                            url: "/admin/charge/user/limit/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>