<%@ 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" />
    <%--<style>
        .layui-form-item .layui-input-inline {
            width: 195px;
        }
    </style>--%>
</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" action="" style="margin-top: 10px">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="userName" id="userName" placeholder="请输入商户名称" 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="cashNo" id="cashNo" placeholder="请输入提现单号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">提现状态</label>
                            <div class="layui-input-block">
                                <select class="layui-select" id="status" name="status">
                                    <option value="">请选择</option>
                                    <option value="1">提现中</option>
                                    <option value="2">提现成功</option>
                                    <option value="3">提现失败</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- 表单数据 -->
    <div class="mb50 mt10">
        <div class="whitebg pl10 pr10">
            <table class="layui-hide" id="transferCashTab" lay-filter="transferCashTab"></table>
        </div>
    </div>
</div>

<script type="text/template" id="audit">
    <form class="layui-form" id="form2" style="margin-top: 2rem;">
        <input  name="id" id="id" hidden>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">商户名称</label>
            <div class="layui-input-block">
                <input type="text" name="merchantName" id="auditName" style="cursor: pointer" placeholder="请输入商户名称"  autocomplete="off" class="layui-input layui-disabled" readonly>
            </div>
        </div>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">审核</label>
            <div class="layui-input-block">
                <select class="layui-select" id="auditStatus" name="status" >
                    <option value="">请选择</option>
                    <option value="2">通过</option>
                    <option value="3">驳回</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入备注" name="remark" id="remark" lay-verify="required"  required autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>
</script>


