<?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 actividad';

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 · Actividades</span>
                <h1>Agregar actividad</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/activity_store.php" method="POST">

            <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']; ?>">
            <input type="hidden" name="customer_name" value="<?php echo safe_text($opportunity['customer_name']); ?>">

            <section class="card form-card">
                <div class="card-header">
                    <div>
                        <span class="label">Actividad</span>
                        <h3>Información de la actividad</h3>
                    </div>
                </div>

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

                        <div class="form-field">
                            <label class="label">Tipo de actividad</label>
                            <select class="select" name="activity_type">
                                <option value="nota">Nota</option>
                                <option value="llamada">Llamada</option>
                                <option value="correo">Correo</option>
                                <option value="reunion">Reunión</option>
                                <option value="seguimiento">Seguimiento</option>
                                <option value="documento">Documento</option>
                            </select>
                        </div>

                        <div class="form-field">
                            <label class="label">Fecha</label>
                            <input class="input" type="datetime-local" name="activity_date" value="<?php echo date('Y-m-d\TH:i'); ?>">
                        </div>

                        <div class="form-field">
                            <label class="label">Título</label>
                            <input class="input" type="text" name="title" required placeholder="Ej: Llamada de seguimiento">
                        </div>

                        <div class="form-field">
                            <label class="label">Responsable</label>
                            <input class="input" type="text" name="owner_name" value="<?php echo safe_text($opportunity['owner_name']); ?>" placeholder="Nombre del responsable">
                        </div>

                        <div class="form-field form-field-full">
                            <label class="label">Descripción</label>
                            <textarea class="textarea textarea-lg" name="description" placeholder="Describe los detalles de la actividad..."></textarea>
                        </div>

                    </div>

                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">
                            <i data-lucide="save"></i>
                            <span>Guardar actividad</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>Esta actividad quedará asociada a la oportunidad actual y será visible en el timeline comercial.</span>
                    </div>
                </div>
            </section>

        </form>

    </section>
</main>

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