<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../common.jsp"/>
</head>
<body>
<div class="row animated fadeInRight" style="margin: 0;">
    <div class="wrapper wrapper-content mt5 right-content ml5 mr5">
        <div class="toolbar-panel bdd">
            <div class="toolbar-box">
                <div class="toolbar-head clearfix lgraybg pt5 pb10 pl10 bdbd">
                    <!-- 顶部按钮 -->
                    <div class="fl">
                        查询条件
                    </div>
                    <div class="tools fr">
                        <a href="javascript:void(0);" class="collapse dblock f14 dgray mr5">
                            <i class="fa-2x fa fa-angle-double-up"></i>
                        </a>
                    </div>
                </div>
            </div>
            <!--搜索条件 -->
            <div class="toolbar-body">
                <form class="layui-form" id="form1" action="" style="margin-top: 10px">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-block">
                                <input type="text" name="userId" id="userId" placeholder="请输入用户ID"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商户名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" id="merchantName" placeholder="请输入商户名称"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="mb50 mt10">
        <div class="whitebg pl10 pr10">
            <table class="layui-hide" id="merchantDataTab" lay-filter="merchantDataTab"></table>
        </div>
    </div>
</div>
<script type="text/html" id="leftBar">
    <a class="layui-btn layui-btn-sm" lay-event="search">
        <i class="layui-icon layui-icon-search"></i>查询
    </a>
</script>

