<?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>更新</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="">
    <input type="hidden" name="id"  autocomplete="off">
    <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="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="save">
                    提交
                </button>
            </div>
        </div>
    </div>
</form>
<div id="test"></div>
<script src="/component/layui/layui.js?v=2.8.12"></script>
<script src="/component/pear/pear.js"></script>
<script>
    // API
    const VIEW_API = "/admin/api/website/second-column/view"; // 获取关键词数据
    const SAVE_API = "/admin/api/website/second-column/save"; // 重新保存数据
    const UPLOAD_API = '/upload/image' // 上传图片

    layui.use(['upload', 'jquery', 'popup', 'form'], function(){
        const $ = layui.jquery;
        const form = layui.form
        const col = JSON.parse('<?= $firstColumn ?>')
        layui.form.on("submit(save)", function (data) {
            layui.$.ajax({
                url: SAVE_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;
        });

        // 站点
        form.on('select(website)', function(data){
            const val = data.value
            let html = '<option value="">请选择</option>'
            if (col[val]) {
                col[val].forEach(item => {
                    html += `<option value="${item.id}">${item.name}</option>`
                })
            }

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

        initViewData()
        function initViewData() {
            let viewId = layui.url().search['id']
            $.ajax({
                url: VIEW_API,
                data: {
                    id: viewId
                },
                method: 'get',
                dataType: "json",
                success: function (res) {
                    // ajax产生错误
                    if (res.code) {
                        layui.popup.failure(res.msg);
                    }

                    // 赋值表单
                    layui.each(res.data, function (key, value) {
                        let obj = $('*[name="'+key+'"]');
                        // if (key === "password") {
                        //     obj.attr("placeholder", "不更新密码请留空");
                        //     return;
                        // }
                        if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                        if (obj[0].nodeName.toLowerCase() === "textarea") {
                            obj.html(layui.util.escape(value));
                        } else {
                            obj.attr("value", value);
                        }
                    });

                    $("select[name=website]").val(res.data.website);
                    const val = res.data.website
                    let html = '<option value="">请选择</option>'
                    if (col[val]) {
                        col[val].forEach(item => {
                            html += `<option value="${item.id}">${item.name}</option>`
                        })
                    }

                    $('#first-column').html(html);
                    $("select[name=rid]").val(res.data.rid);
                    layui.form.render("select");
                }
            });
        }
    });
</script>
</body>
</html>
