{
    "widgettype": "VBox",
    "options": {
        "height": "100%",
        "css": "filler",
        "width": "100%",
        "padding": "0",
        "spacing": 0
    },
    "subwidgets": [
        {
            "widgettype": "HBox",
            "options": {
                "width": "100%",
                "bgcolor": "#f8f9fa",
                "padding": "10px 16px",
                "alignItems": "center",
                "borderBottom": "1px solid #e0e0e0"
            },
            "subwidgets": [
                {
                    "widgettype": "Text",
                    "options": {
                        "text": "← 知识库列表",
                        "css": "clickable",
                        "cfontsize": 14,
                        "color": "#4a90d9"
                    },
                    "binds": [
                        {
                            "wid": "self",
                            "event": "click",
                            "actiontype": "urlwidget",
                            "target": "app.rag_main_content",
                            "mode": "replace",
                            "options": {
                                "url": "{{entire_url('/rag/knowledge_bases_list/index.ui')}}"
                            }
                        }
                    ]
                },
                {
                    "widgettype": "Text",
                    "options": {
                        "text": " / ",
                        "cfontsize": 14,
                        "color": "#ccc"
                    }
                },
                {
                    "widgettype": "Text",
                    "options": {
                        "text": "📚 {{params_kw.kb_name or '知识库'}}",
                        "cfontsize": 16,
                        "fontWeight": "bold",
                        "color": "#333"
                    }
                },
                {
                    "widgettype": "Text",
                    "options": {
                        "text": "",
                        "css": "filler"
                    }
                },
                {
                    "widgettype": "Button",
                    "options": {
                        "label": "👤 人脸",
                        "cfontsize": 12,
                        "bgcolor": "#e3f2fd",
                        "color": "#1565c0",
                        "padding": "4px 12px",
                        "borderRadius": "14px",
                        "marginLeft": "8px"
                    },
                    "binds": [
                        {
                            "wid": "self",
                            "event": "click",
                            "actiontype": "urlwidget",
                            "target": "PopupWindow",
                            "popup_options": {
                                "title": "👤 人脸库",
                                "width": "60%",
                                "height": "80%",
                                "movable": true,
                                "resizable": true,
                                "modal": true
                            },
                            "options": {
                                "url": "{{entire_url('./media_cards.dspy')}}?kb_id={{params_kw.kb_id}}&kind=face"
                            }
                        }
                    ]
                },
                {
                    "widgettype": "Button",
                    "options": {
                        "label": "🎤 声纹",
                        "cfontsize": 12,
                        "bgcolor": "#f3e5f5",
                        "color": "#7b1fa2",
                        "padding": "4px 12px",
                        "borderRadius": "14px",
                        "marginLeft": "8px"
                    },
                    "binds": [
                        {
                            "wid": "self",
                            "event": "click",
                            "actiontype": "urlwidget",
                            "target": "PopupWindow",
                            "popup_options": {
                                "title": "🎤 声纹库",
                                "width": "60%",
                                "height": "80%",
                                "movable": true,
                                "resizable": true,
                                "modal": true
                            },
                            "options": {
                                "url": "{{entire_url('./media_cards.dspy')}}?kb_id={{params_kw.kb_id}}&kind=voice"
                            }
                        }
                    ]
                }
            ]
        },
        {
            "widgettype": "HBox",
            "options": {
                "css": "filler",
                "width": "100%",
                "spacing": 0
            },
            "subwidgets": [
                {
                    "widgettype": "VBox",
                    "options": {
                        "cwidth": 16,
                        "bgcolor": "#fafafa",
                        "borderRight": "1px solid #e0e0e0",
                        "spacing": 0,
                        "overflow": "hidden",
                        "flexShrink": "0"
                    },
                    "subwidgets": [
                        {
                            "widgettype": "Tree",
                            "id": "dir_tree",
                            "options": {
                                "select_only": true,
                                "title": "目录",
                                "parentField": "parentid",
                                "idField": "id",
                                "textField": "label",
                                "dataurl": "{{entire_url('./get_tree_data.dspy')}}?kb_id={{params_kw.kb_id}}",
                                "newdata_params": {
                                    "kb_id": "{{params_kw.kb_id}}"
                                },
                                "editable": {
                                    "fields": [
                                        {
                                            "name": "name",
                                            "title": "名称",
                                            "type": "str",
                                            "length": 255,
                                            "uitype": "str",
                                            "label": "名称"
                                        }
                                    ],
                                    "add_url": "{{entire_url('./new_tree_item.dspy')}}",
                                    "update_url": "{{entire_url('./update_tree_item.dspy')}}",
                                    "delete_url": "{{entire_url('./delete_tree_item.dspy')}}"
                                }
                            }
                        }
                    ]
                },
                {
                    "widgettype": "VBox",
                    "id": "detail_content",
                    "options": {
                        "css": "filler",
                        "padding": "16px",
                        "spacing": "12px"
                    },
                    "subwidgets": [
                        {
                            "widgettype": "UiFile",
                            "id": "file_drop_zone",
                            "options": {
                                "accept": "",
                                "multiple": true,
                                "preview": false,
                                "otext": "📂 拖拽文件到此处上传，或点击选择文件",
                                "width": "100%",
                                "cheight": 10,
                                "css": "card",
                                "border": "2px dashed #3b82f6",
                                "alignItems": "center",
                                "justifyContent": "center",
                                "kb_id": "{{params_kw.kb_id}}"
                            },
                            "binds": [
                                {
                                    "wid": "self",
                                    "event": "dragover",
                                    "actiontype": "script",
                                    "script": "event.preventDefault();event.stopPropagation()"
                                },
                                {
                                    "wid": "self",
                                    "event": "drop",
                                    "actiontype": "script",
                                    "script": "event.preventDefault();event.stopPropagation()"
                                }
                            ]
                        },
                        {
                            "widgettype": "Text",
                            "id": "upload_status",
                            "options": {
                                "cfontsize": 12,
                                "color": "#50b86c",
                                "marginTop": "0",
                                "text": "支持 TXT/PDF/JPG/PNG/MP3/WAV/MP4"
                            }
                        },
                        {
                            "widgettype": "VBox",
                            "options": {
                                "css": "filler",
                                "width": "100%",
                                "spacing": "0"
                            },
                            "subwidgets": [
                                {
                                    "widgettype": "VScrollPanel",
                                    "id": "file_list_panel",
                                    "options": {
                                        "css": "filler",
                                        "padding": "0",
                                        "spacing": "0"
                                    },
                                    "subwidgets": [
                                        {
                                            "widgettype": "urlwidget",
                                            "options": {
                                                "url": "{{entire_url('./file_list.dspy')}}?kb_id={{params_kw.kb_id}}",
                                                "css": "filler"
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "binds": [
        {
            "wid": "dir_tree",
            "event": "node_selected",
            "actiontype": "script",
            "script": "window._rag_folder=event.params.id||''"
        },
        {
            "wid": "file_drop_zone",
            "event": "changed",
            "actiontype": "script",
            "target": "file_drop_zone",
            "script": "var kb_id=this.opts.kb_id||'';var folder=window._rag_folder||'';if(folder==='__root__')folder='';var files=this.value;if(!files)return;if(!Array.isArray(files))files=[files];var st=document.getElementById('upload_status');var done=0;var total=files.length;function refreshList(){var fp=bricks.getWidgetById('file_list_panel',bricks.app.root);fp.clear_widgets();var url=location.origin+'/rag/knowledge_bases_list/file_list.dspy?_webbricks_=1&kb_id='+kb_id+(folder?'&id='+folder:'');bricks.widgetBuild({widgettype:'urlwidget',options:{url:url,css:'filler'}},fp).then(function(w){if(w)fp.add_widget(w)})};for(var i=0;i<files.length;i++){(function(f){var x=new XMLHttpRequest();x.open('POST','/rag/knowledge_bases_list/upload_file.dspy?kb_id='+encodeURIComponent(kb_id)+'&file_name='+encodeURIComponent(f.name)+'&folder='+encodeURIComponent(folder));x.onload=function(){done++;var ok=x.status===200;var msg='';try{var r=JSON.parse(x.responseText);if(r&&(r.status==='error'||r.error)){ok=false;msg=r.message||r.error||'上传失败'}}catch(e){}if(st){st.innerText=ok?('上传成功 '+done+'/'+total):(msg||'上传失败');st.style.color=ok?'#50b86c':'#f56c6c'}if(done>=total)refreshList()};x.onerror=function(){done++;if(st){st.innerText='上传失败';st.style.color='#f56c6c'}if(done>=total)refreshList()};x.send(f)})(files[i])}"
        },
        {
            "wid": "dir_tree",
            "event": "node_selected",
            "actiontype": "urlwidget",
            "target": "file_list_panel",
            "mode": "replace",
            "options": {
                "url": "{{entire_url('./file_list.dspy')}}?kb_id={{params_kw.kb_id}}"
            }
        }
    ]
}
