<!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)
        }

        .summary-card {
            background: #fff;
            border: 1px solid #e6e6e6;
            border-radius: 4px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .summary-item {
            display: inline-block;
            margin-right: 40px;
            text-align: center;
        }

        .summary-label {
            font-size: 14px;
            color: #666;
            margin-bottom: 5px;
        }

        .summary-value {
            font-size: 24px;
            font-weight: bold;
            color: #1890ff;
        }

        .summary-money {
            color: #f5222d;
        }
    </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>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" style="width: 240px;" id="likeFiled"
                               placeholder="请输入平台交易号">
                    </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="summary-card">
            <div class="summary-item">
                <div class="summary-label">今日军保交易笔数</div>
                <div class="summary-value" id="todayCount">0</div>
            </div>
            <div class="summary-item">
                <div class="summary-label">今日军保交易总金额</div>
                <div class="summary-value summary-money" id="todayAmount">¥0.00</div>
            </div>
        </div>
        
        <div class="tableTitle">
            <span class="tableName">军保对账统计明细</span>
            <span class="selectDate">&nbsp;</span>
        </div>
        <table id="demo" lay-filter="test"></table>
    </div>

</div>

</body>
<script th:inline="javascript">
    let bizTypeList = [[${bizTypeList}]];

    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() + 160), // 增加高度以适应汇总卡片
            title: '军保对账统计明细',
            page: true,//开启分页
            limit: 20,
            limits: [20, 30, 50],
            //toolbar: '', //开启工具栏，此处显示默认图标，可以自定义模板
            defaultToolbar: [],
            id: 'test',
            //skin:'line',
            even: 'true',
            //size:'lg', 默认普通尺寸 sm 小 lg 大
            cols: [
                [
                    {field: 'HISOPERCODE', align: 'center', title: '操作员', width: 120, sort: false},
                    {
                        field: 'PAYMETHOD',
                        align: 'center',
                        title: '类别',
                        width: 120,
                        sort: false,
                        templet: function (d) {
                            let result = "";
                            if (d.PAYMETHOD === '1') {
                                result = '门诊';
                            } else if (d.PAYMETHOD === '2') {
                                result = '住院';
                            }
                            return result;
                        }
                    },
                    {
                        field: 'TRADINGSTATUS',
                        align: 'center',
                        title: '交易状态',
                        width: 120,
                        sort: false,
                        templet: function (d) {
                            let tradingstatusStr = "";
                            if (d.TRADINGSTATUS === '1') {
                                tradingstatusStr = '收款记录';
                            } else if (d.TRADINGSTATUS === '2') {
                                tradingstatusStr = '退款记录';
                            }
                            return tradingstatusStr;
                        }
                    },
                    {
                        field: 'BIZTYPE',
                        align: 'center',
                        title: '业务类型',
                        width: 120,
                        sort: false,
                        templet: function (d) {
                            let result = "";
                            for (let i = 0; i < bizTypeList.length; i++) {
                                let obj = bizTypeList[i];
                                if (d.BIZTYPE === obj.dicvalue) {
                                    result = obj.dicname;
                                    break;
                                }
                            }
                            return result;
                        }
                    },
                    {
                        field: 'PAYTYPE',
                        align: 'center',
                        title: '支付方式',
                        width: 120,
                        sort: false,
                        templet: function (d) {
                            if (d.PAYTYPE === '3') {
                                return '医院垫支';
                            } else if (d.PAYTYPE === '8') {
                                return '军保支付';
                            }
                            return d.PAYTYPE;
                        }
                    },

                    {field: 'TRADETIME', align: 'center', title: '时间', width: 120, sort: false},
                    {field: 'AMOUNT', align: 'center', title: '金额 ', width: 120, sort: false},
                    {field: 'PLATFORMTRANSID', align: 'center', title: '平台订单号 ', width: 120, sort: false},
                    {field: 'HISTRANSID', align: 'center', title: 'his订单号  ', width: 120, sort: false},
                    {field: 'HISTRANSID', align: 'center', title: 'HIS交易ID', width: 120, sort: false},
                    {field: 'PATIENTID', align: 'center', title: '患者id ', width: 120, sort: false},
                    {field: 'PATIENTNAME', align: 'center', title: '患者姓名 ', width: 120, sort: false},
                    {field: 'TRADE_DATE', align: 'center', title: '交易时间', width: 120, sort: false},
                    {field: 'SOURCE', align: 'center', title: '来源', width: 120, sort: false},
                ]
            ],
            data: []
        });

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

        search();
        loadSummaryData(); // 加载汇总数据
    });

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

        param.likeFiled = $("#likeFiled").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;

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

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

        // 重新加载汇总数据
        loadSummaryData();
    }

    // 加载汇总数据
    function loadSummaryData() {
        let url = "/militaryInsurance/findMilitaryInsuranceCountData";
        let param = {};

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

            // 确保时间格式正确，添加时分秒
            if (startTime.length === 10) { // 只有日期，没有时间
                startTime += " 00:00:00";
            }
            if (endTime.length === 10) { // 只有日期，没有时间
                endTime += " 23:59:59";
            }

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

        AjaxPostJson(url, param, function (data) {
            if (data.errCode === "0") {
                $("#todayCount").text(data.num || 0);
                $("#todayAmount").text("¥" + (parseFloat(data.money || 0).toFixed(2)));
            } else {
                console.error("加载汇总数据失败：" + data.errMsg);
                $("#todayCount").text("0");
                $("#todayAmount").text("¥0.00");
            }
        });
    }

    //导出
    function exportExcel() {
        let url = "/militaryInsurance/exportMilitaryInsuranceDetail";
        let param = {};

        param.likeFiled = $("#likeFiled").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>
