<?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());
});

?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>insert</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="main-container mr-5">
        <div class="layui-form-item">
            <label class="layui-form-label required">平台</label>
            <div class="layui-input-block">
                <select id="website" name="website" lay-search="" lay-filter="website">
                    <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="first-column" name="rid" 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">
                <input type="text" name="name" value="" required lay-verify="required" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">栏目地址</label>
            <div class="layui-input-block">
                <input type="text" name="url" value="" required lay-verify="required" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">栏目描述</label>
            <div class="layui-input-block">
                <input type="text" name="desc" value="" required lay-verify="required" class="layui-input" autocomplete="off">
            </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="insert">
                    提交
                </button>
            </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 INSERT_API = "/admin/api/website/second-column/insert";
    const UPLOAD_API = '/upload/image' // 上传图片

    layui.use(['upload', 'jquery', 'popup', 'form'], function(){
        const $ = layui.jquery;
        const form = layui.form;

        // 站点
        form.on('select(website)', 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)
            }

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

        layui.form.on("submit(insert)", function (data) {
            console.log(data)
            layui.$.ajax({
                url: INSERT_API,
                type: "POST",
                dateType: "json",
                data: data.field,
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }

                    return layui.popup.success("操作成功", function () {
                        parent.refreshTable();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
