<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Upload Document',
    'workspaceTitle'        => 'Upload Customer Document',
    'workspaceDesc'         => 'Attach a file to a customer company.',
]) ?>

<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-folder-open"></i>
        <h2 class="enterprise-settings-card__title">Document Details</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="<?= base_url('sale/customer-documents') ?>" method="post" enctype="multipart/form-data" novalidate>
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field">
                <label class="enterprise-field-label">Company <span class="text-danger">*</span></label>
                <select name="company_id" class="form-select" required>
                    <option value="">— Select —</option>
                    <?php foreach ($companies as $c): ?>
                        <option value="<?= $c['id'] ?>" <?= (string) old('company_id', $prefillCompanyId ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                            <?= esc($c['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Title <span class="text-danger">*</span></label>
                <input type="text" name="title" class="form-control" value="<?= esc(old('title')) ?>" required>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Category</label>
                <input type="text" name="category" class="form-control" placeholder="Contract, Proposal, Invoice…" value="<?= esc(old('category')) ?>">
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">File <span class="text-danger">*</span></label>
                <input type="file" name="document_file" class="form-control" accept=".pdf,.jpg,.jpeg,.png,.webp,.doc,.docx" required>
                <div class="form-text">PDF, images, or Word — max 5MB.</div>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Notes</label>
                <textarea name="notes" class="form-control" rows="3"><?= esc(old('notes')) ?></textarea>
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('sale/customer-documents') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Upload</button>
        </div>
    </form>
</article>

<?= $this->endSection() ?>
