<script>
import { TITLE } from "../store/mutation-types"

export default {
    data() {
        return {
            transactionsFile: null,
            assignmentsFile: null
        }
    },
    computed: {
        hasFiles() {
            return this.$data.transactionsFile != null && this.$data.assignmentsFile != null;
        }
    },
    mounted() {
        this.$store.commit(TITLE, "Settings")
    },
    methods: {
        gotAssignments(e){
            this.$data.assignmentsFile = e.target.files[0];
        },
        gotTransactions(e) {
            this.$data.transactionsFile = e.target.files[0];
        },
        clearBudget() {
            fetch("/api/v1/budget/" + this.$store.getters.CurrentBudget.ID + "/settings/clear", {
                method: "POST", 
                headers: {
                    'Authorization': 'Bearer ' + this.$store.state.Session.Token
                },
            })
        },
        cleanNegative() {
            // <a href="/budget/{{.Budget.ID}}/settings/clean-negative">Fix all historic negative category-balances</a>
        },
        ynabImport() {
            // <input type="hidden" name="budget_id" value="{{$store.getters.CurrentBudget.ID}}" />
            let formData = new FormData();
            formData.append("transactions", this.$data.transactionsFile);
            formData.append("assignments", this.$data.assignmentsFile);
            this.$store.dispatch("YNAB", formData);
        }
    }
}
</script>

<template>
    <v-container>
    <h1>Danger Zone</h1>
    <v-row>
    <v-col>
        <v-card max-width="380">
            <v-card-header>
                <v-card-header-text>
                    <v-card-title>
                        Clear Budget
                    </v-card-title>
                    <v-card-subtitle>
                        This removes transactions and assignments to start from scratch. Accounts and categories are kept. Not undoable!
                    </v-card-subtitle>
                </v-card-header-text>
            </v-card-header>
            <v-card-actions>
                <v-btn @click="clearBudget">Clear budget</v-btn>
            </v-card-actions>
        </v-card>
    </v-col>
    <v-col max-width="380">
        <v-card max-width="380">
            <v-card-header>
                <v-card-header-text>
                    <v-card-title>
                        Fix all historic negative category-balances
                    </v-card-title>
                    <v-card-subtitle>
                        This restores YNABs functionality, that would substract any overspent categories' balances from next months inflows.
                    </v-card-subtitle>
                </v-card-header-text>
            </v-card-header>
            <v-card-actions>
                <v-btn @click="cleanNegative">Fix negative</v-btn>
            </v-card-actions>
        </v-card>
    </v-col>
    <v-col max-width="380">
        <v-card max-width="380">
            <v-card-header>
                <v-card-header-text>
                    <v-card-title>
                        Import YNAB Budget
                    </v-card-title>
                    <v-card-content>
                        <label for="transactions_file">
                            Transaktionen:
                            <input type="file" @change="gotTransactions" accept="text/*" />
                        </label>
                        <label for="assignments_file">
                            Budget:
                            <input type="file" @change="gotAssignments" accept="text/*" />
                        </label>
                    </v-card-content>
                </v-card-header-text>
            </v-card-header>
            <v-card-actions>
                <button v-if="hasFiles" @click="ynabImport">Importieren</button>
            </v-card-actions>
        </v-card>
    </v-col>
</v-row>
    <v-card>
    </v-card>
</v-container>
</template>