window.addEventListener('load', function () {
    if (document.querySelectorAll('.delete-address') != null) {
        var elem = document.querySelectorAll('.delete-address');
        for (i = 0; i < elem.length; i++) {
            elem[i].addEventListener('click', function () {
                var id = this.getAttribute("data-id");
                CustomAlert({
                    icon: 'question',
                    title: 'Adres sil',
                    text: 'Bu adresi silmek istediğinizden emin misiniz?',
                    showCancelButton: true,
                    cancelButtonText: 'İptal',
                    confirmButtonText: 'Sil'
                }).then((result) => {
                    if (result.value) {
                        var formData = new FormData();
                        formData.append("id", id);
                        formData.append("type", "action");
                        formData.append("action", "delete");

                        request('/common/api/user/address.php', formData, function (data) {
                            data = JSON.parse(data);
                            if (data['status']) {
                                location.reload();
                            } else {
                                new Toast({
                                    type: "danger",
                                    title: "Hata!",
                                    message: data['message']
                                });
                            }
                        });
                    }
                })

            });
        }
    }

    if (document.querySelectorAll('.new-address') != null) {
        var elem = document.querySelectorAll('.new-address');
        for (i = 0; i < elem.length; i++) {
            elem[i].addEventListener('click', function () {
                var formData = new FormData();
                formData.append("type", "get");
                formData.append("action", "add");

                request('/common/api/user/address.php', formData, function (data) {
                    data = JSON.parse(data);
                    if (data['status']) {
                        createCustomModal(data['modal_id'], data['content']);
                        document.getElementById("city").onchange = function () { get_towns(this.value, 'town'); }
                        document.getElementById("address-form").onsubmit = function (event) {
                            event.preventDefault();
                            event.stopPropagation();
                            submitForm();
                        }
                        invoiceTypeChanger();
                    } else {
                        new Toast({
                            type: "danger",
                            title: "Hata!",
                            message: data['message']
                        });
                    }
                });
            });
        }
    }

    if (document.querySelectorAll('.update-address') != null) {
        var elem = document.querySelectorAll('.update-address');
        for (i = 0; i < elem.length; i++) {
            elem[i].addEventListener('click', function () {
                var id = this.getAttribute("data-id");
                var formData = new FormData();
                formData.append("id", id);
                formData.append("type", "get");
                formData.append("action", "update");

                request('/common/api/user/address.php', formData, function (data) {
                    data = JSON.parse(data);
                    if (data['status']) {
                        createCustomModal(data['modal_id'], data['content']);
                        document.getElementById("city").onchange = function () { get_towns(this.value, 'town'); }
                        document.getElementById("address-form").onsubmit = function (event) {
                            event.preventDefault();
                            event.stopPropagation();
                            submitForm();
                        }
                        invoiceTypeChanger();
                    } else {
                        new Toast({
                            type: "danger",
                            title: "Hata!",
                            message: data['message']
                        });
                    }
                });
            });
        }
    }
});

function submitForm() {
    request('/common/api/user/address.php', '#address-form', function (data) {
        data = JSON.parse(data);
        if (!data['status']) {
            new Toast({
                type: "danger",
                title: "Hata!",
                message: data['message']
            });
        } else {
            document.getElementById("modal-close").click();
            new Toast({
                type: "success",
                title: "",
                message: data['message']
            });
            location.reload();
        }
    });
}