
<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="referrer" content="no-referrer">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link href="/css/common/common.css" rel="stylesheet" type="text/css" />
    <link href="/css/admin/console.css" rel="stylesheet" type="text/css"/>

    <link href="/ref/layui-v2.6.8/layui/css/layui.css" rel="stylesheet" type="text/css" />
    <script src="/ref/jquery/jquery-3.4.1.js" type="text/javascript"></script>
    <script src="/ref/layui-v2.6.8/layui/layui.js" type="text/javascript"></script>

    <style>
        .search-condition-module {
            position: relative;
            width: 99%;
            height: 60px;
            line-height: 60px;
            border-bottom: 1px solid whitesmoke;
        }
        .subject-btn {
            position: absolute;
            right: 50px;
            top: 15px;
            height: 35px;
            width: 100px;
            border: 0px;
            color: white;
            background-color: dodgerblue;
            box-shadow: 0px 0px 5px #3f8ddb;
            border-radius: 3px;
        }
        button:hover {
            cursor: pointer;
        }
        .search-condition-elem {
            position: relative;
            float: left;
            margin-right: 20px;
            margin-top: 15px;
            height: 35px;
            width: 200px;
            border: 1px solid #cdcccc;
            line-height: 35px;
            text-indent: 10px;
        }
    </style>
</head>
<body style="background-color: white; width: 94%; margin-left: 20px;">
<center>

    <div class="search-condition-module">

        <select class="search-condition-elem"  id="search-status">
            <option value="">全部状态</option>
            <option value="0">新建</option>
            <option value="1">上线</option>
        </select>
        <button class="subject-btn" style="left: 250px" id="search-btn">检索</button>

        <button class="subject-btn" id="add-btn" style="background-color: orangered; box-shadow: 0px 0px 5px
        #e56535;">新增滑块</button>
    </div>

    <table class="account-apply-tab layui-table" id="search-result-tab">
        <thead>
        <tr>
            <th style="width: 100px">编号</th>
            <th style="width: 320px">滑块图片</th>
            <th style="width: 100px">状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <div id="page"></div>
</center>
</body>


