<template>
<v-dialog v-model="dialog_journal_balik" persistent width="90vw" scrollable>
    <v-card>
        <v-card-title class="headline grey lighten-2">
            KOREKSI JURNAL
        </v-card-title>
        <v-card-text style="max-height: 80vh;">
            <v-layout row wrap>
                <v-flex xs6 pa-1>
                    <div class="font-weight-bold">Company Name</div>
                    <div class="pa-2 grey lighten-2">
                        {{ selected_journal_closed.M_BranchCompanyName  }}
                    </div>
                </v-flex>
                <v-flex xs6 pa-1>
                    <div class="font-weight-bold">Journal</div>
                    <div class="pa-2 grey lighten-2">
                        {{ selected_journal_closed.jurnalTitle  }}
                    </div>
                </v-flex>
                <v-flex xs3 pa-1>
                    <div class="font-weight-bold">Regional</div>
                    <div class="pa-2 grey lighten-2">
                        {{ selected_journal_closed.S_RegionalName  }}
                    </div>
                </v-flex>
                <v-flex xs3 pa-1>
                    <div class="font-weight-bold">Branch</div>
                    <div class="pa-2 grey lighten-2">
                        {{ selected_journal_closed.M_BranchName ?? "-" }}
                    </div>
                </v-flex>
                <v-flex xs3 pa-1>
                    <div class="font-weight-bold">Journal Date</div>
                    <div class="pa-2 grey lighten-2">
                        {{ selected_journal_closed.jurnalDate  }}
                    </div>
                </v-flex>
                <v-flex xs3 pa-1>
                    <div class="font-weight-bold">Journal Type</div>
                    <div class="pa-2 grey lighten-2">
                        {{ selected_journal_closed.JurnalTypeName }}
                    </div>
                </v-flex>
            </v-layout>

            <v-layout row wrap class="mt-2">
                <v-flex xs6 pr-1>
                    <h3>Jurnal Awal</h3>
                    <v-data-table
                        :headers="headers" class="elevation-1"
                        :items="selected_journal_closed.detail" 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.coaDescription }}</td>
                            <td class="text-xs-left pa-2">{{ props.item.jurnalAddOnValue }}</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="3" class="font-weight-bold pa-2">TOTAL</td>
                            <td class="text-xs-right pa-2 font-weight-bold">
                                {{ formatCurrency(selected_journal_closed.totals.total_debit ?? 0) }}
                            </td>
                            <td class="text-xs-right pa-2 font-weight-bold">
                                {{ formatCurrency(selected_journal_closed.totals.total_kredit ?? 0) }}
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" class="font-weight-bold pa-2">BALANCE</td>
                            <td class="text-xs-right pa-2 font-weight-bold">
                                {{ formatCurrency(selected_journal_closed.totals.balance ?? 0) }}
                            </td>
                        </tr>
                        </template>
                    </v-data-table>
                </v-flex>
                <v-flex xs6 pl-1>
                    <h3>Jurnal Balik</h3>
                    <v-data-table
                        :headers="headers_balik" 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.coaDescription }}</td>
                            <td class="text-xs-left pa-2">{{ props.item.jurnalAddOnValue }}</td>
                            <td class="text-xs-right pa-2">{{ formatCurrency(props.item.jurnalTxCredit) }}</td>
                            <td class="text-xs-right pa-2">{{ formatCurrency(props.item.jurnalTxDebit) }}</td>
                        </tr>
                        </template>
                        <template v-slot:footer>
                        <tr>
                            <td colspan="3" class="font-weight-bold pa-2">TOTAL</td>
                            <td class="text-xs-right pa-2 font-weight-bold">
                                {{ formatCurrency(journal_detail_balik.summary.total_kredit ?? 0) }}
                            </td>
                            <td class="text-xs-right pa-2 font-weight-bold">
                                {{ formatCurrency(journal_detail_balik.summary.total_debit ?? 0) }}
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" class="font-weight-bold pa-2">BALANCE</td>
                            <td class="text-xs-right pa-2 font-weight-bold">
                                {{ formatCurrency(journal_detail_balik.summary.balance ?? 0) }}
                            </td>
                        </tr>
                        </template>
                    </v-data-table>
                </v-flex>
            </v-layout>

            <v-layout row wrap class="mt-2">
                <v-flex grow pr-1>
                    <h3>Jurnal Penyesuaian</h3>
                </v-flex>
                <v-flex shrink pl-1>
                    <v-btn 
                        color="primary" dark @click="addTransactions()" 
                        style="min-width: 50px; margin: 0;" small
                    >
                        <v-icon small>add</v-icon>
                    </v-btn>
                </v-flex>
                <v-flex xs12 mt-2>
                    <v-data-table
                        :headers="headers_trx" class="elevation-1"
                        :items="journal_balik_new" 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.coaDescription }}</td>
                            <td class="text-xs-left pa-2">{{ props.item.jurnalAddOnValue }}</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>
                            <td class="text-xs-center pa-2">
                                <v-btn 
                                    color="red" dark @click="removeTransaction(props.item)" 
                                    style="min-width: 50px; margin: 0;" small
                                >
                                    <v-icon small>delete</v-icon>
                                </v-btn>    
                            </td>
                        </tr>
                    </template>
                    <template v-slot:footer>
                    <tr>
                        <td colspan="3" class="font-weight-bold pa-2">TOTAL</td>
                        <td class="text-xs-right pa-2 font-weight-bold">
                            {{ formatCurrency(summary_balik_new.total_debit) }}
                        </td>
                        <td class="text-xs-right pa-2 font-weight-bold">
                            {{ formatCurrency(summary_balik_new.total_kredit) }}
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" class="font-weight-bold pa-2">BALANCE</td>
                        <td class="text-xs-right pa-2 font-weight-bold">
                            {{ formatCurrency(summary_balik_new.balance) }}
                        </td>
                    </tr>
                    </template>
                    </v-data-table>
                </v-flex>
            </v-layout>
        </v-card-text>
        <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn @click="batalBalikJurnal()" color="red" class="white--text">Batal</v-btn>
            <v-btn @click="simpanBalikJurnal()" color="primary" class="white--text">Simpan</v-btn>
        </v-card-actions>
    </v-card>
