{extend name="../../admin/view/table"}

{block name="button"}
<!--{if auth("black")}-->
<button data-action='{:url("black")}' data-table-id="UserData" data-rule="openid#{openid};black#1" class='layui-btn layui-btn-sm layui-btn-primary'>拉入黑名单</button>
<button data-action='{:url("black")}' data-table-id="UserData" data-rule="openid#{openid};black#0" class='layui-btn layui-btn-sm layui-btn-primary'>移出黑名单</button>
<!--{/if}-->

<!--{if auth("truncate")}-->
<button data-load='{:url("truncate")}' data-confirm="确定要清空所有用户数据吗？" class='layui-btn layui-btn-sm layui-btn-primary'>清空用户数据</button>
<!--{/if}-->

<!--{if auth("sync")}-->
<button data-queue='{:url("sync")}' data-table-id="UserData" data-confirm="确定要创建同步用户数据的后台任务？" class='layui-btn layui-btn-sm layui-btn-primary'>同步用户数据</button>
<!--{/if}-->
{/block}

{block name="content"}
<div class="think-box-shadow">
    {include file='fans/index_search'}
    <table id="UserData" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
</div>
{/block}

{block name='script'}
<script>
    $(function () {
        $('#UserData').layTable({
            even: true, height: 'full',
            sort: {field: 'subscribe_time', type: 'desc'},
            cols: [[
                {checkbox: true},
                {
                    field: 'headimg', title: '头像', width: 65, align: "center", templet: function (d) {
                        d.headimgurl = d.headimgurl || '';
                        return d.headimgurl ? '<div class="headimg headimg-xs margin-0" data-tips-image data-tips-hover data-lazy-src="' + d.headimgurl + '" style="background-image:url(' + d.headimgurl + ')"></div>' : '';
                    }
                },
                {field: 'nickname', title: '微信昵称', align: "center", minWidth: 100},
                {field: 'province', title: '所在区域', align: "center", minWidth: 120, templet: '<div>{{d.country}} {{d.province}} {{d.city}}</div>'},
                {field: 'sex', title: '性别', align: 'center', minWidth: 80, templet: '<div>{{d.sex==1 ? "男" : (d.sex==2 ? "女" : "未知")}}</div>'},
                {field: 'language', title: '使用语言', align: 'center', minWidth: 100, templet: '<div>{{d.language}}</div>'},
                {
                    field: 'subscribe', title: '订阅状态', align: "center", templet: function (d) {
                        if (d.subscribe > 0) return '<span class="layui-badge layui-bg-green">已订阅</span>';
                        else return '<span class="layui-badge">未订阅</span>';
                    }
                },
                {field: 'subscribe_time', title: '订阅时间', minWidth: 170, align: 'center', sort: true, templet: '<div>{{d.subscribe_at}}</div>'},
                {field: 'is_black', title: '是否黑名单', align: 'center', minWidth: 110, templet: '#StatusSwitchTpl'},
                {toolbar: '#toolbar', title: '操作面板', align: 'center', fixed: 'right'}
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {
            var data = {openid: obj.value, black: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('black')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UserData').trigger('reload'); // 操作异常时重载数据
                });
                return false;
            }, false);
        });
    });
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <!--{if auth("black")}-->
    <input type="checkbox" value="{{d.openid}}" lay-skin="switch" lay-text="已拉黑|未拉黑" lay-filter="StatusSwitch" {{d.is_black>0?'checked':''}}>
    <!--{else}-->
    {{-d.status ? '<b class="color-red">已拉黑</b>' : '<b class="color-green">未拉黑</b>'}}
    <!--{/if}-->
</script>

<script type="text/html" id="toolbar">
    <!--{if auth("remove")}-->
    <a class="layui-btn layui-btn-sm layui-btn-danger" data-action="{:url('remove')}" data-value="id#{{d.id}}" data-confirm="确定要删除该用户吗？">删 除</a>
    <!--{/if}-->
</script>
{/block}