<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>
    @media screen and (max-width: 500px) {
        .layui-layer.layui-layer-page { width: 100% !important; overflow: hidden !important; left: 0 !important; }
        .layui-layer-title { width: 100% !important; box-sizing: border-box; }
        .layui-layer-content { width: 100% !important; }
    }
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsDistributionGrade/GetIndex" lay-done="layui.data.done(d);">
    <div class="warning-msg" style="border: 1px solid #e6e6e6;margin: 10px 10px 0 10px;">
        <div class="warning-msg-content" style="margin-bottom: 0px;">
            <p>分销等级排序越大，等级越高</p>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsDistributionGrade-tableBox" lay-filter="LAY-app-CoreCmsDistributionGrade-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsDistributionGrade-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
    </div>
</script>

<script type="text/html" id="LAY-app-CoreCmsDistributionGrade-tableBox-bar">
    <!--<a class="layui-btn layui-btn-xs" lay-event="edit">升级及佣金设置</a>-->
    <!--<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"  lay-event="delParent">删除</a>
    
</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', 'dropdown', '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-CoreCmsDistributionGrade-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsDistributionGrade-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsDistributionGrade-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsDistributionGrade/GetPageList',
                    toolbar: '#LAY-app-CoreCmsDistributionGrade-toolbar',
                    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: 80, sort: false },
                            { field: 'name', title: '等级名称', sort: false, width: 205 },
                            { field: 'isDefault', title: '是否默认等级', width: 95, templet: '#switch_isDefault', sort: false, unresize: true },
                            { field: 'isAutoUpGrade', title: '是否自动升级', width: 95, templet: '#switch_isAutoUpGrade', sort: false, unresize: true },
                            { field: 'sortId', title: '等级排序', sort: false, width: 105 },
                            { field: 'description', title: '等级说明', sort: false },
                            { width: 162, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsDistributionGrade-tableBox-bar' }
                        ]
                    ]
                });

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

                //头工具栏事件
                table.on('toolbar(LAY-app-CoreCmsDistributionGrade-tableBox)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addData':
                            doCreate();
                            break;
                    };
                });
                //执行创建操作
                function doCreate() {
                    coreHelper.Post("Api/CoreCmsDistributionGrade/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['1000px', '500px'],
                                id: 'LAY-popup-CoreCmsDistributionGrade-create',
                                success: function (layero, index) {
                                    view(this.id).render('distribution/grade/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsDistributionGrade-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isDefault = field.isDefault == 'on';
                                                field.isAutoUpGrade = field.isAutoUpGrade == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsDistributionGrade/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsDistributionGrade-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 doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsDistributionGrade/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑升级及佣金设置',
                                area: ['1000px', '98%'],
                                id: 'LAY-popup-CoreCmsDistributionGrade-edit',
                                success: function (layero, index) {
                                    view(this.id).render('distribution/grade/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsDistributionGrade-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.isDefault = field.isDefault == 'on';
                                                field.isAutoUpGrade = field.isAutoUpGrade == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsDistributionGrade/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsDistributionGrade-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);
                        }
                    });
                }

                //监听 表格复选框操作

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

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

            });
    };
</script>


<!--设置是否默认等级-->
<script type="text/html" id="switch_isDefault">
    <input type="checkbox" name="switch_isDefault" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isDefault" {{ d.isDefault ? 'checked' : '' }}>
</script>

<!--设置是否自动升级-->
<script type="text/html" id="switch_isAutoUpGrade">
    <input type="checkbox" name="switch_isAutoUpGrade" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="switch_isAutoUpGrade" {{ d.isAutoUpGrade ? 'checked' : '' }}>
</script>
