<!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="keyword" 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-block">
                <input type="checkbox" name="platform[]" value="1" title="百度PC">
                <input type="checkbox" name="platform[]" value="2" title="百度WAP">
                <input type="checkbox" name="platform[]" value="3" title="头条PC">
                <input type="checkbox" name="platform[]" value="4" title="头条WAP">
                {{--            <input type="checkbox" name="arr[2]" title="选项3">--}}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排除的链接(我们自己做的)</label>
            <div class="layui-input-block">
                <textarea name="ignore_url" 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="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/keywords/monitor/view"; // 获取关键词数据
    const SAVE_API = "/admin/api/keywords/monitor/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>
