<!DOCTYPE html>
<html lang="en" 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">
</head>
<style>
    .table-container {
        display: flex;
        justify-content: space-between;
    }
    .table-wrapper {
        width: 48%;
    }
    .highlight {
        background-color: yellow !important;
    }
</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" action="" lay-filter="formDemo">
            <div class="table-container">
                <div class="table-wrapper">
                    <h1>新数据</h1>
                    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </div>
                <div class="table-wrapper">
                    <h1>旧数据</h1>
                    <table class="layui-hide" id="currentTableId1" lay-filter="currentTableFilter"></table>
                </div>
            </div>
            <!-- 右侧悬浮按钮 -->
            <div class="layui-form layuimini-form" style="padding-bottom: 100px">
                <div class="right-bottom-btn">
                    <button class="layui-btn" lay-filter="cancleBtn">
                        <i class="layui-icon layui-icon-close"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<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 th:src="@{/lib/xm-select-v1.2.2/xm-select.js}" charset="utf-8"></script>
<script th:inline="javascript" type="text/javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            table = layui.table;

        var prjAnnualbudgetRecord = [[${prjAnnualbudgetRecord}]];
        var prjAnnualbudget = [[${prjAnnualbudget}]];
        var ysId = [[${ysId}]];
        form.render();
        //经费预算
        var dataBudget1 = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjAnnualbudget/list_xg/' + ysId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                dataBudget1 = result;
                console.log(dataBudget1)
            }
        });
        var nf = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjAnnualbudget/getNf/' + ysId,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                nf = result;
                console.log(nf)
            }
        });
        var yzxqkcols = new Array();
        var field = "";
        var title = "已执行情况";
        if (nf.length === 1) {
            field = nf[0].title;
            title = title + nf[0].title;
            yzxqkcols.push(
                {field:'yzxqk'+field,width:'15%',title:title}
            )
            nf = new Array();
        }
        if (nf.length>1){
            for (let i = 0; i < nf.length; i++) {
                title = "已执行情况";
                field = nf[i].title;
                title=title+nf[i].title;
            yzxqkcols.push(
                {field:'yzxqk'+field,width:'15%',title:title}
            )
            }
        }
        console.log(yzxqkcols)
        var newcols = [
            {type: 'numbers', align: 'center',rowspan: 2},
            {field: 'fymc', width: '30%', title: '费用名称',rowspan: 2},
            {field: 'xmzys', width: '16%', title: '项目总预算',rowspan: 2},
            {field: 'ysje', width: '16%', title: '预算金额',rowspan: 2},
            {field: 'xmz', width: '16%', title: '项目组',rowspan: 2},
            {field: 'byj', width: '16%', title: '备用金',rowspan: 2},
        ];
        var oldcols = [
            {type: 'numbers', align: 'center',rowspan: 2},
            {field: 'fymc', width: '30%', title: '费用名称',rowspan: 2},
            {field: 'xmzys', width: '16%', title: '项目总预算',rowspan: 2},
            {field: 'ysje', width: '16%', title: '预算金额',rowspan: 2},
            {field: 'xmz', width: '16%', title: '项目组',rowspan: 2},
            {field: 'byj', width: '10%', title: '备用金',rowspan: 1},
        ].concat(yzxqkcols);

        var currTable = table.render({
            elem: '#currentTableId',
            data: prjAnnualbudgetRecord,
            method: 'post',
            cols: [newcols],
            autoSort: false,
            limits: [20, 25, 50, 100],
            limit: 30,
            maxmin: true,
            page: true,
            skin: 'grid',
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.total,
                    "data": res.data.rows,
                };
            },
            done: function(res, curr, count) {
                    compareDataAndHighlight();
            }
        });

        var prevTable = table.render({
            elem: '#currentTableId1',
            data: dataBudget1,
            method: 'post',
            cols: [oldcols],
            autoSort: false,
            limits: [20, 25, 50, 100],
            limit: 30,
            maxmin: true,
            page: true,
            skin: 'grid',
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.total,
                    "data": res.data.rows,
                };
            },
            done: function(res, curr, count) {
                    compareDataAndHighlight();
            }
        });
        // 新旧数据区别高亮显示
        function compareDataAndHighlight() {
            if (prjAnnualbudget.length === 0 || prjAnnualbudgetRecord.length === 0) {
                return;
            }
            var fieldsToCompare = ['ysje', 'xmz', 'byj']; // 比较的字段

            for (var i = 0; i < prjAnnualbudget.length; i++) {
                var currentRow = prjAnnualbudget[i];
                var previousRow = prjAnnualbudgetRecord[i];
                if (previousRow) {
                    fieldsToCompare.forEach(function(field) {
                        // 将 null 值替换为 0 进行比较
                        var currentValue = currentRow[field] === null ? 0 : currentRow[field];
                        var previousValue = previousRow[field] === null ? 0 : previousRow[field];
                        if (currentValue !== previousValue) {                            // 不同的数据进行高亮处理
                            var newCell = document.querySelector(`#currentTableId + div .layui-table-body tr[data-index="${i}"] td[data-field="${field}"]`);
                            if (newCell) {
                                newCell.classList.add('highlight');
                            }
                            var oldCell = document.querySelector(`#currentTableId1 + div .layui-table-body tr[data-index="${i}"] td[data-field="${field}"]`);
                            if (oldCell) {
                                oldCell.classList.add('highlight');
                            }
                        }
                    });
                }
            }
        }

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