<?php

require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/format.php';
require_once '../../services/crm/CrmService.php';
require_once '../../core/permissions.php'; // En los _store.php asegúrate de ponerlo junto a los otros require_once

require_login();
require_permission('module.crm'); // Bloqueo de acceso

$opportunityId = isset($_GET['opportunity_id']) ? (int) $_GET['opportunity_id'] : 0;

$crm = new CrmService();
$opportunity = $crm->getOpportunity($opportunityId);

if (!$opportunity) {
    die('Oportunidad no encontrada.');
}

$page_title = 'Agregar documento';

require_once '../../layouts/header.php';
require_once '../../layouts/sidebar.php';

?>

<main class="app-main">
    <?php require_once '../../layouts/topbar.php'; ?>

    <section class="content">

        <div class="page-header page-header-compact">
            <div class="page-header-content">
                <span class="label">CRM · Documentos</span>
                <h1>Agregar documento</h1>
                <p class="text-secondary"><?php echo safe_text($opportunity['title']); ?> · <?php echo safe_text($opportunity['customer_name']); ?></p>
            </div>
        </div>

        <form class="opportunity-form" action="/modules/crm/document_store.php" method="POST" enctype="multipart/form-data">

            <input type="hidden" name="_csrf_token" value="<?php echo safe_text(csrf_token()); ?>">
            <input type="hidden" name="opportunity_id" value="<?php echo (int) $opportunity['id']; ?>">
            <input type="hidden" name="customer_id" value="<?php echo (int) $opportunity['customer_id']; ?>">

            <section class="card form-card">
                <div class="card-header">
                    <div>
                        <span class="label">Documento</span>
                        <h3>Información del documento</h3>
                    </div>
                </div>

                <div class="card-body">
                    <div class="form-grid form-grid-2">

                        <div class="form-field">
                            <label class="label">Tipo documento</label>
                            <select class="select" name="document_type">
                                <option value="cotizacion">Cotización</option>
                                <option value="orden_compra">Orden de compra</option>
                                <option value="factura">Factura</option>
                                <option value="nota_credito">Nota de crédito</option>
                                <option value="nota_debito">Nota de débito</option>
                                <option value="otro">Otro</option>
                            </select>
                        </div>

                        <div class="form-field">
                            <label class="label">Nombre documento</label>
                            <input class="input" type="text" name="document_name" required placeholder="Ej: Cotización FACTO 1421">
                        </div>

                        <div class="form-field">
                            <label class="label">Número</label>
                            <input class="input" type="text" name="document_number" placeholder="Ej: 1421">
                        </div>

                        <div class="form-field">
                            <label class="label">Fecha</label>
                            <input class="input" type="date" name="document_date" value="<?php echo date('Y-m-d'); ?>">
                        </div>

                        <div class="form-field">
                            <label class="label">Monto neto</label>
                            <input class="input" type="number" name="net_amount" min="0" step="1" placeholder="$ 0">
                        </div>

                        <div class="form-field">
                            <label class="label">Monto total</label>
                            <input class="input" type="number" name="total_amount" min="0" step="1" placeholder="$ 0">
                        </div>

                        <div class="form-field">
                            <label class="label">ID documento FACTO</label>
                            <input class="input" type="text" name="facto_document_id" placeholder="Opcional">
                        </div>

                        <div class="form-field">
                            <label class="label">Archivo PDF</label>
                            <input class="input file-input" type="file" name="document_file" accept="application/pdf">
                        </div>

                        <div class="form-field form-field-full">
                            <label class="label">Notas</label>
                            <textarea class="textarea textarea-lg" name="notes" placeholder="Agrega observaciones del documento..."></textarea>
                        </div>

                    </div>

                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">
                            <i data-lucide="save"></i>
                            <span>Guardar documento</span>
                        </button>

                        <a class="btn btn-outline" href="/modules/crm/opportunity.php?id=<?php echo (int) $opportunity['id']; ?>">
                            Cancelar
                        </a>
                    </div>
                </div>
            </section>

            <section class="card form-card">
                <div class="card-header">
                    <div>
                        <span class="label">Contexto</span>
                        <h3>Información adicional</h3>
                    </div>
                </div>

                <div class="card-body">
                    <div class="form-info-box">
                        <i data-lucide="info"></i>
                        <span>El documento quedará asociado a la oportunidad y será registrado automáticamente en el timeline.</span>
                    </div>
                </div>
            </section>

        </form>

    </section>
</main>

<?php require_once '../../layouts/footer.php'; ?>
