<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理后台</title>
    <link rel="stylesheet" type="text/css" href="../static/css/basic.css">
    <link src="../static/lib/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../static/lib/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">
    <script src="../static/js/jquery.min.js"></script>
    <script src="../static/lib/bootstrap/js/bootstrap.min.js"></script>
    <script src="../static/js/filter.js"></script>
    <script src="../static/lib/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
    <script src="../static/lib/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
    <script src="../static/js/basic.js"></script>
    <style>
        .search label {
            font-weight: normal;
            margin-right: 20px;
        }
        #status {
            height: 30px;
            line-height: 30px;
        }
        .search-button {
            margin-left: 40px;
        }
        .modal-body label {
            font-weight: normal;
            margin-left: 2%;
            margin-top: 2%;
        }
        #result-modal label {
            margin-right: 50px;
        }
        #table-body tr{
            height: 25px;
            line-height: 25px;
        }
    </style>
</head>
<body>
    <div class="search">
        <label>
            开始时间：
            <input type="text" name="" value="" id="start-time" class="start-time" value="">
        </label>
        <label>
            结束时间：
            <input type="text" name="" value="" id="end-time" class="end-time" value="">
        </label>
        <label>
            商户订单号：
            <input type="text" id="merchantOrderId">
        </label>
        <label>
            系统订单号：
            <input type="text" id="bankOrderId">
        </label>
        <select id="status">
            <option value="">请选择</option>
            <option value="payfor_confirm">待审核</option>
            <option value="payfor_solving">系统处理中</option>
            <option value="payfor_banking">银行处理中</option>
            <option value="payfor_success">代付成功</option>
            <option value="payfor_fail">代付失败</option>
        </select>
        <input type="button" class="btn btn-primary search-button" value="搜索">
    </div>

    <div class="menu-table table-responsive">
        <table>
            <thead class="thead-dark">
            <tr>
                <th>序列号</th>
                <th>商户名称</th>
                <th>商户订单号</th>
                <th>系统订单号</th>
                <th>代付金额</th>
                <th>开户名</th>
                <th>银行名称</th>
                <th>银行账号</th>
                <th>状态</th>
                <th>打款类型</th>
                <th>时间</th>
                <th>代付通道</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="table-body">
            </tbody>
        </table>
    </div>
    <!-- 分页插件 -->
    <div class="cut_page">
        <li>
            每页显示
            <select id="display_count">
                <option value="20">20</option>
                <option value="30">30</option>
                <option value="50">50</option>
                <option value="100">100</option>
            </select>
        </li>

        <li class="current_total_page">第<span class="current_page">0</span>/<span class="total_page">0</span>页</li>
        <li class="pre_page">上一页</li>
        <li class="next_page">下一页</li>
        <li class="jump_page">跳转 <input type="text" name="jump_page" value=""> <button type="button" class="btn btn-default">Go</button></li>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="result-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <input type="text" value="" hidden="hidden" id="result-bank-order-id">
                <div class="modal-body">
                    <label>
                        打款成功：
                        <input type="radio" name="result-payfor" value="payfor_success">
                    </label>
                    <label for="">
                        打款失败：
                        <input type="radio" name="result-payfor" value="payfor_fail">
                    </label>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="result-cannel">取消</button>
                    <input type="button" class="btn btn-primary" value="确定" onclick="resultSubmit();">
                </div>
            </div>
        </div>
    </div>
    <script>
        function getCurPageValues() {
            let displayCount = $("#display_count").val();
            let currentPage = $(".current_page").html();
            let totalPage = $(".total_page").html();
            let jumpPage = $(".jump_page input").val();
            let startTime = $("#start-time").val();
            let endTime = $("#end-time").val();
            let merchantOrderId = $("#merchantOrderId").val();
            let bankOrderId = $("#bankOrderId").val();
            let status = $("#status").val();

            return {
                "displayCount":displayCount,
                "currentPage":currentPage,
                "totalPage":totalPage,
                "jumpPage":jumpPage,
                "startTime":startTime,
                "endTime":endTime,
                "merchantOrderId":merchantOrderId,
                "bankOrderId":bankOrderId,
                "status":status
            }
        }
        function setCutPageValues(res) {
            $(".current_page").html(res.CurrentPage);
            $(".total_page").html(res.TotalPage);
            $("#display_count option").each(function() {
                if ($(this).text() == res.DisplayCount) {
                    $(this).attr('selected', true);
                }
            });
            $(".jump_page input").val("");
        }
        function randPayForList(res) {
            setCutPageValues(res);
            let str = "";
            for (let i = 0; i < res.PayForList.length; i ++) {
                let v = res.PayForList[i];
                let tmp = "<tr>";
                if (v.Status == "payfor_success") {
                    tmp = "<tr style='color: blue;'>";
                } else if(v.Status == "payfor_fail") {
                    tmp = "<tr style='color: red;'>"
                }
                tmp  =  tmp + "<th>" + (res.StartIndex+i+1) + "</th>" +
                        "<th>" + v.MerchantName + "</th>" + "<th>" + v.MerchantOrderId + "</th>" + "<th>" + v.BankOrderId + "</th>" +
                        "<th>" + v.PayforAmount + "</th>" + "<th>" + v.BankAccountName + "</th>" + "</th>" + "<th>" + v.BankName + "</th>" +
                        "<th>" + v.BankAccountNo + "</th>" + "<th>" + v.Status + "</th>" + "<th>" + v.GiveType + "</th>" + "<th>" + v.CreateTime + "</th>" +
                        "<th>" + v.RoadName + "</th>" + "<th>" ;
                if (v.Status == "payfor_confirm") {
                    tmp = tmp + '<button type="button" value="' + v.BankOrderId +'" class="btn btn-default" onclick="confirm(this.value);">' + "审核" +'</button>';
                } else if ((v.Status == "payfor_solving" && v.GiveType == "payfor_hand") || v.Status == "payfor_banking") {
                    tmp = tmp + '<button type="button" value="' + v.BankOrderId +'" class="btn btn-default" style="color: blue;" onclick="result(this.value);">' + "处理" +'</button>';
                    if (v.Status == "payfor_banking") {
                        tmp = tmp + '<button type="button" value="' + v.BankOrderId + '" class="btn btn-default" style="color: blue;" onclick="query(this.value);">' + "查询" + '</button>';
                    }
                } else if (v.Status == "payfor_success") {
                    tmp = tmp + '<button type="button" value="' + v.BankOrderId +'" class="btn btn-default" style="color: green;">' + "成功" +'</button>';
                } else if (v.Status == "payfor_fail") {
                    tmp = tmp + '<button type="button" value="' + v.BankOrderId + '" class="btn btn-warning">失败</button>';
                } else {
                    tmp = tmp + '<button type="button" class="btn btn-primary">处理中</button>';
                }
                tmp = tmp + /*'</div>' +*/ "</th>" + "</tr>";
                str = str + tmp.replace("payfor_fail", "失败").replace("payfor_success","成功").replace("payfor_confirm","待审核").replace("payfor_solving","系统处理中").
                replace("payfor_banking","银行处理中").replace("payfor_hand","手动打款").replace("payfor_refuse","拒绝打款").replace("payfor_road","通道打款");
            }

            $("#table-body").html(str);
        }

        function notify(bankOrderId) {
            $.ajax({
                url: "/self/send/notify",
                data: {
                    "bankOrderId":bankOrderId
                },
                success: function (res) {
                    if (res.Code === 404) {
                        window.parent.location = "/login.html";
                    } else {
                        alert(res.Msg);
                    }
                },
                error: function () {
                    alert("系统异常，请稍后再试");
                }
            });
        }

        function query(bankOrderId) {
            $.ajax({
                url: "/supplier/payfor/query",
                data: {
                    "bankOrderId":bankOrderId
                },
                success: function (res) {
                    if (res.Code === 404) {
                        window.parent.location = "/login.html";
                    } else {
                        alert(res.Msg);
                    }
                },
                error: function () {
                    alert("系统异常，请稍后再试");
                }
            });
        }

        function AjaxPayForList(dataJSON) {
            $.ajax({
                url: "/get/payfor",
                data: dataJSON,
                success: function (res) {
                    if (res.Code == 404) {
                        window.parent.location = "/login.html";
                    } else {
                        randPayForList(res);
                    }
                },
                error: function () {
                    alert("系统异常，请稍后再试");
                }
            });
        }
        //当每页显示数更改后，执行的操作
        $("#display_count").change(function() {
            let dataJSON = getCurPageValues();
            AjaxPayForList(dataJSON);
        });

        //点击上一页的按钮
        $(".pre_page").click(function() {
            let dataJSON = getCurPageValues();

            if (dataJSON["currentPage"] == 1) {
                return;
            }
            dataJSON["currentPage"] = parseInt(dataJSON["currentPage"]) - 1;
            AjaxPayForList(dataJSON);
        });
        //点击下一页的按钮时
        $(".next_page").click(function() {
            let dataJSON = getCurPageValues();
            if (dataJSON["currentPage"] == dataJSON["totalPage"]) {
                return;
            }
            dataJSON["currentPage"] = parseInt(dataJSON["currentPage"]) + 1;
            AjaxPayForList(dataJSON);
        });
        //点击跳转那一页的按钮
        $(".jump_page button").click(function() {
            let dataJSON = getCurPageValues();

            if (dataJSON["jumpPage"].length <= 0) {
                return;
            }
            AjaxPayForList(dataJSON);
        });
        $(".search-button").click(function () {
            let dataJSON = getCurPageValues();
            AjaxPayForList(dataJSON);
        });
        
        function result(bankOrderId) {
            $("#result-bank-order-id").val(bankOrderId);
            $("#result-modal").modal();
        }
        function resultSubmit() {
            let bankOrderId = $("#result-bank-order-id").val();
            let resultType = $("input[name='result-payfor']:checked").val();

            $.ajax({
                url: "/result/payfor",
                data: {
                    "bankOrderId":bankOrderId,
                    "resultType":resultType
                },
                success: function (res) {
                    if (res.Code === 404) {
                        window.parent.location = "/login.html";
                    } else if (res.Code === -1){
                        alert(res.Msg);
                    } else {
                        $("#result-cannel").trigger('click');
                        let dataJSON = getCurPageValues();
                        AjaxPayForList(dataJSON);
                    }
                },
                error: function () {
                    alert("系统异常，请稍后再试");
                }
            });
        }
        //审核操作
        function confirm(bankOrderId) {
            $("#main-bank-order-id").val(bankOrderId);
            loadMainContent("/confirm.html")
        }

        $(function () {
            let day = new Date(new Date().getTime() - 86400000);
            let s = dateFtt("yyyy-MM-dd hh:mm:ss", day);
            $("#start-time").val();
            let dataJSON = getCurPageValues();
            AjaxPayForList(dataJSON);
        });
    </script>
</body>
</html>