<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base"/>
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
</head>
<body>
   <div class="layui-fluid">
       <div class="layui-form layui-card">
           <div class="layui-tab layui-tab-brief">
               <ul class="layui-tab-title">
                   <sfTags:pagePermission authFlag="EJDLS_EJDLS_EJDLS_T"
                                          html='<li class="layui-this"><a href="${ctx}/trader/second/index">二级代理商</a></li>'/>
                   <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset" onclick="reset()">
                       <i class="layui-icon fs-12">&#xe9aa;</i>
                       重置
                   </button>
                   <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10" id="searchBtn" lay-submit
                           lay-filter="LAY-user-back-search"><i class="layui-icon fs-12">&#xe615;</i>查询
                   </button>
               </ul>
           </div>
           <div class="layui-card-body layuiadmin-card-header-auto" id="searchForm">
               <div class="layui-form-item">
                   <div class="layui-col-md2 layui-col-sm2">
                       <label class="layui-form-label">代理商名称：</label>
                       <div class="layui-input-block">
                           <input type="text" name="name" class="layui-input" id="name">
                       </div>
                   </div>
                   <div class="layui-col-md2 layui-col-sm2">
                       <label class="layui-form-label">状态：</label>
                       <div class="layui-input-block">
                           <select name="status">
                               <option value="">请选择</option>
                               <option value="0">已启用</option>
                               <option value="1">已禁用</option>
                           </select>
                       </div>
                   </div>
               </div>
           </div>
       </div>
       <div class="layui-card">
           <div class="layui-card-body">
               <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
               <script type="text/html" id="toolbarDemo">
                   <div class="layui-btn-container">
                       <sfTags:pagePermission authFlag="EJDLS_ADD_B" html='<button class="layui-btn layui-btn-sm" id="addBtn" lay-event="addBtn"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
                       <button class="layui-btn layui-btn-sm" id="exportBtn" lay-event="exports"><i class="layui-icon layui-icon-export"></i>导出</button>
                   </div>
               </script>
           </div>
       </div>
   </div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/data.js"></script>
