<?php $pageTitle = 'Upload Document'; ?>
<div class="panel form-panel">
    <div class="panel-header"><div><p class="eyebrow">Files</p><h2>Upload Document</h2><p class="muted">Attach BOL, POD, rate confirmations, stop photos, challans, insurance, registration, and other files.</p></div></div>
    <form method="post" action="<?= url('/documents/store') ?>" enctype="multipart/form-data" class="form-grid" data-location-form>
        <?= csrf_field() ?>
        <input type="hidden" name="latitude" data-latitude>
        <input type="hidden" name="longitude" data-longitude>
        <label>
            <span>Related Type *</span>
            <select name="related_type" required>
                <?php foreach (Document::RELATED_TYPES as $type): ?>
                    <option value="<?= e($type) ?>" <?= old('related_type', $relatedType) === $type ? 'selected' : '' ?>><?= e(relation_label($type)) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label>
            <span>Related Record ID *</span>
            <input type="number" min="1" name="related_id" value="<?= e(old('related_id', $relatedId ?: '')) ?>" required>
        </label>
        <label>
            <span>Document Type *</span>
            <select name="document_type" required>
                <?php foreach (['Carrier Packet', 'W-9', 'Operating Authority', 'Certificate of Insurance', 'Cargo Insurance', 'Rate Confirmation', 'BOL', 'POD', 'Load Photo', 'Stop Photo', 'Seal Photo', 'Washout Receipt', 'Reefer Temperature Photo', 'Temperature Report', 'Inspection Document', 'Challan / Ticket', 'Accident / Damage Photo', 'Fuel Receipt', 'Scale Ticket', 'Lumper Receipt', 'Registration', 'Other'] as $type): ?>
                    <option value="<?= e($type) ?>" <?= old('document_type', 'POD') === $type ? 'selected' : '' ?>><?= e($type) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label>
            <span>Expiry Date</span>
            <input type="date" name="expires_at" value="<?= e(old('expires_at')) ?>">
        </label>
        <label class="span-2">
            <span>Location / Address</span>
            <input name="location_address" data-location-address value="<?= e(old('location_address')) ?>" placeholder="Optional location for photo/challan">
        </label>
        <label class="span-2">
            <span>Notes</span>
            <textarea name="notes" rows="3" placeholder="Optional notes, challan number, amount, incident details, etc."><?= e(old('notes')) ?></textarea>
        </label>
        <label class="span-2">
            <span>File *</span>
            <input type="file" name="file" accept="image/*,application/pdf,.doc,.docx" capture="environment" required>
            <small class="muted">Allowed: PDF, images, DOC, DOCX. Max 10 MB.</small>
        </label>
        <div class="form-actions span-2">
            <button class="btn btn-primary" type="submit">Upload Document</button>
            <a class="btn btn-light" href="<?= url('/documents') ?>">Cancel</a>
        </div>
    </form>
</div>
