<template>
    <form @submit.prevent="submit">
        <div v-if="storedDraft !== undefined" class="alert alert-info d-flex flex-column flex-md-row">
            <div class="p-2">
                <Icon v="pencil-ruler" />
                <T :params="{ time: $datetime(storedDraft.savedAt) }">form.persistent.present.question</T>
            </div>
            <div class="p-2 text-nowrap">
                <button class="btn btn-primary" @click.prevent="load">
                    <Icon v="cloud-download" />
                    <T>form.persistent.present.load</T>
                </button>
                <button class="btn btn-outline-danger" @click.prevent="abandon">
                    <Icon v="trash" />
                    <T>form.persistent.present.abandon</T>
                </button>
            </div>
        </div>
        <slot></slot>
        <p v-if="draftSaved" class="text-muted small">
            <Icon v="pencil-ruler" />
            <T :params="{ time: $datetime(draftSaved) }">form.persistent.saved</T>
        </p>
    </form>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
    props: {
        value: { required: true, type: Object },
        uid: { required: true, type: String },
    },
    data() {
        return {
            originalValue: JSON.stringify(this.value),
            storageKey: `persistent-form-draft--${this.uid}`,
            storedDraft: undefined as { draft: any, savedAt: number } | undefined,
            draftSaved: null as number | null,
        };
    },
    watch: {
        value: {
            handler(newValue) {
                this.draftSaved = Math.round(Date.now() / 1000);
                localStorage.setItem(this.storageKey, JSON.stringify({ draft: newValue, savedAt: this.draftSaved }));
            },
            deep: true,
        },
    },
    async mounted() {
        if (!process.browser) {
            return;
        }

        const storedRaw = localStorage.getItem(this.storageKey);
        if (!storedRaw) {
            return;
        }
        const stored = JSON.parse(storedRaw);

        if (stored && JSON.stringify(stored.draft) !== this.originalValue) {
            this.storedDraft = stored;
        }
    },
    methods: {
        load() {
            if (!this.storedDraft) {
                return;
            }
            this.$emit('input', this.storedDraft.draft);
            this.storedDraft = undefined;
        },
        abandon() {
            localStorage.removeItem(this.storageKey);
            this.storedDraft = undefined;
        },
        submit(event: Event) {
            this.abandon();
            this.$emit('submit', event);
        },
    },
});
</script>
