<script>
    import {getContext} from "svelte";

    const channel = getContext("channel");
    export let selected;
    export let schema;
    export let filter;

    function deleteRecords(e) {
        e.preventDefault();
        axios
            .post(channel.lucentUrl + "/records/delete", {
                ids: selected.map((s) => s.id),
            })
            .then((response) => {
                window.location.reload();
            })
            .catch((error) => {
                console.log(error);
            });
    }

    function changeStatus(e, status) {
        axios
            .post(channel.lucentUrl + "/records/status/" + status, {
                schemaName: schema.name,
                records: selected
            })
            .then((response) => {
                window.location.reload();
            })
            .catch((error) => {
                console.log(error);
            });
    }
</script>

<div class="d-flex align-items-center mb-3">
    <span class="me-2">{selected.length} records selected</span>
    <div class="btn-group " role="group" aria-label="Basic example">
        <button
            on:click|preventDefault={(e) => changeStatus(e, "published")}
            type="button"
            class="btn btn-sm btn-outline-primary">Publish
        </button
        >
        <button
            on:click|preventDefault={(e) => changeStatus(e, "draft")}
            type="button"
            class="btn btn-sm btn-outline-primary">Make Draft
        </button
        >
        {#if filter["status_in"] === "trashed"}
            <button
                on:click|preventDefault={(e) => changeStatus(e, "published")}
                type="button"
                class="btn btn-sm btn-outline-primary">Publish
            </button
            >
            {#if schema.hasDrafts}
                <button
                    on:click|preventDefault={(e) => changeStatus(e, "draft")}
                    type="button"
                    class="btn btn-sm btn-outline-primary">Make Draft
                </button
                >
            {/if}
            <button
                on:click|preventDefault={deleteRecords}
                type="button"
                class="btn btn-sm btn-outline-primary">Delete forever
            </button
            >
        {:else}
            <button
                type="button"
                on:click|preventDefault={(e) => changeStatus(e, "trashed")}
                class="btn btn-sm btn-outline-primary">Move to trash
            </button
            >
        {/if}
    </div>
</div>
