<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="referrer" content="no-referrer"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">

    <link rel="shortcut icon" href="/favicon.ico"/>
    <link rel="bookmark" href="/favicon.ico"/>


    <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/layui-v2.6.8/layui/layui.js" type="text/javascript"></script>
    <script src="/ref/jquery/jquery-3.4.1.js" type="text/javascript"></script>
    <script src="/js/common/common.js" type="text/javascript"></script>
</head>
<body style="overflow-x: hidden">
<center>
    <div class="aiqr-container" style="margin-bottom: 0px; padding-top: 40px; width: calc(100% - 100px); margin-bottom: 20px">
        <div class="cms-manage-title">
                <input class="search-condition-elem" style="margin-top: 0px" id="search-name" placeholder="活动名称">
                <select class="search-condition-elem" style="margin-top: 0px"  id="search-status">
                    <option value="">全部状态</option>
                    <option value="1">筹备中</option>
                    <option value="2">进行中</option>
                    <option value="3">已结束</option>
                    <option value="4">已删除</option>
                </select>
                <button class="cms-bar-btn" id="search-btn">检索</button>
                <button class="cms-bar-btn" id="add-lottery-activity-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: 150px">活动业务编号</th>
                <th style="width: 100px">活动名称</th>
                <th style="width: 80px">封面</th>
                <th style="width: 200px">活动描述</th>
                <th style="width: 200px">基础配置</th>
                <th style="width: 80px">状态</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
        <div id="page"></div>
        <div style="clear: both"></div>
    </div>
</center>
</body>

<script>
    var firstSearch = true;
    var curr = 0, size = 10, total = 0;
    $("#add-lottery-activity-btn").on('click', function() {
        layer.closeAll();
        layer.open({
            type: 1,
            title: false,
            skin: 'layui-layer-demo', //样式类名
            closeBtn: 0, //不显示关闭按钮
            anim: 2,
            area: ['380px', '560px'],
            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">' +
                '<input class="popup-input-number" id="create-activity-name" placeholder="活动名称（30字以内）">' +
                '</div>' +
                '<div class="popup-module-core-div-item">' +
                '<input class="popup-input-number" id="create-activity-money" placeholder="本期抽奖需要消耗多少钱">' +
                '</div>' +
                '<div class="popup-module-core-div-item" style="height: 100px; overflow: hidden">' +
                '<textarea class="allDiv" id="create-activity-description" style="border: 0px; padding: 5px; line-height: 23px; font-size: 14px; height: 100px; width: 260px; resize: none" placeholder="活动描述"></textarea>' +
                '</div>' +
                '<div class="popup-module-core-div-item layui-upload" style="width: 160px; height: 100px" id="upload-activity-cover">' +
                '<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="activity-cover">' +
                '活动主图 <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; margin-top: 20px;\">' +
                '<button class=\"popup-btn\" onclick=\"createLotteryActivity($(\'#create-activity-name\').val(),  $(\'#create-activity-money\').val(), $(\'#create-activity-description\').val(), $(\'#activity-cover\').attr(\'src\'))\">创建抽奖活动</button>' +
                '</div>' +
                '</div>' +
                '</div>'
        });
        layui.use(['upload'], function(){
            var $ = layui.jquery
                ,upload = layui.upload
            upload.render({
                elem: '#upload-activity-cover'
                ,url: '/api/file/moment/upload' //改成您自己的上传接口
                ,accept: 'images'
                ,acceptMime: '*',
                done: function(res){
                    if(!res.success){
                        return layer.msg('上传失败');
                    } else {
                        $('#activity-cover').attr('src', res.data.url);
                    }
                }
            });
        });
    })


    function createLotteryActivity(name, money, description, cover) {
        if (name == '') {
            layer.msg("活动名不能为空");
            return;
        }
        if (description == '') {
            layer.msg("描述不能为空");
            return;
        }
        if (cover == '') {
            layer.msg("封面不能为空");
            return;
        }
        if (money != null && money < 0) {
            layer.msg("钱数不能为负数");
            return;
        }
        $.ajax({
            url: "/api/lottery/activity/save",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                name: name,
                description: description,
                cover: cover,
                money: money
            }),
            success:function (data) {
                data = eval(data);
                if (data.success){
                    layer.msg("创建成功");
                    pageNo = 1;
                    searchList();
                    layer.closeAll();
                } else {
                    layer.msg(data.msg);
                }
            },error:function () {
                layer.msg("服务端异常");
            }
        })
    }

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

    searchList();
    function searchList() {
        $("#search-result-tab").children("tbody").empty();
        $.ajax({
            url:"/api/lottery/activity/page?pageNo="+(curr == 0 ? 1 : curr)+"&pageSize="+size+"&name="+$("#search-name").val()+"&status="+$("#search-status").val(),
            type:"get",
            success:function (data) {
                data = eval(data);
                if (data.success){
                    $.each(data.data, function (index, value) {
                        var tr = "<tr>"+
                            "<td>"+value.activityId+"</td>"+
                            "<td>"+value.name+"</td>"+
                            "<td><img style='height: 40px' src='"+value.cover+"'></td>"+
                            "<td>"+value.description+"</td>"+
                            "<td>" +
                            "   <a  onclick=\"currentActivityId = '"+value.activityId+"'; loadFrame('/admin/lottery-cms-item');\" style='margin-right: 10px; color: #0b5eaf'>奖品配置</a>" +
                            "</td>"+
                            "<td>"+value.statusName+"</td>"+
                            "<td>"+formatMsgTime(value.createTime)+"</td>"+
                            "<td style='width: 200px;'>";
                        if (value.status == 1){
                            tr+="<button class=\"cms-publish-btn\"  onclick=\"updateStatus('"+value.activityId+"', 2)\">发布</button>";
                            tr+="<button class=\"cms-recommend-btn\" onclick=\"updateStatus('"+value.activityId+"', 4)\">删除</button>";
                        } else if (value.status == 2){
                            tr+="<button class=\"cms-new-btn\" onclick=\"updateStatus('"+value.activityId+"', 3)\">暂停</button>";
                        } else if (value.status == 3){
                            tr+="<button class=\"cms-recommend-btn\" onclick=\"updateStatus('"+value.activityId+"', 4)\">删除</button>";
                        } else if (value.status == 4){}
                        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;
                    searchList();
                }
            });
        })
    }

    function updateStatus(activityId, status) {
        if (activityId == '') {
            layer.msg("活动ID为空");
            return;
        }
        if (status == '') {
            layer.msg("变更状态为空");
            return;
        }
        $.ajax({
            url: "/api/lottery/activity/updateStatus",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: "{\"activityId\":\""+activityId+"\", \"status\": "+status+"}",
            success:function (data) {
                data = eval(data);
                if (data.success){
                    searchList();
                } 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>