<!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="">
    <input type="hidden" name="id"  autocomplete="off">
    <div class="main-container mr-5">
        <div class="layui-form-item">
            <label class="layui-form-label">网站域名</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="app_domain" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">网站名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="app_name" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">网站描述</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="app_description" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">关键词</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="app_keywords" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公司名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="app_company" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
            </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/config/view"; // 获取关键词数据
    const SAVE_API = "/admin/api/website/config/save"; // 重新保存数据

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

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

        function initViewData() {
            let viewId = layui.url().search['id']
            $.ajax({
                url: VIEW_API,
                data: {
                    id: viewId
                },
                method: 'get',
                dataType: "json",
                success: function (res) {

                    // 赋值表单
                    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);
                        }
                    });

                    const form = layui.form;

                    const selectedPlatforms = res.data.platform; // 后端数据

                    selectedPlatforms?.forEach(val => {
                        // 设置 value 对应的 checkbox 为选中
                        document.querySelectorAll('input[name="platform[]"]').forEach(el => {
                            if (el.value == val) {
                                el.checked = true;
                            }
                        });
                    });

                    form.render('checkbox'); // 重新渲染
                    // ajax产生错误
                    if (res.code) {
                        layui.popup.failure(res.msg);
                    }

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