</v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            headers: [
                {
                    text: "AKUN", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 info white--text",
                },
                {
                    text: "DESKRIPSI", align: "center", sortable: false,
                    value: "action", width: "25%", class: "pa-1 info white--text",
                },
                {
                    text: "INFO", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 info white--text",
                },
                {
                    text: "DEBIT", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 info white--text",
                },
                {
                    text: "KREDIT", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 info white--text",
                },
            ],
            headers_balik: [
                {
                    text: "AKUN", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 orange white--text",
                },
                {
                    text: "DESKRIPSI", align: "center", sortable: false,
                    value: "action", width: "25%", class: "pa-1 orange white--text",
                },
                {
                    text: "INFO", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 orange white--text",
                },
                {
                    text: "DEBIT", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 orange white--text",
                },
                {
                    text: "KREDIT", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 orange white--text",
                },
            ],
            headers_trx: [
                {
                    text: "AKUN", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "DESKRIPSI", align: "center", sortable: false,
                    value: "action", width: "25%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "INFO", align: "center", sortable: false,
                    value: "action", width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "DEBIT", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "KREDIT", align: "center", sortable: false,
                    value: "action", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false,
                    value: "action", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        dialog_journal_balik: {
            get() {
                return this.$store.state.journal.dialog_journal_balik
            },
            set(val) {
                this.$store.commit("journal/update_dialog_journal_balik", val)
            }
        },
        selected_journal_closed: {
            get() {
                return this.$store.state.journal.selected_journal_closed
            },
            set(val) {
                this.$store.commit("journal/update_selected_journal_closed", val)
            }
        },
        journal_detail_balik: {
            get() {
                return this.$store.state.journal.journal_detail_balik
            },
            set(val) {
                this.$store.commit("journal/update_journal_detail_balik", val)
            }
        },
        journal_balik_new: {
            get() {
                return this.$store.state.journal.journal_balik_new
            },
            set(val) {
                this.$store.commit("journal/update_journal_balik_new", val)
            }
        },
        summary_balik_new: {
            get() {
                return this.$store.state.journal.summary_balik_new
            },
            set(val) {
                this.$store.commit("journal/update_summary_balik_new", val)
            }
        }
    },
    methods: {
        formatCurrency(amount) {
            const price = parseFloat(amount)
            const IDR = new Intl.NumberFormat("id-ID", {
                style: "currency",
                currency: "IDR"
            })
            return `${IDR.format(price)}`
        },
        resetState() {
            this.selected_journal_closed = {
                totals: {
                    total_debit: 0,
                    total_kredit: 0,
                    balance: 0,
                }
            }
            this.journal_detail_balik = {
                transaction: [],
                summary: {
                    total_debit: 0,
                    total_kredit: 0,
                    balance: 0,
                }
            }

            this.summary_balik_new = {
                total_debit: 0,
                total_kredit: 0,
                balance: 0,
            }
        },
        batalBalikJurnal() {
            this.resetState()
            this.dialog_journal_balik = false
            this.$store.commit("journal/update_dialog_journal", true)
        },
        addTransactions() {
            this.$store.commit("journal/update_dialog_add_jurnaltrx", true)
        },
        removeTransaction(item) {
            let current_trx = JSON.parse(JSON.stringify(this.journal_balik_new))
            const new_trx = current_trx.filter(trx => trx.jurnalTxID != item.jurnalTxID)

            const summary = new_trx.reduce((acc, item) => {
                const debit = Number(item.jurnalTxDebit)
                const credit = Number(item.jurnalTxCredit)

                acc.total_debit += debit
                acc.total_kredit += credit
                return acc
            }, {total_debit: 0, total_kredit: 0 })
            summary.balance = summary.total_debit - summary.total_kredit

            this.journal_balik_new = new_trx
            this.summary_balik_new = summary
        },
        simpanBalikJurnal() {
            this.$store.dispatch("journal/saveKoreksiJurnal")
        }
    },
    watch: {
        
    },
}
</script>