<!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 style="">新数据</h1>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </div>
                <div class="table-wrapper">
                    <h1 style="">旧数据</h1>
                    <table class="layui-hide" id="currentTableId1" lay-filter="currentTableFilter"></table>
                </div>
            </div>
            <div class="table-container">
                <div class="table-wrapper">
                    <table id="newDataTable" lay-filter="newDataFilter"></table>
                </div>
                <div class="table-wrapper">
                    <table id="oldDataTable" lay-filter="oldDataFilter"></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', 'treeTable', 'tableSelect', 'laydate','table'], function () {

        var form = layui.form,
            table = layui.table;
        var prjBudget = [[${prjBudget}]];
        var prjBudgetRecord = [[${prjBudgetRecord}]];

        form.render();
        var dataCompany = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjCompletecompany/list/' + prjBudgetRecord[0].xm,
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                dataCompany = result.data.rows;
            }
        });
        var dynamicColumns = [];
        for (var i = 0; i < dataCompany.length; i++) {
            dynamicColumns.push({field: "dw" + (i + 1), width: '10%', title: dataCompany[i].dwmc + '(万元)'})
            if (i >= 9)
                break;
        }
        var newcols =[
            {type: 'numbers', align: 'center'},
            {field: 'mc', width: '30%', title: '费用名称'},
            {field: 'ysje', width: '16%', title: '预算金额'},
        ].concat(dynamicColumns)
        var currTable = table.render({
            elem: '#currentTableId',
            data: prjBudgetRecord,
            method: 'post',
            cols:[newcols],
            autoSort: false,
            limits: [20, 25, 50, 100],
            limit: 30,
            maxmin:true,
            page: true,
            skin: 'grid',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows, //解析数据列表
                };
            },
            done: function(res, curr, count) {
                compareDataAndHighlight();
            }
        });
        var currTable = table.render({
            elem: '#currentTableId1',
            data: prjBudget,
            method: 'post',
            cols:[newcols],
            autoSort: false,
            limits: [20, 25, 50, 100],
            limit: 30,
            maxmin:true,
            page: true,
            skin: 'grid',
            parseData: function (res) { //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 (prjBudget.length === 0 || prjBudgetRecord.length === 0) {
                return;
            }
            var fieldsToCompare = Object.keys(prjBudget[0]);
            for (var i = 0; i < prjBudget.length; i++) {
                var currentRow = prjBudget[i];
                var previousRow = prjBudgetRecord[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>