<script>
    const $remove = $("#remove");
    const $update = $("#update");

    const $table = $("#packages");
    $table.on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table",
        function () {
            $remove.prop("disabled", !$table.bootstrapTable("getSelections").length);
            $update.prop("disabled", !$table.bootstrapTable("getSelections").length);
        })

    const $successForm = $("#successForm");
    const $successDetails = $("#successDetails");
    $successForm.on("hidden.bs.modal", function() { window.location.reload(); });

    const $failedForm = $("#failedForm");
    const $errorDetails = $("#errorDetails");
    $failedForm.on("hidden.bs.modal", function() { window.location.reload(); });

    const $package = $("#package");
    const $knownPackages = $("#knownPackages");
    $package.keyup(function () {
        const $this = $(this);
        clearTimeout($this.data("timeout"));

        $this.data("timeout", setTimeout($.proxy(function () {
            const $value = $package.val();

            $.ajax({
                url: "/service-api/v1/search",
                data: {"for": $value},
                type: "GET",
                dataType: "json",
                success: function (resp) {
                    const $options = resp.map(function (pkg) {
                        const $option = document.createElement("option");
                        $option.value = pkg.package;
                        $option.innerText = `${pkg.package} (${pkg.description})`;
                        return $option;
                    });
                    $knownPackages.empty().append($options);
                    $this.focus();
                },
            })
        }, this), 500));
    })

    function doPackageAction($uri, $packages) {
        if ($packages.length === 0)
            return;
        $.ajax({
            url: $uri,
            data: JSON.stringify({packages: $packages}),
            type: "POST",
            contentType: "application/json",
            success: function (_) {
                const $details = $packages.map(function (pkg) {
                    const $li = document.createElement("li");
                    $li.innerText = pkg;
                    return $li;
                });
                $successDetails.empty().append($details);
                $successForm.modal("show");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $errorDetails.text(errorThrown);
                $failedForm.modal("show");
            },
        })
    }

    function getSelection() {
        return $.map($table.bootstrapTable("getSelections"), function(row) {
            return row._data["package-base"];
        })
    }

    function addPackages() {
        const $packages = [$package.val()]
        doPackageAction("/service-api/v1/add", $packages);
    }

    function requestPackages() {
        const $packages = [$package.val()]
        doPackageAction("/service-api/v1/request", $packages);
    }

    function removePackages() { doPackageAction("/service-api/v1/remove", getSelection()); }

    function updatePackages() { doPackageAction("/service-api/v1/add", getSelection()); }

    $(function () {
        $table.bootstrapTable("uncheckAll");
    })
</script>