<template>
<div style="width: 100%;">
    <v-toolbar dark class="blue lighten-3 white--text">
        <v-toolbar-title class="white--text">JURNAL PENYESUAIAN</v-toolbar-title>
        <v-spacer></v-spacer>
    </v-toolbar>

    <v-card class="pa-2">
        <v-layout row align-center>
            <v-flex grow>
                <span>
                    <v-chip outline color="red">
                        {{ selected_periode.periodeStartDate }} - {{ selected_periode.periodeEndDate }}
                    </v-chip>
                </span>
            </v-flex>
            <v-flex xs2 pl-2>
                <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_balik.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_balik.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_balik.typeJournalID" item-value="JurnalTypeID" label="Tipe Jurnal"
                ></v-autocomplete>
            </v-flex>
            <v-flex xs2 pl-2>
                <v-text-field
                    v-model="filter_balik.search" hide-details
                    label="Cari nomor jurnal" outline
                ></v-text-field>
            </v-flex>
            <v-flex shrink pl-2>
                <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-flex shrink pl-2>
                <v-btn
                    color="primary" dark @click="addKoreksiJurnal()"
                    style="min-width: 50px; height: 50px; margin: 0;"
                >
                    <v-icon>add</v-icon>
                </v-btn>
            </v-flex>
            <v-flex text-xs-right>
                <v-btn
                    color="orange" class="white--text"
                    @click="choosePeriode()"
                >
                    Periode Accounting
                </v-btn>
            </v-flex>
        </v-layout>
    </v-card>

    <v-card class="pa-2 mt-2">
        <v-data-table
            :headers="headers" :items="list_journal_balik.records" 
            class="elevation-1" hide-actions
        >
            <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-chip color="info" outline small>
                        {{ props.item.jurnalPenyesuaianStatus }}
                    </v-chip>
                </td>
                <td class="text-xs-center pa-2">
                    <v-tooltip bottom>
                        <template v-slot:activator="{ on, attrs }">
                            <v-btn 
                                outline small color="info"
                                style="min-width: 50px; margin: 0"
                            >
                                <v-icon 
                                    v-bind="attrs" v-on="on" color="info"
                                    @click="openDialogJurnalBalik(props.item)"
                                >visibility</v-icon>
                            </v-btn>
                        </template>
                        <span>Detail</span>
                    </v-tooltip>
                    <!-- <v-tooltip bottom v-if="props.item.jurnalPenyesuaianStatus == 'draft'">
                        <template v-slot:activator="{on, attrs}">
                            <v-btn 
                                outline small color="primary"
                                style="min-width: 50px; margin: 0"
                            >
                                <v-icon
                                    v-bind="attrs" v-on="on" color="primary"
                                    @click="{}"
                                >check</v-icon>
                            </v-btn>
                        </template>
                        <span>Verifikasi</span>
                    </v-tooltip> -->
                </td>
            </tr>
            </template>
        </v-data-table>
        <v-divider class="my-1"></v-divider>
        <div class="pa-2 mt-1 text-xs-center">
            <v-pagination v-model="currpage_balik" :length="pagilength" :total-visible="10"></v-pagination>
        </div>
    </v-card>

    <v-snackbar v-model="snackbar.active" multi-line top :timeout="3000" :color="snackbar.color">
        {{ snackbar.message }}
        <v-btn class="ml-2" round flat @click="snackbar.active = false">Tutup</v-btn>
    </v-snackbar>

    <dialog-pilih-jurnal></dialog-pilih-jurnal>
    <dialog-balik-jurnal></dialog-balik-jurnal>
    <dialog-add-jurnaltrx></dialog-add-jurnaltrx>
    <dialog-view-jurnal></dialog-view-jurnal>
    <dialog-sel-periode></dialog-sel-periode>
</div>
</template>

<script>
module.exports = {
    components: {
        'dialog-pilih-jurnal': httpVueLoader('./dialogSelectJurnal.vue'),
        'dialog-balik-jurnal': httpVueLoader('./dialogBalikJurnal.vue'),
        'dialog-add-jurnaltrx': httpVueLoader('./dialogAddRowJurnal.vue'),
        'dialog-view-jurnal': httpVueLoader('./dialogViewJurnal.vue'),
        'dialog-sel-periode': httpVueLoader('./dialogSelectPeriode.vue')
    },
    mounted() {
        this.$store.dispatch("journal/getUserApproveLevel")
        this.$store.dispatch("journal/getDaftarTipeJurnal")
        this.$store.dispatch("journal/getDaftarAccount")
        this.$store.dispatch("journal/getDaftarPeriode")
            .then(() => {
                this.$store.dispatch("journal/getDaftarJournalBalik")
            })
    },
    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: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JOURNAL TYPE", align: "center", sortable: false,
                    value: "action", width: "5%", 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: {
        filter_balik: {
            get() {
                return this.$store.state.journal.filter_balik
            },
            set(val) {
                this.$store.commit("journal/update_filter_balik", val)
            }
        },
        formattedStartdate() {
            return this.formatDate(this.filter_balik.startdate)
        },
        formattedEnddate() {
            return this.formatDate(this.filter_balik.enddate)
        },
        list_typejurnal() {
            return this.$store.state.journal.list_typejurnal
        },
        list_periode() {
            return this.$store.state.journal.list_periode
        },
        selected_periode() {
            return this.$store.state.journal.selected_periode
        },
        list_journal_balik() {
            return this.$store.state.journal.list_journal_balik
        },
        currpage_balik: {
            get() {
                return this.$store.state.journal.currpage_balik
            },
            set(val) {
                this.$store.commit("journal/update_currpage_balik", val)
                this.$store.dispatch("journal/getDaftarJournalBalik")
            }
        },
        pagilength() {
            let total = this.list_journal_balik.total
            if (total == undefined || total < 1) return 1
            return Math.ceil(total/10)
        },
        snackbar: {
            get() {
                return this.$store.state.journal.snackbar
            },
            set(val) {
                this.$store.commit("journal/update_snackbar", val)
            }
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        addKoreksiJurnal() {
            this.$store.commit("journal/update_dialog_journal", true)
        },
        searchJurnal() {
            this.$store.dispatch("journal/getDaftarJournalBalik")
        },
        choosePeriode() {
            this.$store.commit("journal/update_dialog_pick_periode", true)
        },
        openDialogJurnalBalik(item) {
            this.$store.commit("journal/update_selected_journal_balik", item)
            this.$store.dispatch("journal/getDetailJournalBalik")
            this.$store.commit("journal/update_dialog_view_jurnal", true)
        }
    },
}
</script>