<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})"></head>

<body>
<div class="layui-form timo-compile">
    <form th:action="@{/api/sys/notify/receiver}">
        <input type="hidden" name="id" th:value="${notifyGroup?.id}"/>
        <div class="layui-form-item">
            <label class="layui-form-label required">通知类型</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="text" name="notifyType" placeholder="请输入通知类型" required th:value="${notifyGroup?.notifyType}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">通知账户</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="text" name="notifyAccountId" placeholder="请输入通知账户" th:value="${notifyGroup?.notifyAccountId}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">接收标识</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="text" name="groupId" placeholder="请输入接收组标识" required th:value="${notifyGroup?.groupId}">
            </div>
        </div>
        <table class="layui-table timo-detail-table" id="notifyReceiverTable">
            <tbody>
                <span th:if="${notifyGroup} != null">
                    <span th:if="${not #lists.isEmpty(notifyGroup.receivers)}">
                        <tr th:each="receiver,stat:${notifyGroup.receivers}">
                            <th>通知接收地址</th>
                            <td >
                                <div class="layui-form-item">
                                    <div class="layui-input-inline">
                                        <input class="layui-input" type="text" th:attr="name=@{'receivers[' + ${stat.index} + ']'}" placeholder="请添加通知地址" th:value="${receiver}">
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div>
                                    <a class="layui-btn layui-btn-xs addNotifyReceiver">添加</a>
                                    <a class="layui-btn layui-btn-danger layui-btn-xs delNotifyReceiver">删除</a>
                                </div>
                            </td>
                        </tr>
                    </span>
                    <span th:if="${#lists.isEmpty(notifyGroup.receivers)}">
                        <tr>
                            <th>通知接收地址</th>
                            <td >
                                <div class="layui-form-item">
                                    <div class="layui-input-inline">
                                        <input class="layui-input" type="text" name="receivers[0]" placeholder="请添加通知地址" required>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div>
                                    <a class="layui-btn layui-btn-xs addNotifyReceiver">添加</a>
                                    <a class="layui-btn layui-btn-danger layui-btn-xs delNotifyReceiver">删除</a>
                                </div>
                            </td>
                        </tr>
                    </span>
                </span>
                <span th:if="${notifyGroup} == null">
                    <tr>
                        <th>通知接收地址</th>
                        <td >
                            <div class="layui-form-item">
                                <div class="layui-input-inline">
                                    <input class="layui-input" type="text" name="receivers[0]" placeholder="请添加通知地址" required>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div>
                                <a class="layui-btn layui-btn-xs addNotifyReceiver">添加</a>
                                <a class="layui-btn layui-btn-danger layui-btn-xs delNotifyReceiver">删除</a>
                            </div>
                        </td>
                    </tr>
                </span>
            </tbody>
        </table>
        <div class="layui-form-item timo-finally">
            <button class="layui-btn ajax-submit"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary close-popup"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script th:replace="/common/template :: script"></script>
<script type="text/javascript" th:src="@{/js/plugins/jquery-2.2.4.min.js}"></script>
<script type="text/javascript" th:inline="javascript">
    var receiversSize = $('#notifyReceiverTable tbody tr').length;
    // 在表格末尾添加一行
    $('body').on('click', '.addNotifyReceiver', function() {
        // 需要添加的 html
        var html =
            '<tr>\n' +
            '                    <th>通知接收地址</th>\n' +
            '                    <td >\n' +
            '                        <div class="layui-form-item">\n' +
            '                            <div class="layui-input-inline">\n' +
            '                                <input class="layui-input" type="text" name="receivers[' + receiversSize + ']" placeholder="请添加通知地址" required>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '                    </td>\n' +
            '                    <td>\n' +
            '                        <div>\n' +
            '                            <a class="layui-btn layui-btn-xs addNotifyReceiver">添加</a>\n' +
            '                            <a class="layui-btn layui-btn-danger layui-btn-xs delNotifyReceiver">删除</a>\n' +
            '                        </div>\n' +
            '                    </td>\n' +
            '                    </tr>'

        // 添加到表格最后
        $(html).appendTo($('#notifyReceiverTable tbody:last'));
        layui.use(['form'], function () {
            var form = layui.form;
            // 因为有select元素，所有添加后要重新渲染一次
            form.render();
        });
        receiversSize++;
    });

    // 删除表格末尾的行
    $('body').on('click', '.delNotifyReceiver', function() {
        if ($('#notifyReceiverTable tbody tr').length === 1) {
            layer.msg('只有一条不允许删除', {
                time : 1000
            });
        } else {
            console.log('删除表格末尾的行')
            // 删除当前按钮所在的tr
            $(this).closest('tr').remove();
            receiversSize--;
        }
    });
</script>
</body>
</html>