<script type="text/template" id="settle">
    <form class="layui-form" id="form3" style="margin-top: 2rem;">
        <div class="layui-inline">
            <label class="layui-form-label">结算名称</label>
            <div class="layui-input-block">
                <input type="text" name="settleName" id="settleName" style="outline:none;border: none"  autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结算卡号</label>
            <div class="layui-input-block">
                <input type="text" name="settleNo" id="settleNo"  style="outline:none;border: none" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">银行名称</label>
            <div class="layui-input-block">
                <input type="text" name="bankName" id="bankName" style="outline:none;border: none"  autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">支行名称</label>
            <div class="layui-input-block">
                <input type="text" name="branchName" id="branchName" style="outline:none;border: none" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block">
                <input type="text" name="phone" id="phone" style="outline:none;border: none" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">省市区</label>
            <div class="layui-input-block">
                <input type="text" name="bankAddr" id="bankAddr" style="outline:none;border: none" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">支付宝账号</label>
            <div class="layui-input-block">
                <input type="text" name="zfbAcount" id="zfbAcount" style="outline:none;border: none"  autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="toolBar">
    {{#  if(d.status == 1){ }}
    <button type="button" class="layui-btn layui-btn-sm" lay-event="audit"><i class="layui-icon">&#xe642;</i>审核</button>
    {{# } }}
    <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="settle"><i class="layui-icon">&#xe65e;</i>结算信息</button>
</script>

<script type="text/html" id="leftBar">
    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="search"><i class="layui-icon">&#xe615;</i>查询</button>
</script>


<script>
    layui.use(['table', 'layer','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var tableIns = table.render({
            elem: '#transferCashTab'
            ,url:'${ctx}/merchantTransferCash/page'
            ,id: 'transferCashTab'
            ,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.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            ,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:'userName', title:'用户名称'}
                ,{field:'cashNo', title:'提现单号'}
                ,{field:'cashAmt', title:'提现金额(元)',sort:true,templet:function (d) {
                    return d.cashAmt.toFixed(2);
                 }}
                ,{field:'status', title:'提现状态',templet:function(d){
                    if(d.status == '1'){
                        return '<span style="color: orange">提现中</span>';
                    }else if(d.status == '2'){
                        return '<span style="color: green">提现成功</span>';
                    }else if(d.status == '3'){
                        return '<span style="color: red">提现失败</span>';
                    }
                 }}
                ,{field:'cashTime', title:'提现时间',width:220,templet : "<div>{{layui.util.toDateString(d.cashTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                ,{field:'remark', title:'备注'}
                ,{fixed: 'right', title:'操作',width:200, toolbar: '#toolBar'}
            ]]
            ,request: {
                pageName: 'current' //页码的参数名称，默认：page
                ,limitName: 'size' //每页数据量的参数名，默认：limit
            }
            ,page: true
        });
        //监听头工具栏事件
        table.on('toolbar(transferCashTab)', function (obj) {
            switch (obj.event) {
                case 'search':
                    var params= serialize($('#form1'));
                    table.reload('transferCashTab', {
                        where: params
                        , page: {
                            size: 10
                            , current: 1
                        }
                    });
                break;
            }
        });

        table.on('tool(transferCashTab)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'audit'){ //审核
                toAudit(obj);
            }else if(layEvent === 'settle'){
                showSettle(obj);
            }
        });
        function showSettle(obj) {
            var userId = obj.data.userId;
            $.ajax({
                url: '${ctx}/merchantTransferCash/settle/'+userId,
                method: 'get',
                dataType: 'JSON',
                success: function(res) {
                   if(res.code == 200){
                       var bankCard = res.data.bankCard;
                       var index = layer.open({
                           type: 1
                           ,shade: 0.5
                           ,title: '结算信息'
                           ,maxmin: true
                           ,content: $("#settle").html()
                           ,area: ['450px', '400px']
                           ,shadeClose: true
                           ,btn2: function(index, layero){
                               layer.close(index);
                           }
                           ,success:function () {
                               $("#settleName").val(bankCard.bankCertName);
                               $("#settleNo").val(bankCard.bankCardNo);
                               $("#bankName").val(bankCard.bankName);
                               $("#branchName").val(bankCard.branchName);
                               $("#phone").val(bankCard.phone);
                               $("#bankAddr").val(bankCard.branchProvince+bankCard.branchCity+bankCard.branchArea);
                               $("#zfbAcount").val(res.data.zfbAccount);
                           }
                       });
                   }else{
                       layer.msg(res.message, {
                           icon: 2
                       });
                   }
                }
            })
        }

        function toAudit(obj) {
            var index = layer.open({
                type: 1
                ,shade: 0.5
                ,title: '审核'
                ,maxmin: true
                ,content: $("#audit").html()
                ,area: ['400px', '400px']
                ,btn: ['保存','取消',]
                ,shadeClose: true
                ,btn1: function(index, layero) {
                    if($("#auditStatus").val() === "3"){
                        if(!$("#remark").val()){
                            layer.msg("驳回需填写备注!");
                            return;
                        }
                    }
                    if(!$("#auditStatus").val()){
                        layer.msg("请先选择审核通过或驳回!");
                        return;
                    }
                    layer.close(index);
                    toAuditEdit(obj);
                }
                ,btn2: function(index, layero){
                    layer.close(index);
                }
                ,success:function () {
                    $("#id").val(obj.data.id);
                    $("#auditName").val(obj.data.merchantName);
                }
            });
            form.render();
        }
        function toAuditEdit(obj) {
            var data = $("#form2").serializeObject();
            $.ajax({
                url: '${ctx}/merchantTransferCash/saveOrUpdate',
                method: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType:"application/json",
                success: function(res) {
                    if(res.code == 200) {
                        layer.msg(res.data, {
                            icon: 1
                        });
                        obj.update({
                            status: $("#auditStatus").val()
                        });
                    } else {
                        layer.msg(res.data, {
                            icon: 2
                        });
                    }
                }
            })
        }
    });
    jQuery.prototype.serializeObject=function(){
        var obj=new Object();
        $.each(this.serializeArray(),function(index,param){
            if(!(param.name in obj)){
                obj[param.name]=param.value;
            }
        });
        return obj;
    };
</script>
</body>
</html>
