<template>
    <v-dialog v-model="dialog_close_jurnal" persistent width="40vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                TUTUP SEMUA JURNAL PADA PERIODE {{ selected_periode.periodeName }}
            </v-card-title>
            <v-card-text>
                <div v-for="item in jurnal_to_close" :key="item.jurnalIsPosted">
                    <p v-if="item.jurnalIsPosted == 'N'">
                        Jumlah jurnal yang tidak dapat ditutup:
                        <strong>{{ item.total_jurnal }}</strong>
                    </p>
                    <p v-else>
                        Jumlah jurnal yang dapat ditutup: <strong>{{ item.total_jurnal }}</strong>
                    </p>
                </div>
                <p>
                    Apakah anda yakin akan menutup semua jurnal yang dibuat di
                    <strong> {{ area_jurnal }} </strong> dalam rentang waktu <br />
                    <strong>{{ selected_periode.periode }}</strong> <br />
                </p>
            </v-card-text>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="error" outline flat @click="dialog_close_jurnal = false">Batal</v-btn>
                <v-btn color="primary" dark @click="closeJurnal()">Ya</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {

        }
    },
    computed: {
        selected_periode() {
            return this.$store.state.journal.selected_periode
        },
        area_jurnal() {
            const user = this.$store.state.journal.user
            if (user.M_BranchID == '0') {
                return `Regional ${user.S_RegionalName}`
            } else {
                return `Cabang ${user.M_BranchName}, Regional ${user.S_RegionalName}`
            }
        },
        jurnal_to_close() {
            return this.$store.state.journal.jurnal_to_close
        },
        dialog_close_jurnal: {
            get() {
                return this.$store.state.journal.dialog_close_jurnal
            },
            set(val) {
                this.$store.commit("journal/update_dialog_close_jurnal", val)
            }
        }
    },
    methods: {
        closeJurnal() {
            var jurnal = this.jurnal_to_close.find(item => item.jurnalIsPosted === 'Y')
            if (jurnal && Number(jurnal.total_jurnal) > 0) {
                this.$store.dispatch("journal/closeJurnalPerPeriode", { typeid: jurnal.JurnalTypeID })
                return;
            }
            this.$store.commit("journal/update_open_dialog_info", true);
            this.$store.commit("journal/update_msg_info",
                "Tidak ada jurnal yang dapat ditutup"
            );
        }
    },
}
</script>
