<template>
    <v-dialog v-model="dialog_view_jurnal" persistent width="70vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                DETAIL JURNAL PENYESUAIAN
            </v-card-title>
            <v-card-text>
                <v-layout row wrap>
                    <v-flex xs6 pa-1 pr-3>
                        <div class="font-weight-bold">Company Name</div>
                        <div class="pa-2 grey lighten-2">
                            {{ selected_journal_balik.M_BranchCompanyName  }}
                        </div>
                    </v-flex>
                    <v-flex xs6 pa-1 pl-3>
                        <div class="font-weight-bold">Journal</div>
                        <div class="pa-2 grey lighten-2">
                            {{ selected_journal_balik.jurnalTitle  }}
                        </div>
                    </v-flex>
                    <v-flex xs6 pa-1 pr-3>
                        <div class="font-weight-bold">Regional</div>
                        <div class="pa-2 grey lighten-2">
                            {{ selected_journal_balik.S_RegionalName  }}
                        </div>
                    </v-flex>
                    <v-flex xs6 pa-1 pl-3>
                        <div class="font-weight-bold">Journal Date</div>
                        <div class="pa-2 grey lighten-2">
                            {{ selected_journal_balik.jurnalDate  }}
                        </div>
                    </v-flex>
                    <v-flex xs6 pa-1 pr-3>
                        <div class="font-weight-bold">Branch</div>
                        <div class="pa-2 grey lighten-2">
                            {{ selected_journal_balik.M_BranchName }}
                        </div>
                    </v-flex>
                    <v-flex xs6 pa-1 pl-3>
                        <div class="font-weight-bold">Journal Type</div>
                        <div class="pa-2 grey lighten-2">
                            {{ selected_journal_balik.JurnalTypeName }}
                        </div>
                    </v-flex>
                </v-layout>

                <v-layout row wrap mt-3>
                    <v-flex xs12>
                        <v-data-table
                            :headers="headerdetails" class="elevation-1"
                            :items="journal_detail_balik.transaction" hide-actions 
                        >
                            <template slot="items" slot-scope="props">
                                <tr>
                                    <td class="text-xs-center pa-2">{{ props.item.coaAccountNo }}</td>
                                    <td class="text-xs-left pa-2"> {{ props.item.jurnalTxDescription }}</td>
                                    <td class="text-xs-left pa-2">{{ props.item.jurnalAddOnValue }}</td>
                                    <td class="text-xs-center pa-2">{{ props.item.coaCurrencyCode }}</td>
                                    <td class="text-xs-right pa-2">{{ formatCurrency(props.item.jurnalTxDebit) }}</td>
                                    <td class="text-xs-right pa-2">{{ formatCurrency(props.item.jurnalTxCredit) }}</td>
                                </tr>
                            </template>
                            <template v-slot:footer>
                                <tr>
                                    <td colspan="4" class="font-weight-bold pa-2">TOTAL</td>
                                    <td class="text-xs-right pa-2 font-weight-bold">
                                        {{ formatCurrency(journal_detail_balik.summary.total_debit) }}
                                    </td>
                                    <td class="text-xs-right pa-2 font-weight-bold">
                                        {{ formatCurrency(journal_detail_balik.summary.total_kredit) }}
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="5" class="font-weight-bold pa-2">BALANCE</td>
                                    <td width="10%" class="text-xs-right pa-2 font-weight-bold">
                                        {{ formatCurrency(journal_detail_balik.summary.balance) }}
                                    </td>
                                </tr>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
            </v-card-text>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="error" outline flat @click="closeEditDialog()">Tutup</v-btn>
                <div v-if="(user_level.M_ApproveLevelID == '1' && user_level.DivisionKodeSurat == 'KEU') 
                        || Number(user_level.M_ApproveLevelID) >= 2"
                    >
                    <v-btn 
                        v-if="selected_journal_balik.jurnalPenyesuaianStatus == 'draft'"
                        color="primary" dark @click="updateJurnalStatus('verified')"
                    >Verifikasi</v-btn>
                    <v-btn 
                        v-if="selected_journal_balik.jurnalPenyesuaianStatus == 'verified'"
                        color="primary" dark @click="updateJurnalStatus('approved')"
                    >Approve</v-btn>
                </div>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            headerdetails: [
                {
                    text: "AKUN", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-2 pl-2 blue lighten-3 white--text",
                },
                {
                    text: "DESKRIPSI", align: "center", sortable: false,
                    value: "mr", width: "25%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "INFO", align: "center", sortable: false,
                    value: "lab", width: "15%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "CURRENCY", align: "center", sortable: false,
                    value: "lab", width: "10%", class: "pa-2  blue lighten-3 white--text",
                },
                {
                    text: "DEBIT", align: "center", sortable: false,
                    value: "lab", width: "15%", class: "pa-2 blue lighten-3 white--text",
                },
                {
                    text: "CREDIT", align: "center", sortable: false,
                    value: "lab", width: "15%", class: "pa-2 blue lighten-3 white--text",
                }
            ]
        }
    },
    computed: {
        dialog_view_jurnal: {
            get() {
                return this.$store.state.journal.dialog_view_jurnal
            },
            set(val) {
                this.$store.commit("journal/update_dialog_view_jurnal", val)
            }
        },
        selected_journal_balik: {
            get() {
                return this.$store.state.journal.selected_journal_balik;
            },
            set(val) {
                this.$store.commit("journal/update_selected_journal_balik", val);
            },
        },
        journal_detail_balik: {
            get() {
                return this.$store.state.journal.journal_detail_balik
            },
            set(val) {
                this.$store.commit("journal/update_journal_detail_balik", val)
            }
        },
        user_level() {
            return this.$store.state.journal.user_level
        }
    },
    methods: {
        formatCurrency(val) {
            const price = parseFloat(val)
            const IDR = new Intl.NumberFormat("id-ID", {
                style: "currency",
                currency: "IDR"
            })
            return `${IDR.format(price)}`
        },
        closeEditDialog() {
            let detailnull ={
                transaction: [],
                summary: {
                    total_debit: 0,
                    total_kredit: 0,
                    balance: 0,
                }
            }
            
            this.journal_detail_balik = detailnull
            this.selected_journal_balik = {}
            this.dialog_view_jurnal = false
        },
        updateJurnalStatus(status) {
            this.$store.dispatch("journal/updateStatusJurnalKoreksi", status)
        }
    },
}
</script>