<style>
    .layui-table-cell{
        height:auto !important;
    }
</style>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="init">同步授权数据</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">配置</a>
    </div>
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var formX = layui.formX;
        var notice = layui.notice;
        var dropdown = layui.dropdown;//下拉菜单的
        var laydate = layui.laydate
        var xmSelect = layui.xmSelect
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: 'api/oauth-manage',
            page: true,
            limit:50,
            limits:[50,100,200,500,1000,10000],
            toolbar: true,
            defaultToolbar:['exports','print'],
            cellMinWidth: 100,
            cols: [[
                {field: 'uid', title: '账户主体ID', align: 'center'},
                {field: 'updated_at', title: '授权时间', align: 'center'},
                {fixed: 'right', title:'操作', width: 240, minWidth: 155, templet: '#toolDemo'}
            ]]
        });
        /* 表格搜索 */
        form.on('submit(tbAdvTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });
        // /* 添加时间 */
        // laydate.render({
        //     elem: '#formAdvDateSel1',
        //     range: true,
        //     trigger: 'click'
        // });
        // 添加
        $('#btnAddUser123').click(function () {
            showEditModel();
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'edit') { // 修改
                edit(data);
            } else if (obj.event === 'init') { // 删除
                initOauthAdmin(data);
            }
            // dropdown.hideAll();
        });
        // 显示表单弹窗
        function showEditModel(mUser) {
            var url_get = 'oauthmanageconfig';
            var layIndex = admin.open({
                title: (mUser ? '修改' : '添加') + '用户',
                url: url_get,
                data: {user: mUser},     // 传递数据到表单页面
                end: function () {
                    //
                },
                success: function (layero, dIndex) {
                    // 弹窗超出范围不出现滚动条
                    var mUser = admin.getLayerData('#modelUserForm').user;  // 列表页面传递的数据，#modelUserForm这个只要写弹窗内任意一个元素的id即可
                    console.log(mUser);
                    form.val('modelUserForm', mUser);
                    // 回显数据
                    if (mUser) {
                        $('.hide').remove()
                    }
                    // 表单提交事件
                    var id = mUser ? mUser.id : 0;
                    form.on('submit(modelSubmitUser)', function (data) {
                        admin.showLoading( 'body',1, '.7');
                        var url = mUser ? 'users/' + mUser.id : 'users';
                        if (mUser) {
                            data.field._method = 'PUT'
                        }
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 666) {
                                msg('消息通知',res.msg,1)
                                insTb.reload({}, 'data');
                                admin.closeDialog('#modelUserForm');  // 关闭页面层弹窗
                            } else {
                                msg('消息通知',res.msg,2)
                            }
                            admin.removeLoading( 'body',true, true);
                        }, 'json');
                        return false;
                    });
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        function edit(data) {
            console.log(data)
            layer.open({
                type: 2,
                title: '授权账户管理',
                shadeClose: true,
                shade: 0.8,
                area: ['380px', '80%'],
                content: 'oauthmanageconfig?uid=' + data.uid // iframe 的 url
            });
        }

        function tuisong(mUser, _this){
            var data = {'xs_id':mUser.id}
            layer.confirm('确定推送？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function(i){
                layer.close(i);
                admin.showLoading('body', 1, '.7');
                $.post('xiansuo/tuisong', {
                    _token: 'lY9I6mn5ij6xKtr0iil9xVXbyaHaowdYC3IWSovG',
                    data: data
                }, function (res) {
                    if (res.code == 666) {
                        msg('提示',res.msg,1)
                        insTb.reload({}, 'data');
                    } else {
                        msg('提示',res.msg,2)
                    }
                    admin.removeLoading('body', true, true);
                }, 'json');
            })
        }

        function initOauthAdmin(obj){
            admin.showLoading('body', 1, '.7');
            $.ajax({
                url: 'api/init-oauth-admin',
                data: {
                    uid: obj.uid
                },
                dataType: "json",
                type: "post",
                success: function (res) {
                    admin.removeLoading('body', true, true);
                    // layer.close(loading);
                    // if (res.code) {
                    //     return layui.popup.failure(res.msg);
                    // }
                    // return layui.popup.success("操作成功", refreshTable);
                }
            })
        }
    });
</script>
<script>
    window.clearTimeout(localStorage.getItem("in_user"))
    localStorage.removeItem("in_user")
    localStorage.removeItem("in_user_box")

</script>