<!DOCTYPE html>
<html>
<head>
    <:include file="../common/css.html" title="轮播图管理"/>
    <style>
        .layui-upload-img {
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            轮播图管理
        </div>
        <div class="layui-card-body">
            <!-- 数据表格 -->
            <table id="carouselTable" lay-filter="carouselTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="carouselTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"
       data-dropdown="#carouselTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"
         id="carouselTbDelDrop{{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" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>
        </div>
    </div>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="carouselEditDialog">
    <form id="carouselEditForm" lay-filter="carouselEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播标题:</label>
            <div class="layui-input-block">
                <input name="title" placeholder="请输入标题" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">链接地址:</label>
            <div class="layui-input-block">
                <input name="link" placeholder="请输入对应的链接地址" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 15px">
            <label class="layui-form-label">轮播图片:</label>
            <div class="layui-input-block" style="width: auto">
                <input id="imageLogo" name="imageLogo" placeholder="轮播图片/可使用外链" class="layui-input"/>
                <div class="layui-upload" style="margin-top: 10px">
                    <button type="button" class="layui-btn" id="image">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="image_url">
                    </div>
                </div>
                <div class="layui-word-aux">推荐图片尺寸为【710x250】</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">简介:</label>
            <div class="layui-input-block">
                <textarea name="summary" lay-verify="required" placeholder="请输入简介" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">前台显示:</label>
            <div class="layui-input-block">
                <input type="radio" name="enabled" value="1" title="显示" checked>
                <input type="radio" name="enabled" value="0" title="下架">
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="carouselEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- 分类状态 -->
<script type="text/html" id="carouselTbStatus">
    <input type="checkbox" lay-filter="carouselTbStatuCk" value="{{d.id}}" lay-skin="switch"
           lay-text="显示|下架" {{d.enabled==1?'checked':''}}/>
</script>

<:include file="../common/js.html"/>
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'util', 'admin', 'dropdown', 'fileChoose'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;
        var fileChoose = layui.fileChoose;

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#carouselTable',
            url: 'carousel/page',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加轮播图</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'title', title: '轮播标题', maxWidth: 160},
                {field: 'link', title: '链接地址', maxWidth: 160},
                {field: 'imageLogo', title: '图片地址'},
                {field: 'summary', title: '简介'},
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },
                {field: 'enabled', title: '前台显示', templet: '#carouselTbStatus'},
                {title: '操作', toolbar: '#carouselTbBar', align: 'center', width: 200, maxWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(carouselTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(carouselTable)', function (obj) {
            if ('edit' === obj.event) { // 修改
                showEditModel(obj.data);
            } else if ('del' === obj.event) { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(carouselTable)', function (obj) {
            if ('add' === obj.event) { // 添加
                showEditModel();
            } else if ('del' === obj.event) { // 删除
                var checkRows = table.checkStatus('carouselTable');
                if (0 === checkRows.data.length) {
                    return layer.msg('请选择要删除的数据', {icon: 2, anim: 6});
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '轮播图',
                content: $('#carouselEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('carouselEditForm', mData);
                    if (mData != null) {
                        $('#image_url').attr('src', mData.imageLogo); //图片链接
                    }
                    // 表单提交事件
                    form.on('submit(carouselEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? 'carousel/update' : 'carousel/save', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2, anim: 6});
                            }
                        });
                        return false;
                    });

                    $('#image').click(function (res) {
                        fileChoose.open({
                            fileUrl: '${ctxPath}/file',
                            listUrl: '${ctxPath}/file/list',
                            upload: {url: '${ctxPath}/file/upload'},
                            num: 1,
                            onChoose: function (urls) {
                                var list = urls;
                                $("#imageLogo").val("/file" + list[0].url);
                                $('#image_url').attr('src', "${ctxPath}/file" + list[0].url); //图片链接（base64）
                            },
                            response: {code: 0}
                        });
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            if (obj.ids) {  // 批量删除
                admin.confirm('确定要删除选中数据吗？', function (i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    admin.req('carousel/removeBatch', JSON.stringify(obj.ids), function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    }, 'post');
                });
            } else {  // 单个删除
                var loadIndex = layer.load(2);
                $.get('carousel/remove', {id: obj.data.id}, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2, anim: 6});
                    }
                });
            }
        }

        /* 修改状态 */
        form.on('switch(carouselTbStatuCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.post('carousel/status/update', {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.close(loadIndex);
                if (0 === res.code) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox', $('#carouselTable').next().attr('lay-filter'));
                }
            });
        });

    });
</script>
</body>
</html>