<!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/app/config/app/deploy}">
        <input type="hidden" name="id" th:value="${id}"/>
        <table class="layui-table timo-detail-table" id="deployConfigTable">
            <tbody>
            <span th:if="${not #lists.isEmpty(list)}">
                    <tr th:each="deployConfig,stat:${list}">
                    <th>机器地址</th>
                    <td>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <select th:attr="name=@{'deployConfigs[' + ${stat.index} + '].machineId'}">
                                    <option th:each="item : ${machines}" th:value="${item.key}" th:selected="${deployConfig.machineId} eq ${item.key}">[[${item.value}]]</option>
                                </select>
                            </div>
                        </div>
                    </td>
                    <th>启动脚本</th>
                    <td>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <textarea class="layui-textarea" th:attr="name=@{'deployConfigs[' + ${stat.index} + '].startScript'}" placeholder="请输入启动脚本" required th:text="${deployConfig?.startScript}"></textarea>
                            </div>
                        </div>
                    </td>
                    <!--<th>启动目录</th>
                    <td>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <input class="layui-input" type="text" th:attr="name=@{'deployConfigs[' + ${stat.index} + '].startHome'}" placeholder="请输入启动目录(可选)" required th:value="${deployConfig?.startHome}">
                            </div>
                        </div>
                    </td>-->
                    <td>
                        <div>
                            <a class="layui-btn layui-btn-xs addDeployConfig">添加</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs delDeployConfig">删除</a>
                        </div>
                    </td>
                    </tr>
                </span>
            <span th:if="${#lists.isEmpty(list)}">
                    <tr>
                    <th>机器地址</th>
                    <td>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <select name="deployConfigs[0].machineId">
                                    <option th:each="item : ${machines}" th:value="${item.key}">[[${item.value}]]</option>
                                </select>
                            </div>
                        </div>
                    </td>
                    <th>启动脚本</th>
                    <td>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <textarea class="layui-textarea" name="deployConfigs[0].startScript" placeholder="请输入启动脚本" required></textarea>
                            </div>
                        </div>
                    </td>
                    <!--<th>启动目录</th>
                    <td>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <input class="layui-input" type="text" name="deployConfigs[0].startHome"  placeholder="请输入启动目录" required>
                            </div>
                        </div>
                    </td>-->
                    <td>
                        <a class="layui-btn layui-btn-xs addDeployConfig">添加</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs delDeployConfig">删除</a>
                    </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 machineList = [[${machines}]]
    var deployConfigSize = $('#deployConfigTable tbody tr').length
    // 在表格末尾添加一行
    $('body').on('click', '.addDeployConfig', function() {
        console.log('在表格末尾添加一行')
        var selectMachineHtml = '<select name="deployConfigs[' + deployConfigSize + '].machineId">\n'
        for (let i in machineList) {
            selectMachineHtml += '<option value="' + machineList[i].key + '">' + machineList[i].value + '</option>\n'
        }
        selectMachineHtml += '</select>'

        // 需要添加的 html
        var html =
            '<tr>\n' +
            '                <th>机器地址</th>\n' +
            '                <td>\n' +
            '                    <div class="layui-form-item">\n' + selectMachineHtml +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </td>\n' +
            '                <th>启动脚本</th>\n' +
            '                <td>\n' +
            '                    <div class="layui-form-item">\n' +
            '                        <div class="layui-input-inline">\n' +
            '                            <textarea class="layui-textarea" name="deployConfigs[' + deployConfigSize + '].startScript" placeholder="请输入启动脚本" required></textarea>\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </td>\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="deployConfigs[' + deployConfigSize + '].startHome"  placeholder="请输入启动目录" required>\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </td>\n' +*/
            '                <td>\n' +
            '                    <a class="layui-btn layui-btn-xs addDeployConfig">添加</a>\n' +
            '                    <a class="layui-btn layui-btn-danger layui-btn-xs delDeployConfig">删除</a>\n' +
            '                </td>\n' +
            '                </tr>';

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

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