<?php
ob_start();
?>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="<?= url('/dashboard') ?>">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="<?= url('/saidas') ?>">Saídas</a></li>
                        <li class="breadcrumb-item active">Nova Saída</li>
                    </ol>
                </div>
                <h4 class="page-title">
                    <i class="bi bi-plus-circle me-2"></i> Nova Saída (Conta a Pagar)
                </h4>
            </div>
        </div>
    </div>

    <form id="saidaForm" method="POST" action="<?= url('/saidas/criar') ?>">
        <?php
        $conta = [];
        include ROOT_PATH . '/views/saidas/form.php';
        ?>

        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-footer">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg me-2"></i> Salvar Saída
                        </button>
                        <a href="<?= url('/saidas') ?>" class="btn btn-secondary">
                            <i class="bi bi-arrow-left me-2"></i> Voltar
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    $('#saidaForm').on('submit', function(e) {
        e.preventDefault();

        const formData = $(this).serialize();

        $.ajax({
            url: appUrl('/saidas/criar'),
            type: 'POST',
            data: formData,
            beforeSend: function() {
                LoadingOverlay.show();
            },
            success: function(response) {
                LoadingOverlay.hide();

                if (response.success) {
                    Swal.fire({
                        title: 'Sucesso!',
                        text: response.message,
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = appUrl(response.data?.redirect || response.redirect || '/saidas');
                    });
                } else {
                    Swal.fire({
                        title: 'Erro!',
                        text: response.message,
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                }
            },
            error: function() {
                LoadingOverlay.hide();
                Swal.fire({
                    title: 'Erro!',
                    text: 'Erro ao salvar saída',
                    icon: 'error',
                    confirmButtonText: 'OK'
                });
            }
        });
    });
});
</script>

<?php
$content = ob_get_clean();
$activeMenu = 'saidas';
include ROOT_PATH . '/views/layouts/app.php';
?>

