<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>对账记录查询</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/common.css}">
    <!-- 引入组件库 -->
    <script th:src="@{/layui/jquery-3.4.1.min.js}"></script>
    <script th:src="@{/layui/layui.js}"></script>
    <script th:src="@{/layui/lay/xmSelect/xm-select.js}"></script>
    <script th:src="@{/js/common.js}"></script>

    <style>
        body {
            background: #f2f2f2
        }

        #boxDiv {
            background: #ffffff
        }

        #titleDiv {
            overflow: visible;
        }

        .content {
            padding: 0 8px;
        }

        .tableTitle {
            height: 20px;
            margin-top: 10px;
            margin-bottom: 10px;
        }

        .tableTitle > * {
            height: 20px;
            line-height: 20px;
            display: inline-block;
            margin-right: 5px;
        }

        .tableName {
            border-left: 3px solid #0000FF;
            padding-left: 5px;
            font-size: 16px;
            font-weight: bold;
        }

        .selectDate {
            font-size: 14px;
            color: rgba(0, 0, 0, 0.65)
        }
    </style>
</head>
<body class="layui-layout-body">
<div id="boxDiv" style="left:8px;right: 8px;bottom:8px;top:8px;position:absolute;">
    <div class="toolbar" id="titleDiv">
        <div style="display: inline-block;">
            <form class="layui-form">

                <div class="layui-inline">
                    <label class="layui-form-label">日期选择</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input formWidthTwo" id="searchDate" placeholder=" ~ "
                               th:value="${startTime+' ~ '+endTime}">
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm layui-btn-normal" data-type="search" id="search" onclick="search()"><i
                    class="layui-icon layui-icon-search"></i>查询
            </button>
            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="exportExcel()"><i class="layui-icon">&#xe67d;</i>导出
            </button>
        </div>
    </div>
    <div class="content">
        <div class="tableTitle">
            <span class="tableName">汇总</span>
            <span class="selectDate">&nbsp;</span>
        </div>
        <table id="demo" lay-filter="test"></table>
    </div>

