File size: 4,125 Bytes
79c7b05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Manager</title>
    <style>
        body {
            font-family: 'Courier New', monospace;
            background-color: #000;
            color: #00ff00;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #00ff00;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #00ff00;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #003300;
        }
        button {
            background-color: #003300;
            color: #00ff00;
            border: 1px solid #00ff00;
            padding: 5px 10px;
            cursor: pointer;
        }
        button:hover {
            background-color: #004400;
        }
        a {
            color: #00ff00;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>Document Manager</h1>
    <a href="/" style="margin-bottom: 20px; display: block;">Back to Chat</a>
    <table id="docTable">
        <thead>
            <tr>
                <th>Title</th>
                <th>Count</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <!-- Table rows will be dynamically added here -->
        </tbody>
    </table>

    <h2>Upload New Document</h2>
    <form id="uploadForm" enctype="multipart/form-data">
        <input type="text" id="docTitle" name="title" placeholder="Document Title" required>
        <input type="file" id="docFile" name="file" accept=".txt,.pdf,.jpg,.jpeg,.png" required>
        <button type="submit">Upload</button>
    </form>

    <script>
        async function fetchDocuments() {
            let documents = [];
            try {
                documents = await fetch('/documents').then(r => r.json());
                if (!Array.isArray(documents)) documents = [];
            } catch {}
            const tableBody = document.querySelector('#docTable tbody');
            tableBody.innerHTML = '';
            documents.forEach(doc => {
                const row = tableBody.insertRow();
                row.insertCell(0).textContent = doc.title;
                row.insertCell(1).textContent = doc.count;
                const deleteButton = document.createElement('button');
                deleteButton.textContent = 'Delete';
                deleteButton.onclick = () => deleteDocument(doc.title);
                row.insertCell(2).appendChild(deleteButton);
            });
        }

        async function deleteDocument(title) {
            const response = await fetch('/delete_document', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ title: title }),
            });
            if (response.ok) {
                fetchDocuments();
            } else {
                alert('Error deleting document');
            }
        }

        async function uploadDocument(formData) {
            const response = await fetch('/upload_document', {
                method: 'POST',
                body: formData
            });
            if (response.ok) {
                alert('Document uploaded successfully');
                fetchDocuments();
            } else {
                let msg = 'Error uploading document';
                try {
                    const data = await response.json();
                    if (data && data.error) msg = data.error;
                } catch {}
                alert(msg);
            }
        }

        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            uploadDocument(formData);
        });

        fetchDocuments();
    </script>
</body>
</html>