<!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">
                        <select id="searchBusiness">
                            <option value="">请选择接口厂商</option>
                            <option value="1">中联</option>
                            <option value="2">卫宁</option>
                        </select>
                    </div>
                </div>-->
                <div class="layui-inline">
                    <label class="layui-form-label">获取状态</label>
                    <div class="layui-input-inline">
                        <select id="searchStatus">
                            <option value="">请选择状态</option>
                            <option value="1">成功</option>
                            <option value="0">失败</option>
                        </select>
                    </div>
                </div>
                <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="showInport()"><i-->
            <!--                    class="layui-icon layui-icon-upload"></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 id="inputCash" style="display:none;">
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" onclick="$('#loadfile').click();"><i class="layui-icon"></i>添加文件
            </button>
            <input tyle="text" id="upload_fileName" class="layui-input formWidthTwo">
        </div>
        <form action="" id="uploadForm" method="post" enctype="multipart/form-data">
            <input type="file" name="spareFile" id="loadfile" style="display:none;" onchange="showUploadFileName(this)">
        </form>
    </div>

</div>

<!-- 表格操作栏 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="restartGet">重新获取</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: [
                [ //表头
                    {type: 'numbers', title: '序号', width: 50},
                    {field: 'TRADE_DATE', title: '交易日期', sort: false},
                    {field: 'BILL_TABLE_NAME', title: '来源表格', sort: false},
                    {field: 'MODIFY_TIME', title: '最近更新时间', sort: false},
                    {
                        field: 'IS_OK', title: '是否下载成功', sort: false, templet: function (d) {
                            return d.IS_OK === '1' ? '是' : '否';
                        }
                    },
                    {title: '操作', align: 'center', width: 150, toolbar: '#barDemo'},
                ]
            ],
            data: []
        });

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

        search(null);


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

            if (obj.event === 'restartGet') {
                //如果本来就获取成功的，弹出框提示
                if (data.IS_OK === '1') {

                    layer.confirm("之前数据获取正常，确认重新获取数据？", function (index) {
                        restartGet(data);
                        layer.close(index);
                    });
                } else {
                    restartGet(data);
                }
            }
        });
    });

    //重新获取
    function restartGet(data) {
        let url = "/thirdBillLog/restartGetBill";
        let param = {};
        param.trade_date = data.TRADE_DATE;
        param.bill_table_name = data.BILL_TABLE_NAME;
        let load = layer.load();
        AjaxPostJson(url, param, function (resultDate) {
            layer.close(load);
            if (resultDate.errCode === "0") {
                layer.alert("重新获取成功");
            } else {
                layer.alert(resultDate.errMsg);
            }
        });
    }

    //显示文件名称
    function showUploadFileName(obj) {
        $('#upload_fileName').val(obj.files[0].name);
    }

    //显示导入银行数据窗口
    function showInport() {
        layer.open({
            type: 1,
            title: "导入第三方记录",
            shade: 0,
            area: ['350px', '300px'],
            offset: 'auto',
            content: $("#inputCash"),
            btn: ['导入', '取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                inportExcel();
            },
            btn2: function () {

            }
        });
    }

    function inportExcel() {
        let fileObj = document.getElementById("loadfile").files[0];
        if (fileObj === undefined) {
            layer.alert('请先选择上传文件');
            return false;
        }
        layer.msg("上传中,请等待", {time: 1000});
        let formData = new FormData($('#uploadForm')[0]);
        $.ajax({
            url: "/receiveData/receiveBankData",
            type: "post",
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data.errCode === "0") {
                    layer.alert("现金记录导入成功");
                    window.location.reload();
                } else {


                    // let errNumList = data.errNumList;
                    // let errStr = "";
                    // for (let i = 0; i < errNumList.length; i++) {
                    //     errStr = errStr + errNumList[i] + ",";
                    // }
                    // layer.alert(data.errMsg + "\n错误所在行：" + errStr);
                    layer.alert(data.errMsg);
                }
            }
        });
    }

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

        let business = $("#searchBusiness").val();
        let paymentMethod = $("#searchPaymentMethod").val();
        let status = $("#searchStatus").val();

        param.business = business;
        param.paymentMethod = paymentMethod;
        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
            }
        });

    }
</script>
</html>