</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="getInfo">详细信息</a>
</script>
</body>
<script th:inline="javascript">
    let layer, laydate, table, tree, form;

    layui.use(['element', 'table', 'laydate', 'layer', 'form', 'tree'], function () {
        layer = layui.layer;
        table = layui.table;
        laydate = layui.laydate;
        tree = layui.tree;
        form = layui.form;

        //表格加载
        table.render({
            elem: '#demo',
            height: 'full-' + ($(".toolbar").height() + 60),
            title: 'HIS账单日志记录',
            page: true,//开启分页
            limit: 20,
            limits: [20, 30, 50],
            //toolbar: '', //开启工具栏，此处显示默认图标，可以自定义模板
            defaultToolbar: [],
            id: 'test',
            //skin:'line',
            even: 'true',
            //size:'lg', 默认普通尺寸 sm 小 lg 大
            cols: [
                [
                    // {field:'AREA',title: '院区', align:'center',rowspan:2,width:120,totalRowText:'合计'},
                    {
                        field: 'TRADE_DATE', 
                        title: '日期', 
                        align: 'center', 
                        rowspan: 2, 
                        width: 120, 
                        totalRowText: '合计',
                        templet: function(d) {
                            // Layui合计行的特征：TOTAL_NUMS字段存在
                            if (d.TOTAL_NUMS !== undefined) {
                                return '<span style="font-weight: bold; color: #333;">合计</span>';
                            }
                            return d.TRADE_DATE || '';
                        }
                    },
                    {title: '结算金额', align: 'center', colspan: 4},
                    {title: '单边账', align: 'center', colspan: 2},
                    {title: '对账情况', align: 'center', colspan: 2},
                    {title: '对账详情', align: 'center', rowspan: 2, width: 160, toolbar: '#barDemo'},
                ],
                [
                    {field: 'THIRD_MONEY', align: 'center', title: '第三方支付结算', width: 130, totalRow: true},
                    {field: 'HIS_MONEY', align: 'center', title: 'HIS结算金额', width: 130, sort: false, totalRow: true},
                    /*{field: 'DIFFERENCE',align:'center', title: '差额', width: 100, sort: false,totalRow: true},*/
                    {
                        field: 'THIRD_UNILATERAL',
                        align: 'center',
                        title: '三方单边账',
                        width: 100,
                        sort: false,
                        totalRow: true
                    },
                    {
                        field: 'HIS_UNILATERAL',
                        align: 'center',
                        title: 'HIS单边账',
                        width: 100,
                        sort: false,
                        totalRow: true
                    },
                    {
                        field: 'MANAGER_HISTORY_UNILATERAL',
                        align: 'center',
                        title: '处理历史单边账金额',
                        width: 160,
                        sort: false,
                        totalRow: true
                    },
                    /*{field: 'TODAY_UNILATERAL', align:'center',title: '当日结算单边账金额', width: 160, sort: false,totalRow: true},*/
                    {
                        field: 'UNILATERAL_NUM',
                        align: 'center',
                        title: '当日结算单边账条数',
                        width: 160,
                        sort: false,
                        totalRow: true
                    },
                    /*{field: 'PENDING_UNILATERAL',align:'center', title: '当日待处理单边账金额', width: 170, sort: false,totalRow: true},
                    {field: 'PENDING_UNILATERAL_NUM',align:'center', title: '当日待处理单边账笔数', width: 170, sort: false,totalRow: true},*/
                    {
                        field: 'STATUS',
                        align: 'center',
                        title: '是否通过',
                        width: 100,
                        sort: false,
                        templet: function (d) {
                            // 合计行不显示状态
                            if (d.TOTAL_NUMS !== undefined) {
                                return '-';
                            }
                            return d.STATUS === '1' ? '通过' : '不通过';
                        }
                    },
                ]
            ],
            totalRow: true,
            data: [],
            done: function (res, curr, count) {
                let dataList = res.data;
                if (dataList.length > 0) {
                    let objStr = "";
                    if (dataList.length === 1) {
                        objStr = dataList[0].TRADE_DATE;
                        // 只有一条数据时，隐藏合计行
                        $('.layui-table-total').hide();
                    } else {
                        objStr = dataList[0].TRADE_DATE;
                        objStr = objStr + "~";
                        objStr = objStr + dataList[dataList.length - 1].TRADE_DATE;
                        // 多条数据时，显示合计行并设置第一列文字为"合计"
                        $('.layui-table-total').show();
                        $('.layui-table-total tr.layui-table-sum td').first().find('div').html('<span style="font-weight: bold;">合计</span>');
                    }
                    $(".selectDate").text(objStr);
                }

            }
        });

        //时间控件
        laydate.render({
            elem: '#searchDate'
            , type: 'date'
            , range: '~'
        });

        table.on('tool(test)', function (obj) {
            selectData = obj.data;
            let data = obj.data;

            if (obj.event === 'getInfo') {
                $("a[url='/reconciliationResult/toReconciliationResult']", parent.document).parent().removeClass("layui-this");
                $("a[url='/paymentStatistics/toSummary']", parent.document).parent().addClass("layui-this");

                location.href = "/paymentStatistics/toSummary?trade_date=" + data.TRADE_DATE;
            }
        });

        search();

    });

    //查询
    function search(num) {
        num = num === null ? 1 : num;
        let url = "/reconciliationResult/findReconciliationResultPageList";
        let param = {};

        let status = $("#searchStatus").val();
        param.status = status;
        let date = $("#searchDate").val();
        if (date !== '') {
            let time = date.split("~");
            let startTime = time[0].trim();
            let endTime = time[1].trim();

            param.startTime = startTime;
            param.endTime = endTime;

            $(".selectDate").text(startTime + " ~ " + endTime);
        }

        table.reload('test', {
            method: 'get',
            url: url,
            where: param,
            page: {
                curr: num
            }
        });

    }

    //导出
    function exportExcel() {
        let url = "/reconciliationResult/exportReconciliationResult";
        let param = {};

        param.status = $("#searchStatus").val();
        let date = $("#searchDate").val();
        if (date !== '') {
            let time = date.split("~");
            let startTime = time[0].trim();
            let endTime = time[1].trim();

            param.startTime = startTime;
            param.endTime = endTime;

        }

        let dowloadName = "对账记录";
        param.dowloadName = dowloadName;
        let load = layer.load();
        AjaxPostJson(url, param, function (data) {
            layer.close(load);
            if (data.errCode === "0") {
                let fileName = data.dlName;
                location.href = '/download?fileName=' + fileName + '&dowloadName=' + dowloadName;
            } else {
                layer.alert(data.errMsg);
            }
        });
    }
</script>
</html>