<template>
    <div class="row m-l-0 m-b-10 m-r-0 parentContainer" :class="[{'b-a': item.status === 4}, {'b-danger': item.status === 4}]" >
        <div class="col">
            <div class="row" v-if="!item.transaction_bill">
                <div class="col">
                    <div class="row">
                        <div class="col p-t-10 p-b-10 p-r-0 pointer" @click="clickExpand()" :class="[{'bg-master-lighter': item.status === 1 && item.type !== 6}, {'bg-white': item.status !== 1 && item.status !== 4}, {'bg-warning-lighter': item.type === 6}]">
                            <div class="row m-b-5">
                                <div class="col-auto">
                                    <div class="font-heading fs-8 muted all-caps">Status</div>
                                    <div class="font-heading fs-10 bold" v-if="item.type === 1" :class="[{'text-danger': item.status === 1 || item.status === 4}, {'text-success': item.status !== 1 && item.status !== 4}]">
                                        {{ item.status === 1 ? 'Pending Verification' : item.status === 4 ? 'Rejected' : 'Processing Payment'}}
                                    </div>
                                    <div class="font-heading fs-10 bold" v-if="item.type === 6" :class="[{'text-danger': item.status === 1 || item.status === 4}, {'text-success': item.status !== 1 && item.status !== 4}]">
                                        {{ item.status === 1 ? 'Pending Verification' : item.status === 4 ? 'Rejected' : 'Processing Payment'}}
                                    </div>
                                </div>
                                <div class="col-auto p-l-0">
                                    <div class="font-heading fs-8 muted all-caps">Payment Amount</div>
                                    <div class="font-heading fs-10 bold">
                                        {{item.currency.short_code}} {{(Math.round((item.amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
                                    </div>
                                </div>
                                <div class="col-auto p-l-0" v-if="totalRefunds !== 0">
                                    <div class="font-heading fs-8 muted all-caps">Refunded Amount</div>
                                    <div class="font-heading fs-10 bold text-danger">
                                        {{item.original_currency.short_code}} {{(Math.round((totalRefunds + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col">
                                    <div class="font-heading fs-8 all-caps" :class="[{'text-danger': item.status === 4}, {'text-primary': item.status !== 4}]">{{ item.status === 2 ? 'Received' : item.status === 4 ? 'Rejected' : 'Submitted'}} On: {{item.updated_at}}</div>
                                </div>
                            </div>
                        </div>
                        <div class="col-auto" v-if="item.status !== 3" :class="[{'bg-master-light': item.status === 1  && item.type !== 6}, {'bg-master-lighter': item.status === 2}, {'bg-warning-light': item.type === 6}]">
                            <div class="row align-items-center h-100" v-if="item.status !== 1 || item.payment_method !== 5">
                                <div class="col">
                                    <i class="fa" :class="[{'fa-cloud-download': item.status === 1 || item.status === 2}, {'fa-ban': item.status === 4}, {'muted': item.status === 1 || item.status === 2}, {'text-danger':  item.status === 4}]"></i>
                                </div>
                            </div>
                            <div class="row align-items-center h-100" v-if="item.payment_method === 5 && item.status === 1">
                                <div class="col">
                                    <a :href="route('billplz.bill', item.payment_reference)"><i class="fa fa-repeat text-success"></i></a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" v-if="item.transaction_bill">
                <div class="col">
                    <div class="row">
                        <div class="col p-t-10 p-b-10 p-r-0 pointer" @click="clickExpand()" :class="[{'bg-master-lighter': item.transaction_bill.status === 1}, {'bg-white': item.transaction_bill.status !== 1 && item.transaction_bill.status !== 4}]">
                            <div class="row m-b-5">
                                <div class="col-auto">
                                    <div class="font-heading fs-8 muted all-caps">Status</div>
                                    <div class="font-heading fs-10 bold" :class="[{'text-danger': item.transaction_bill.status === 1 || item.transaction_bill.status === 4}, {'text-success': item.transaction_bill.status !== 1 && item.transaction_bill.status !== 4}]">
                                        {{ item.transaction_bill.status === 1 ? 'Processing Payment' : 'Transferred'}}
                                    </div>
                                </div>
                                <div class="col-auto p-l-0">
                                    <div class="font-heading fs-8 muted all-caps">Payment Amount</div>
                                    <div class="font-heading fs-10 bold">
                                        {{item.original_currency.short_code}} {{(Math.round((item.original_amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}
                                    </div>
                                </div>
                                <div class="row align-items-end m-b-10 bold text-danger" v-if="totalRefunds !== 0">
                                    <div class="col">
                                        <div class="font-heading all-caps fs-10">Refunded Amount</div>
                                    </div>
                                    <div class="col-auto text-right">
                                        <div class="font-heading fs-12">{{item.original_currency.short_code}} {{(Math.round((totalRefunds + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}</div>
                                    </div>
                                </div>
                                <div class="col-auto" v-if="$store.getters.isAdmin">
                                    <div class="font-heading fs-8 muted all-caps">Supplier</div>
                                    <div class="font-heading fs-10 bold">
                                        {{item.transaction_bill.issuer_name}}
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col">
                                    <div class="font-heading fs-8 all-caps" >{{ item.transaction_bill.status === 1 ? 'Paid On: ' + item.updated_at : 'Transferred On:' + item.transaction_bill.updated_at }}</div>
                                </div>
                            </div>
                        </div>
                        <div class="col-auto" v-if="item.transaction_bill.status !== 3 && item.transaction_bill.status !== 2" :class="[{'bg-master-light': item.transaction_bill.status === 1}, {'bg-master-lighter': item.transaction_bill.status === 2}]">
                            <div class="row align-items-center h-100">
                                <div class="col">
                                    <i class="fa" :class="[{'fa-cloud-download': item.transaction_bill.status === 1 || item.transaction_bill.status === 2}, {'fa-ban': item.transaction_bill.status === 4}, {'muted': item.transaction_bill.status === 1 || item.transaction_bill.status === 2}, {'text-danger':  item.transaction_bill.status === 4}]"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-auto bg-success" v-if="item.transaction_bill.status === 2 || item.transaction_bill.status === 3">
                            <document-file-viewer-component class="h-100" :file="item.transaction_bill.documents.files[0]">
                                <template slot="button">
                                    <div class="row align-items-center h-100">
                                        <div class="col">
                                            <i class="fa fa-cloud-download text-white"></i>
                                        </div>
                                    </div>
                                </template>
                            </document-file-viewer-component>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" v-if="expandPaymentDetails">
                <div class="col bg-white padding-15">
                    <div class="row align-items-end m-b-10 text-success bold">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Recipient Gets</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-10">{{item.original_currency.short_code}} {{(item.original_amount).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}</div>
                        </div>
                    </div>
                    <div class="row align-items-end m-b-10 bold text-danger" v-if="totalRequestedRefund != 0">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Requested Refund Amount</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-10">{{item.original_currency.short_code}} {{(Math.round((totalRequestedRefund + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}</div>
                        </div>
                    </div>
                    <div class="row align-items-end m-b-10 bold text-danger" v-if="totalRefunds != 0">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Refunded Amount</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-12">{{item.original_currency.short_code}} {{(Math.round((totalRefunds + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}</div>
                        </div>
                    </div>
                    <div class="row align-items-end bold m-b-10 text-primary">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Rate</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-10 ">{{(Math.round((item.currency_rate + Number.EPSILON) * 100000) / 100000).toFixed(5) }}</div>
                        </div>
                    </div>
                    <div class="row align-items-end m-b-10 hint-text">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Transfer Charges</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-10">MYR {{(Math.round((item.service_charge + Number.EPSILON) * 100) / 100).toFixed(2)}}</div>
                        </div>
                    </div>
                    <div class="row align-items-end m-b-5 hint-text">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Tax</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-10">MYR {{(Math.round((item.tax  + Number.EPSILON) * 100) / 100).toFixed(2)}}</div>
                        </div>
                    </div>
                    <div class="row align-items-end m-b-10 bold text-success">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Your Payment</div>
                        </div>
                        <div class="col-auto text-right">
                            <div class="font-heading fs-12">MYR {{(Math.round((item.amount + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}}</div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <div class="font-heading all-caps fs-10 m-b-5">Your Payment Proof</div>
                            <div class="row no-margin" v-if="item.payment_method !== 5">
                                <div v-if="item.documents != null">
                                    <div v-for="file in item.documents.files" v-bind:key="file.id"  class="col-auto no-padding m-r-5">
                                        <document-file-viewer-component :file="file">
                                            <template slot="button">
                                                <div class="icon-thumbnail fs-11 text-white icon-25 bg-primary btn-rounded float-left m-r-5">
                                                    <i class="fa fa-file-image-o fs-10"></i>
                                                </div>
                                            </template>
                                        </document-file-viewer-component>
                                    </div>
                                </div>
                            </div>
                            <div class="row no-margin" v-if="item.payment_method === 5 && (item.status === 2 || item.status === 3)">
                                <a :href="route('billplz.bill', item.payment_reference)" target="_blank">
                                    <div class="icon-thumbnail fs-11 text-white icon-25 bg-primary btn-rounded float-left m-r-5">
                                        <i class="fa fa-file-image-o fs-10"></i>
                                    </div>
                                </a>
                            </div>
                        </div>
                        <div class="col text-right">
                            <div class="font-heading all-caps fs-10 m-b-5">Our Payment Proof</div>
                            <div class="row" v-if="item.transaction_bill">
                                <div class="col">
                                    <div class="row" v-if="item.transaction_bill.status !== 2 && item.transaction_bill.status !== 3">
                                        <div class="col">
                                            <p class="fs-12 muted">Payment proof will be uploaded {{ (parseFloat(item.interval.duration) + 1) <= 0 ? 'today' : (parseFloat(item.interval.duration) + 1) === 2 ? 'tomorrow' : 'in '+(parseFloat(item.interval.duration) + 1)+' days'}} at 4:00 PM</p>
                                        </div>
                                    </div>
                                    <div class="row no-margin justify-content-end" v-if="(item.transaction_bill.status === 2 || item.transaction_bill.status === 3)">
                                        <div v-for="file in item.transaction_bill.documents.files" v-bind:key="file.id"  class="col-auto no-padding m-l-5">
                                            <document-file-viewer-component :file="file">
                                                <template slot="button">
                                                    <div class="icon-thumbnail fs-11 text-white icon-25 bg-primary btn-rounded float-left m-r-0">
                                                        <i class="fa fa-file-image-o fs-10"></i>
                                                    </div>
                                                </template>
                                            </document-file-viewer-component>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row" v-if="!item.transaction_bill">
                                <div class="col">
                                    <p class="fs-12 muted">Payment proof will be uploaded {{ (parseFloat(item.interval.duration) + 1) <= 0 ? 'today' : (parseFloat(item.interval.duration) + 1) === 2 ? 'tomorrow' : 'in '+(parseFloat(item.interval.duration) + 1)+' days'}} at 4:00 PM</p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-t-10" v-show="[2, 3].includes(item.status) && totalRequestedRefund < data.booking.amount">
                        <div class="col hide">
                            <button class="btn btn-xs all-caps b-rad-none bg-master-lighter btn-block no-border requestModal" data-type="transferSummary">Request Refund</button>
                            <modal-component class="animate__animated animate__fast animate__fadeIn" styleType="fill-in" type="transferSummary" size="large">
                                <refund-confirmation-component :data="data" :section="section" :totalRefunds="totalRequestedRefund + totalRefunds"></refund-confirmation-component>
                            </modal-component>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import componentHandler from '../../../general/mixins/componentHandler';
    export default {
        data(){
            return {
                expandPaymentDetails: false,
                amount: (Math.round(1000 * 100) / 100).toFixed(2),
                parameters: {
                    amount: (Math.round(1000 * 100) / 100).toFixed(2),
                    bank_id: 1
                },
                section: 'bookingDetailSection',
            }
        },
        computed: {
            totalRequestedRefund() {
                var TotalRequestedRefund = 0;
                this.data.transaction_refunds.forEach(function(refunds) {
                    TotalRequestedRefund += refunds.status === 1 ? refunds.original_amount : 0;
                });
                return TotalRequestedRefund;
            },
            totalRefunds() {
                var TotalRequestedRefund = 0;
                this.data.transaction_refunds.forEach(function(refunds) {
                    TotalRequestedRefund += refunds.status === 2 ? refunds.original_amount : 0;
                });
                return TotalRequestedRefund;
            }
        },
        methods: {
            clickExpand(){
                this.expandPaymentDetails = !this.expandPaymentDetails;
            },
        },
        mixins: [componentHandler]
    }
</script>
