<?php require __DIR__ . '/../../includes/navBar.php'; ?>

<div class="container my-5">

    <div class="row justify-content-center">

        <div class="col-xl-8 col-lg-9 col-md-10">

            <div class="card card-rounded card-shadow border-0">

                <!-- HEADER -->

                <div class="card-header header-dark px-4 py-3">

                    <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
                        <h4 class="mb-0 tittle-shadow">
                            <i class="fas fa-user-plus me-2"></i>
                            Crear Proveedor
                        </h4>

                        <a href="index.php?opt=<?= htmlspecialchars($_SESSION['return_opt'] ?? 'proveedores') ?>" class="btn btn-soft-secondary text-white">
                            <i class="fas fa-arrow-left me-1"></i> Volver
                        </a>

                    </div>
                </div>

                <!-- BODY -->

                <div class="card-body p-4 bg-white">

                    <form id="form-crear-proveedor" method="POST" action="?opt=crear_proveedor" novalidate>

                        <!-- CAMPO OCULTO (para verificación de token) -->

                        <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
                    
                        <!-- CAMPO OCULTO (para saber desde que pagina se solicito la creacion del cliente -->

                        <input type="hidden" name="redirect_opt" value="<?= htmlspecialchars($_SESSION['return_opt'] ?? 'proveedores') ?>">

                        <div class="row g-3">

                            <!-- RAZÓN SOCIAL -->
                    
                            <div class="col-md-6">
                                <label class="form-label">Razón social</label>
                                <input type="text" name="razon_social" class="form-control">
                            </div>

                            <!-- TELÉFONO -->

                            <div class="col-md-6">
                                <label class="form-label">Teléfono</label>
                                <input type="text" name="telefono" class="form-control">
                            </div>

                            <!-- EMAIL -->

                            <div class="col-md-6">
                                <label class="form-label">Email</label>
                                <input type="text" name="email" class="form-control">
                            </div>

                            <!-- CONDICIÓN IVA -->

                            <div class="col-md-6">
                                <label class="form-label">Condición IVA</label>

                                <select
                                    name="cond_iva"
                                    class="form-select">

                                    <option value="consumidor_final">
                                        Consumidor Final
                                    </option>

                                    <option value="monotributo">
                                        Monotributo
                                    </option>

                                    <option value="responsable_inscripto">
                                        Responsable Inscripto
                                    </option>

                                </select>
                            </div>

                            <!-- CUIT -->

                            <div class="col-md-6">
                                <label class="form-label">CUIT</label>

                                <div class="d-flex gap-2">

                                    <input
                                        type="text"
                                        name="cuit_1"
                                        maxlength="2"
                                        class="form-control text-center"
                                        data-bs-toggle="tooltip"
                                        title="Tipo de persona">

                                    <input
                                        type="text"
                                        name="cuit_2"
                                        maxlength="8"
                                        class="form-control text-center"
                                        data-bs-toggle="tooltip"
                                        title="DNI">

                                    <input
                                        type="text"
                                        name="cuit_3"
                                        maxlength="1"
                                        class="form-control text-center"
                                        data-bs-toggle="tooltip"
                                        title="Dígito verificador">

                                </div>
                            </div>
                        
                        </div>

                        <!-- BOTON SUBMIT -->

                        <div class="d-flex justify-content-between align-items-center mt-4 pt-4 border-top">

                            <button type="submit" class="btn btn-success">
                                <i class="fas fa-user-plus me-1"></i> Crear Proveedor
                            </button>

                        </div>

                    </form>

                    <!-- Errores provinientes de proveedorController -->

                    <!-- ERROR -->

                    <script>
                        const serverErrors = <?php 
                            if (!empty($_SESSION['flash_error'])) {
                                $errorsArray = explode('<br>', $_SESSION['flash_error']);
                                unset($_SESSION['flash_error']);
                                echo json_encode($errorsArray);
                            } else {
                                echo '[]';
                            }
                        ?>;
                    </script>

                    <!-- SUCCES -->

                    <script>
                        const serverSuccess = <?php
                            if (!empty($_SESSION['flash_success'])) {
                                $successArray = explode('<br>', $_SESSION['flash_success']);
                                unset($_SESSION['flash_success']);
                                echo json_encode($successArray);
                            } else {
                                echo '[]';
                            }
                        ?>;
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>

<!-- Validar mediante js -->

<script src="js/proveedores/proveedorValidator.js"></script>

<!-- Pasar al siguiente input en CUIT -->

<script>
    document.querySelectorAll("input[name^='cuit_']").forEach((input, index, inputs) => {
        input.addEventListener("input", () => {
            if (input.value.length === input.maxLength && inputs[index + 1]) {
                inputs[index + 1].focus();
            }
        });
    });
</script>

<!-- Tooltip -->

<script>
    document.addEventListener("DOMContentLoaded", () => {
        const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
        tooltipTriggerList.forEach(el => new bootstrap.Tooltip(el));
    });
</script>