<template>
<v-dialog v-model="dialog_journal" persistent width="80vw">
    <v-card>
        <v-card-title class="headline grey lighten-2" primary-title>
            PILIH JURNAL
        </v-card-title>
        <v-card-text>
            <v-layout row align-center>
                <v-flex xs2>
                    <v-menu
                        v-model="menuStartdateFilter" offset-y lazy min-width="290px"
                        transition="scale-transition" max-width="290px" full-width
                        :nudge-right="40" :close-on-content-click="false"
                    >
                        <template v-slot:activator="{on}">
                            <v-text-field
                                v-model="formattedStartdate" label="Tanggal Awal"
                                hide-details readonly v-on="on" outline
                            ></v-text-field>
                        </template>
                        <v-date-picker
                            v-model="filter_closed.startdate" no-title 
                            @input="menuStartdateFilter = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-menu
                        v-model="menuEnddateFilter" offset-y lazy min-width="290px"
                        transition="scale-transition" max-width="290px" full-width
                        :nudge-right="40" :close-on-content-click="false"
                    >
                        <template v-slot:activator="{on}">
                            <v-text-field
                                v-model="formattedEnddate" label="Tanggal Akhir"
                                hide-details readonly v-on="on" outline
                            ></v-text-field>
                        </template>
                        <v-date-picker
                            v-model="filter_closed.enddate" no-title 
                            @input="menuEnddateFilter = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-autocomplete
                        :items="list_typejurnal" outline hide-details item-text="JurnalTypeName"
                        v-model="filter_closed.typeJournalID" item-value="JurnalTypeID" label="Tipe Jurnal"
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-autocomplete
                        :items="list_periode" outline hide-details item-text="periodeName"
                        v-model="filter_closed.periodeID" item-value="periodeID" label="Periode"
                    >
                        <template slot="item" slot-scope="{item}">
                            <v-list-tile-content>
                                <v-list-tile-title>{{ item.periodeName }}</v-list-tile-title>
                                <v-list-tile-sub-title>
                                    {{ item.periodeStartDate }} - {{ item.periodeEndDate }}
                                </v-list-tile-sub-title>
                            </v-list-tile-content>
                        </template>
                    </v-autocomplete>
                </v-flex>
                <v-flex grow pl-2>
                    <v-text-field
                        v-model="filter_closed.search" hide-details
                        label="Cari nomor jurnal" outline
                    ></v-text-field>
                </v-flex>
                <v-flex shrink pl-2 text-xs-right>
                    <v-btn
                        color="info" dark @click="searchJurnal()"
                        style="min-width: 50px; height: 50px; margin: 0;"
                    >
                        <v-icon>search</v-icon>
                    </v-btn>
                </v-flex>
            </v-layout>

            <v-layout row wrap class="mt-4">
                <v-flex xs12>
                    <v-data-table
                        :headers="headers" class="elevation-1" hide-actions
                        :items="list_journal_closed.records"
                    >
                        <template slot="items" slot-scope="props">
                        <tr>
                            <td class="text-xs-center pa-2">{{ props.item.jurnalDate }}</td>
                            <td class="text-xs-center pa-2">{{ props.item.jurnalNo }}</td>
                            <td class="text-xs-center pa-2">{{ props.item.jurnalTitle }}</td>
                            <td class="text-xs-center pa-2">{{ props.item.jurnalDescription }}</td>
                            <td class="text-xs-center pa-2">{{ props.item.JurnalTypeName }}</td>
                            <td class="text-xs-center pa-2">
                                <v-btn
                                    outline small color="primary"
                                    @click="openDialogJurnalBalik(props.item)"
                                >
                                    KOREKSI
                                    <v-icon small right dark>assignment_return</v-icon>
                                </v-btn>
                            </td>
                        </tr>
                        </template>
                    </v-data-table>
                    <v-divider class="my-1"></v-divider>
                    <div class="pa-2 mt-2 text-xs-center">
                        <v-pagination 
                            v-model="currpage_closed" :length="tablelength"
                            :total-visible="10"
                        ></v-pagination>
                    </div>
                </v-flex>
            </v-layout>
        </v-card-text>
        <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn @click="closeDialogJournal()" color="red" class="white--text">Tutup</v-btn>
        </v-card-actions>
    </v-card>
</v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            menuStartdateFilter: false,
            menuEnddateFilter: false,
            headers: [
                {
                    text: "JOURNAL DATE", align: "center", sortable: false,
                    value: "action", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JOURNAL NO", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JOURNAL", align: "center", sortable: false,
                    value: "action", width: "25%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JOURNAL DESCRIPTION", align: "center", sortable: false,
                    value: "action", width: "25%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JOURNAL TYPE", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "ACTION", align: "center", sortable: false,
                    value: "action", width: "10%", class: "pa-1 blue lighten-3 white--text",
                }
            ]
        }
    },
    computed: {
        dialog_journal: {
            get() {
                return this.$store.state.journal.dialog_journal
            },
            set(val) {
                this.$store.commit("journal/update_dialog_journal", val)
            }
        },
        filter_closed: {
            get() {
                return this.$store.state.journal.filter_closed
            },
            set(val) {
                this.$store.commit("journal/update_filter_closed", val)
            }
        },
        formattedStartdate() {
            return this.formatDate(this.filter_closed.startdate)
        },
        formattedEnddate() {
            return this.formatDate(this.filter_closed.enddate)
        },
        list_typejurnal() {
            return this.$store.state.journal.list_typejurnal
        },
        list_periode() {
            return this.$store.state.journal.list_periode
        },
        list_journal_closed() {
            return this.$store.state.journal.list_journal_closed
        },
        currpage_closed: {
            get() {
                return this.$store.state.journal.currpage_closed
            },
            set(val) {
                this.$store.commit("journal/update_currpage_closed", val)
                this.$store.dispatch("journal/getDaftarJournalClosed")
            }
        },
        tablelength() {
            let total = this.list_journal_closed.total
            if (total == undefined || total < 1) return 1
            return Math.ceil(total/5)
        },
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        searchJurnal() {
            this.$store.dispatch("journal/getDaftarJournalClosed")
        },
        closeDialogJournal() {
            this.dialog_journal = false
        },
        openDialogJurnalBalik(jurnal) {
            jurnal.detail = []
            jurnal.totals = {
                total_debit: 0,
                total_kredit: 0,
                balance: 0,
            }

            this.$store.commit("journal/update_selected_journal_closed", jurnal)
            this.$store.dispatch("journal/getDetailJournalClosed")
            this.$store.commit("journal/update_dialog_journal_balik", true)
            this.dialog_journal = false
        }
    },
    watch: {
        dialog_journal(val) {
            if (val) {
                this.$nextTick(() => {
                    this.$store.dispatch("journal/getDaftarJournalClosed")
                })
            }
        }
    },
}
</script>