<?php
$platforms = call_user_func(function () {
    return \App\Model\AppWebsiteConfig::query()->where('is_delete', 0)->get()->toArray();
});

$firstColumn = call_user_func(function () {
    return json_encode(\App\Model\AppNewsColumn::query()->where('is_delete', 0)->get()->groupBy('website')->toArray());
});

$secondColumn = call_user_func(function () {
    return json_encode(\App\Model\AppNewsSecondColumn::query()->where('is_delete', 0)->get()->groupBy('rid')->toArray());
});
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>导入</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/admin/css/reset.css" />
</head>
<body>

<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container mr-5">
            <div class="layui-form-item">
                <label class="layui-form-label required">发布平台</label>
                <div class="layui-input-block">
                    <select name="platform" lay-search="" lay-filter="platform">
                        <option value="">请选择</option>
                        <?php foreach ($platforms ?: [] as $platform): ?>
                        <option value="<?= $platform['id'] ?>"><?= $platform['app_name'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">栏目</label>
                <div class="layui-input-block">
                    <select id="column" name="column" lay-search="" lay-filter="first-column">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">二级栏目</label>
                <div class="layui-input-block">
                    <select id="second-column" name="column2" lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">文章封面</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="test1">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <img style="max-height: 150px;" src="" id="cover">
                    <input type="hidden" name="cover" value="" class="layui-input" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">json数据</label>
                <div class="layui-input-block">
                    <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="submit">
                        提交
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js?v=2.8.12"></script>
<script src="/component/pear/pear.js"></script>
<script>
    // api
    const SAVE_API = "/admin/api/news/import"; // 插入新闻
    const UPLOAD_API = '/upload/image' // 上传图片

    layui.use(['upload', 'jquery', 'popup'], function(){
        const upload = layui.upload;
        const $ = layui.jquery;
        const form = layui.form;
        // 站点
        form.on('select(platform)', function(data){
            const val = data.value
            var firstColumn = JSON.parse('<?= $firstColumn ?>')
            console.log(firstColumn[val])
            let html = '<option value="">请选择</option>'
            if (firstColumn[val]) {
                firstColumn[val].forEach(item => {
                    html += `<option value="${item.id}">${item.name}</option>`
                })
                console.log(html)
            }

            $('#column').html(html);
            form.render('select'); // 重新渲染 select 以应用新的选项和样式
        });

        // 站点
        form.on('select(first-column)', function(data){
            const val = data.value
            var firstColumn = JSON.parse('<?= $secondColumn ?>')
            console.log(firstColumn[val])
            let html = '<option value="0">请选择</option>'
            if (firstColumn[val]) {
                firstColumn[val].forEach(item => {
                    html += `<option value="${item.id}">${item.name}</option>`
                })
                console.log(html)
            }

            $('#second-column').html(html);
            form.render('select'); // 重新渲染 select 以应用新的选项和样式
        });

        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: UPLOAD_API // 上传接口，实际使用时改成您自己的上传接口即可。
            ,done: function(res){
                //上传完毕回调
                console.log(res)
                // 若上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                // …
                document.querySelector('[id="cover"]').src = res.data.url
                $("input[name=cover]").val(res.data.url);
            }
            ,error: function(){
                //请求异常回调
            }
        });

        layui.form.on("submit(submit)", function (data) {
            var index = layer.load();
            layui.$.ajax({
                url: SAVE_API,
                type: "POST",
                dateType: "json",
                data: data.field,
                success: function (res) {

                    layer.close(index);
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        parent.refreshTable();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }, error: function (res) {
                    layui.popup.failure('请求异常。');
                    layer.close(index);
                }
            });
            return false;
        });
    });
</script>
<style>
  #editor—wrapper {
    border: 1px solid #ccc;
    z-index: 100; /* 按需定义 */
  }
  #toolbar-container {
    border-bottom: 1px solid #ccc;
  }
  #editor-container {
    height: 500px;
  }
</style>
<style>
    .res-options {
        padding: 0 10px;
        line-height: 36px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .layui-upload-list {
        margin-top: 20px;
        display: grid;
        grid-template-columns: repeat(auto-fill, 120px);
        gap: 15px;
    }

    .img-item {
        position: relative;
        border: 1px solid #e6e6e6;
        border-radius: 4px;
        cursor: move;
    }

    .img-preview {
        width: 100%;
        height: 120px;
        object-fit: contain;
    }

    .img-operate {
        position: absolute;
        top: 5px;
        right: 5px;
        opacity: 0;
        transition: .3s;
    }

    .img-item:hover .img-operate {
        opacity: 1;
    }

    #images-tpl-container .layui-icon-close,
    #images-tpl-container .layui-icon-util {
        color: #fff;
        background: #ff5722;
        border-radius: 50%;
        padding: 2px;
        cursor: pointer;
    }

    .layui-upload-list {
        /* 保持原有样式 */
        margin-top: 20px;
        display: grid;
        grid-template-columns: repeat(auto-fill, 120px);
        gap: 15px;
    }
    /* 新增显示限制 */
    .img-item:nth-child(n+31) {
        display: none;
    }
    .load-more-btn {
        grid-column: 1 / -1;
        text-align: center;
        padding: 10px 0;
    }
</style>
</body>
</html>
