/**
 * wizwiz v 10.3.1 Beta
 * https://github.com/wizwizdev/wizwizxui-timebot
 *
 * Copyright (c) @wizwizch
 */
function fadeOut() {
    var myDiv = document.getElementById("myDiv");
    var opacity = 1;
    var interval = setInterval(function () {
        if (opacity <= 0) {
            clearInterval(interval);
            myDiv.style.display = "none";
        } else {
            myDiv.style.opacity = opacity;
            opacity -= 0.1;
        }
    }, 100);
}

setTimeout(function () {
    fadeOut();
}, 2000); // Wait for 3 seconds before fading out the div




$(document).ready(function () {
    $("#insert_volume_gb").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "add-volume.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_volume_gb",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#insert_volume_day").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "add-volume.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_volume_day",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#insert_plans1").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "singleplans.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_plans1",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});

$(document).ready(function () {
    $("#insert_plans2").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "multipleplans.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_plans2",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});

$(document).ready(function () {
    $("#insert_discount").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "discount.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_discount",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#insert_rahgozar").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "rahgozar.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_rahgozar",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});

$(document).ready(function () {
    $("#insert_gift").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "gift.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_gift",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});

$(document).ready(function () {
    $("#insert_software").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "software.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_software",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});

$(document).ready(function () {
    $("#save_admin").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "settings.php",
            type: "POST",
            data: $(this).serialize() + "&action=save_admin",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#save_backup").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "settings.php",
            type: "POST",
            data: $(this).serialize() + "&action=save_backup",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#save_pay").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "settings.php",
            type: "POST",
            data: $(this).serialize() + "&action=save_pay",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#save_state").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "settings.php",
            type: "POST",
            data: $(this).serialize() + "&action=save_state",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});
$(document).ready(function () {
    $("#insert_category").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "category.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_category",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});


$(document).ready(function () {
    $("#insert_server_backup").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
            url: "wizwizbackup.php",
            type: "POST",
            data: $(this).serialize() + "&action=insert_server_backup",
            success: function (response) {
                $("#result").html(response);
            }
        });
    });
});

function wizRenderInboundPicker($picker) {
    var serverSelect = $($picker.data("server-select"));
    var inputName = $picker.data("input-name") || "inbound_ids_plan";
    var serverId = serverSelect.val();
    if (!serverId) {
        $picker.html('<p class="text-xs text-gray-500 mt-1">ابتدا سرور را انتخاب کنید.</p>');
        return;
    }
    $picker.html('<p class="text-xs text-gray-400 mt-1">⏳ در حال بارگذاری...</p>');
    $.getJSON("api/list-inbounds.php", { server_id: serverId })
        .done(function (res) {
            var html = '';
            if (res.server_type === 'pasarguard') {
                // Pasarguard: show group picker (group determines inbound routing)
                if (!res.groups || !res.groups.length) {
                    $picker.html('<p class="text-xs text-orange-500 mt-1">⚠ هیچ گروهی در پنل Pasarguard یافت نشد. ابتدا از پنل Pasarguard یک Group ایجاد کنید.</p>');
                    return;
                }
                html = '<label class="text-sm text-gray-600 dark:text-gray-300 font-medium">گروه پاسارگارد</label>';
                html += '<select name="pasarguard_group_id" required class="mt-1 w-full border border-gray-300 rounded px-3 py-2 text-sm dark:bg-gray-700 dark:text-gray-200">';
                html += '<option value="">— انتخاب گروه —</option>';
                res.groups.forEach(function (g) {
                    html += '<option value="' + g.id + '">' + $('<div>').text(g.label).html() + '</option>';
                });
                html += '</select>';
                html += '<p class="text-xs text-gray-400 mt-1">گروه، پروتکل‌ها و اینباندهای اختصاص‌یافته به کاربر را تعیین می‌کند.</p>';
                $picker.html(html);
                return;
            }
            if (res.server_type === 'marzban') {
                $picker.html('<p class="text-xs text-blue-500 mt-1">ℹ سرور Marzban از انتخاب inbound در پلن پشتیبانی نمی‌کند.</p>');
                return;
            }
            // X-UI: standard inbound picker
            if (!res.inbounds || !res.inbounds.length) {
                $picker.html('<p class="text-xs text-orange-500 mt-1">⚠ هیچ inboundی از پنل دریافت نشد. اتصال به پنل را بررسی کنید.</p>');
                return;
            }
            var size = Math.min(Math.max(res.inbounds.length, 3), 8);
            html = '<label class="text-sm text-gray-600 dark:text-gray-300 font-medium">Inboundها <span class="font-normal text-gray-400">(Ctrl+کلیک برای چند انتخاب)</span></label>';
            html += '<select name="' + inputName + '[]" multiple size="' + size + '" class="mt-1 w-full border border-gray-300 rounded px-3 py-2 text-sm dark:bg-gray-700 dark:text-gray-200">';
            res.inbounds.forEach(function (ib) {
                html += '<option value="' + ib.id + '">' + $('<div>').text(ib.label).html() + '</option>';
            });
            html += '</select>';
            $picker.html(html);
        })
        .fail(function (xhr) {
            if (xhr.status === 401) {
                $picker.html('<p class="text-xs text-red-500 mt-1">⛔ نشست منقضی شده — صفحه را رفرش کنید.</p>');
            } else {
                $picker.html('<p class="text-xs text-red-500 mt-1">⚠ خطا در دریافت اطلاعات پنل.</p>');
            }
        });
}

$(document).ready(function () {
    $(".wiz-inbound-picker").each(function () {
        var $picker = $(this);
        wizRenderInboundPicker($picker);
        var serverSelect = $($picker.data("server-select"));
        serverSelect.on("change", function () {
            wizRenderInboundPicker($picker);
        });
    });
});

