<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<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="minWidth=device-minWidth, 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" action="" style="margin-top: 10px" lay-filter="formCondition">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="请输入用户ID" id="userId" name="userId" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">上级用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="请输入用户上级ID" id="parentId" name="parentId"
                                       class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">电话</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="请输入电话" id="phone" name="phone" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="请输入姓名" id="userName" name="userName"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">账号</label>
                            <div class="layui-input-inline">
                                <input type="text" placeholder="请输入账号" id="loginName" name="loginName"
                                       class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">自动审核</label>
                            <div class="layui-input-inline">
                                <select name="autoAudit" id="autoAudit">
                                    <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">
                                <select name="payEcdemicSwitch" id="payEcdemicSwitch">
                                    <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">
                                <select name="isMarket" id="isMarket">
                                    <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">
                                <select name="isUnionPay" id="isUnionPay">
                                    <option value="">请选择</option>
                                    <option value="1">开启</option>
                                    <option value="0">关闭</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="mb50 mt10">
        <div class="whitebg pl10 pr10">
            <table class="layui-hide" id="agentInfoTab" lay-filter="agentInfoTab"></table>
        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="search">查询</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="part">详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</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: '#agentInfoTab'
            , url: ctx + '/agentInfo/subAgentPage'
            , where: form.val('formCondition')
            , initSort: {
                field: 'createTime'
                , type: 'desc'
            }
            , 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: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , 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', type: 'numbers'}
                , {field: 'userId', title: '用户id', fixed: 'left', sort: true}
                , {field: 'parentId', title: '上级用户id', fixed: 'left', sort: true}
                , {field: 'loginName', title: '账号',minWidth: 100}
                , {field: 'phone', minWidth: 120, title: '电话'}
                , {field: 'userName', minWidth: 80, title: '姓名'}
                , {
                    field: 'sumPrice', title: '总收益', templet: function (d) {
                        return (d.sumPrice - 0).toFixed(2);
                    }
                }
                , {
                    field: 'beforeMonthPrice', title: '上月收益', templet: function (d) {
                        return (d.beforeMonthPrice - 0).toFixed(2);
                    }
                }
                , {
                    field: 'nowMonthPrice', title: '本月收益', templet: function (d) {
                        return (d.nowMonthPrice - 0).toFixed(2);
                    }
                }
                , {
                    field: 'cashAmt', title: '已提现', templet: function (d) {
                        return (d.cashAmt - 0).toFixed(2);
                    }
                }
                , {
                    field: 'surplusPrice', title: '余额', templet: function (d) {
                        return (d.sumPrice - d.cashAmt).toFixed(2);
                    }
                }
                , {
                    field: 'rate', title: '费率(%)', sort: true, templet: function (d) {
                        return (d.rate / 100).toFixed(2);
                    }
                }
                , {
                    field: 'isFixedRate', title: '费率类型', templet: function (d) {
                        if (d.isFixedRate == "1") {
                            return "固定";
                        } else {
                            return "浮动";
                        }
                    }
                }
                , {
                    title: '自动审核', minWidth: 95, align: "center", templet: (d) => {
                        return '<input type="checkbox" '+(d.autoAudit == "1" ? "checked" : "") + ' lay-filter="switchAutoAudit" data-userId="' + d.userId + '" lay-skin="switch" lay-text="开启|关闭">';
                    }
                }
                , {
                    title: '异地支付', minWidth: 95, align: "center", templet: (d) => {
                        return '<input type="checkbox" '+(d.payEcdemicSwitch == "1" ? "checked" : "") + ' lay-filter="switchPayEcdemicSwitch" lay-skin="switch" data-userId="' + d.userId + '" lay-text="开启|关闭">';
                    }
                }
                , {
                    title: '营销商户', minWidth: 95, align: "center", templet: (d) => {
                        return '<input type="checkbox" '+(d.isMarket == "1" ? "checked" : "") + ' lay-filter="switchMarket" lay-skin="switch" data-userId="' + d.userId + '" lay-text="开启|关闭">';
                    }
                }
                , {
                    title: '银联支付', minWidth: 95, align: "center", templet: (d) => {
                        return '<input type="checkbox" '+(d.isUnionPay == "1" ? "checked" : "") + ' lay-filter="switchUnionPay" lay-skin="switch" data-userId="' + d.userId + '" lay-text="开启|关闭">';
                    }
                }
                , {
                    field: 'createTime',
                    title: '创建日期',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {
                    field: 'updateTime',
                    title: '修改日期',
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {fixed: 'right', title: '操作', minWidth: 165, toolbar: '#barDemo'}
            ]]
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , page: true
            , done: function () {
                $("[data-field='forceUpdate']").children().each(function () {
                    if ($(this).text() == '0') {
                        $(this).text('否');
                    } else if ($(this).text() == '1') {
                        $(this).text('是');
                    }
                });
            }
        });

        //头工具栏事件
        table.on('toolbar(agentInfoTab)', function (obj) {
            switch (obj.event) {
                case 'add':
                    // 添加
                    toAdd();
                    break;
                case 'search':
                    // 查询
                    search();
                    break;
            }
        });

        function saveUserApp(msg, param) {
            debugger;
            layer.confirm(msg , function (index) {
                $.ajax({
                    url: ctx + "/userApp/updateByUserId"
                    , type: 'post'
                    , data: param
                    , success: function(data) {
                        layer.close(index);
                        table.reload('agentInfoTab', {
                            where: form.val('formCondition')
                        });
                    }
                });
                layer.close(index);
            }, function(index) {
                table.reload('agentInfoTab', {
                    where: form.val('formCondition')
                });
            });
        }


        form.on('switch(switchAutoAudit)', function(data) {
            debugger
            let userId = $(data.elem).data().userid;
            let param = {};
            param.userId = userId;
            param.autoAudit = this.checked? '1': '0';
            let msg = this.checked? '是否开启自动审核': '是否关闭自动审核?';
            saveUserApp(msg, param);
        })

        form.on('switch(switchPayEcdemicSwitch)', function(data) {
            let userId = $(data.elem).data().userid;
            let param = {};
            param.userId = userId;
            param.payEcdemicSwitch = this.checked? '1': '0';
            let msg = this.checked? '是否开启异地支付?': '是否关闭异地支付?';
            saveUserApp(msg, param);
        })

        form.on('switch(switchMarket)', function(data) {
            let userId = $(data.elem).data().userid;
            let param = {};
            param.userId = userId;
            param.isMarket = this.checked? '1': '0';
            let msg = this.checked? '是否开启营销商户': '是否关闭营销商户?';
            saveUserApp(msg, param);
        })

        form.on('switch(switchUnionPay)', function(data) {
            let userId = $(data.elem).data().userid;
            let param = {};
            param.userId = userId;
            param.isUnionPay = this.checked? '1': '0';
            let msg = this.checked? '是否开启银联支付': '是否关闭银联支付?';
            saveUserApp(msg, param);
        })

        function search() {
            table.reload('agentInfoTab', {
                page: {
                    curr: 1
                }
                , where: form.val('formCondition')
            });
        }

        //监听行工具事件
        table.on('tool(agentInfoTab)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么?', function (index) {
                    obj.del(data);
                    layer.close(index);
                    del(obj);
                });
            } else if (obj.event === 'edit') {
                toBaseEdit(obj.data);
            } else if (obj.event === 'part') {
                part(obj.data);
            }
        });

        function toAdd(obj) {
            var url = '${ctx}/agentInfo/detail/';
            if (obj != undefined) {
                url = "${ctx}/agentInfo/detail/" + obj.userId
            }
            var index = layer.open({
                type: 2
                , title: "添加"
                , shade: 0.5
                , maxmin: true
                , content: url
                , area: ['550px', '500px']
            });
        }

        function toBaseEdit(obj) {
            var url = '${ctx}/agentInfo/detail/';
            if (obj != undefined) {
                url = "${ctx}/agentInfo/detail/" + obj.userId
            }
            var index = layer.open({
                type: 2
                , title: "编辑"
                , shade: 0.5
                , maxmin: true
                , content: url
                , area: ['550px', '500px']
            });
        }

        function part(obj) {
            var url = "${ctx}/agentInfo/part/" + obj.userId;
            var index = layer.open({
                type: 2
                , title: "编辑"
                , shade: 0.5
                , maxmin: true
                , content: url
                , area: ['550px', '300px']
            });
            layer.full(index);
        }

        function del(obj) {
            $.ajax({
                url: '${ctx}/agentInfo/delete',
                method: 'post',
                data: JSON.stringify(obj.data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.data, {
                            icon: 1
                        });
                    } else {
                        layer.msg(res.data, {
                            icon: 5
                        });
                    }
                }
            })
        }
    });
</script>
</body>
</html>