<!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>

        <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="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" data-id="{{ d.id }}" 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;


        function changeSwitchStatus(rowId, status) {
            var switchElement = $('input[data-id="' + rowId + '"]');
            if (switchElement.length) {
                switchElement.prop('checked', status);
                form.render('checkbox');
            }
        }

        form.on('switch(status)', function (obj) {
            let name = this.name;

            let data = {
                id: this.value,
                [name]: obj.elem.checked ? 1 : 0
            };
            $.ajax({
                url: "{{ route('wallet.setStatus') }}",
                method: "post",
                headers: {'X-CSRF-TOKEN': "{{ csrf_token() }}"},
                data,
                success(res) {
                    if (res.code !== 0) {
                        layer.msg(res.msg);
                        changeSwitchStatus(obj.elem.dataset.id, !obj.elem.checked);
                    } else {


                    }
                }
            });
        });
        var count = 0, active = {
            add() {

                layer.open({
                    type: 2,
                    title: '添加产品',
                    content: "{{ route('wallet.edit') }}",
                    resize: false,
                    area: ['350px', '350px'],
                    btn: ['确定', '取消'],
                    success(layero, index, that) {
                        let iframe = layero.find('iframe')[0];
                        let iframeWindow = iframe.contentWindow;
                        iframeWindow.layui.form.val('form', {
                            status: true
                        });
                    },
                    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) : '';
        });
        let tableList = table.render({
            elem: '#LAY-user-manage',
            url: "{{url('wallet/getList')}}",
            toolbar: '#head-toolbar',
            defaultToolbar: [],
            cols: [[
                {field: 'name', title: '产品名称'},
                {field: 'product_id', title: '产品ID'},
                {field: 'price', title: '售价（$）'},
                {field: 'wallet', title: '实际到账（$）'},
                {title: "状态", width: 100, templet: "#status"},
                {title: '操作', width: 200, toolbar: '#toolbar'}
            ]],
            page: false,
            where: {lang: 'zh-CN'},
            height: 'full-80',
            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('tool(LAY-user-manage)', function (obj) {
            switch (obj.event) {
                case "del":
                    layer.alert('删除？', function (index) {
                        $.ajax({
                            url: "{{ route('wallet.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('wallet.edit') }}",
                        resize: false,
                        area: ['350px', '350px'],
                        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', {
                                name: obj.data.name,
                                id: obj.data.id,
                                productId: obj.data.product_id,
                                price: obj.data.price,
                                wallet: obj.data.wallet,
                                status: obj.data.status,
                            });
                            if (obj.data.status === 1) {
                                iframeWindow.layui.form.val('form', {
                                    status: true
                                });
                            }
                        },
                        end() {
                            tableList.reloadData();
                        },
                        yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                            submit.click();
                        }
                    });
                    break;
            }
        });
    });
</script>
</body>
</html>
