<script type="text/Javascript">

function resetForm() {
    $("#send_whatsapp")[0].reset();
    $("#response_log").hide();
    $("#log_content").html('');
}

$("#send_whatsapp").submit(function(e) {
    e.preventDefault();

    var user_type = $("#user_type").val();
    var phone_number = $("#phone_number").val();
    var message = $("#message").val();

    // Validation
    if (user_type == "0" && phone_number == "") {
        swal({
            title: "Error",
            text: "Please select User Type or enter a Mobile Number",
            icon: "error",
            buttons: false,
            timer: 2000
        });
        return false;
    }

    if (message == "") {
        swal({
            title: "Error",
            text: "Please enter a message",
            icon: "error",
            buttons: false,
            timer: 2000
        });
        return false;
    }

    // Show processing
    $("#whatsapp_form_container").hide();
    $("#whatsapp_processing").show();
    $("#response_log").hide();

    var values = $("#send_whatsapp").serialize();

    $.ajax({
        url: "sendWhatsapp.php",
        type: "post",
        data: values,
        success: function(response) {
            $("#whatsapp_form_container").show();
            $("#whatsapp_processing").hide();

            try {
                var result = jQuery.parseJSON(response);

                if (result['error'] == 1) {
                    swal({
                        title: "Error",
                        text: result['error_msg'],
                        icon: "error",
                        buttons: false,
                        timer: 3000
                    });
                } else {
                    swal({
                        title: "Success",
                        text: result['error_msg'],
                        icon: "success",
                        buttons: false,
                        timer: 2000
                    });

                    // Show response log if available
                    if (result['log']) {
                        $("#log_content").html(result['log']);
                        $("#response_log").show();
                    }

                    // Reset form
                    $("#send_whatsapp")[0].reset();
                }
            } catch (e) {
                swal({
                    title: "Error",
                    text: "Invalid response from server",
                    icon: "error",
                    buttons: false,
                    timer: 2000
                });
                console.log("Parse error:", e);
                console.log("Response:", response);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            $("#whatsapp_form_container").show();
            $("#whatsapp_processing").hide();
            swal({
                title: "Error",
                text: "Server error. Please try again.",
                icon: "error",
                buttons: false,
                timer: 2000
            });
            console.log(textStatus, errorThrown);
        }
    });
});
</script>
