<template>
    <div class="row m-b-10 parentContainer">
        <div class="col">
            <loading-component style="height: 200px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row p-b-5 b-b b-grey" v-show="!isLoading">
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Date</div>
                            <div class="font-heading fs-10">
                                {{item.created_at}}
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Supplier</div>
                            <div class="font-heading fs-10">
                                {{item.issuer_name}}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Currency Rate</div>
                            <div class="font-heading fs-10">
                                {{item.currency_rate}}
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">Currency Amount</div>
                            <div class="font-heading fs-10">
                                {{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="col text-right">
                            <div class="font-heading fs-10 muted all-caps">Amount</div>
                            <div class="font-heading fs-14 text-success 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>
                    <div class="row">
                        <div class="col-auto">
                            <div class="font-heading fs-10 muted all-caps">reference</div>
                            <document-file-viewer-component v-if="item.documents.currency_order" :file="item.documents.currency_order.files[0]">
                                <template slot="button">
                                    <button class="btn btn-xs btn-default bg-master-lightest b-rad-none no-border">
                                        <i class="fa fa-eye"></i>
                                    </button>
                                </template>
                            </document-file-viewer-component>
                            <!--<span class="font-heading fs-10" v-for="transaction in item.transactions">-->
                                <!--<a :href="route('booking.details', transaction.booking.marking)">{{transaction.booking.marking}}&nbsp;</a>-->
                            <!--</span>-->
                        </div>
                        <div class="col">
                            <div class="row">
                                <div class="col">
                                    <div class="font-heading fs-10 muted all-caps">PO Completion</div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-auto p-r-0" v-if="item.documents.purchase_order" >
                                    <document-file-viewer-component :file="item.documents.purchase_order.files[0]">
                                        <template slot="button">
                                            <button class="btn btn-xs btn-default bg-master-lightest b-rad-none no-border">
                                                <i class="fa fa-file-pdf-o"></i>
                                            </button>
                                        </template>
                                    </document-file-viewer-component>
                                </div>
                                <div class="col">
                                    <p class="font-heading fs-12 bold text-success pointer" @click="expanded = !expanded"><span :class="[{'text-danger': item.complete_transactions.length !== item.transactions.length}]">{{item.complete_transactions.length}}</span>/{{item.transactions.length}}</p>
                                </div>
                            </div>
                            <!--<span class="font-heading fs-10" v-for="transaction in item.transactions">-->
                            <!--<a :href="route('booking.details', transaction.booking.marking)">{{transaction.booking.marking}}&nbsp;</a>-->
                            <!--</span>-->
                        </div>
                        <div class="col-auto">
                            <div class="row parentContainer">
                                <div class="col p-l-0">
                                    <a :href="route('group.text', item.id)" target="_blank">
                                        <button class="btn btn-xs b-rad-none">
                                            <i class="fa fa-align-left"></i>
                                        </button>
                                    </a>
                                    <button class="btn btn-xs btn-complete b-rad-none">
                                        <i class="fa fa-refresh" @click="updateDo()"></i>
                                    </button>
                                    <button class="btn btn-xs btn-warning b-rad-none requestModal" data-type="editTransactionGroup">
                                        <i class="fa fa-pencil"></i>
                                    </button>
                                    <modal-component small type="editTransactionGroup">
                                        <edit-transaction-group-form-component :section="section" :currency_rate="item.currency_rate" :supplier_id ="data.issuer_id" :id="item.id"></edit-transaction-group-form-component>
                                    </modal-component>
                                    <button class="btn btn-xs btn-danger b-rad-none requestModal" data-type="deleteTransactionGroup">
                                        <i class="fa fa-times"></i>
                                    </button>
                                    <modal-component small type="deleteTransactionGroup">
                                        <div class="row">
                                            <div class="col text-center">
                                                <div class="row">
                                                    <div class="col text-center">
                                                        <div class="row m-b-20">
                                                            <div class="col">
                                                                <h5 class="all-caps">Delete Transaction Group</h5>
                                                                <div class="fs-11">Are you sure that you want to delete this transaction group?</div>
                                                            </div>
                                                        </div>
                                                        <div class="row">
                                                            <div class="col p-r-5">
                                                                <div data-dismiss="modal" class="btn btn-sm btn-default bg-master-lighter btn-block b-rad-none">Cancel</div>
                                                            </div>
                                                            <div class="col p-l-5">
                                                                <div data-dismiss="modal" class="btn btn-sm btn-danger btn-block b-rad-none" @click="deleteGroupTransaction()">Confirm</div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </modal-component>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row" v-if="expanded">
                        <div class="col">
                            <div class="row" v-for="transaction in item.transactions">
                                <div class="col">
                                    <a :href="route('booking.details', transaction)" target="_blank">
                                        <span :class="[{'text-success': item.complete_transactions.includes(transaction)}, {'text-danger': !item.complete_transactions.includes(transaction)}]">{{ transaction }}</span>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import componentHandler from '../../../general/mixins/componentHandler';
    import staticFormHandler from '../../../general/mixins/staticFormHandler'
    export default {
        props: {
            section:{
                type: String,
                required: true
            }
        },
        data(){
            return {
                expanded: false
            }
        },
        methods: {
            deleteGroupTransaction() {
                this.isLoading = true;
                this.submit(this.route('api.transaction.group.delete', this.item.id), 'delete', this.section, true, true);
            },
            updateDo() {
                this.isLoading = true;
                this.submit(this.route('api.transaction.group.bulk.po'), 'post', this.section, true, true);
            }

        },
        mixins: [componentHandler, staticFormHandler]
    }
</script>
