<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Document - Document Manager</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/static/css/styles.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">Optimal Document Manager</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="/">Documents</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/upload">Upload</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h4 class="mb-0">Upload New Document</h4>
                    </div>
                    <div class="card-body">
                        <form id="uploadForm" enctype="multipart/form-data">
                            <div class="mb-3" id="drop-zone">
                                <label for="documentFile" class="form-label">Document File</label>
                                <div class="border-dashed p-5 text-center">
                                    <i class="bi bi-upload fs-1"></i>
                                    <p class="mt-2">Drag & drop a file here, or click to select a file</p>
                                    <p class="text-muted small">You can also paste an image from your clipboard anywhere on the page.</p>
                                    <input class="form-control" type="file" id="documentFile" 
                                        accept=".pdf,.doc,.docx,.jpg,.jpeg,.png,.gif" style="display: none;">
                                    <div id="file-info" class="mt-3"></div>
                                </div>
                                <div class="form-text mt-2">
                                    Supported formats: PDF, DOC, DOCX, JPG, PNG, GIF (Max 10MB)
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="documentName" class="form-label">Document Name</label>
                                    <input type="text" class="form-control" id="documentName" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="controlNumber" class="form-label">Control Number</label>
                                    <div class="input-group">
                                        <span class="input-group-text">DOC-</span>
                                        <input type="text" class="form-control" id="controlNumber" 
                                            placeholder="Leave blank to auto-generate">
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="documentCategory" class="form-label">Category</label>
                                    <select class="form-select" id="documentCategory" required>
                                        <option value="">Select a category</option>
                                        <!-- Categories will be populated by JavaScript -->
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="documentType" class="form-label">Document Type</label>
                                    <select class="form-select" id="documentType" required>
                                        <option value="invoice">Invoice</option>
                                        <option value="receipt">Receipt</option>
                                        <option value="contract">Contract</option>
                                        <option value="other" selected>Other</option>
                                    </select>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="documentDate" class="form-label">Document Date</label>
                                <input type="date" class="form-control" id="documentDate" required>
                            </div>

                            <div class="mb-3">
                                <label for="documentDescription" class="form-label">Description</label>
                                <textarea class="form-control" id="documentDescription" rows="3"></textarea>
                            </div>

                            <div class="mb-3">
                                <label for="documentTags" class="form-label">Tags</label>
                                <input type="text" class="form-control" id="documentTags" 
                                    placeholder="tag1, tag2, tag3">
                                <div class="form-text">Separate tags with commas</div>
                            </div>

                            <div class="d-grid gap-2">
                                <button type="submit" class="btn btn-primary" id="uploadButton">
                                    <span class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true" id="uploadSpinner"></span>
                                    <span id="uploadButtonText">Upload Document</span>
                                </button>
                                <button type="button" class="btn btn-info d-none" id="mobileUploadBtn">Upload from Mobile</button>
                                <a href="/" class="btn btn-outline-secondary">Cancel</a>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- Upload Progress -->
                <div class="card mt-4 d-none" id="uploadProgressCard">
                    <div class="card-header">
                        Upload Progress
                    </div>
                    <div class="card-body">
                        <div class="progress mb-3">
                            <div id="uploadProgress" class="progress-bar progress-bar-striped progress-bar-animated" 
                                role="progressbar" style="width: 0%">0%</div>
                        </div>
                        <div id="uploadStatus" class="text-muted">Preparing upload...</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Success Modal -->
    <!-- Mobile Upload Modal -->
    <div class="modal fade" id="mobileUploadModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Upload from Mobile</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body text-center">
                    <p>Scan this QR code with your mobile device to upload a file.</p>
                    <div id="qrCodeContainer"></div>
                    <p class="text-muted small mt-2">Waiting for upload...</p>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="successModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header bg-success text-white">
                    <h5 class="modal-title">Upload Successful</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>Your document has been uploaded successfully!</p>
                    <div class="alert alert-info">
                        <strong>Document ID:</strong> <span id="uploadedDocumentId"></span>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href="/" class="btn btn-primary">View All Documents</a>
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Upload Another</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/upload.js"></script>
</body>
</html>
