<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <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">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">商品类型</label>
                    <div class="layui-input-block">
                        <select name="type" lay-filter="type">
                            <option value="0">普通区</option>
                            <option value="1">金币区</option>
                            <option value="2">至尊区</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div class="layui-hide" style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        </div>
    </div>
</div>

@verbatim
    <script type="text/html" id="cover">
        <div style="width: 26px;height:26px;display: inline-block;text-align: center;">
            <img src="{{ d.cover}}" style="max-height: 100%;max-width: 100%;cursor: pointer;" lay-event="cover"/>
        </div>
        <span>{{d.goods_name}}</span>
    </script>

    <script type="text/html" id="toolbar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="del">删除</a>
    </script>


    <script type="text/html" id="status">
        <input lay-event="status" type="checkbox" name="status" value="{{ d.id }}" title="上架|下架" lay-skin="switch" lay-filter="status" {{ d.status == 1 ? "checked" : "" }}>
    </script>

    <script type="text/html" id="head-toolbar">
        <div class="layui-table-tool-temp">
            <div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
        </div>
    </script>

@endverbatim
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'form', 'table', 'layer'], function () {
        const $ = layui.$, form = layui.form, table = layui.table, layer = layui.layer;
        form.on('switch(status)', function (obj) {
            let name = this.name;
            let data = {
                id: this.value,
                [name]: obj.elem.checked ? 1 : 0
            };
            $.ajax({
                url: "{{ route('goods.setStatus') }}",
                method: "post",
                headers: {'X-CSRF-TOKEN': "{{ csrf_token() }}"},
                data,
                success(res) {
                    if (res.code !== 0) {
                        layer.msg(res.msg);
                        tableList.reloadData();
                    }
                }
            });
        });

        form.on('select(type)', function (data) {
            tableList.reloadData({
                where: {type: data.value}
            });
        });


        var count = 0, active = {
            add() {
                if (count >= 12) return layer.alert('最多不超过12个选项');
                layer.open({
                    type: 2,
                    title: '添加奖品',
                    content: "{{ route('goods.edit') }}",
                    resize: false,
                    area: ['350px', '360px'],
                    btn: ['确定', '取消'],
                    success(layero, index, that) {
                        let iframe = layero.find('iframe')[0];
                        let iframeWindow = iframe.contentWindow;
                        iframeWindow.layui.form.val('form', {
                            type: tableList.config.where.type,
                        });
                    },
                    end() {
                        tableList.reloadData();
                    },
                    yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        table.on('toolbar(LAY-user-manage)', function (obj) {
            let type = obj.event;
            active[type] ? active[type].call(this) : '';
        });


        // $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        // });
        let tableList = table.render({
            elem: '#LAY-user-manage',
            url: "{{url('goods/getList')}}",
            // toolbar: true,
            toolbar: '#head-toolbar',
            defaultToolbar: [],
            cols: [[
                {field: 'cover', title: '商品', templet: "#cover"},
                {field: 'rank', title: '排序', width: 100, edit: "text",},
                {field: 'probability', title: '权重', width: 100, edit: 'text'},

                {field: 'stock', title: '库存', hide: true},
                {
                    field: 'is_prize', title: '中奖', width: 100, templet(d) {
                        if (d.is_prize === 0) {
                            return "否";
                        } else {
                            return "是";
                        }
                    }
                },
                {title: "状态", width: 100, templet: "#status"},
                {title: '操作', width: 200, toolbar: '#toolbar'}
            ]],
            page: true,
            limit: 50,
            limits: [50, 100, 200, 300],
            where: {lang: 'zh-CN', type: 0},
            height: 'full-130',
            text: {none: '无数据'},
            parseData(res) {
                count = res.data.count;
                return {
                    code: res.data.code,
                    msg: res.data.msg,
                    count: res.data.count,
                    data: res.data.data
                };
            }
        });
        table.on('edit(LAY-user-manage)', function (obj) {

            var value = obj.value;
            var data = obj.data; // 得到所在行所有键值
            let test = /^(0|[1-9]\d*)$/;
            switch (obj.field) {
                case "rank":
                    test = /^(0|[1-9]\d*)$/;
                    if (!test.test(value)) {
                        layer.tips('请输入正确的数值', this, {tips: 1});
                        return obj.reedit();
                    }
                    if (value > 1000) {
                        layer.tips('最大1000', this, {tips: 1});
                        return obj.reedit();
                    }
                    $.ajax({
                        url: "{{ route('goods.setRank') }}",
                        method: "post",
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}",
                        },
                        data: {
                            id: data.id,
                            rank: value
                        },
                        success(res) {
                            if (res.code !== 0) {
                                layer.alert(res.msg);
                                return obj.reedit();
                            } else {
                                tableList.reloadData();
                            }
                        }
                    });


                    break;
                case "probability":
                    test = /^(0|[1-9]\d*)$/;
                    if (!test.test(value)) {
                        layer.tips('请输入正确的数值', this, {tips: 1});
                        return obj.reedit();
                    }
                    if (value > 1000) {
                        layer.tips('最大1000', this, {tips: 1});
                        return obj.reedit();
                    }
                    $.ajax({
                        url: "{{ route('goods.setProbability') }}",
                        method: "post",
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}",
                        },
                        data: {
                            id: data.id,
                            probability: value
                        },
                        success(res) {
                            if (res.code !== 0) {
                                layer.alert(res.msg);
                                return obj.reedit();
                            }
                        }
                    });
                    break;
            }


        });
        table.on('tool(LAY-user-manage)', function (obj) {
            switch (obj.event) {

                case 'cover':

                    layer.photos({
                        toolbar: false,
                        photos: {
                            title: "奖品图片",
                            id: obj.data.id,
                            start: 0,
                            data: [{
                                "alt": obj.data.goods_name,
                                "pid": obj.data.id,
                                "src": obj.data.cover,
                                "thumb": obj.data.cover
                            }]
                        }
                    });


                    break;
                case "del":
                    layer.alert('删除？', function (index) {
                        $.ajax({
                            url: "{{ route('goods.del') }}",
                            method: 'post',
                            data: {id: obj.data.id},
                            headers: {'X-CSRF-TOKEN': "{{ csrf_token() }}"},
                            success(res) {
                                if (res.code === 0) {
                                    layer.close(index);
                                    tableList.reloadData();
                                } else {
                                    layer.msg(res.msg);
                                }
                            }
                        });
                    });
                    break;
                case "edit":
                    layer.open({
                        type: 2,
                        title: '编辑奖品',
                        content: "{{ route('goods.edit') }}",
                        resize: false,
                        area: ['350px', '360px'],
                        btn: ['确定', '取消'],
                        success(layero, index, that) {
                            let iframe = layero.find('iframe')[0];
                            layero.find('iframe').contents().find("#cover").attr('src', obj.data.cover);
                            let iframeWindow = iframe.contentWindow;
                            iframeWindow.layui.form.val('form', {
                                type: obj.data.type,
                                id: obj.data.id,
                                goodsName: obj.data.goods_name,
                                probability: obj.data.probability,
                                cover: obj.data.cover
                            });
                            if (obj.data.is_prize === 1) {
                                iframeWindow.layui.form.val('form', {
                                    isPrize: true
                                });
                            }
                        },
                        end() {
                            tableList.reloadData();
                        },
                        yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                            submit.click();
                        }
                    });
                    break;

                case "video":
                    let src = obj.data.video;
                    let deo = document.createElement('video');
                    deo.src = src;
                    deo.onloadedmetadata = function () {
                        let videoWidth = deo.videoWidth;
                        let videoHeight = deo.videoHeight;
                        let maxWidth = 800;
                        let maxHeight = 600;
                        if (videoWidth > maxWidth) {
                            let scale = maxWidth / videoWidth;
                            videoWidth = maxWidth;
                            videoHeight = videoHeight * scale;
                        }
                        if (videoHeight > maxHeight) {
                            let scale = maxHeight / videoHeight;
                            videoHeight = maxHeight;
                            videoWidth = videoWidth * scale;
                        }
                        layer.open({
                            type: 2,
                            title: false,
                            area: [`${videoWidth}px`, `${videoHeight}px`],
                            shade: 0.8,
                            closeBtn: 0,
                            shadeClose: true,
                            content: src, // video 地址
                        });

                    };
                    break;
                case "mp4":
                    let url = obj.data.photo[0];
                    let video = document.createElement('video');
                    video.src = url;
                    video.onloadedmetadata = function () {
                        let videoWidth = video.videoWidth;
                        let videoHeight = video.videoHeight;
                        let maxWidth = 800;
                        let maxHeight = 600;
                        if (videoWidth > maxWidth) {
                            let scale = maxWidth / videoWidth;
                            videoWidth = maxWidth;
                            videoHeight = videoHeight * scale;
                        }
                        if (videoHeight > maxHeight) {
                            let scale = maxHeight / videoHeight;
                            videoHeight = maxHeight;
                            videoWidth = videoWidth * scale;
                        }

                        layer.open({
                            type: 2,
                            title: false,
                            area: [`${videoWidth}px`, `${videoHeight}px`],
                            shade: 0.8,
                            closeBtn: 0,
                            shadeClose: true,
                            content: url, // video 地址

                        });
                    };


                    layer.msg('点击遮罩区域可关闭');
                    break;
                case "img":
                    let start = this.dataset.index - 1;
                    let data = [];
                    for (let i = 1; i < obj.data.photo.length; i++) {
                        if (obj.data.photo[i]) {
                            data.push({
                                "alt": `个人自拍_${i}`,
                                "pid": (i - 1),
                                "src": obj.data.photo[i],
                                "thumb": obj.data.photo[i],
                            });
                        }
                    }
                    layer.photos({
                        toolbar: false,
                        photos: {
                            title: "个人自拍",
                            id: obj.data.id,
                            start: start,
                            data: data
                        }
                    });
                    break;
            }

            // console.log(this.src);
            // console.log(obj)
        });
        form.on('submit(LAY-user-front-search)', function (data) {
            let where = data.field;
            tableList.reloadData({where});
        });
    });
</script>
</body>
</html>
