<!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">
        <!--<div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label " style="width: 85px">序号</label>
            <div class="layui-input-inline" style="width: 625px">
                <input type="hidden" name="xm" id="xm" class="layui-input" th:value="${xm}">
                <input type="number" id="xh" name="xh" class="layui-input" autocomplete="off">
            </div>
        </div>-->
        <input type="hidden" name="xm" id="xm" class="layui-input" th:value="${xm}">
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 85px">单位</label>
            <div class="layui-input-inline" style="width: 625px">
                <input type="hidden" id="dw" name="dw" class="layui-input">
                <input type="text" id="dwmc" name="dwmc" lay-verify="required" lay-reqtext="单位不能为空" class="layui-input"
                       readonly>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 85px">主要研究内容</label>
            <div class="layui-input-inline" style="width: 625px">
                <textarea id="zyyjnr" name="zyyjnr" class="layui-textarea" lay-verify="required"
                          lay-reqtext="主要研究内容不能为空" style="height: 180px" autocomplete="off"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 85px">经费(万元)</label>
            <div class="layui-input-inline" style="width: 625px">
                <input type="number" id="jf" name="jf" lay-verify="required" lay-reqtext="经费不能为空" autocomplete="off"
                       class="layui-input">
            </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', 'tableSelect'], function () {
        var form = layui.form,
            tableSelect = layui.tableSelect;

        var xmId = parseInt($("#xm").val());//项目任务书ID

        //单位gst
        tableSelect.render({
            elem: '#dwmc',
            checkedKey: 'id',
            searchKey: 'mc',
            searchPlaceholder: '搜索单位',
            table: {
                url: AjaxUtil.ctx + 'prjCompletecompany/dwlist/' + xmId,
                cols: [
                    [
                        {type: 'radio',title: '😊'},
                        {type: 'numbers', align: 'center',title: '序号'},
                        {field: 'dwmc', title: '单位',width:'60%'}
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = [];
                var NEWJSON1 = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.dwmc);
                    NEWJSON1.push(item.dw);
                })
                elem.val(NEWJSON.join(","));
                $("#dw").val(NEWJSON1.join(","));
            }
        });

        $(document).ready(function () {
            $('#jf').on('input change',function () {
                const jf = $(this).val();
                if (jf < 0 || jf == 0){
                    layer.msg('经费必须大于0',{icon:2},function () {
                        $('#jf').val('');
                    })
                }
                
            })
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // var selectedUnits = data.field.dwmc.split(","); // 将多选结果分割成数组
            // var selectedUnitCodes = data.field.dw.split(","); // 将多选结果的单位代码分割成数组

            // 在这里使用选定的单位和单位代码进行后续处理，例如保存到数据库等操作

            // 将选定的单位信息保存在localStorage中
            // localStorage.setItem('selectedUnits', JSON.stringify(selectedUnits));
            // localStorage.setItem('selectedUnitCodes', JSON.stringify(selectedUnitCodes));


            parent.setFgCompany(data.field);
            parent.setUpdateFlag();
            parent.layer.close(parent.layer.getFrameIndex(window.name));// 关闭子窗口

            return false;
        });
        // 获取保存的单位信息
        var selectedUnits = localStorage.getItem('selectedUnits');
        var selectedUnitCodes = localStorage.getItem('selectedUnitCodes');

        // // 如果存在保存的数据，则将其解析为数组
        //  if (selectedUnits) {
        //      selectedUnits = JSON.parse(selectedUnits);
        //  }
        //
        //  if (selectedUnitCodes) {
        //      selectedUnitCodes = JSON.parse(selectedUnitCodes);
        //  }

        // 监听取消按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });
</script>
</body>
</html>