<template>
    <v-dialog v-model="dialog_edit_jurnal" persistetnt width="70vw">
        <v-card>
            <v-card-title class="headline grey lighten-2" primary-title>
                EDIT JOURNAL PENDAPATAN CASH
            </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.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.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.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.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.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.JurnalTypeName }}
                        </div>
                    </v-flex>
                </v-layout>

                <v-layout row wrap mt-3>
                    <v-flex xs12>
                        <v-data-table
                            :headers="headerdetails" :items="journal_edit"
                            hide-actions class="elevation-1"
                        >
                            <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">
                                        <v-autocomplete
                                            label="Account" hide-details outline
                                            v-model="props.item.coaAccountNo"
                                            :items="list_coa" item-text="coaDescription"
                                            item-value="coaAccountNo"
                                        ></v-autocomplete>
                                    </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(xsummary.debit) }}
                                    </td>
                                    <td class="text-xs-right pa-2 font-weight-bold">
                                        {{ formatCurrency(xsummary.credit) }}
                                    </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(xsummary.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()">Batal</v-btn>
                <v-btn color="primary" dark @click="simpanEditDialog()">Simpan</v-btn>
            </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_edit_jurnal: {
            get() {
                return this.$store.state.journal.dialog_edit_journal;
            },
            set(val) {1
                this.$store.commit("journal/update_dialog_edit_journal", val);
            }
        },
        selected_journal: {
            get() {
                return this.$store.state.journal.selected_journal;
            },
            set(val) {
                this.$store.commit("journal/update_selected_journal", val);
            },
        },
        journal_edit: {
            get() {
                return this.$store.state.journal.journal_edit
            },
            set(val) {
                this.$store.commit("journal/update_journal_edit", val);
            }
        },
        selected_periode() {
            return this.$store.state.journal.selected_periode
        },
        xsummary() {
          return this.$store.state.journal.summary
        },
        list_coa() {
            return this.$store.state.journal.list_coa
        }
    },
    methods: {
        formatCurrency(val) {
            const price = parseFloat(val);
            const IDR = new Intl.NumberFormat("id-ID", {
                style: "currency",
                currency: "IDR"
            });
            return `${IDR.format(price)}`;
        },
        simpanEditDialog() {
            const originaldetail = this.selected_journal.detailtx;
            const editnewdetails = this.journal_edit;
            const listcoa = this.list_coa;

            const originalMap = new Map();
            for (const item of originaldetail) {
                originalMap.set(item.jurnalTxID, item.coaAccountNo);
            }

            const coaMap = new Map();
            for (const coaItem of listcoa) {
                coaMap.set(coaItem.coaAccountNo, coaItem);
            }

            const editeddetail = editnewdetails.flatMap(detail => {
                const originalAcc = originalMap.get(detail.jurnalTxID);
                if (originalAcc !== detail.coaAccountNo) {
                    const accMatch = coaMap.get(detail.coaAccountNo);
                    if (accMatch) {
                        return [{
                            jurnalID: detail.jurnalTxJurnalID,
                            jurnalTxID: detail.jurnalTxID,
                            coaID: accMatch.coaID,
                            coaDescription: accMatch.coaDescription,
                            coaAccountNo: accMatch.coaAccountNo
                        }]
                    } else {
                        return [];
                    }
                } else {
                    return [];
                }
            })

            this.$store.dispatch("journal/saveEditJurnal", editeddetail);
        },
        closeEditDialog() {
            this.selected_journal = {};
            this.journal_edit = [];
            this.dialog_edit_jurnal = false;
        }
    },
    watch: {

    },
}
</script>