<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('草莓护照徽章列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            徽章名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            徽章类型：<select name="type" th:with="type=${@dict.getType('adam_caomei_badge_type')}">
                                <option value="">所有</option>
                                <option value="1">护照纪念徽章</option>
                                <option value="2">演出纪念徽章</option>
                                <option value="3">特殊徽章</option>
                            </select>
                        </li>
                        <li>
                            状态：<select name="displayStatus">
                                <option value="">所有</option>
                                <option value="0">下架</option>
                                <option value="1">已发布</option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="adam:caomei:badge:add">
                <i class="fa fa-plus"></i> 新增
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('adam:caomei:badge:edit')}]];
    var prefix = ctx + "adam/caomei/badge";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            modalName: "草莓护照徽章",
            columns: [{
                checkbox: true
            },
            {
                field: 'mid',
                title: '主键ID',
                visible: false
            },
            {
                field: 'badgeId',
                title: '徽章ID'
            },
            {
                field: 'icon',
                title: '图标',
                formatter: function(value, row, index) {
                    // 如果是图片链接，可以用 $.table.imageView(value)
                    // 如果是 emoji，直接返回
                    if(value && (value.startsWith('http') || value.startsWith('/'))) {
                        return $.table.imageView(value);
                    }
                    return '<span style="font-size: 24px;">' + value + '</span>';
                }
            },
            {
                field: 'name',
                title: '名称'
            },
            {
                field: 'type',
                title: '类型',
                formatter: function(value, row, index) {
                    if (value == 1) return '<span class="badge badge-info">护照纪念</span>';
                    if (value == 2) return '<span class="badge badge-primary">演出纪念</span>';
                    if (value == 3) return '<span class="badge badge-warning">特殊徽章</span>';
                    return value;
                }
            },
            {
                field: 'createdAt',
                title: '添加时间',
                formatter: function(value, row, index) {
                    if (!value) return '-';
                    return value.length >= 10 ? value.substring(0, 10) : value;
                }
            },
            {
                field: 'claimedCount',
                title: '领取人数'
            },
            {
                field: 'sort',
                title: '排序（越大越靠前）'
            },
            {
                field: 'displayStatus',
                title: '状态',
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return '<span class="badge badge-primary" style="background-color: #e6f3ff; color: #1890ff; padding: 5px 10px; border-radius: 4px;">已发布</span>';
                    } else {
                        return '<span class="badge badge-default" style="background-color: #f5f5f5; color: #999; padding: 5px 10px; border-radius: 4px;">下架</span>';
                    }
                }
            },
            {
                title: '操作',
                align: 'center',
                formatter: function(value, row, index) {
                    var actions = [];
                    // 详情按钮
                    actions.push('<a class="btn btn-info btn-sm" style="margin-right: 5px;" href="javascript:void(0)" onclick="detail(\'' + row.mid + '\')"><i class="fa fa-eye"></i>详情</a>');
                    
                    // 仅下架状态可修改(编辑)
                    if (row.displayStatus == 0) {
                        actions.push('<a class="btn btn-success btn-sm ' + editFlag + '" style="margin-right: 5px;" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.mid + '\')"><i class="fa fa-edit"></i>编辑</a>');
                    }

                    // 下架/发布按钮
                    if (row.displayStatus == 1) {
                        actions.push('<a class="btn btn-danger btn-sm" style="margin-right: 5px;" href="javascript:void(0)" onclick="disable(\'' + row.mid + '\')"><i class="fa fa-arrow-down"></i>下架</a>');
                    } else {
                        actions.push('<a class="btn btn-primary btn-sm" style="margin-right: 5px;" href="javascript:void(0)" onclick="enable(\'' + row.mid + '\')"><i class="fa fa-upload"></i>发布</a>');
                    }
                    
                    // 领取用户按钮
                    actions.push('<a class="btn btn-warning btn-sm" href="javascript:void(0)" onclick="claimedUsers(\'' + row.badgeId + '\')"><i class="fa fa-users"></i>领取用户</a>');
                    
                    return actions.join('');
                }
            }]
        };
        $.table.init(options);
    });

    /* 徽章下架 */
    function disable(mid) {
        $.modal.confirm("确认要下架该徽章吗？", function() {
            $.operate.post(prefix + "/changeStatus", { "mid": mid, "displayStatus": 0 });
        });
    }

    /* 徽章发布 */
    function enable(mid) {
        $.modal.confirm("确认要发布该徽章吗？", function() {
            $.operate.post(prefix + "/changeStatus", { "mid": mid, "displayStatus": 1 });
        });
    }

    /* 详情 */
    function detail(mid) {
        $.modal.open("徽章详情", prefix + "/detail/" + mid);
    }

    /* 领取用户 */
    function claimedUsers(badgeId) {
        if (!badgeId) {
            $.modal.msgError("未找到徽章ID");
            return;
        }
        $.modal.open("领取用户", prefix + "/claimUsers/" + badgeId);
    }
</script>
</body>
</html>