<%@ 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="JXSGL_JXSGL_JXSGL_T" html='<li class="layui-this"><a href="${ctx}/trader/index">代理商管理</a></li>'/>
                        <sfTags:pagePermission authFlag="KHGL_DLSGL_HTGL_T" html='<li><a href="${ctx}/trader/contract/listPage">合同管理</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-md3 layui-col-sm3">
                    <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-md3 layui-col-sm3">
                    <label class="layui-form-label">联系人：</label>
                    <div class="layui-input-block">
                        <input type="text" name="contacts" class="layui-input" id="contacts" >
                    </div>
                </div>
                <div class="layui-col-md3 layui-col-sm3">
                    <label class="layui-form-label">联系电话：</label>
                    <div class="layui-input-block">
                        <input type="text" name="mobile" class="layui-input" id="mobile" >
                    </div>
                </div>
                <div class="layui-col-md3 layui-col-sm3">
                    <label class="layui-form-label">申请时间：</label>
                    <div class="layui-input-block">
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeMin" class="layui-input pointer" readonly
                                   id="createTimeMin"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs1 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeMax" class="layui-input pointer" readonly
                                   id="createTimeMax"
                                   placeholder="结束日期">
                        </div>
                    </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="JXSGL_JXSGL_JXSGL_EXPORT_B" html='<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;
        //代理商信息列表
        var cskc = '${fns:checkPagePermission(request, "JXSGL_JXSGL_JXSGL_CSKC_B")}';
        var detail = '${fns:checkPagePermission(request, "JXSGL_JXSGL_JXSGL_EDIT_B")}';
        var forbid = '${fns:checkPagePermission(request, "JXSGL_JXSGL_JXSGL_FORBID_B")}';
        var enable = '${fns:checkPagePermission(request, "JXSGL_JXSGL_JXSGL_ENABLE_B")}';
        var type = "${user.userType}";
        tableExt.render({
            elem: '#test'
            , height: 'full-145'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/trader/list?type=1'
            , 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 : '250',align : 'center',  fixed: 'left',templet:function(d){
                        var html = "";
                        console.log('detail'+detail);
                        if(detail=='0'){
                            html += "<a href=\"#\" class=\"layui-table-link\" onclick=\"editApply('"+d.id+"')\">编辑</a>";
                        }
                        if(d.contract_signing =='0'){
                            html += " &nbsp;&nbsp; <a href=\"#\" class=\"layui-table-link\" onclick=\"inviteSign('"+d.id+"')\">邀请签约</a>";
                        }else
                        if(d.contract_signing =='1'){
                            html += " &nbsp;&nbsp; <a href=\"#\" class=\"layui-table-link\" onclick=\"impower('"+d.id+"')\">授权</a>";
                        }
                        if(cskc==='0'){
                            html += "&nbsp;&nbsp;<a href=\"#\" class=\"layui-table-link\" onclick=\"inintstock('"+d.id+"')\">初始化库存</a>";
                        }
                            if (d.userStatus === '0') {
                                if (forbid === '0') {
                                    html += '&nbsp;&nbsp;<a href="#" class="layui-table-link" onclick="changeStatus(\'' + d.id + '\',\'1\')">禁用</a>';
                                }
                            } else {
                                if (enable === '0') {
                                    html += '&nbsp;&nbsp;<a href="#" class="layui-table-link" onclick="changeStatus(\'' + d.id + '\',\'0\')">启用</a>';
                                }
                            }
                        return html;
                    }}

                , {field: 'name', title: '代理商名称', minWidth: 260, align: 'center',exportable:true , fixed: 'left',templet:function(d){
                            var html = "&nbsp;&nbsp;<a href=\"#\" class=\"layui-table-link\" onclick=\"traderDetail('"+d.id+"')\">"+d.name+"</a>";

                            return html;
                        }}
                , {field: 'status', title: '状态', minWidth: 100, align: 'center', exportable:true,
                    templet: function (d) {
                        if (d.userStatus === '0') {
                            return '已启用';
                        } else {
                            return '已禁用';
                        }
                    }, expFormat: function (d) {
                        if (d.userStatus === '0') {
                            return '已启用';
                        } else {
                            return '已禁用';
                        }
                    }
                }
                ,{field: 'qqCode', title: '代理商编码', minWidth: 120, align: 'center',exportable:true }
                , {field: 'actual_contacts', title: '实控人', minWidth: 120, align: 'center',exportable:true }
                , {field: 'telephone', title: '联系电话', minWidth: 120, align: 'center',exportable:true }

                , {field: 'contacts', minWidth: 150, title: '联系人', align: 'center',exportable:true }
                , {field: 'mobile', minWidth: 180, title: '联系人电话', align: 'center',exportable:true}
                , {field: 'login_name', title: '登录账号', minWidth: 200, align: 'center',exportable:true }
                ,{field:'bond', title: '打款保证金',align: 'center',minWidth: 120,exportable:true}
                ,{field:'bank_name', title: '开户银行',align: 'center',minWidth: 200,exportable:true}
                ,{field:'bank_code', title: '银行账号',align: 'center',minWidth: 180,exportable:true}

                ,{field:'tax_number', title: '统一信用代码',align: 'center',minWidth: 180,exportable:true}
                ,{field:'traderAddressfrom', title: '代理商地址',align: 'center',minWidth: 300,exportable:true}
                ,{field:'takeAddress', title: '收货地址',align: 'center',minWidth: 300,exportable:true}
                ,{field:'remarks', title: '申请备注',align: 'center',minWidth: 180,exportable:true}
                ,{field:'creator', title: '申请人',align: 'center',minWidth: 100,exportable:true}
                ,{field:'department_name', title: '所属部门',align: 'center',minWidth: 150,exportable:true}
                ,{field:'apply_time', title: '申请时间',align: 'center',minWidth: 165,exportable:true}
                ,{field:'create_time', title: '准入时间',align: 'center',minWidth: 165,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"));
                }
            }
        });
        //开始日期
        var createTimeMin = laydate.render({
            elem: '#createTimeMin'
            , type: 'date'
            , done: function (value, date) {
                //更新结束日期的最小日期
                createTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                createTimeMax.config.elem[0].focus();
            }
        });
        //结束日期
        var createTimeMax = laydate.render({
            elem: '#createTimeMax'
            , type: 'date'
            , done: function (value, date) {
                //更新开始日期的最大日期
                createTimeMin.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });


        table.on('toolbar(test)',function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
           if (obj.event === 'delete') {
                var data = checkStatus.data;
                if (data.length < 1) {
                    layer.msg("请先选择数据",{icon:5,anim:6});
                    return;
                }
                var ids="";
                for(var i=0;i<data.length;i++){
                    ids+=data[i].id+",";
                }
                var indexla = layer.confirm('您确定要删除代理商吗？', {
                    icon: 3,
                    btn: ['确定', '取消'] //按钮
                }, function () {
                    $.ajax({
                        type: "POST",
                        url: "${ctx}/trader/delete",
                        data: {
                            ids: ids
                        },
                        success: function (result) {
                            if(result.code=='200'){
                                layer.msg('删除成功！', {icon: 1, time: 1000}, function () {
                                    window.location.reload();
                                });
                            }else {
                                layer.msg('删除失败!', {icon: 2});
                            }
                        },
                        error:{
                        }
                    })
                }, function () {
                    layer.close(indexla);
                });
            }
        });

        //监听事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'add') {//添加地址
                var editId = data.id;
                layer.open({type: 2,
                    title: '添加地址',
                    content: '${ctx}/siteInformation/addRess?id='+editId,
                    area: ['500px', '350px'],
                    btn : [ '保存', '取消' ],
                    yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-addRess'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            addMark(field, index,editId);
                        });
                        submit.trigger('click');
                    },
                    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");
                    }
                });
            }
        });

        $('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 noRepeatAdd = false;
        function addMark(data, index,id) {
            if (noRepeatAdd) {
                return;
            }
            noRepeatAdd = true;
            $.ajax({
                url: '${ctx}/siteInformation/addRessSave',
                type: 'post',
                data: {
                    id:id,
                    province: data.province,
                    city: data.city,
                    area: data.area,
                    address: data.address
                },
                success: function (data) {
                    if (data.code == "200") {
                        layer.msg("添加成功！", {icon: 1});
                        table.reload('test'); //数据刷新
                        layer.close(index); //关闭弹层
                    } else if (data.code == "205") {
                        layer.msg("添加失败！", {icon: 2});
                        table.reload('test'); //数据刷新
                    }
                },
                complete: function () {
                    noRepeatAdd = false;
                }
            });
        }

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

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



    function  denglsj(id,userId) {
        console.log("siteInfo" + id +" userId : "+ userId)
        parent.denglusdj(id,userId);
    }
    function editApply(id) {
        layer.open({
            type: 2,
            title:"编辑代理商信息",
            area: ['90%', '90%'],
            resize:false,
            content: '${ctx}/trader/editApply?id='+id
        });
    }
    /*邀请签约*/
    function inviteSign(id){
        layer.open({
            type: 2
            ,title: '邀请签约'
            ,content: '${ctx}/trader/inviteSign?id='+id
            ,area: ['90%','70%']
            ,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");
            }
        });
    }

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

    function impower(id){
        layer.open({
            type: 2
            ,title: '授权'
            ,content: '${ctx}/trader/toEmpower?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");
            }
        });
    }

    function inintstock(id){
        layer.open({
            type: 2
            ,title: '初始化库存'
            ,content: '${ctx}/trader/toInint?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");
            }
        });
    }



    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }
    //代理商禁用启用
    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/first/changeStatus",
                data: {
                    id: id,
                    status: status
                },
                success: function (result) {
                    if (result.code == "200") {
                        layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg('操作失败！', {icon: 2})
                    }
                }
            });
        }, function () {
            layer.close(indexla)
        });
    }
</script>
</body>
</html>
