<!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-tab layui-tab-card">
        <ul class="layui-tab-title">
            <li class="layui-this">常规</li>

        </ul>
        <div class="layui-tab-content">
            <!--常规开始-->
            <div class="layui-tab-item layui-show" style="min-height: 500px">
                <form class="layui-form" action="" lay-filter="formDemo">
                    <div class="layui-form layuimini-form" style="padding-bottom: 100px">
                        <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: 350px">
                                <input type="hidden" name="xm" id="xm" class="layui-input" th:value="${subtopic.xm}">
                                <input type="hidden" name="id" id="id" class="layui-input" th:value="${subtopic.id}">
                                <input type="text" id="ktmc" name="ktmc" lay-verify="required" lay-reqtext="课题名称不能为空"
                                       autocomplete="off" class="layui-input" th:value="${subtopic.ktmc}" readonly>
                            </div>
<!--                            <label class="layui-form-label " style="width: 110px">序号</label>
                            <div class="layui-input-inline" style="width: 350px">
                                <input type="number" id="xh" name="xh" autocomplete="off" class="layui-input"
                                       th:value="${subtopic.xh}">
                            </div>-->
                        </div>
                        <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: 350px">
                                <input type="hidden" id="fzr" name="fzr" class="layui-input" th:value="${subtopic.fzr}">
                                <input type="text" id="fzrxm" name="fzrxm" class="layui-input"
                                       th:value="${subtopic.fzrxm}" readonly>
                            </div>
                            <label class="layui-form-label" style="width: 110px">负责人所在单位</label>
                            <div class="layui-input-inline" style="width: 350px">
                                <input type="hidden" id="fzrszdw" name="fzrszdw" class="layui-input"
                                       th:value="${subtopic.fzrszdw}">
                                <input type="text" id="fzrszdwmc" name="fzrszdwmc" class="layui-input"
                                       th:value="${subtopic.fzrszdwmc}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 85px">负责人2</label>
                            <div class="layui-input-inline" style="width: 350px">
                                <input type="hidden" id="fzr2" name="fzr2" class="layui-input"
                                       th:value="${subtopic.fzr2}">
                                <input type="text" id="fzr2xm" name="fzr2xm" lay-filter="fzr" class="layui-input"
                                       th:value="${subtopic.fzr2xm}" readonly>
                            </div>
                            <label class="layui-form-label" style="width: 110px">负责人2所在单位</label>
                            <div class="layui-input-inline" style="width: 350px">
                                <input type="hidden" id="fzr2szdw" name="fzr2szdw" class="layui-input"
                                       th:value="${subtopic.fzr2szdw}">
                                <input type="text" id="fzr2szdwmc" name="fzr2szdwmc" class="layui-input"
                                       th:value="${subtopic.fzr2szdwmc}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 85px">单位分工</label>
                            <div class="layuimini-container" style="width: 850px; margin-left: 113px">
                                <div class="layui-btn-group" style="margin-bottom: -8px">
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subAdd1"><i class="layui-icon"></i>新建
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subEdit1"><i class="layui-icon"></i>编辑
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subView1"><i class="layui-icon">&#xe63c;</i>查看
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subDelete1"><i class="layui-icon"></i>删除
                                    </button>
                                </div>
                                <table class="layui-hide" id="currentTableId1" lay-filter="currentTableFilter1"
                                       style="padding-top: 0px;margin-top: 0px"></table>
                                <label>如果有2个或2个以上单位完成请填入此表</label>
                            </div>
                        </div>
                        <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: 850px">
                                <textarea id="zyyjnr" name="zyyjnr" class="layui-textarea"
                                          style="width: 100%;height: 100px" th:text="${subtopic.zyyjnr}"></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: 350px">
                                <input type="number" id="jf" name="jf" class="layui-input" th:value="${subtopic.jf}"
                                       lay-verify="required" lay-reqtext="经费不能为空" readonly>
                            </div>
                            <label class="layui-form-label required" style="width: 110px">起止时间</label>
                            <div class="layui-input-inline" style="width: 162px">
                                <input type="text" name="qssj" id="qssj" placeholder="开始日期"
                                       lay-reqtext="开始日期不能为空" autocomplete="off" class="layui-input"
                                       th:value="${subtopic.qssj}">
                                <tip>日期格式：yyyy-MM-dd</tip>
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline" style="width: 162px">
                                <input type="text" name="jssj" id="jssj" placeholder="结束日期"
                                       lay-reqtext="结束日期不能为空" autocomplete="off" class="layui-input"
                                       th:value="${subtopic.jssj}">
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label required" style="width: 100px">子课题研究计划</label>
                            <div class="layuimini-container" style="width: 850px; margin-left: 113px">
                                <div class="layui-btn-group" style="margin-bottom: -8px">
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subAdd2"><i class="layui-icon"></i>新建
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subEdit2"><i class="layui-icon"></i>编辑
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subView2"><i class="layui-icon">&#xe63c;</i>查看
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subDelete2"><i class="layui-icon"></i>删除
                                    </button>
                                </div>
                                <table class="layui-hide" id="currentTableId2" lay-filter="currentTableFilter2"></table>
                            </div>
                        </div>
                        <!-- 右侧悬浮按钮 -->
                        <div class="right-bottom-btn">
                            <button class="layui-btn" lay-submit lay-filter="saveBtn1">
                                <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>
            </div>
            <!--常规结束-->
            <!--计划开始-->
            <!--            <div class="layui-tab-item"  style="height: 500px; padding-left: 20px; padding-right: 20px">-->
            <!--                <form class="layui-form" action="" lay-filter="formDemo">-->
            <!--                    <div class="layuimini-form" style="padding-bottom: 100px">-->
            <!--                        <div class="layui-btn-group" style="margin-bottom: -8px">-->
            <!--                            <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subAdd2"><i class="layui-icon"></i>新建</button>-->
            <!--                            <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subEdit2"><i class="layui-icon"></i>编辑</button>-->
            <!--                            <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subView2"><i class="layui-icon">&#xe63c;</i>查看</button>-->
            <!--                            <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subDelete2"><i class="layui-icon"></i>删除</button>-->
            <!--                        </div>-->
            <!--                        <table class="layui-hide" id="currentTableId2" lay-filter="currentTableFilter2"></table>-->

            <!--                        &lt;!&ndash; 右侧悬浮按钮 &ndash;&gt;-->
            <!--                        <div class="right-bottom-btn">-->
            <!--                            <button class="layui-btn" lay-submit lay-filter="saveBtn2">-->
            <!--                                <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>-->
            <!--            </div>-->
            <!--            &lt;!&ndash;计划结束&ndash;&gt;-->
        </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="@{/lib/xm-select-v1.2.2/xm-select.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 = /*[[@{/}]]*/'';
    var fgCompany = [];//当前分工单位
    var subtopicPlan = [];//当前子课题计划
    var updateflag = 0;


    layui.use(['form', 'table', 'tableSelect', 'laydate'], function () {
        var form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            layer = layui.layer,
            tableSelect = layui.tableSelect;
        var qsrq = [[${qsrq}]]
        var wcrq = [[${wcrq}]]

        var xmId = $("#xm").val();//项目任务书ID
        var zktId = $("#id").val();//子课题ID

        //日期
        laydate.render({
            elem: '#qssj',
            done: function (value, date, endDate) {
                if (value<qsrq){
                    alert("开始时间不能早于任务书执行周期!!")
                    $("#qssj").val("");
                    return;
                }
                var jssj = $("#jssj").val();
                if (jssj != ''||jssj!= null){
                    if (value>jssj){
                        alert('开始日期必须小于结束日期');
                        $("#qssj").val("");
                    }
                }
            }
        });
        laydate.render({
            elem: '#jssj',
            done: function (value, date, endDate) {
                if (value>wcrq){
                    alert("结束时间不能晚于任务书执行周期!!")
                    $("#jssj").val("");
                    return;
                }
                var qssj = $("#qssj").val();
                if (qssj != ''||qssj!= null){
                    if (value<qssj){
                        alert('结束日期必须大于开始日期!!');
                        $("#jssj").val("");
                        return;
                    }
                }

            }
        });

        //负责人
        tableSelect.render({
            elem: '#fzrxm',
            checkedKey: 'id',
            searchKey: 'ryxm',
            searchPlaceholder: '搜索姓名',
            table: {
                url: AjaxUtil.ctx + 'prjMember/rylist/' + xmId,
                cols: [
                    [
                        {type: "radio", fixed: 'left',title: '😊'},
                        {type: 'numbers', align: 'center',title: '序号'},
                        {field: 'ryxm', width: '15%', title: '姓名', align: 'left'},
                        {field: 'dwmc', title: '公司/单位',width: '60%'}
                    ]
                ],
                done: function (res, curr, count) {

                },
                page: true
            },
            done: function (elem, data) {
                var NEWJSON = [];
                var NEWJSON1 = [];
                var NEWJSON2 = [];
                var NEWJSON3 = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.ryxm);
                    NEWJSON1.push(item.ry);
                    NEWJSON2.push(item.dw);
                    NEWJSON3.push(item.dwmc);
                })
                elem.val(NEWJSON.join(","));
                $("#fzr").val(NEWJSON1.join(","));
                $("#fzrszdw").val(NEWJSON2.join(","));
                $("#fzrszdwmc").val(NEWJSON3.join(","));
            }
        })
        //负责人2
        tableSelect.render({
            elem: '#fzr2xm',
            checkedKey: 'id',
            searchKey: 'ryxm',
            searchPlaceholder: '搜索姓名',
            table: {
                url: AjaxUtil.ctx + 'prjMember/rylist/' + xmId,
                cols: [
                    [
                        {type: "radio", fixed: 'left',title: '😊'},
                        {type: 'numbers', align: 'center',title: '序号'},
                        {field: 'ryxm', width: '15%', title: '姓名', align: 'left'},
                        {field: 'dwmc', title: '公司/单位',width: '60%'}
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = [];
                var NEWJSON1 = [];
                var NEWJSON2 = [];
                var NEWJSON3 = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.ryxm);
                    NEWJSON1.push(item.ry);
                    NEWJSON2.push(item.dw);
                    NEWJSON3.push(item.dwmc);
                })
                elem.val(NEWJSON.join(","));
                $("#fzr2").val(NEWJSON1.join(","));
                $("#fzr2szdw").val(NEWJSON2.join(","));
                $("#fzr2szdwmc").val(NEWJSON3.join(","));
            }
        })

        // 用于存放表格数据
        var dataCompany = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjDivisioncompany/list/' + zktId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                dataCompany = result.data.rows;
            }
        });
        //单位分工gst
        table.render({
            elem: '#currentTableId1',
            data: dataCompany,
            cols: [
                [
                    {type: "checkbox"},
                    {type: 'numbers', title: '序号', width: '8%', align: 'center'},
                    {field: 'dwmc', title: '单位', width: '30%'},
                    {field: 'zyyjnr', title: '主要研究内容', width: '40%'},
                    {field: 'jf', title: '经费(万元)'}
                ]
            ],
            autoSort: true,
            limits: [10, 15, 20, 25, 50, 100],
            limit: Number.MAX_VALUE,
            page: true,
            initSort: {
                field: 'xh', //排序字段
                type: 'asc' //排序方式:desc 降序,asc 升序
            }
        });
        // 监听新建按钮（单位分工）
        $("button[lay-filter='subAdd1']").click(function () {
            layer.open({
                title: '新建分工单位',
                type: 2,
                // maxmin: true,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'prjDivisioncompany/forAdd/' + xmId,
                end: function (index) {
                    if (updateflag == 1) {
                        updateflag = 0;
                        var oldData = table.cache["currentTableId1"];

                        // var items = fgCompanies; // fgCompanies是从弹出层获取的多选单位数据
                        // for (var i = 0; i < items.length; i++) {
                        var item = {
                            "xh": fgCompany.xh,
                            "dw": fgCompany.dw,
                            "dwmc": fgCompany.dwmc,
                            "dwqc": fgCompany.dwqc,
                            "zyyjnr": fgCompany.zyyjnr,
                            "jf": fgCompany.jf
                            // "xh": items[i].xh,
                            // "dw": items[i].dw,
                            // "dwmc": items[i].dwmc,
                            // "dwqc": items[i].dwqc,
                            // "zyyjnr": items[i].zyyjnr,
                            // "jf": items[i].jf
                        };
                        //刷新表格
                        oldData.push(item);
                        // }
                        table.reload('currentTableId1', {
                            data: oldData
                        });

                        //经费计算
                        var jf = 0;
                        for (let item of oldData) {
                            jf += parseFloat(item.jf);
                        }
                        $("#jf").val(jf);
                    }
                }
            });
        });
        // 监听编辑按钮(单位分工）
        $("button[lay-filter='subEdit1']").click(function () {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要编辑的单位！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行编辑操作！", 1500);
            } else {
                fgCompany = {
                    "xh": data[0].xh,
                    "zkt": data[0].zkt,
                    "dw": data[0].dw,
                    "dwmc": data[0].dwmc,
                    "dwqc": data[0].dwqc,
                    "zyyjnr": data[0].zyyjnr,
                    "jf": data[0].jf
                };

                layer.open({
                    title: '编辑分工单位',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjDivisioncompany/forUpdate',
                    end: function (index) {

                        if (updateflag == 1) {
                            updateflag = 0;
                            var oldData = table.cache["currentTableId1"];

                            var currCompany = {
                                "xh": fgCompany.xh,
                                "dw": fgCompany.dw,
                                "dwmc": fgCompany.dwmc,
                                "dwqc": fgCompany.dwqc,
                                "zyyjnr": fgCompany.zyyjnr,
                                "jf": fgCompany.jf
                            };

                            for (var i = 0; i < oldData.length; i++) {
                                var item = oldData[i];
                                if (item.LAY_CHECKED) {//条件：选中
                                    // oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                                    oldData[i] = currCompany;
                                    break;
                                }
                            }
                            //刷新表格
                            var currData = oldData;
                            // currData.push(currCompany);
                            table.reload('currentTableId1', {
                                data: currData
                            });

                            //经费计算
                            var jf = 0;
                            for (let item of currData) {
                                jf += parseFloat(item.jf);
                            }
                            $("#jf").val(jf);
                        }
                    }
                });
            }
        });
        // 监听查看按钮(单位分工）
        $("button[lay-filter='subView1']").click(function () {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要浏览的单位！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行浏览！", 1500);
            } else {
                fgCompany = {
                    "xh": data[0].xh,
                    "dwmc": data[0].dwmc,
                    "dwqc": data[0].dwqc,
                    "zyyjnr": data[0].zyyjnr,
                    "jf": data[0].jf
                };

                layer.open({
                    title: '浏览分工单位',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjDivisioncompany/forView',
                    end: function (index) {
                        layer.close(index);
                    }
                });
            }
        });
        // 监听删除按钮（单位分工）
        $("button[lay-filter='subDelete1']").click(function () {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的单位！", 1000);
                return;
            }
            var oldData = table.cache["currentTableId1"];
            for (var i = 0; i < oldData.length; i++) {
                var item = oldData[i];
                if (item.LAY_CHECKED) {//条件：选中
                    oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            var currData = oldData;
            table.reload('currentTableId1', {
                data: currData
            });

            //经费计算
            if (currData.length == 0)
                $("#jf").val('');
            else {
                var jf = 0;
                for (let item of currData) {
                    jf += parseFloat(item.jf);
                }
                $("#jf").val(jf);
            }
        });
        //双击(单位分工)
        table.on('rowDouble(currentTableFilter1)', function (obj) {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要编辑的单位！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行编辑操作！", 1500);
            } else {
                fgCompany = {
                    "xh": data[0].xh,
                    "zkt": data[0].zkt,
                    "dw": data[0].dw,
                    "dwmc": data[0].dwmc,
                    "dwqc": data[0].dwqc,
                    "zyyjnr": data[0].zyyjnr,
                    "jf": data[0].jf
                };

                layer.open({
                    title: '编辑分工单位',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjDivisioncompany/forUpdate',
                    end: function (index) {

                        if (updateflag == 1) {
                            updateflag = 0;
                            var oldData = table.cache["currentTableId1"];

                            var currCompany = {
                                "xh": fgCompany.xh,
                                "dw": fgCompany.dw,
                                "dwmc": fgCompany.dwmc,
                                "dwqc": fgCompany.dwqc,
                                "zyyjnr": fgCompany.zyyjnr,
                                "jf": fgCompany.jf
                            };

                            for (var i = 0; i < oldData.length; i++) {
                                var item = oldData[i];
                                if (item.LAY_CHECKED) {//条件：选中
                                    // oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                                    oldData[i] = currCompany;
                                    break;
                                }
                            }
                            //刷新表格
                            var currData = oldData;
                            // currData.push(currCompany);
                            table.reload('currentTableId1', {
                                data: currData
                            });

                            //经费计算
                            var jf = 0;
                            for (let item of currData) {
                                jf += parseFloat(item.jf);
                            }
                            $("#jf").val(jf);
                        }
                    }
                });
            }
            /*            var data = obj.data;
            fgCompany = {
                "xh": data.xh,
                "dwmc": data.dwmc,
                "dwqc": data.dwqc,
                "zyyjnr": data.zyyjnr,
                "jf": data.jf
            };

            layer.open({
                title: '浏览分工单位',
                type: 2,
                // maxmin: true,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'prjDivisioncompany/forView',
                end: function (index) {
                    layer.close(index);
                }
            });*/
        });
        //存储表格内容
        var dateTable = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjSubtopicplan/list/' + zktId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                dateTable = result.data.rows;
            }
        });
        // 子课题计划
        table.render({
            elem: '#currentTableId2',
            data: dateTable,
            cols: [
                [
                    {type: "checkbox"},
                    {type: 'numbers', align: 'center',title: '😊'},
                    {field: 'yjnr', title: '研究内容', width: '35%'},
                    {field: 'khzb', title: '考核指标', width: '25%'},
                    {field: 'qssj', title: '起始时间', width: '13%', align: 'center'},
                    {field: 'jssj', title: '结束时间', width: '13%', align: 'center'}
                ]
            ],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        });
        // 监听新建按钮（子课题计划）
        $("button[lay-filter='subAdd2']").click(function () {
            // if(zktId == -1) {
            //     // Message.error("请先创建子课题并保存",2000);
            // } else {
            subtopicPlan = {
                "zktmc": $("#ktmc").val()
            };
            layer.open({
                title: '新建子课题计划',
                type: 2,
                // maxmin: true,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'prjSubtopicplan/forAdd',
                end: function (index) {
                    if (updateflag == 1) {
                        updateflag = 0;
                        var oldData = table.cache["currentTableId2"];
                        oldData.push({
                            "zktmc": $("#ktmc").val(),
                            "yjnr": subtopicPlan.yjnr,
                            "khzb": subtopicPlan.khzb,
                            "qssj": subtopicPlan.qssj,
                            "jssj": subtopicPlan.jssj
                        });

                        table.reload('currentTableId2', {
                            data: oldData
                        });
                    }
                }
            });
        });
        // 监听编辑按钮(子课题计划）
        $("button[lay-filter='subEdit2']").click(function () {
            var plans = table.cache["currentTableId2"];
            if (plans.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId2')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要编辑的计划！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行编辑操作！", 1500);
            } else {
                subtopicPlan = {
                    "zktmc": $("#ktmc").val(),
                    "yjnr": data[0].yjnr,
                    "khzb": data[0].khzb,
                    "qssj": data[0].qssj,
                    "jssj": data[0].jssj
                };

                layer.open({
                    title: '编辑子课题计划',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjSubtopicplan/forUpdate',
                    end: function (index) {
                        if (updateflag == 1) {
                            updateflag = 0;
                            var oldData = table.cache["currentTableId2"];

                            var currPlan = {
                                "zktmc": $("#ktmc").val(),
                                "yjnr": subtopicPlan.yjnr,
                                "khzb": subtopicPlan.khzb,
                                "qssj": subtopicPlan.qssj,
                                "jssj": subtopicPlan.jssj
                            };
                            for (var i = 0; i < oldData.length; i++) {
                                var item = oldData[i];
                                if (item.LAY_CHECKED) {//条件：选中
                                    oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                                    break;
                                }
                            }
                            //刷新表格
                            var currData = oldData;
                            currData.push(currPlan);
                            table.reload('currentTableId2', {
                                data: currData
                            });
                        }
                    }
                });
            }
        });
        // 监听查看按钮(子课题计划）
        $("button[lay-filter='subView2']").click(function () {
            var plans = table.cache["currentTableId2"];
            if (plans.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId2')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要浏览的计划！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行浏览！", 1500);
            } else {
                subtopicPlan = {
                    "zktmc": $("#ktmc").val(),
                    "yjnr": data[0].yjnr,
                    "khzb": data[0].khzb,
                    "qssj": data[0].qssj,
                    "jssj": data[0].jssj
                };

                layer.open({
                    title: '浏览子课题计划',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjSubtopicplan/forView'
                });
            }
        });
        // 监听删除按钮（子课题计划）
        $("button[lay-filter='subDelete2']").click(function () {
            var plans = table.cache["currentTableId2"];
            if (plans.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId2')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的计划！", 1000);
                return;
            }

            var oldData = table.cache["currentTableId2"];
            for (var i = 0; i < oldData.length; i++) {
                var item = oldData[i];
                if (item.LAY_CHECKED) {//条件：选中
                    oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            var currData = oldData;
            table.reload('currentTableId2', {
                data: currData
            });
        });
        // 监听确认按钮（基本信息）
        form.on('submit(saveBtn1)', function (data) {
            var url = AjaxUtil.ctx;
            if (zktId == -1)
                url += "prjSubtopic/doAdd";
            else
                url += "prjSubtopic/doUpdate";

            //基本信息
            var index = layer.load(0, {shade: 0.1});
            AjaxUtil.post({
                url: url,
                data: data.field,
                success: function (res) {
                    layer.close(index);
                    if (res.code === 0) {
                        Message.success(1000, res.message, function () {
                            zktId = res.data.id;
                            $("#id").val(zktId);
                            parent.setUpdateFlag();

                            //单位分工
                            var companys = [];
                            var oldCompanys = table.cache["currentTableId1"];
                            for (const item of oldCompanys) {
                                var company = {
                                    "xh": item.xh,
                                    "zkt": zktId,
                                    "dw": item.dw,
                                    "dwmc": item.dwmc,
                                    "zyyjnr": item.zyyjnr,
                                    "jf": item.jf
                                };
                                companys.push(company);
                            }
                            AjaxUtil.post({
                                url: AjaxUtil.ctx + "prjDivisioncompany/batchInsert/" + zktId,
                                contentType: "application/json",
                                data: JSON.stringify(companys),
                                success: function (res) {
                                    if (res.code === 0) {
                                        // Message.success(1000, res.message, function () {
                                        //
                                        // });
                                    } else {
                                        // Message.error(res.message, 1000);
                                    }
                                },
                                error: function (error) {
                                }
                            });
                            //子课题计划
                            var plans = [];
                            var oldPlans = table.cache["currentTableId2"];
                            for (let item of oldPlans) {
                                var plan = {
                                    "zkt": zktId,
                                    "yjnr": item.yjnr,
                                    "khzb": item.khzb,
                                    "qssj": item.qssj,
                                    "jssj": item.jssj
                                };
                                plans.push(plan);
                            }
                            AjaxUtil.post({
                                url: AjaxUtil.ctx + "prjSubtopicplan/batchInsert/" + zktId,
                                contentType: "application/json",
                                data: JSON.stringify(plans),
                                success: function (res) {
                                    if (res.code === 0) {
                                        Message.success(1000, res.message, function () {
                                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                                            parent.layer.close(iframeIndex);
                                        });
                                    } else {
                                        Message.error(res.message, 1000);
                                    }
                                    parent.setUpdateFlag();
                                },
                                error: function (error) {
                                }
                            });

                            //关闭当前层
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        Message.error(res.message, 1000);
                    }
                },
                error: function (error) {
                    layer.close(index);
                }
            });

            return false;
        });

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

    //设置更新标志
    function setUpdateFlag() {
        updateflag = 1;
    }

    //获取分工单位
    function getFgCompany() {
        return fgCompany;
    }

    //设置分工单位
    function setFgCompany(company) {
        fgCompany = company;
    }

    function getSubtopicPlan() {
        return subtopicPlan;
    }

    function setSubtopicPlan(plan) {
        subtopicPlan = plan;
    }
</script>
</body>
</html>