<!-- 笔记本设置的移动功能 -->
<!-- 废弃, see group_select_script.html -->

<div class="note_group_tree_select col-md-12 scroll-y">
    
    <div class="group-select-search">
        <input id="groupSelectInput" type="text" class="nav-search-input group_search_input" 
            placeholder="搜索" onchange="xnote.note.onSearchGroupTree()"/>
        <button class="nav-search-btn btn-default" onclick="xnote.note.onSearchGroupTree()">
            <i class="fa fa-search"></i>
        </button>
    </div>

    <div class="book-item hide no_match_result">
        <span class="center">没有搜索到结果</span>
    </div>

    <div class="book-item">
        <a class="move-link" data-id="{{parent.parent_id}}">
            <span class="tag system">系统</span>
            <span>上级目录</span>
        </a>
    </div>
    
    <div class="book-item">
        <a class="move-link" data-id="0">
            <span class="tag system">系统</span>
            <span>根目录</span>
        </a>
        <button class="float-right do-move-btn btn-default" data-id="0" onclick="xnote.handleMoveTo(this)">移到这里</button>
    </div>

    {% for item in files %}
        <div class="book-item">
            <a class="move-link" data-id="{{item.id}}" data-parent="{{item.parent_id}}">
                <i class="fa {{item.icon}} fa-{{item.icon}} black"></i>                
                <span>{{item.name}}</span>
                <span class="black">({{item.children_count}})</span>
            </a>
            <button class="float-right do-move-btn btn-default" data-id="{{item.id}}" onclick="xnote.handleMoveTo(this)">移到这里</button>
        </div>
    {% end %}
    
</div>

<script>
    xnote.handleMoveTo = function (target) {
        var parentId = $(target).attr("data-id");
        var params = { id: "{{id}}", parent_id: parentId};
        xnote.http.post("/note/move", params, function (resp){
            console.log(resp);
            if (resp.code == "success") {
                window.location.reload();
            } else {
                xnote.alert(resp.message);
            }
        });
    };

    var noteSearchTree = $(".note_group_tree_select")

    xnote.note.onSearchGroupTree = function () {
        var searchKey = noteSearchTree.find(".group_search_input").val();
        // console.log("search", searchKey);

        $(".book-item").show();
        noteSearchTree.find(".no_match_result").hide();

        if (searchKey == "") {
            return;
        }

        var hasMatch = false;

        $(".book-item").each(function (index, element) {
            var noteName = $(element).find("a").text();
            if (noteName.indexOf(searchKey) < 0) {
                $(this).hide();
            } else {
                hasMatch = true;
            }
        });

        if (!hasMatch) {
            noteSearchTree.find(".no_match_result").show();
        }
    }

    $(".move-link").click(function(e) {
        console.log("move-link clicked");
        console.log(e.target);
        var parentId = $(this).attr("data-id");
        var params = {
            "view": "tree",
            "id": "{{id}}",
            "parent_id": parentId,
        };

        xnote.http.get("/note/group/select", params, function(html) {
            $(".note-group-select").parent().html(html);
        });
    });
</script>