import {post} from "./Request.js";

export const sweetalert = (conclusion, tittle, message, url, time) => {
    Swal.fire({
        icon: conclusion,
        title: tittle,
        text: message,
        timer: time,
        showConfirmButton: true
    }).then(() => {
        if (url.length > 0) {
            window.location.href = url;
        }
    });
}

export const sweetModal = async (label,form,buttonTextOkay,buttonTextCanceled,buttonColorOkay,buttonColorCanceled,footer,url) => {
    const {value: text} = await Swal.fire({
        title: label,
        html: form,
        footer: footer,
        confirmButtonText: buttonTextOkay,
        confirmButtonColor: buttonColorOkay,
        cancelButtonText: buttonTextCanceled,
        cancelButtonColor: buttonColorCanceled,
        showCancelButton: true
    })
    if (text) {
       const formatter = document.getElementById("formatter")
        let data = new FormData(formatter);
        post(url,data).then(result => {
            sweetalert(`${result?.conclusion}`,``,`${result?.message}`,``,1500)
        }).catch(reason => {
            $("button").removeAttr('disabled')
            sweetalert(`${result?.conclusion}`,``,`${result?.message}`,``,1500)
            Swal.fire({
                icon: "error",
                title: "sıkıntı var",
                text: reason,
                showConfirmButton: false
            })
        })
    }
}