<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.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-10 hint-text">
                        <div class="col">
                            <div class="font-heading all-caps fs-10">Voucher</div>
                        </div>
                        <div class="col-auto text-right" v-if="item.redemption && item.redemption.value">
                            <div class="font-heading fs-10">- MYR {{(Math.round((item.redemption.value + Number.EPSILON) * 100) / 100).toFixed(2)}}</div>
                        </div>
                        <div class="col-auto text-right" v-else>
                            <div class="font-heading fs-10">- MYR 0.00</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 class="col-auto no-padding m-l-5 requestModal pointer" v-if="$store.getters.isSuperAdmin" data-type="deletePaymentProofModal">
                                            <div class="icon-thumbnail fs-11 text-white icon-25 bg-danger btn-rounded float-left m-r-0">
                                                <i class="fa fa-trash fs-10"></i>
                                            </div>
                                            <modal-component type="deletePaymentProofModal">
                                                <reject-payment-transaction-form-component :data="item.transaction_bill" :section="section" class="text-center"></reject-payment-transaction-form-component>
                                            </modal-component>
                                        </div>
                                        <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" v-if="$store.getters.isAdmin && item.transaction_bill">
                        <div class="col-6 m-auto">
                            <div class="row p-t-20 p-b-20 b-a b-dashed b-success requestModal pointer" v-if="item.transaction_bill.status === 1" data-type="paymentProofModal">
                                <div class="col">
                                    <div class="row align-item-center justify-content-center h-100">
                                        <div class="col-auto" >
                                            <div class="row align-items-center h-100">
                                                <div class="col">
                                                    <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
                                                        width="40" height="40"
                                                        viewBox="0 0 172 172"
                                                        style=" fill:#000000;"><defs><linearGradient x1="86" y1="45.01563" x2="86" y2="152.92681" gradientUnits="userSpaceOnUse" id="color-1_48314_gr1"><stop offset="0" stop-color="#1ac86f"></stop><stop offset="1" stop-color="#1ac86f"></stop></linearGradient><linearGradient x1="86" y1="45.01563" x2="86" y2="152.92681" gradientUnits="userSpaceOnUse" id="color-2_48314_gr2"><stop offset="0" stop-color="#1ac86f"></stop><stop offset="1" stop-color="#1ac86f"></stop></linearGradient><linearGradient x1="86" y1="16.79688" x2="86" y2="92.05225" gradientUnits="userSpaceOnUse" id="color-3_48314_gr3"><stop offset="0" stop-color="#67eba8"></stop><stop offset="1" stop-color="#67eba8"></stop></linearGradient><linearGradient x1="86" y1="45.01563" x2="86" y2="152.92681" gradientUnits="userSpaceOnUse" id="color-4_48314_gr4"><stop offset="0" stop-color="#1ac86f"></stop><stop offset="1" stop-color="#1ac86f"></stop></linearGradient></defs><g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0" font-family="none" font-weight="none" font-size="none" text-anchor="none" style="mix-blend-mode: normal"><path d="M0,172v-172h172v172z" fill="none"></path><g><path d="M147.8125,48.375h-32.25v5.375h32.25c1.50769,0 2.6875,1.12875 2.6875,2.56925v75.25c0,1.51844 -1.23087,2.80575 -2.6875,2.80575h-123.625c-1.45662,0 -2.6875,-1.28731 -2.6875,-2.80575v-75.25c0,-1.4405 1.17981,-2.56925 2.6875,-2.56925h32.25v-5.375h-32.25c-4.52038,0 -8.0625,3.49106 -8.0625,7.94425v75.25c0,3.55019 2.25481,6.54944 5.375,7.67819v3.07181c0,4.51231 3.61737,8.18075 8.0625,8.18075h112.875c4.44512,0 8.0625,-3.66844 8.0625,-8.18075v-3.07181c3.12019,-1.12875 5.375,-4.128 5.375,-7.67819v-75.25c0,-4.45319 -3.54212,-7.94425 -8.0625,-7.94425zM142.4375,145.125h-112.875c-1.45662,0 -2.6875,-1.28731 -2.6875,-2.80575v-2.56925h118.25v2.56925c0,1.51844 -1.23087,2.80575 -2.6875,2.80575z" fill="url(#color-1_48314_gr1)"></path><path d="M126.3125,129h-83.3125v-2.6875c0,-7.51425 -6.02806,-13.4375 -13.4375,-13.4375h-2.6875v-37.625h2.6875c6.3425,0 13.4375,-5.79156 13.4375,-13.46706v-2.70363l13.4375,0.04569v5.375l-8.2775,-0.01613c-1.26044,7.95231 -7.94425,14.61731 -15.91,15.86969v27.219c8.25869,1.18788 14.80006,7.76956 15.94762,16.05244h75.6155c1.17444,-8.30438 7.70238,-14.85112 15.93688,-16.03631v-27.262c-7.96844,-1.247 -14.65494,-7.89587 -15.91269,-15.82669h-8.27481v-5.375h13.4375v2.6875c0,6.30219 5.74587,13.4375 13.4375,13.4375h2.6875v37.50675l-2.6875,0.01613c-7.40944,0 -13.4375,6.03344 -13.4375,13.45094v2.6875z" fill="url(#color-2_48314_gr2)"></path><path d="M104.8125,53.75h-8.0625c-1.4835,0 -2.6875,1.14487 -2.6875,2.63106v29.61356c0,1.48619 -1.204,2.69288 -2.6875,2.69288h-10.75c-1.4835,0 -2.6875,-1.20669 -2.6875,-2.69288v-29.61356c0,-1.48619 -1.204,-2.63106 -2.6875,-2.63106h-8.0625c-2.2145,0 -3.47763,-2.881 -2.15,-4.87781l16.65175,-25.04481c2.05056,-3.08256 6.57094,-3.08794 8.6215,-0.00806l16.65175,25.05556c1.32762,1.99681 0.0645,4.87512 -2.15,4.87512z" fill="url(#color-3_48314_gr3)"></path><path d="M86,112.875c-10.37375,0 -18.8125,-8.0625 -18.8125,-18.8125h5.375c0,8.0625 6.02806,13.4375 13.4375,13.4375c7.40944,0 13.4375,-5.375 13.4375,-13.4375h5.375c0,10.75 -8.43875,18.8125 -18.8125,18.8125z" fill="url(#color-4_48314_gr4)"></path></g></g></svg>
                                                </div>
                                            </div>
                                        </div>
                                        <modal-component type="paymentProofModal">
                                            <payment-proof-form-component :section="section" :id="item.transaction_bill.id" :data="item.transaction_bill"></payment-proof-form-component>
                                        </modal-component>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row m-t-10" v-show="[2, 3].includes(item.status) && totalRequestedConvertRefund < data.booking.amount">
                        <div class="col">
                            <button class="btn btn-xs all-caps b-rad-none bg-master-lighter btn-block no-border requestModal hide" 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() {
                let vm = this;
                var TotalRequestedRefund = 0;
                this.data.transaction_refunds.forEach(function(refunds) {
                    TotalRequestedRefund += refunds.status === 1 ? refunds.original_amount : 0;
                });

                return TotalRequestedRefund;
            },
            totalRequestedConvertRefund() {
                let vm = this;
                var TotalRequestedRefund = 0;
                this.data.transaction_refunds.forEach(function(refunds) {
                    TotalRequestedRefund += refunds.status === 1 ? refunds.original_amount : 0;
                });
                if (vm.data.booking.fixed_currency.id != 1 && this.data.transaction_refunds[0]) {
                    TotalRequestedRefund = (TotalRequestedRefund * this.data.transaction_refunds[0].currency_rate);
                }
                return ((Math.round((TotalRequestedRefund + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
            },
            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>
