<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 200px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row m-l-5 m-t-15" v-if="wallet">
                <div class="col-8 p-r-30">
                    <div class="row p-b-5 b-b b-grey m-b-10 m-l-0 m-r-0 padding-15">
                        <div class="col no-padding">
                            <h6>Transaction History</h6>
                        </div>
                    </div>
                    <div class="row m-t-10 m-b-10">
                        <div class="col">
                            <div class="d-flex align-items-center h-100">
                                <span class="btn btn-md fs-11 bg-primary text-white fs-12 m-r-5" :class="[{'bg-primary-darker': showingPreciseAmount}]" @click="showingPreciseAmount=!showingPreciseAmount">{{ showingPreciseAmount ? 'Showing Precise Wallet Transaction' : 'Show Precise Wallet Transaction'}}</span>
                                <a v-if="wallet" :href="route('wallet.details-export', wallet.id, showingPreciseAmount)" target="_blank" class="btn btn-md btn-primary fs-11"><i class="fa fa-download m-r-5"></i>{{ showingPreciseAmount ? 'Download Precise Transaction' : 'Download Transaction'}}</a>
                            </div>
                        </div>
                        <div class="col-3">
                            <validation-wrapper-component selectable :validator="$v.showingTransactionCount">
                                <label>Showing Rows</label>
                                <select-component :options="[5, 10, 20, 30, 50]" v-model="showingTransactionCount"></select-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-5" @keyup.enter="submitSearch">
                        <div class="col p-r-0">
                            <validation-wrapper-component :validator="$v.reference_no">
                                <label class="all-caps">Order Reference</label>
                                <input type="text" class="form-control" v-model="reference_no">
                            </validation-wrapper-component>
                        </div>
                        <div class="col p-r-0">
                            <validation-wrapper-component :validator="$v.startDate">
                                <label class="all-caps">Start Date</label>
                                <date-picker-component v-model.lazy="startDate"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col p-r-0">
                            <validation-wrapper-component :validator="$v.endDate">
                                <label class="all-caps">End Date</label>
                                <date-picker-component v-model.lazy="endDate"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col col-md-auto d-flex justify-content-center align-items-center">
                            <button type="button" class="btn btn-lg btn-primary fs-11 w-100" @click="submitSearch()">Search</button>
                        </div>
                    </div>
                    <div class="row" v-if="wallet.transactions">
                        <div class="col">
                            <div class="row padding-10">
                                <div class="col-3 fs-10">Date</div>
                                <div class="col fs-10">Description</div>
                                <div class="col-2 fs-10 text-center">Incoming</div>
                                <div class="col-2 fs-10 text-center">Outgoing</div>
                                <div class="col-2 fs-10 text-right">Balance</div>
                            </div>

                            <list-component :key="key" section="walletTransactionSection" :endpoint="route('api.transaction.wallet.list')" :options="options">
                                <template slot="list" slot-scope="{data}">
                                    <customer-wallet-transaction-component :data="data" :showingPreciseAmount="showingPreciseAmount" ></customer-wallet-transaction-component>
                                </template>
                            </list-component>
                        </div>
                    </div>

                    <!-- <div class="row align-items-center justify-content-center p-t-50 p-b-50" v-if="!wallet.transactions || !wallet.transactions.length">
                        <div class="col-12">
                            <div class="row align-items-center justify-content-center hint-text">
                                <div class="col-4 hint-text"><img src="/images/not-found-illustration.png" class="w-100 hint-text"></div>
                            </div>
                            <div class="row text-center">
                                <div class="col">
                                    <div class="row m-t-20">
                                        <div class="col">
                                            <p class="all-caps no-margin fs-11" style="letter-spacing: 2px;">Nothing To Show Here</p>
                                        </div>
                                    </div>
                                    <div class="row m-t-5 align-items-center justify-content-center">
                                        <div class="col">
                                            <small class="fs-9 muted all-caps font-lato" style="letter-spacing: 2px">There is no results found, Try adjusting your filters to find what you are looking for.</small>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div> -->
                </div>
                <div class="col-4">
                    <wallet-component :data="wallet" :company_module_id="id" section="CompanyWalletTransactionSection" :creditable=true></wallet-component>
                    <div class="row m-t-20">
                        <div class="col">
                            <div class="row m-b-10">
                                <div class="col">
                                    <div class="font-head fs-10 all-caps">Top Up Records</div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col">
                                    <wallet-top-up-history-component v-for="item in wallet.top_up_records" v-bind:key="item.id" :data="item"></wallet-top-up-history-component>
                                </div>
                            </div>
                            <div class="row align-items-center justify-content-center p-t-50 p-b-50" v-if="!wallet.top_up_records.length">
                                <div class="col-10">
                                    <div class="row align-items-center justify-content-center hint-text">
                                        <div class="col-4 hint-text"><img src="/images/not-found-illustration.png" class="w-100 hint-text"></div>
                                    </div>
                                    <div class="row text-center">
                                        <div class="col">
                                            <div class="row m-t-20">
                                                <div class="col">
                                                    <p class="all-caps no-margin fs-11" style="letter-spacing: 2px;">Nothing To Show Here</p>
                                                </div>
                                            </div>
                                            <div class="row m-t-5 align-items-center justify-content-center">
                                                <div class="col">
                                                    <small class="fs-9 muted all-caps font-lato" style="letter-spacing: 2px">There is no results found, Try adjusting your filters to find what you are looking for.</small>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        id: {
            type: Number,
            required: true
        },
    },
    data(){
        return {
            key: 1,
            section: 'customerTransactionSection',
            isLoading: true,
            wallet: null,
            showingPreciseAmount: false,
            showingTransactionCount: 10,
            attention: false,
            reference_no: null,
            startDate: null,
            endDate: null,
            options: {
                status_in: [2, 3], 
                owner_type: 'App\\Models\\Wallet', 
                owner_id: 0, 
                per_page: this.showingTransactionCount
            }
        }
    },
    computed: {
        pendingQueue () {
            return this.$store.getters.isInCompleteQueue(this.section);
        }
    },
    watch: {
        pendingQueue(inComplete){
            if(inComplete){
                this.fetchWallet();
            }
        },
        showingTransactionCount() {
            this.key ++;
        }
    },
    validations: {
        showingTransactionCount: {  },
        reference_no: {  },
        startDate: {  },
        endDate: {  },
    },
    created(){
        this.$store.dispatch('updateListQueue', {'name': this.section});
    },
    methods: {
        fetchWallet(){
            this.isLoading = true;
            var filters = {with_transactions: true};
            this.submit(route('api.wallet.company_module.show', this.id) + '?filters=' + JSON.stringify(filters), 'get', this.section, false, false);
        },
        submitSearch() {
            console.log("searcvhing");
            delete this.options.with_order_reference_like;
            delete this.options.created_after_or_equal;
            delete this.options.created_before_or_equal;

            if (this.reference_no) {
                this.options.with_order_reference_like = this.reference_no
            }
            if (this.startDate) {
                this.options.created_after_or_equal = this.startDate
            }
            if (this.endDate) {
                this.options.created_before_or_equal = this.endDate
            }
            
            this.key ++;
        },
        successHandler(response){
            this.isLoading = false;
            this.wallet = response.payload.data;
            this.options.owner_id = this.wallet.id
            this.key ++;
        }
    }
}
</script>
