<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新建</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="formDemo">
    <div class="layui-form layuimini-form" style="margin-right: 20px">
        <input type="hidden" id="rws" name="rws" class="layui-input">
        <input type="hidden" id="ht" name="ht" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label required" style="width: 70px">计划名称</label>
            <div class="layui-input-block">
                <!--                <select name="jhmc" id="jhmc" lay-filter="status" lay-verify="required" lay-reqtext="请选择计划名称">-->
                <!--                    <option value="">请选择</option>-->
                <!--                    <option th:each="item:${jhmcList}" th:text="${item.name}"-->
                <!--                            th:value="${item.name}"></option>-->
                <!--                </select>-->
                <input type="text" id="jhmc" name="jhmc" class="layui-input" placeholder="请输入计划名称" lay-verify="required"
                       lay-reqtext="请输入计划名称">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 70px">付款要求</label>
            <div class="layui-input-block">
                <textarea id="fkyq" name="fkyq" class="layui-textarea" style="width: 100%;height: 100px"></textarea>
                <tip>按合同约定填写</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 90px; margin-left: -20px">支付比例(%)</label>
            <!--            <label class="layui-form-label required" style="width: 90px; margin-left: -20px">支付比例(%)</label>-->
            <div class="layui-input-block">
                <!--                <input type="number" id="zfbl" name="zfbl" autocomplete="off" class="layui-input" lay-verify="required" lay-reqtext="支付比例不能为空">-->
                <input type="number" id="zfbl" name="zfbl" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required" style="width: 70px">支付金额(万元)</label>
            <div class="layui-input-block">
                <input type="number" id="zfje" name="zfje" autocomplete="off" class="layui-input" lay-verify="required"
                       lay-reqtext="支付金额不能为空">
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 10%">付款信息</label>
            <div class="layuimini-container" style="margin-left: 13%; padding-bottom: 10px;width: 90%">
                <input type="hidden" id="fkxx" name="fkxx" class="layui-input">
                <div class="layui-btn-group" style="margin-bottom: -8px">
                    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-filter="add"><i class="layui-icon">&#xe63c;</i>添加行</button>
                    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-filter="delete"><i class="layui-icon">&#xe63c;</i>删除行</button>
                </div>
                <table class="layui-hide" id="currentTableId2" name="currentTableId2" lay-filter="currentTableFilter2" style="padding-top: 0px;margin-top: 0px"></table>
            </div>
        </div>


        <!-- 右侧悬浮按钮 -->
        <div class="right-bottom-btn">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">
                <i class="layui-icon layui-icon-ok"></i>保存
            </button>
            <button class="layui-btn" lay-filter="cancleBtn">
                <i class="layui-icon layui-icon-close"></i>取消
            </button>
        </div>
    </div>
</form>

<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'layer','table'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var htInfo = parent.getHtInfo();
        $("#rws").val(htInfo.rws);
        $("#ht").val(htInfo.ht);

        //支付比例
        $("#zfbl").on("input", function (e) {
            //获取input输入的值
            var zfbl = parseFloat(e.delegateTarget.value);
            if (!isNaN(zfbl)) {
                var zfje = htInfo.htje * zfbl * 0.01;
                $("#zfje").val(zfje.toFixed(2));
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            const zfje = parseFloat(document.getElementById('zfje').value);
            const zfbl = parseFloat(document.getElementById('zfbl').value);
            const htje = parseFloat(htInfo.htje);
            const zfjes = parseFloat(htje * zfbl * 0.01);
            var data_prjPayplan = [];
            for (const item of prjPayplan ) {
                var dfWb = {
                    "id": item.id,
                    "name": item.name,
                    "fkje": item.fkje,
                    "fksm": item.fksm
                };
                data_prjPayplan.push(dfWb);
            }
            $("#fkxx").val(JSON.stringify(data_prjPayplan));
            data.field.fkxx = JSON.stringify(data_prjPayplan);
            if (zfje > htje){
                layer.msg('支付金额不能大于合同总金额!');
                return false;
            }
            // var index = layer.load(0, {shade: 0.1});
            // AjaxUtil.post({
            //     url: AjaxUtil.ctx + "prjPayplan/doAdd/?htje="+htje,
            //     data: data.field,
            //     success: function (res) {
            //         layer.close(index);
            //         if (res.code === 0) {
            //             parent.setUpdateFlag();
            //             parent.layer.close(parent.layer.getFrameIndex(window.name));
            //         } else {
            //             layer.msg(res.message);
            //         }
            //
            //     },
            //     error: function (error) {
            //         Message.error(error.message, 1000);
            //     }
            // });
            parent.setFkjh(data.field);
            parent.setUpdateFlag();
            parent.layer.close(parent.layer.getFrameIndex(window.name));// 关闭子窗口
            // return false;
        });
        var prjPayplan = new Array();
        var currTable2 = table.render({
            elem: '#currentTableId2',
            data: prjPayplan,
            cols: [
                [
                    {type: "checkbox", align: 'center'},
                    {type: 'numbers', align: 'center'},
                    {field: 'name', title: '公司名称', width: '30%',align:'center',edit: 'text'},
                    {field: 'fkje', title: '付款金额', width: '15%', align: 'center',edit:'text'},
                    {field: 'fksm', title: '付款说明', align: 'center',edit: 'text'}
                ]
            ],
            autoSort: false,

            limit: Number.MAX_VALUE,
            page: false,
            skin: 'grid',
            done: function (res, curr, count) { // done为数据渲染完的回调
            }
        });
        $("button[lay-filter='add']").click(function () {
            var newRow={"name":"","fkje":"","fksm":""   };
            prjPayplan.push(newRow)
            currTable2.reload();
        });
        $("button[lay-filter='delete']").click(function () {
            if (prjPayplan.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId2')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的文件！", 1000);
                return;
            }
            for (var i = 0; i < prjPayplan.length; i++) {
                var item = prjPayplan[i];
                if (item.LAY_CHECKED) {//条件：选中
                    prjPayplan.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable2.reload();
        });
        // 监听取消按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });
</script>
</body>
</html>