<script type="text/javascript">
    var saveMark = false;
    //初始数据
    var $form;
    var form;
    var upload;
    var $;
    var table;
    var laydate;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form','excel'], function(){
        $ = layui.$
            ,form = layui.form
            ,tableExt = layui.tableExt;
        laydate = layui.laydate;
        table = layui.table;
        //代理商信息列表
        let editFlag = '${fns:checkPagePermission(request, "EJDLS_EDIT_B")}';
        let deleteFlag = '${fns:checkPagePermission(request, "EJDLS_DELETE_B")}';
        let sqFlag = '${fns:checkPagePermission(request, "EJDLS_EJDLS_EJDLS_T_SQ")}';
        tableExt.render({
            elem: '#test'
            , height: 'full-145'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/trader/list?type=2'
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , page: true
            , toolbar:"#toolbarDemo"
            , method:"post"
            , limits: [20, 50, 100]
            , limit: 20
            , cols: [[
                {field: 'number', minWidth: 50, title: '序号', align: 'center', type: 'numbers', fixed: 'left'}
                , {field: 'id', title: '操作', minWidth : '150', align: 'center', fixed: 'left', templet:function(d){
                        let html = '';
                        if (editFlag === '0') {
                            html += '<a href="#" class="layui-table-link" onclick="editApply(\'' + d.id + '\')">编辑</a>&nbsp;&nbsp;&nbsp;';

                        }
                        if (deleteFlag === '0') {
                            if (d.status === '0') {
                                if (sqFlag === '0'){
                                    html += " <a href=\"#\" class=\"layui-table-link\" onclick=\"impower('"+d.id+"')\">授权</a> &nbsp;&nbsp;&nbsp;";
                                }
                                html += '<a href="#" class="layui-table-link" onclick="changeStatus(\'' + d.id + '\',\'1\')">禁用</a>';
                            } else {
                                html += '<a href="#" class="layui-table-link" onclick="changeStatus(\'' + d.id + '\',\'0\')">启用</a>';
                            }
                        }
                        if (isBlank(html)) {
                            html = '--';
                        }
                        return html;
                    }
                }
                , {field: 'name', title: '代理商名称', minWidth: 150, align: 'center',exportable:true , fixed: 'left',
                    templet: function (d) {
                        return "<a href=\"#\" class=\"layui-table-link\" onclick=\"traderDetail('" + d.id + "')\">" + d.name + "</a>";
                    }
                }
                , {field: 'status', title: '状态', minWidth: 100, align: 'center', exportable:true,
                    templet: function (d) {
                        if (d.status === '0') {
                            return '已启用';
                        } else {
                            return '已禁用';
                        }
                    }, expFormat: function (d) {
                        if (d.status === '0') {
                            return '已启用';
                        } else {
                            return '已禁用';
                        }
                    }
                }
                , {field: 'parent_name', title: '一级代理商', minWidth: 150, align: 'center',exportable:true }
                , {field: 'login_name', title: '账号', minWidth: 200, align: 'center',exportable:true }
                , {field: 'creator', title: '登记人',align: 'center',minWidth: 150,exportable:true}
                , {field: 'create_time', title: '登记时间',align: 'center',minWidth: 200,exportable:true}
            ],
            ]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '二级代理商信息',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '二级代理商信息',
                where: function (){
                 //   console.log(_serializeJson($("#searchForm")));//当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(test)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'addBtn') {
                layer.open({
                    type: 2,
                    title: '新增',
                    content: '${ctx}/trader/second/add',
                    area: ['50%', '60%'],
                });
            }
        });

        $('input').on('keydown', function(event){
            if(event.keyCode == 13){
            //执行相关操作
                console.log($('#name').val().trim())
                $("#searchBtn").click();
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field,
                page: {
                    curr: 1
                }
            });
        });

        var active = {
            reload: function(){
                reload();
            }
        };
    });

    function reload(){
        table.reload('test', {
            where: {
                name: $('#name').val().trim(),
                province:$("#province").val().trim(),
                telephone:$("#telephone").val().trim()
            }
        });
    }

    function editApply(id) {
        layer.open({
            type: 2,
            title:"编辑代理商信息",
            area: ['98%', '90%'],
            resize:false,
            content: '${ctx}/trader/second/add?id='+id
        });
    }

    function changeStatus(id, status) {
        let content = "启用后，该代理商及其员工账号都将可以登录系统操作，确认启用该代理商吗？";
        if (status === '1') {
            content = "禁用后，该代理商及其员工账号都不能再登录使用，确认禁用该代理商吗？";
        }
        var indexla = layer.confirm(content, {
            icon: 3,
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: "POST",
                url: "${ctx}/trader/second/changeStatus",
                data: {
                    id: id,
                    status: status
                },
                success: function (result) {
                    if (result.code == "200") {
                        layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                            window.location.reload();
                        });
                    } else if(result.code == "206"){
                        layer.msg('一级代理商处于禁用状态，请先启用一级代理商！', {icon: 2})
                    }else{
                        layer.msg('操作失败！', {icon: 2})
                    }
                }
            });
        }, function () {
            layer.close(indexla)
        });
    }

    // 代理商信息详情
    function traderDetail(id) {
        layer.open({
            type: 2,
            title: "代理商信息",
            area: ['98%', '90%'],
            resize: false,
            content: '${ctx}/trader/second/detail?id=' + id
        });
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }

    function impower(id){
        layer.open({
            type: 2
            ,title: '授权'
            ,content: '${ctx}/trader/toTwoEmpower?id='+id
            ,area: ['80%','100%']
            ,success : function(layero) {
                layero.find('.layui-layer-btn').css('text-align', 'center');
                layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
            }
        });
    }


</script>
</body>
</html>