<script>

    var firstSearch = true;
    var keyword = "全部";
    var curr = 0, size = 100, total = 0;

    $("#add-btn").on('click', function() {
        layer.closeAll();
        layer.open({
            type: 1,
            title: false,
            skin: 'layui-layer-demo', //样式类名
            closeBtn: 0, //不显示关闭按钮
            anim: 2,
            area: ['380px', '350px'],
            shadeClose: true, //开启遮罩关闭
            content: '<div class=\"popup-module-header-div\" style="height: 70px;">' +
                '<p class=\"popup-module-title\" style="padding-top: 20px">新建滑块图形</p>' +
                '</div>' +
                '<div class=\"popup-module-core-div\" style="margin-top: -40px">' +
                '<div class="popup-module-core-div-item layui-upload" style="width: 160px; height: 100px" id="upload-subject-image">' +
                '<div style="width: 160px; border: 1px solid #f5f5f5; height: 100px; text-align: center; color: #c7c7c7; font-size: 20px; font-weight: 700; line-height: 100px">' +
                '<img style="position: absolute; left:0px; top:0px; width: 100%; height: 100%; border: 0px" id="create-image">' +
                '滑块图片 <i class="layui-icon" style="font-size: 25px; margin-right: 20px;">&#xe60d;</i>   ' +
                '</div>'+
                '</div>' +
                '<div class="popup-module-core-div-item" style=\"border: 0px\">' +
                '<button class=\"popup-btn\" onclick=\"createSlide($(\'#create-image\').attr(\'src\'))\">创建滑块验证资源</button>' +
                '</div>' +
                '</div>' +
                '</div>'
        });
        layui.use(['upload'], function(){
            var $ = layui.jquery
                ,upload = layui.upload
            //常规使用 - 普通图片上传
            upload.render({
                elem: '#upload-subject-image'
                ,url: '/api/file/chat/upload' //改成您自己的上传接口
                ,accept: 'images'
                ,acceptMime: '*'
                ,done: function(res){
                    if(!res.success){
                        return layer.msg('上传失败');
                    } else {
                        $('#create-image').attr('src', res.data.url);
                    }
                }
            });
        });
    })


    function createSlide(image) {
        if (image == '') {
            layer.msg("image不能为空");
            return;
        }
        $.ajax({
            url: "/api/slide/verification/save",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                image: image
            }),
            success:function (data) {
                data = eval(data);
                if (data.success){
                    layer.msg("创建成功");
                    curr = 0;
                    searchSlideList();
                    layer.closeAll();
                } else {
                    layer.msg(data.msg);
                }
            },error:function () {
                layer.msg("服务端异常");
            }
        })
    }

    $("#search-btn").on('click', function() {
        curr = 0;
        searchSlideList();
    })

    searchSlideList();
    function searchSlideList() {
        $.ajax({
            url:"/api/slide/verification/page?pageNo="+(curr == 0 ? 1 : curr)+"&pageSize="+size+"&status="+$("#search-status").val(),
            type:"get",
            success:function (data) {
                data = eval(data);
                if (data.success){
                    data = data.data;
                    $("#search-result-tab").children("tbody").empty();
                    $.each(data, function (index, value) {
                        var tr = "<tr>"+
                            "<td>"+value.id+"</td>"+
                            "<td><img style='height: 80px' src='"+value.image+"'></td>"+
                            "<td>"+(value.status == 0 ? "新建" : "上线")+"</td>"+
                            "<td style='width: 200px;'>";
                        if (value.status == 1){
                            tr+="<button class=\"layui-btn layui-btn-sm layui-btn-warm\" onclick=\"updateStatus("+value.id+", 0)\">下线</button>";
                        } else {
                            tr+="<button class=\"layui-btn layui-btn-sm layui-btn-norma\" onclick=\"updateStatus("+value.id+", 1)\">上线</button>";
                        }
                        tr+="</td></tr>";
                        if (!firstSearch) {
                            $("#search-result-tab").children("tbody").append(tr);
                        }
                    });
                    if (curr == 0 || data.total != total) {
                        curr = 1;
                        firstSearch = false;
                        total = data.total;
                        initPage();
                    }
                }
            },
            error:function () {
                layer.msg("error");
            }
        })
    }

    function initPage() {
        layui.use('laypage', function(){
            var laypage = layui.laypage;
            laypage.render({
                elem: 'page',
                count: total,
                limit: size,
                curr: curr,
                jump: function(obj){
                    curr = obj.curr;
                    searchSlideList();
                }
            });
        })
    }

    function updateStatus(id, status) {
        if (id == '') {
            layer.msg("任务ID为空");
            return;
        }
        $.ajax({
            url: "/api/slide/verification/status/"+id+"/"+status,
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success:function (data) {
                data = eval(data);
                if (data.success){
                    searchSlideList();
                } else {
                    layer.msg(data.msg);
                }
            },error:function () {
                layer.msg("服务端异常");
            }
        })
    }

</script>
<style>
    .layui-laypage a, .layui-laypage span {
        display: inline-block;
        *display: inline;
        *zoom: 1;
        vertical-align: middle;
        padding: 3px 13px;
        height: 28px;
        line-height: 28px;
        margin: 0px 5px;
        border-radius: 4px;
        background-color: #fff;
        color: #4e6ef2;
        font-size: 14px;
        border: none;
    }
    .layui-laypage a:hover, .layui-laypage span:hover {
        background: #4e6ef2;
        color: #fff;
    }
    .layui-laypage .layui-laypage-curr .layui-laypage-em {
        position: absolute;
        left: -1px;
        top: -1px;
        padding: 1px;
        width: 100%;
        height: 100%;
        border-radius: 4px;
        background: #4e6ef2;
        color: #fff;
        font-weight: normal;
    }
    .layui-laypage-next, .layui-laypage-prev {
        border-radius: 4px;
    }
    .layui-laypage-default {
        float: right;
    }
</style>
</html>