<!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">
            <div style="float: right">
                <input class="search-condition-elem" style="margin-top: 0px" id="search-orderId" placeholder="订单id">
                <input class="search-condition-elem"  style="margin-top: 0px" id="search-activityId" placeholder="活动id">
                <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>
                </select>
                <button class="cms-bar-btn" id="search-list-btn">检索</button>
            </div>
        </div>
        <table class="account-apply-tab layui-table" id="search-result-tab">
            <thead>
            <tr>
                <th style="width: 150px">订单号</th>
                <th style="width: 150px">活动信息</th>
                <th style="width: 200px">商品信息</th>
                <th style="width: 200px">中奖用户</th>
                <th style="width: 70px">中奖时间</th>
                <th style="width: 65px">兑换时间</th>
                <th style="width: 65px">状态</th>
                <th style="width: 65px">操作</th>
            </tr>
            </thead>
            <tbody>

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

<script>
    var activityId = getUserParamByName("activityId");
    if (activityId != '') {
        $("#search-activityId").val(activityId);
    }

    var firstSearch = true;
    var curr = 0, size = 10, total = 0;

    function updateIntegral() {

        layer.open({
            type: 1,
            title: false,
            skin: 'layui-layer-demo', //样式类名
            closeBtn: 0, //不显示关闭按钮
            anim: 2,
            area: ['380px', '506px'],
            shadeClose: true, //开启遮罩关闭
            content: '<div class=\"login-module-header-div\">' +
                '<p class=\"login-module-title\">系统积分管理</p>' +
                '<p class=\"login-module-desc\">积分操作需谨慎</p>' +
                '</div>' +
                '<div class=\"login-module-core-div\">' +
                '<div class="login-module-core-div-item">' +
                '<select class="login-input-number" style="width: 100%; height: 100%; border: 0px" id="integral-reason"><option value="">类型</option>' +
                '<option value="SYSTEM_REWARD">系统奖励</option>' +
                '<option value="SYSTEM_COMPENSATE">系统补偿</option>' +
                '<option value="SYSTEM_DEDUCTION">系统扣除</option>' +
                '</select>' +
                '</div>' +
                '<div class="login-module-core-div-item">' +
                '<input class="login-input-number" id="integral-user-id" placeholder="用户ID">' +
                '</div>' +
                '<div class="login-module-core-div-item">' +
                '<input class="login-input-number" id="integral-count" placeholder="积分值" value="0">' +
                '</div>' +
                '<div class="login-module-core-div-item" style=\"border: 0px\">' +
                '<button class=\"login-btn\" onclick=\"updateIntegralBySystem($(\'#integral-reason\').val(), $(\'#integral-user-id\').val(), $(\'#integral-count\').val())\">积分变更</button>' +
                '</div>' +
                '</div>' +
                '</div>'
        });
    }

    function updateIntegralBySystem(integralReason, integralUserId, integralCount) {
        if (integralReason == '') {
            layer.msg("积分操作类型不能为空");
            return;
        }
        if (integralUserId == '') {
            layer.msg("操作目标用户不能为空");
            return;
        }
        if (integralCount == '') {
            layer.msg("积分值不能为空");
            return;
        }
        $.ajax({
            url: "/api/integral/trans",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: "{\"userId\":\""+integralUserId+"\", \"reasonCode\": \""+integralReason+"\", \"count\": "+integralCount+"}",
            success:function (data) {
                data = eval(data);
                if (data.success){
                    layer.closeAll();
                    layer.msg("操作成功");
                    pageNo = 1;
                    searchList();
                } else {
                    layer.msg(data.msg);
                }
            },error:function () {
                layer.msg("服务端异常");
            }
        })
    }

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

    searchList();
    function searchList() {
        $("#search-result-tab").children("tbody").empty();
        $.ajax({
            url:"/api/lottery/order/page?orderId="+$("#search-orderId").val()+"&activityId="+$("#search-activityId").val()+"&pageNo="+(curr == 0 ? 1 : curr)+"&pageSize="+size+"&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.orderId+"</td>"+
                            "<td>"+value.activity.name+"<br/>"+value.activity.activityId+"</td>"+
                            "<td><img style='height: 25px; margin-right: 10px;' src='"+value.item.icon+"'>"+value.item.name+"</td>"+
                            "<td><a href='/coder?userid="+value.user.userId+"'><img style='height: 25px; border-radius: 100px; margin-right: 10px;' src='"+value.user.photo+"'>"+value.user.userName+"</a></td>"+
                            "<td>"+formatMsgTime(value.luckTime)+"</td>"+
                            "<td>"+(value.exchangeTime == 0 ? "/" : formatMsgTime(value.exchangeTime))+"</td>"+
                            "<td>"+value.orderStatusName+"</td>"+
                            "<td style='width: 200px;'>";
                        if (value.orderStatus == 1){
                            tr+="<button class=\"layui-btn layui-btn-sm layui-btn-warm\"  onclick=\"updateStatus('"+value.orderId+"', 2)\">改为已兑换</button>";
                            tr+="<button class=\"layui-btn layui-btn-sm layui-btn-norma\" onclick=\"updateStatus('"+value.orderId+"', 3)\">删除</button>";
                        } else if (value.orderStatus == 2){
                            tr+="<button class=\"layui-btn layui-btn-sm layui-btn-norma\" onclick=\"updateStatus('"+value.orderId+"', 1)\">改为未兑换</button>";
                            tr+="<button class=\"layui-btn layui-btn-sm layui-btn-norma\" onclick=\"updateStatus('"+value.orderId+"', 3)\">删除</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();
                    }
                } else {}
            },error:function () {}
        })
    }

    function updateStatus(orderId, status) {
        if (orderId == '') {
            layer.msg("订单ID为空");
            return;
        }
        if (status == '') {
            layer.msg("变更状态为空");
            return;
        }
        $.ajax({
            url: "/api/lottery/order/updateStatus",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: "{\"orderId\":\""+orderId+"\", \"status\": "+status+"}",
            success:function (data) {
                data = eval(data);
                if (data.success){
                    searchList();
                } else {
                    layer.msg(data.msg);
                }
            },error:function () {
                layer.msg("服务端异常");
            }
        })
    }

    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();
                }
            });
        })
    }

</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>