<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/CoreCmsServices/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="title" placeholder="请输入项目名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="status">
                        <option value="">请选择项目状态</option>
                        {{# layui.each(d.data.status, function(index, item){ }}
                        <option value="{{ item.value }}">{{ item.description }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select name="validityType">
                        <option value="">请选择核销类型</option>
                        {{# layui.each(d.data.types, 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-CoreCmsServices-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>
        </div>
    </div>
</script>
<div class="table-body">
    <table id="LAY-app-CoreCmsServices-tableBox" lay-filter="LAY-app-CoreCmsServices-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-CoreCmsServices-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>
        <button class="layui-btn layui-btn-sm" lay-event="selectExportExcel"><i class="layui-icon layui-icon-add-circle"></i>选择导出</button>
        <button class="layui-btn layui-btn-sm" lay-event="queryExportExcel"><i class="layui-icon layui-icon-download-circle"></i>查询导出</button>
    </div>
</script>
<script type="text/html" id="LAY-app-CoreCmsServices-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="#CoreCmsServicesTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide" id="CoreCmsServicesTbDelDrop{{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>
            确定要删除吗？
        </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.data); }

        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;

                laydate.render({
                    elem: '#searchTime-CoreCmsServices-startTime',
                    type: 'datetime',
                    range: '到',
                });
                laydate.render({
                    elem: '#searchTime-CoreCmsServices-endTime',
                    type: 'datetime',
                    range: '到',
                });
                laydate.render({
                    elem: '#searchTime-CoreCmsServices-validityStartTime',
                    type: 'datetime',
                    range: '到',
                });
                laydate.render({
                    elem: '#searchTime-CoreCmsServices-validityEndTime',
                    type: 'datetime',
                    range: '到',
                });
                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-CoreCmsServices-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-CoreCmsServices-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsServices-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsServices/GetPageList',
                    method: 'POST',
                    toolbar: '#LAY-app-CoreCmsServices-toolbar',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-189',//面包屑142px,搜索框4行172,3行137,2行102,1行67
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { type: "checkbox", fixed: "left" },
                            { field: 'id', title: '序列', width: 70, sort: false },
                            { field: 'title', title: '项目名称', sort: false, width: 205 },
                            {
                                field: 'thumbnail', title: '缩略图', sort: false, width: 85,
                                templet: function (d) {
                                    if (d.thumbnail) {
                                        return '<a href="javascript:void(0);" onclick=layui.coreHelper.viewImage("' + d.thumbnail + '")><image style="max-width:28px;max-height:28px;" src="' + d.thumbnail + '" /></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>';
                                    }
                                }, width: 60
                            },
                            { field: 'money', title: '售价', sort: false, width: 80, templet: function (data) { return '￥' + data.money } },

                            //{ field: 'description', title: '项目概述', sort: false, width: 105 },
                            //{ field: 'contentBody', title: '项目详细说明', sort: false, width: 105 },
                            {
                                field: 'allowedMembership', title: '允许购买会员级别', sort: false, width: 305, templet: function (data) {
                                    var arr = data.allowedMembership.split(',');
                                    var outStr = "";
                                    for (var i = 0; i < arr.length; i++) {
                                        for (var j = 0; j < d.data.userGrade.length; j++) {
                                            if (d.data.userGrade[j].id == arr[i]) {
                                                outStr += '<span class="layui-badge layui-badge-gray">' + d.data.userGrade[j].title + '</span>&nbsp;&nbsp;';
                                            }
                                        }
                                    }
                                    return outStr;
                                }
                            },
                            {
                                field: 'status',
                                title: '项目状态',
                                sort: false,
                                width: 85,
                                templet: function (data) {
                                    for (var j = 0; j < d.data.status.length; j++) {
                                        if (d.data.status[j].value == data.status) {
                                            return d.data.status[j].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            { field: 'maxBuyNumber', title: '项目重复购买次数', sort: false, width: 125 },
                            { field: 'amount', title: '项目可销售数量', sort: false, width: 105 },
                            { field: 'startTime', title: '项目开始时间', width: 130, sort: false },
                            { field: 'endTime', title: '项目截止时间', width: 130, sort: false },
                            {
                                field: 'validityType', title: '核销有效期类型', sort: false, width: 105,
                                templet: function (data) {
                                    for (var j = 0; j < d.data.types.length; j++) {
                                        if (d.data.types[j].value == data.validityType) {
                                            return d.data.types[j].description;
                                        }
                                    }
                                    return "";
                                }
                            },
                            { field: 'validityStartTime', title: '核销开始时间', width: 130, sort: false },
                            { field: 'validityEndTime', title: '核销结束时间', width: 130, sort: false },
                            { field: 'ticketNumber', title: '核销服务券数量', sort: false, width: 105 },
                            { field: 'createTime', title: '项目创建时间', width: 130, sort: false },
                            { field: 'updateTime', title: '项目更新时间', width: 130, sort: false },
                            { width: 142, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsServices-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-CoreCmsServices-tableBox)', function (obj) {
                    table.reload('LAY-app-CoreCmsServices-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //监听行双击事件
                table.on('rowDouble(LAY-app-CoreCmsServices-tableBox)', function (obj) {
                    //查看详情
                    doDetails(obj);
                });
                //头工具栏事件
                table.on('toolbar(LAY-app-CoreCmsServices-tableBox)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addData':
                            doCreate();
                            break;
                        case 'batchDelete':
                            doBatchDelete(checkStatus);
                            break;
                        case 'selectExportExcel':
                            doSelectExportExcel(checkStatus);
                            break;
                        case 'queryExportExcel':
                            doQueryExportexcel();
                            break;
                    };
                });
                //监听工具条
                table.on('tool(LAY-app-CoreCmsServices-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        } else if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });
                //执行创建操作
                function doCreate() {
                    coreHelper.Post("Api/CoreCmsServices/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['1200px', '90%'],
                                id: 'LAY-popup-CoreCmsServices-create',
                                success: function (layero, index) {
                                    view(this.id).render('serviceGoods/services/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsServices-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.contentBody = window.editor.getData();
                                                if (!!!field.contentBody) {
                                                    layer.msg('文章详情内容不能为空', { icon: 5 });
                                                    return false;
                                                }

                                                if (field.validityType == 2) {
                                                    if (!field.validityStartTime || !field.validityStartTime) {
                                                        layer.msg('请设置【核销有效时段】', { icon: 5 });
                                                        return false;
                                                    }
                                                }

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsServices/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsServices-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsServices-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsServices/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['1200px', '90%'],
                                id: 'LAY-popup-CoreCmsServices-edit',
                                success: function (layero, index) {
                                    view(this.id).render('serviceGoods/services/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsServices-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.contentBody = window.editor.getData();
                                                if (!!!field.contentBody) {
                                                    layer.msg('文章详情内容不能为空', { icon: 5 });
                                                    return false;
                                                }

                                                if (field.validityType == 2) {
                                                    if (!field.validityStartTime || !field.validityStartTime) {
                                                        layer.msg('请设置【核销有效时段】', { icon: 5 });
                                                        return false;
                                                    }
                                                }

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsServices/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reload('LAY-app-CoreCmsServices-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsServices-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行预览操作
                function doDetails(obj) {
                    coreHelper.Post("Api/CoreCmsServices/GetDetails", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '查看详情',
                                area: ['1200px', '90%'],
                                id: 'LAY-popup-CoreCmsServices-details',
                                success: function (layero, index) {
                                    view(this.id).render('serviceGoods/services/details', { data: e.data }).done(function () {
                                        form.render();
                                    });
                                    // 禁止弹窗出现滚动条
                                    //$(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    coreHelper.Post("Api/CoreCmsServices/DoDelete", { id: obj.data.id }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reload('LAY-app-CoreCmsServices-tableBox');
                        layer.msg(e.msg);
                    });
                }
                //执行批量删除
                function doBatchDelete(checkStatus) {
                    var checkData = checkStatus.data;
                    if (checkData.length === 0) {
                        return layer.msg('请选择要删除的数据');
                    }
                    layer.confirm('确定删除吗？删除后将无法恢复。',
                        function (index) {
                            var delidsStr = [];
                            layui.each(checkData,
                                function (index, item) {
                                    delidsStr.push(item.id);
                                });
                            coreHelper.Post("Api/CoreCmsServices/DoBatchDelete", { id: delidsStr }, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                table.reload('LAY-app-CoreCmsServices-tableBox');
                                layer.msg(e.msg);
                            });
                        });
                }
                //执行查询条件导出excel
                function doQueryExportexcel() {
                    layer.confirm('确定根据当前的查询条件导出数据吗？',
                        function (index) {
                            var field = searchwhere;
                            coreHelper.PostForm("Api/CoreCmsServices/QueryExportExcel", field, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                if (e.code === 0) {
                                    window.open(e.data);
                                } else {
                                    layer.msg(e.msg);
                                }
                            });
                        });
                }
                //执行选择目录导出数据
                function doSelectExportExcel(checkStatus) {
                    var checkData = checkStatus.data;
                    if (checkData.length === 0) {
                        return layer.msg('请选择您要导出的数据');
                    }
                    layer.confirm('确定导出选择的内容吗？',
                        function (index) {
                            var delidsStr = [];
                            layui.each(checkData,
                                function (index, item) {
                                    delidsStr.push(item.id);
                                });
                            layer.close(index);
                            coreHelper.Post("Api/CoreCmsServices/SelectExportExcel", { id: delidsStr }, function (e) {
                                if (debug) { console.log(e); } //开启调试返回数据
                                if (e.code === 0) {
                                    window.open(e.data);
                                } else {
                                    layer.msg(e.msg);
                                }
                            });
                        });
                }
                //监听 表格复选框操作

            });
    };
</script>