<!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('退款原因列表')"/>
    <style>
        .btn-disabled-edit {
            color: #ccc !important;
            cursor: not-allowed !important;
            pointer-events: none;
        }
    </style>
</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>
                            <label>名称：</label>
                            <input type="text" name="name"/>
                        </li>
                        <li>
                            <label>应用状态：</label>
                            <select name="isApplied">
                                <option value="">全部</option>
                                <option value="1">应用中</option>
                                <option value="0">未应用</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="kylin:refundReasons:create">
                <i class="fa fa-plus"></i> 添加
            </a>
        </div>
        <div class="col-sm-12 select-table table-bordered">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var updateFlag = [[${@permission.hasPermi('kylin:refundReasons:update')}]];
    var removeFlag = [[${@permission.hasPermi('kylin:refundReasons:delete')}]];
    var applyFlag = [[${@permission.hasPermi('kylin:refundReasons:apply')}]];

    var prefix = ctx + "kylin/refundReasons";
    $(function () {
        var options = {
            url: prefix + "/list",
            updateUrl: prefix + "/update/{id}",
            createUrl: prefix + "/create",
            removeUrl: prefix + "/delete",
            modalName: "退款原因",
            columns: [
                {
                    field: 'name',
                    title: '退款理由'
                },
                {
                    field: 'needDetail',
                    title: '需补充说明',
                    formatter: function (value) {
                        return value == 1 ? '是' : '否';
                    }
                },
                {
                    field: 'placeholder',
                    title: '二级输入placeholder'
                },
                {
                    field: 'isApplied',
                    title: '应用状态',
                    formatter: function (value) {
                        if (value == 1) {
                            return '<span class="badge badge-primary">应用中</span>';
                        }
                        return '<span class="badge badge-warning">未应用</span>';
                    }
                },
                {
                    field: 'sort',
                    title: '排序'
                },
                {
                    field: 'updatedAt',
                    title: '更新时间',
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        var date = new Date(value);
                        return date.getFullYear() + '-' +
                            String(date.getMonth() + 1).padStart(2, '0') + '-' +
                            String(date.getDate()).padStart(2, '0') + ' ' +
                            String(date.getHours()).padStart(2, '0') + ':' +
                            String(date.getMinutes()).padStart(2, '0') + ':' +
                            String(date.getSeconds()).padStart(2, '0');
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var actions = [];
                        if (row.isApplied == 1) {
                            actions.push('<a class="btn btn-success btn-xs btn-disabled-edit" href="javascript:void(0)" title="请先取消应用"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-warning btn-xs ' + applyFlag + '" href="javascript:void(0)" onclick="toggleApply(\'' + row.reasonId + '\', 0)"><i class="fa fa-ban"></i>取消应用</a> ');
                        } else {
                            actions.push('<a class="btn btn-success btn-xs ' + updateFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.reasonId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-primary btn-xs ' + applyFlag + '" href="javascript:void(0)" onclick="toggleApply(\'' + row.reasonId + '\', 1)"><i class="fa fa-check"></i>应用</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="removeReason(\'' + row.reasonId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function removeReason(id) {
        $.operate.remove(id, "确认删除该退款原因吗？");
    }

    function toggleApply(reasonId, isApplied) {
        var title = isApplied == 1 ? '应用' : '取消应用';
        $.modal.confirm('确定要' + title + '该退款原因吗？', function () {
            $.ajax({
                type: 'put',
                url: prefix + '/apply',
                data: {reasonId: reasonId, isApplied: isApplied},
                success: function (e) {
                    if (e.code == 0) {
                        $.modal.msgSuccess(title + '成功');
                        $("button[name=refresh]").click();
                    } else {
                        $.modal.alertError(e.msg || (title + '失败'));
                    }
                }
            });
        });
    }
</script>
</body>
</html>
