<title>代理商表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsAgent/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-search-form">
        <div class="layui-form-item">

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="请输入代理商名称" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="mobile" placeholder="请输入手机号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="weixin" placeholder="请输入微信号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="qq" placeholder="请输入qq号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="storeName" placeholder="请输入店铺名称" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="verifyStatus">
                        <option value="">请选择审核状态</option>
                        {{# layui.each(d.data.agentVerifyStatus, function(index, item){ }}
                        <option value="{{ item.value }}">{{ item.description }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="gradeId">
                        <option value="">请选择代理等级</option>
                        {{# layui.each(d.data.grades, function(index, item){ }}
                        <option value="{{ item.value }}">{{ item.description }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-CoreCmsAgent-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsAgent-tableBox" lay-filter="LAY-app-CoreCmsAgent-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsAgent-tableBox-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-dropdown="#CoreCmsAgentTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide" id="CoreCmsAgentTbDelDrop{{d.LAY_INDEX}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除【{{d.name}}】吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn layui-btn-primary cursor" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal cursor" lay-event="del">确定</a>
        </div>
    </div>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coredropdown', 'coreHelper'],
            function () {
                var $ = layui.$, admin = layui.admin, table = layui.table, form = layui.form, laydate = layui.laydate, setter = layui.setter, coreHelper = layui.coreHelper, util = layui.util, view = layui.view;

                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-CoreCmsAgent-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsAgent-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsAgent-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsAgent/GetPageList',
                    method: 'POST',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-189',//无面包屑127,搜索框189,1行62
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            //{ type: "checkbox", fixed: "left" },
                            { field: 'id', title: '序列', width: 60, sort: false },
                            //{ field: 'userId', title: '用户Id', sort: false, width: 105 },
                            { field: 'name', title: '代理商名称', sort: false, width: 105 },
                            {
                                field: 'gradeId', title: '代理等级', sort: false, width: 105, templet: function (data) {
                                    for (var i = 0; i < d.data.grades.length; i++) {
                                        if (d.data.grades[i].id == data.gradeId) {
                                            return d.data.grades[i].name;
                                        }
                                    }
                                }
                            },
                            //{ field: 'gradeId', title: '代理等级', sort: false, width: 105 },
                            { field: 'mobile', title: '手机号', sort: false, width: 90 },
                            { field: 'weixin', title: '微信号', sort: false, width: 90 },
                            { field: 'qq', title: 'qq号', sort: false, width: 90 },
                            { field: 'storeName', title: '店铺名称', sort: false },
                            {
                                field: 'storeLogo', title: '店铺Logo', sort: false, width: 80, templet: function (d) {
                                    if (d.storeLogo) {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + d.storeLogo + '")><image style="max-width:28px;max-height:28px;" src="' + d.storeLogo + '" /></a>';
                                    } else {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + setter.noImagePicUrl + '")><image style="max-width:30px;max-height:30px;" src="' + setter.noImagePicUrl + '" /></a>';
                                    }
                                }
                            },
                            {
                                field: 'storeBanner', title: '店铺Banner', sort: false, width: 80, templet: function (d) {
                                    if (d.storeBanner) {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + d.storeBanner + '")><image style="max-width:28px;max-height:28px;" src="' + d.storeBanner + '" /></a>';
                                    } else {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + setter.noImagePicUrl + '")><image style="max-width:30px;max-height:30px;" src="' + setter.noImagePicUrl + '" /></a>';
                                    }
                                }
                            },
                            //{ field: 'storeDesc', title: '店铺简介', sort: false, width: 105 },
                            {
                                field: 'verifyStatus', title: '审核状态', sort: false, width: 80, templet: function (data) {
                                    for (var i = 0; i < d.data.agentVerifyStatus.length; i++) {
                                        if (d.data.agentVerifyStatus[i].value == data.verifyStatus) {
                                            return d.data.agentVerifyStatus[i].description;
                                        }
                                    }
                                }
                            },
                            { field: 'createTime', title: '创建时间', width: 130, sort: false },
                            { field: 'updateTime', title: '更新时间', width: 130, sort: false },
                            { field: 'verifyTime', title: '审核时间', width: 130, sort: false },
                            //{ field: 'isDelete', title: '是否删除', width: 95, templet: '#switch_isDelete', sort: false, unresize: true },
                            { width: 142, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsAgent-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-CoreCmsAgent-tableBox)', function (obj) {
                    table.reload('LAY-app-CoreCmsAgent-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //监听行双击事件
                table.on('rowDouble(LAY-app-CoreCmsAgent-tableBox)', function (obj) {
                    //查看详情
                    doDetails(obj);
                });

                //监听工具条
                table.on('tool(LAY-app-CoreCmsAgent-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        } else if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });

                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsAgent/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['640px', '300px'],
                                id: 'LAY-popup-CoreCmsAgent-edit',
                                success: function (layero, index) {
                                    view(this.id).render('Agent/agents/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsAgent-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isDelete = field.isDelete == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsAgent/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsAgent-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行预览操作
                function doDetails(obj) {
                    coreHelper.Post("Api/CoreCmsAgent/GetDetails", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '查看详情',
                                area: ['700px', '550px'],
                                id: 'LAY-popup-CoreCmsAgent-details',
                                success: function (layero, index) {
                                    view(this.id).render('Agent/agents/details', { data: e.data }).done(function () {
                                        form.render();
                                    });
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    //console.log(obj.data.id);
                    coreHelper.Post("Api/CoreCmsAgent/DoDelete", { id: obj.data.id }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reload('LAY-app-CoreCmsAgent-tableBox');
                        layer.msg(e.msg);
                    });
                }

                //监听 表格复选框操作

                layui.form.on('switch(switch_isDelete)', function (obj) {
                    coreHelper.Post("Api/CoreCmsAgent/DoSetisDelete", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reload('LAY-app-CoreCmsAgent-tableBox');
                        layer.msg(e.msg);
                    });
                });

            });
    };
</script>

<!--设置是否删除-->
<script type="text/html" id="switch_isDelete">
    <input type="checkbox" name="switch_isDelete" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isDelete" {{ d.isDelete ? 'checked' : '' }}>
</script>