<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>设置我的资料</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-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">网站设置</div>
                <div class="layui-card-body" pad15>

                    <div class="layui-form" lay-filter="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">欢迎页图片</label>
                            <div class="layui-input-inline">
                                <input name="welcomeImg" lay-verify="required" id="LAY_avatarSrc" placeholder="图片地址" value="{{$welcomeImg}}" class="layui-input">
                            </div>
                            <div class="layui-input-inline layui-btn-container" style="width: auto;">
                                <button type="button" class="layui-btn layui-btn-primary" id="welcomePage">
                                    <i class="layui-icon">&#xe67c;</i>上传图片
                                </button>
                                <button class="layui-btn layui-btn-primary" layadmin-event="avartatPreview">查看图片
                                </button>
                            </div>
                        </div>




                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="setMyInfo">确认修改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'admin', 'upload', 'layer'], function () {
        const $ = layui.$, form = layui.form, admin = layui.admin, upload = layui.upload, layer = layui.layer;

        var avatarSrc = $('#LAY_avatarSrc');


        upload.render({
            url: '/upload-image',
            elem: "#welcomePage",
            field: "image",
            data: {
                _token: "{{ csrf_token() }}",
            },
            done: function (res) {
                if (res.code === 0) {
                    avatarSrc.val(res.data.path);
                } else if (res.code === 101009) {
                    let msg = "";

                    Object.keys(res.data).forEach(key => {
                        res.data[key].forEach(m => {
                            msg += m;
                        });
                    });


                    layer.alert(msg, {
                        title: res.msg
                    });
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });


        //查看头像
        admin.events.avartatPreview = function (othis) {
            var src = avatarSrc.val();
            layer.photos({
                photos: {
                    title: "查看图片",
                    data: [{
                        src: src
                    }]
                },
                shade: 0.01,
                closeBtn: 1,
                anim: 5
            });
        };


        form.on('submit(setMyInfo)', function (obj) {
            let data = obj.field;
            console.log(data);
            $.ajax({
                url: "{{ route('set.site.set') }}",
                method: "post",
                headers: {'X-CSRF-TOKEN': "{{ csrf_token() }}"}, data,
                success(res) {
                    if (res.code !== 0) layer.alert(res.msg);
                    else layer.msg('Success');
                }
            });

        });

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