<script type="text/template" id="audit">
    <form class="layui-form" id="form2" style="margin-top: 2rem;">
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">禁用原因</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入禁用原因" name="remark" id="banRemark" lay-verify="required"  required autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="toolBar">
    {{# if (d.risk === 'D1套现' && (d.valid == 0 || d.valid)) { }}
        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="disable">禁用D1</button>
    {{# } else if (d.risk === 'D1套现' && (d.valid == -1)){ }}
        <button type="button" class="layui-btn layui-btn-xs" lay-event="enable">启用D1</button>
    {{# } }}
</script>

<script>
    layui.use(['table', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;

        var tableIns = table.render({
            elem: '#merchantDataTab'
            , url: '${ctx}/mercData/page'
            , id: 'merchantData'
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //默数据列表的字段名称，认：data
                , statusCode: 200
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            , toolbar: '#leftBar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter']
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '商户数据统计'
            , cols: [[
                {type: 'checkbox', fixed: 'left', hidden: true}
                , {field: 'numbers', title: '序号', fixed: 'left', unresize: true, width: 80, type: 'numbers'}
                , {field: 'userId', title: '用户ID'}
                , {field: 'alias', title: '商户名称'}
                // , {field: 'totalAmt', title: '总额',templet:function (d){
                //     if(d.totalAmt){
                //         return d.totalAmt.toFixed(2);
                //     }else{
                //         return "-";
                //     }
                //  }}
                , {field: 'tradeSucAmt', title: '交易额',templet:function (d){
                    if(d.tradeSucAmt){
                        return d.tradeSucAmt.toFixed(2);
                    }else{
                        return "-";
                    }
                }}
                , {field: 'tradeNum', title: '总笔数',templet:function (d){
                        if(d.tradeNum){
                            return d.tradeNum;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'tradeSucNum', title: '成功笔数',templet:function (d){
                        if(d.tradeSucNum){
                            return d.tradeSucNum;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'debitSucNum', title: '储蓄卡',templet:function (d){
                        if(d.debitSucNum){
                            return d.debitSucNum;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'creditSucNum', title: '信用卡',templet:function (d){
                        if(d.creditSucNum){
                            return d.creditSucNum;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'otherSucNum', title: '其他',templet:function (d){
                        if(d.otherSucNum){
                            return d.otherSucNum;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'd1Num', title: 'D1笔数',templet:function (d){
                        if(d.d1Num){
                            return d.d1Num;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'd0Num', title: 'D0笔数',templet:function (d){
                        if(d.d0Num){
                            return d.d0Num;
                        }else{
                            return "-";
                        }
                    }}
                , {field: 'numScale', title: '成功率%',templet:function (d){
                    if(d.numScale){
                        return d.numScale + "%";
                    }else{
                        return "-";
                    }
                }}
                , {field: 'debitScale', title: '储蓄卡%',templet:function (d){
                    if(d.debitScale){
                        return d.debitScale + "%";
                    }else{
                        return "-";
                    }
                }}
                , {field: 'creditScale', title: '信用卡%',templet:function (d){
                    if(d.creditScale){
                        return d.creditScale + "%";
                    }else{
                        return "-";
                    }
                }}
                , {field: 'd1Scale', title: 'D1%',templet:function (d){
                    if(d.d1Scale){
                        return d.d1Scale + "%";
                    }else{
                        return "-";
                    }
                }}
                , {field: 'd0Scale', title: 'D0%',templet:function (d){
                    if(d.d0Scale){
                        return d.d0Scale + "%";
                    }else{
                        return "-";
                    }
                }}
                , {field: 'valid', title: 'D1状态',templet:function (d){
                        if(d.valid == 0 || d.valid){
                            if(d.valid == -1){
                                return "<span style='color: red'>禁用</span>";
                            }else{
                                return "<span style='color: green'>启用</span>";
                            }
                        }else{
                            return "未开通";
                        }
                    }}

                , {field: 'risk', title: '风控备注',templet:function (d){
                    if(d.risk != "-"){
                        return "<span style='color: red'>"+d.risk+"</span>";
                    }
                    return d.risk;
                }}
                ,{fixed: 'right', title:'操作', toolbar: '#toolBar'}
            ]]
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , page: true
        });
        //监听头工具栏事件
        table.on('toolbar(merchantDataTab)', function (obj) {
            switch (obj.event) {
                case 'add':
                    // 添加
                    toAdd();
                    break;
                case 'search':
                    var params = serialize($('#form1'));
                    table.reload('merchantData', {
                        where: params
                        , page: {
                            size: 10
                            , current: 1
                        }
                    });
                    break;
            }
        });
        table.on('tool(merchantDataTab)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'disable') { //
                showBanValidStatus(data);
            }else if(layEvent === 'enable'){
                showOpenValidStatus(data);
            }
        })
        function showOpenValidStatus(data) {
            layer.confirm('确定启用当前商户D1通道？', {icon: 3, title: '提示'}, function (index) {
                var index = layer.load(1, {shade: [0.1, '#000']});
                $.ajax({
                    type: 'POST'
                    , url: ctx + "/merchantStore/openChannelValid"
                    , data: {
                        channelId: data.channel,
                        merchantCode: data.merchantCode
                    }
                    , success: function (res) {
                        layer.close(index);
                        layer.msg(res.message, {
                            icon: res.code == 200 ? 1 : 2
                        });
                        if(res.code == 200){
                            tableIns.reload();
                        }
                    }
                });
            })
        }
        function showBanValidStatus(data) {
            layer.open({
                type: 1,
                title: '提示',
                offset: 'm', //具体配置参考：offset参数项
                area: ['400px', '300px'],
                content: $("#audit").html(),
                btn: ['确认', '取消'],
                shade: 0.3,
                btn1: function (index, layero) {
                    layer.closeAll();
                    var index = layer.load(1, {shade: [0.1, '#000']});
                    $.ajax({
                        type: 'POST'
                        , url: ctx + "/merchantStore/banChannelValid"
                        , data: {
                            channelId: data.channel,
                            merchantCode: data.merchantCode,
                            remark: $("#banRemark").val()
                        }
                        , success: function (res) {
                            layer.close(index);
                            layer.msg(res.message, {
                                icon: res.code == 200 ? 1 : 2
                            });
                            if(res.code == 200){
                                tableIns.reload();
                            }
                        }
                    });
                }
                ,btn2: function(index, layero){
                    layer.close(index);
                }
            });
        }
    });
</script>
</body>
</html>
