<template>
    <div v-if="!initialized">{{ $__("Loading") }}</div>
    <div v-else id="licenses_confirm_delete">
        <h2>{{ $__("Delete license") }}</h2>
        <div>
            <form @submit="onSubmit($event)">
                <fieldset class="rows">
                    <ol>
                        <li>
                            {{ $__("License name") }}:
                            {{ license.name }}
                        </li>
                        <li>
                            {{ $__("Description") }}:
                            {{ license.description }}
                        </li>
                    </ol>
                </fieldset>
                <fieldset class="action">
                    <input
                        type="submit"
                        variant="primary"
                        :value="$__('Yes, delete')"
                    />
                    <router-link
                        to="/cgi-bin/koha/erm/licenses"
                        role="button"
                        class="cancel"
                        >{{ $__("No, do not delete") }}</router-link
                    >
                </fieldset>
            </form>
        </div>
    </div>
</template>

<script>
import { fetchLicense, checkError } from "../../fetch/erm.js"
import { setMessage, setError } from "../../messages"

export default {
    data() {
        return {
            license: {},
            initialized: false,
        }
    },
    beforeRouteEnter(to, from, next) {
        next(vm => {
            vm.license = vm.getLicense(to.params.license_id)
            vm.initialized = true
        })
    },
    methods: {
        async getLicense(license_id) {
            const license = await fetchLicense(license_id)
            this.license = license
            this.initialized = true
        },
        onSubmit(e) {
            e.preventDefault()

            let apiUrl = "/api/v1/erm/licenses/" + this.license.license_id

            const options = {
                method: "DELETE",
                headers: {
                    "Content-Type": "application/json;charset=utf-8",
                },
            }

            fetch(apiUrl, options)
                .then(response => checkError(response, 1))
                .then(response => {
                    if (response.status == 204) {
                        this.$router.push("/cgi-bin/koha/erm/licenses")
                        setMessage(this.$__("License deleted"))
                    } else {
                        setError(response.message || response.statusText)
                    }
                })
                .catch(error => {
                    setError(error)
                })
        },
    },
    name: "LicensesFormConfirmDelete",
}
</script>
