<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<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" onclick="search()"><i
                    class="layui-icon layui-icon-search"></i>查询
            </button>
            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="exportHisExcel()"><i class="layui-icon">&#xe67d;</i>导出HIS账单
            </button>
            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="exportBankExcel()"><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="summaryTable" lay-filter="summaryTable"></table>

        <div class="tableTitle" style="margin-top: 30px;">
            <span class="tableName">HIS住院账单明细</span>
            <span class="selectDate">&nbsp;</span>
        </div>
        <table id="hisTable" lay-filter="hisTable"></table>

        <div class="tableTitle" style="margin-top: 30px;">
            <span class="tableName">银行住院账单明细</span>
            <span class="selectDate">&nbsp;</span>
        </div>
        <table id="bankTable" lay-filter="bankTable"></table>
    </div>
</div>
<script th:inline="javascript">
    let payTypeList = [[${payTypeList}]];
    let bizTypeList = [[${bizTypeList}]];
    let thirdPayList = [[${thirdPayList}]];
    // 兜底防止未传入字典导致脚本报错
    payTypeList = payTypeList || [];
    bizTypeList = bizTypeList || [];
    thirdPayList = thirdPayList || [];
</script>
<script th:inline="none">
    let layer, laydate, table, form;

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

        // 每日汇总表格
        table.render({
            elem: '#summaryTable',
            height: 300,
            title: '每日汇总',
            page: false,
            id: 'summaryTable',
            even: true,
            cols: [[
                {field: 'TRADE_DATE', align: 'center', title: '交易日期', width: 150, sort: false},
                {field: 'HIS_COUNT', align: 'center', title: 'HIS账单笔数', width: 150, sort: false},
                {field: 'HIS_AMOUNT', align: 'center', title: 'HIS账单金额', width: 150, sort: false, templet: function(d) {
                    return d.HIS_AMOUNT ? parseFloat(d.HIS_AMOUNT).toFixed(2) : '0.00';
                }},
                {field: 'BANK_COUNT', align: 'center', title: '银行账单笔数', width: 150, sort: false},
                {field: 'BANK_AMOUNT', align: 'center', title: '银行账单金额', width: 150, sort: false, templet: function(d) {
                    return d.BANK_AMOUNT ? parseFloat(d.BANK_AMOUNT).toFixed(2) : '0.00';
                }},
                {field: 'DIFF_AMOUNT', align: 'center', title: '差额', width: 150, sort: false, templet: function(d) {
                    let hisAmount = d.HIS_AMOUNT ? parseFloat(d.HIS_AMOUNT) : 0;
                    let bankAmount = d.BANK_AMOUNT ? parseFloat(d.BANK_AMOUNT) : 0;
                    let diff = hisAmount - bankAmount;
                    return diff.toFixed(2);
                }}
            ]],
            data: []
        });

        // HIS住院账单表格
        table.render({
            elem: '#hisTable',
            height: 'full-' + ($(".toolbar").height() + 400),
            title: 'HIS住院账单明细',
            page: true,
            limit: 20,
            limits: [20, 30, 50],
            defaultToolbar: [],
            id: 'hisTable',
            even: true,
            cols: [[
                {field: 'HISOPERCODE', align: 'center', title: '操作员', width: 120, sort: false},
                {
                    field: 'TRADINGSTATUS',
                    align: 'center',
                    title: '交易状态',
                    width: 120,
                    sort: false,
                    templet: function (d) {
                        if (d.TRADINGSTATUS === '1') {
                            return '收款记录';
                        } else if (d.TRADINGSTATUS === '2') {
                            return '退款记录';
                        }
                        return '';
                    }
                },
                {
                    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) {
                        let result = "";
                        for (let i = 0; i < payTypeList.length; i++) {
                            let obj = payTypeList[i];
                            if (d.PAYTYPE === obj.dicvalue) {
                                result = obj.dicname;
                                break;
                            }
                        }
                        return result || d.PAYTYPE;
                    }
                },
                {field: 'TRADETIME', align: 'center', title: '交易时间', width: 150, sort: false},
                {field: 'TRADE_DATE', align: 'center', title: '交易日期', width: 120, sort: false},
                {field: 'AMOUNT', align: 'center', title: '金额', width: 120, sort: false},
                {field: 'PLATFORMTRANSID', align: 'center', title: '平台订单号', width: 180, sort: false},
                {field: 'HISTRANSID', align: 'center', title: 'HIS订单号', width: 180, sort: false},
                {field: 'PATIENTID', align: 'center', title: '患者ID', width: 120, sort: false},
                {field: 'PATIENTNAME', align: 'center', title: '患者姓名', width: 120, sort: false}
            ]],
            data: []
        });

        // 银行住院账单表格
        table.render({
            elem: '#bankTable',
            height: 'full-' + ($(".toolbar").height() + 400),
            title: '银行住院账单明细',
            page: true,
            limit: 20,
            limits: [20, 30, 50],
            defaultToolbar: [],
            id: 'bankTable',
            even: true,
            cols: [[
                {field: 'C_JYRQ', align: 'center', title: '交易日期', width: 120, sort: false},
                {field: 'C_JYSJ', align: 'center', title: '交易时间', width: 120, sort: false},
                {field: 'C_QSRQ', align: 'center', title: '清算日期', width: 120, sort: false},
                {field: 'C_LSH', align: 'center', title: '流水号', width: 150, sort: false},
                {field: 'C_SHDDH', align: 'center', title: '商户订单号', width: 180, sort: false},
                {field: 'C_YSDDH', align: 'center', title: '银商订单号', width: 180, sort: false},
                {
                    field: 'C_JYLX',
                    align: 'center',
                    title: '交易类型',
                    width: 120,
                    sort: false,
                    templet: function (d) {
                        if (d.C_JYLX === '1') {
                            return '收款记录';
                        } else if (d.C_JYLX === '2') {
                            return '退款记录';
                        }
                        return '';
                    }
                },
                {field: 'C_CARD', align: 'center', title: '卡号', width: 150, sort: false},
                {field: 'C_FKH', align: 'center', title: '发卡行', width: 120, sort: false},
                {field: 'C_JYJE', align: 'center', title: '交易金额', width: 120, sort: false},
                {field: 'C_QSJE', align: 'center', title: '清算金额', width: 120, sort: false},
                {field: 'C_SXF', align: 'center', title: '手续费', width: 120, sort: false},
                {field: 'C_SJZFJE', align: 'center', title: '实际支付金额', width: 120, sort: false},
                {field: 'C_ZDH', align: 'center', title: '终端号', width: 120, sort: false},
                {
                    field: 'C_ZFFS',
                    align: 'center',
                    title: '支付方式',
                    width: 120,
                    sort: false,
                    templet: function (d) {
                        let result = "";
                        for (let i = 0; i < thirdPayList.length; i++) {
                            let obj = thirdPayList[i];
                            if (d.C_ZFFS === obj.dicvalue) {
                                result = obj.dicname;
                                break;
                            }
                        }
                        return result || d.C_ZFFS;
                    }
                },
                {field: 'C_KLX', align: 'center', title: '卡类型', width: 120, sort: false}
            ]],
            data: []
        });

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

        search();
    });

    //查询
    function search(num) {
        if (typeof table === 'undefined') { // 确保table模块已加载
            return;
        }
        num = num === null ? 1 : num;
        let date = $("#searchDate").val();
        let param = {};

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

        // 查询每日汇总
        $.ajax({
            type: "get",
            url: "/inpatientBill/findDailySummary",
            data: param,
            dataType: "json",
            success: function (data) {
                if (data.code === 0) {
                    table.reload('summaryTable', {
                        data: data.data
                    });
                }
            },
            error: function (xhr, status, error) {
                console.error('查询每日汇总失败:', error);
            }
        });

        // 查询HIS住院账单
        table.reload('hisTable', {
            method: 'get',
            url: '/inpatientBill/findHisInpatientDetail',
            where: param,
            page: {
                curr: num
            }
        });

        // 查询银行住院账单
        table.reload('bankTable', {
            method: 'get',
            url: '/inpatientBill/findBankInpatientDetail',
            where: param,
            page: {
                curr: num
            }
        });
    }

    //导出HIS账单
    function exportHisExcel() {
        if (typeof layer === 'undefined') { // 确保layer模块已加载
            return;
        }
        let url = "/inpatientBill/exportHisInpatientDetail";
        let param = {};

        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 = "HIS住院账单明细";
        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);
            }
        });
    }

    //导出银行账单
    function exportBankExcel() {
        if (typeof layer === 'undefined') { // 确保layer模块已加载
            return;
        }
        let url = "/inpatientBill/exportBankInpatientDetail";
        let param = {};

        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>
</body>
</html>
