<!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>常规</li>
            <li class="layui-this">预算</li>
        </ul>
        <div class="layui-tab-content">
            <!--常规开始-->
            <div class="layui-tab-item" style="min-height: 550px; margin-bottom: 5px">
                <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: 70px">名称</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="hidden" id="id" name="id" class="layui-input"
                                       th:value="${annualbudget.id}">
                                <input type="text" id="mc" name="mc" class="layui-input" th:value="${annualbudget.mc}"
                                       readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 70px">任务书</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="hidden" id="rws" name="rws" class="layui-input"
                                       th:value="${annualbudget.rws}">
                                <input type="text" id="rwsmc" name="rwsmc" class="layui-input"
                                       th:value="${annualbudget.rwsmc}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 70px">项目执行期</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="text" id="xmzxq" name="xmzxq" class="layui-input" autocomplete="off"
                                       th:value="${annualbudget.xmzxq}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label required" style="width: 70px">年份</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="number" id="nf" name="nf" value="2023" lay-verify="required"
                                       lay-reqtext="年份不能为空" class="layui-input" autocomplete="off"
                                       th:value="${annualbudget.nf}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 70px">说明</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <textarea id="sm" name="sm" class="layui-textarea" style="width: 100%;height: 150px"
                                          th:text="${annualbudget.sm}" readonly></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 70px">申请人</label>
                            <div class="layui-input-inline" style="width: 380px">
                                <input type="text" name="sqrxm" class="layui-input" th:value="${annualbudget.sqrxm}"
                                       readonly>
                            </div>
                            <label class="layui-form-label" style="width: 65px">申请时间</label>
                            <div class="layui-input-inline" style="width: 370px">
                                <input type="text" id="sqsj" name="sqsj" class="layui-input"
                                       th:value="${annualbudget.sqsj}" readonly>
                            </div>
                        </div>
                        <!--                        <div class="layui-form-item layui-inline" style="width: 100%">-->
                        <!--                            <label class="layui-form-label" style="width: 70px">审批状态</label>-->
                        <!--                            <div class="layui-input-inline" style="width: 855px">-->
                        <!--                                <input type="text" id="spzt" name="spzt" class="layui-input" value="未提交" readonly>-->
                        <!--                            </div>-->
                        <!--                        </div>-->
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 70px">可研报告</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="text" id="xmmc" name="xmmc" class="layui-input"
                                       th:value="${annualbudget.xmmc}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 70px">ERP编号</label>
                            <div class="layui-input-inline" style="width: 380px">
                                <input type="text" id="erpbh" name="erpbh" class="layui-input"
                                       th:value="${annualbudget.erpbh}" readonly>
                            </div>
                            <label class="layui-form-label" style="width: 65px">项目类型</label>
                            <div class="layui-input-inline" style="width: 370px">
                                <input type="text" id="xmlxmc" name="xmlxmc" class="layui-input"
                                       th:value="${annualbudget.xmlxmc}" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 90px">电科院负责人</label>
                            <div class="layui-input-inline" style="width: 370px">
                                <input type="text" id="xmfzrxm" name="xmfzrxm" class="layui-input"
                                       th:value="${annualbudget.dkyfzrxm}" readonly>
                            </div>
                            <label class="layui-form-label" style="width: 65px">所属部门</label>
                            <div class="layui-input-inline" style="width: 370px">
                                <input type="text" id="ssbmmc" name="ssbmmc" class="layui-input"
                                       th:value="${annualbudget.ssbmmc}" readonly>
                            </div>
                        </div>

                        <!-- 右侧悬浮按钮 -->
                        <div class="right-bottom-btn" style="margin-bottom: 5px">
                            <button class="layui-btn" lay-filter="viewRwsBtn">
                                <i class="layui-icon layui-icon-file"></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 layui-show" style="min-height: 550px; padding-left: 25px; padding-right: 25px">
                <form class="layui-form" action="" lay-filter="formDemo">
                    <div class="layui-form layuimini-form">
                        <div class="layui-form-item" style="margin-bottom: -10px">
                            <div class="layui-inline">
                                <label class="layui-form-label "
                                       style="width: fit-content; margin-bottom: -10px">单位：万元</label>
                                <label class="layui-form-label" style="width: 100px"></label>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label"
                                       style="width: 120px; margin-left: -10px">[[${ysmc}]]</label>
                                <div class="layui-input-inline" style="width: 100px">
                                    <input type="text" id="rwsys" name="rwsys" class="layui-input" th:value="${rwsys}"
                                           readonly>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width: 60px; margin-left: -15px">年度经费</label>
                                <div class="layui-input-inline" style="width: 100px">
                                    <input type="text" id="ndczje" name="ndczje" class="layui-input"
                                           th:value="${ndczje}" readonly>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width: 70px; margin-left: -15px">年度备用金</label>
                                <div class="layui-input-inline" style="width: 100px; margin-right: -15px">
                                    <input type="number" id="ndbyj" name="ndbyj" class="layui-input" readonly>
                                </div>
                            </div>
                        </div>
                        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                        <!-- 右侧悬浮按钮 -->
                        <div class="right-bottom-btn" style="margin-bottom: 5px">
                            <button class="layui-btn" lay-filter="viewRwsBtn">
                                <i class="layui-icon layui-icon-file"></i>查看任务书
                            </button>
                            <button class="layui-btn" lay-filter="cancleBtn">
                                <i class="layui-icon layui-icon-close"></i>关闭
                            </button>
                        </div>
                    </div>
                </form>
            </div>
            <!--预算结束-->
        </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-v2.6.3/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 = /*[[@{/}]]*/'';
    layui.use(['form', 'treeTable', 'layCascader', 'tableSelect', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            treeTable = layui.treeTable,
            laydate = layui.laydate;

        var rwsId = $("#rws").val(),
            ysId = $("#id").val();
        var zjfy = 0;

        // 监听查看任务书按钮
        $("button[lay-filter='viewRwsBtn']").click(function () {
            parent.layer.open({
                title: '查看项目任务书',
                type: 2,
                shade: 0.3,
                maxmin: true,
                shadeClose: true,
                area: ['1100px', '750px'],
                content: AjaxUtil.ctx + 'prjProject/forView/' + rwsId,
                end: function (index) {
                    layer.close(index);
                }
            });
            return false;
        });

        //预算
        var dataBudget = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjAnnualbudgetdetail/list/' + ysId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                dataBudget = result;
                for (let i = 0; i < dataBudget.length; i++) {
                    if (dataBudget[i].fymc.indexOf('总计') > -1) {
                        zjfy = i;
                    }
                }
                $("#ndbyj").val(dataBudget[zjfy].byj);
                $("#rwsys").val(dataBudget[zjfy].xmzys);

            }
        });

        //经费预算
        var dataBudget1 = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjAnnualbudget/list/' + ysId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                dataBudget1 = result;
            }
        });
        var nf = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjAnnualbudget/getNf/' + ysId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                nf = result;
            }
        });
        var field = "";
        var title = "已执行情况";
        if (nf.length === 1) {
            field = nf[0].title;
            title = title + nf[0].title;
            nf = new Array();
        }
        let fjArray = []
        fjArray = Array.from(dataBudget1, ({fj}) => fj)
        for (var j = 0; j < dataBudget1.length; j++) {
            if (!fjArray.includes(dataBudget1[j].bm)) {
                setFjBudget(dataBudget1[j].fj);
            }
        }
        treeTable.render({
            elem: '#currentTableId',
            data: dataBudget1,
            skin: 'grid',
            tree: {
                treeColIndex: 1,
                treeSpid: "",   //最上级的父级id
                iconIndex: 1,   // 折叠图标显示在第几列
                isPidData: true,// 是否是id、pid形式数据
                idName: 'bm',  // id字段名称
                pidName: 'fj', // pid字段名称
                arrowType: 'arrow2',
                getIcon: 'ew-tree-icon-style2',
                openName: 'bm'
            },
            treeDefaultClose: false,
            page: false,
            // toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: 'numbers', width: '3%', rowspan: 2},
                    {field: 'fymc', width: '30%', title: '费用名称', rowspan: 2},
                    {field: 'xmzys', width: '12%', title: '项目总预算', rowspan: 2},
                    {field: 'yzxqk' + field, width: '15%', align: 'center', title: title, colspan: nf.length},
                    {field: 'sjyzx', width: '12%', title: '实际已执行', rowspan: 2},
                    {field: 'ysje', width: '10%', title: '预算金额', rowspan: 2},
                    {field: 'xmz', width: '9%', title: '项目组', rowspan: 2},
                    {field: "byj", width: '9%', title: '备用金', rowspan: 2},
                    /*{field: 'bndsqje', width: '11%', title: '本年度已申请金额'},
                    {field: 'bndbzje', width: '11%', title: '本年度已报账金额'},*/
                    // {field: 'byjysq', width: '11%', title: '备用金已申请'},
                    // {field: 'byjybz', width: '11%', title: '备用金已报账'}
                ], nf
            ],
            done: function () {
            }
        });

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

        //父级预算
        function setFjBudget(fj) {
            if (fj != null && fj != '') {
                var xmzys = 0, xmz = 0, byj = 0, xtjsyzx = 0, sjyzx = 0, bndsqje = 0, bndbzje = 0;
                //子节点的值相加
                for (var j = 0; j < dataBudget1.length; j++) {
                    if (fj == dataBudget1[j].fj) {
                        var t1 = parseFloat(dataBudget1[j].xmz);
                        if (!isNaN(t1))
                            xmz += t1;
                        var t2 = parseFloat(dataBudget1[j].byj);
                        if (!isNaN(t2))
                            byj += t2;
                        var t3 = parseFloat(dataBudget1[j].xtjsyzx);
                        if (!isNaN(t3))
                            xtjsyzx += t3;
                        var t4 = parseFloat(dataBudget1[j].sjyzx);
                        if (!isNaN(t4))
                            sjyzx += t4;
                        var t5 = parseFloat(dataBudget1[j].bndsqje);
                        if (!isNaN(t5))
                            bndsqje += t5;
                        var t6 = parseFloat(dataBudget1[j].bndbzje);
                        if (!isNaN(t6))
                            bndbzje += t6;
                    }
                }
                //更新父节点的值
                for (var j = 0; j < dataBudget1.length; j++) {
                    if (fj == dataBudget1[j].bm) {
                        if (xmz == 0)
                            dataBudget1[j].xmz = '';
                        else
                            dataBudget1[j].xmz = xmz.toFixed(2);
                        if (xtjsyzx == 0)
                            dataBudget1[j].xtjsyzx = '';
                        else
                            dataBudget1[j].xtjsyzx = xtjsyzx.toFixed(2);
                        if (sjyzx == 0)
                            dataBudget1[j].sjyzx = '';
                        else
                            dataBudget1[j].sjyzx = sjyzx.toFixed(2);

                        var ysje = xmz + byj;
                        if (ysje == 0)
                            dataBudget1[j].ysje = "";
                        else
                            dataBudget1[j].ysje = ysje.toFixed(2);

                        if (bndsqje == 0)
                            dataBudget1[j].bndsqje = "";
                        else
                            dataBudget1[j].bndsqje = bndsqje.toFixed(2);

                        if (bndbzje == 0)
                            dataBudget1[j].bndbzje = "";
                        else
                            dataBudget1[j].bndbzje = bndbzje.toFixed(2);
                        setFjBudget(dataBudget1[j].fj);
                        break;
                    }
                }
            }
        }
    });
</script>
</body>
</html>