<?php
$activeMenu = 'contas-receber';
include ROOT_PATH . '/views/layouts/app.php';
?>

<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('/entradas') ?>">Contas a Receber</a></li>
                        <li class="breadcrumb-item active">Editar Conta</li>
                    </ol>
                </div>
                <h4 class="page-title">
                    <i class="bi bi-pencil me-2"></i> Editar Conta a Receber
                </h4>
            </div>
        </div>
    </div>

    <form id="contaForm" method="POST" action="<?= url('/entradas/editar') ?>">
        <?php include ROOT_PATH . '/views/contas-receber/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> Atualizar Conta
                        </button>
                        <a href="<?= url('/entradas') ?>" 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() {
    $('#contaForm').on('submit', function(e) {
        e.preventDefault();

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

        $.ajax({
            url: appUrl('/entradas/editar'),
            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 = response.data.redirect;
                    });
                } else {
                    Swal.fire({
                        title: 'Erro!',
                        text: response.message,
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                }
            },
            error: function(xhr) {
                LoadingOverlay.hide();
                let message = 'Erro ao atualizar conta a receber';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    message = xhr.responseJSON.message;
                }
                Swal.fire({
                    title: 'Erro!',
                    text: message,
                    icon: 'error',
                    confirmButtonText: 'OK'
                });
            }
        });
    });
});
</script>