<template>
    <div>
        <div class="card shadow-sm border-0">
            <div class="card-header d-flex justify-content-between flex-wrap p-4 bg-white border-bottom-0">
                <div class="d-flex flex-column">
                    <div>
                        <h1 class="fw-bold h2">{{ trans('em.myearning') }}</h1>
                    </div>

                    <!-- Filters -->
                    <div class="d-flex">
                        <div class="me-2">
                            <select class="form-select" name="event_id" v-model="event_id" >
                                <option  value=0>{{ trans('em.all_events') }} </option>
                                <option v-for="(event, index) in events" :key ="index" :value="event.id">{{event.title}} </option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="stats-grid mx-2">
                    
                <div class="stat-card">
                    <div class="stat-icon fa-calendar-alt">
                        <i class="fas fa-cart-arrow-down"></i>
                    </div>

                    <div class="stat-content" v-if="total_earnings.length <= 0">
                        <div class="stat-number">0</div>
                        <div class="stat-label">{{ trans('em.total_bookings') }}</div>
                    </div>

                    <div class="stat-content" v-else>
                        <div class="border-bottom my-1  border-gray"  v-for="(total_earning, index) in total_earnings" :key="index">
                            <div class="stat-number">{{ total_earning.customer_paid_total ?? 0 }} </div>
                            <div class="stat-currency">{{ total_earning.currency }}</div>
                        </div>
                        <div class="stat-label">{{ trans('em.total_bookings') }}</div>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-icon fa-tag">
                        <i class="fas fa-tag"></i>
                    </div>
                    <div class="stat-content" v-if="total_earnings.length <= 0">
                        <div class="stat-number">0</div>
                        <div class="stat-label">{{ trans('em.total') }}  {{ trans('em.promocode') }}</div>
                    </div>
                    <div class="stat-content" v-else>
                        <div class="border-bottom my-1  border-gray"  v-for="(total_earning, index) in total_earnings" :key="index">
                            <div class="stat-number">{{ total_earning.promocode_total ?? 0 }}</div>
                            <div class="stat-currency">{{ total_earning.currency }}</div>
                        </div>
                        <div class="stat-label">{{ trans('em.total') }}  {{ trans('em.promocode') }}</div>
                    </div>

                </div>

                <div class="stat-card">
                    <div class="stat-icon fa-percentage">
                        <i class="fas fa-percentage"></i>
                    </div>
                    <div class="stat-content" v-if="total_earnings.length <= 0">
                        <div class="stat-number">0</div>
                        <div class="stat-label">{{ trans('em.total_admin_commission') }}</div>
                    </div>
                    <div class="stat-content" v-else>
                        <div class="border-bottom my-1  border-gray"  v-for="(total_earning, index) in total_earnings" :key="index">
                            <div class="stat-number">{{ total_earning.admin_commission_total ?? 0 }}</div>
                            <div class="stat-currency">{{ total_earning.currency }}</div>
                        </div>
                        <div class="stat-label">{{ trans('em.total_admin_commission') }}</div>
                    </div>
                </div>

                <div class="stat-card">
                    <div class="stat-icon fa-wallet">
                        <i class="fas fa-wallet"></i>
                    </div>
                    <div class="stat-content" v-if="total_earnings.length <= 0">
                        <div class="stat-number">0</div>
                        <div class="stat-label">{{ trans('em.total_profit') }}</div>
                    </div>
                    <div class="stat-content" v-else>
                        <div class="border-bottom my-1  border-gray" v-for="(total_earning, index) in total_earnings" :key="index">
                            <div class="stat-number">{{ total_earning.organiser_earning_total ?? 0 }}</div>
                            <div class="stat-currency">{{ total_earning.currency }}</div>
                        </div>
                        <div class="stat-label">{{ trans('em.total_profit') }}</div>
                    </div>

                </div>

            </div>

            <div class="table-responsive">
                <table class="table text-wrap">
                    <thead class="table-light text-nowrap">
                        <tr>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.event') }}</th>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.bookings') }}</th>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.promocode') }}</th>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.commission') }}</th>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.profit') }}</th>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.month') }}</th>
                            <th class="border-top-0 border-bottom-0">{{ trans('em.transferred') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="(earning, index) in event_earning" :key="index" >
                            <td :data-title="trans('em.event')">
                                <div class="d-flex align-items-center">
                                    <a :href="eventSlug(earning.event_slug)"> 
                                        <img :src="getImageUrl(earning.event_thumbnail)" :alt="earning.event_name" class="rounded img-4by3-md ">
                                    </a>
                                    <div class="ms-3 lh-1">
                                        <h5 class="mb-1"> 
                                            <a :href="eventSlug(earning.event_slug)" class="text-inherit text-wrap">{{ earning.event_name }}</a>
                                        </h5>
                                        <small class="text-success strong"><i class="fas fa-bolt"></i> {{ earning.count_bookings }} {{ trans('em.bookings') }}</small>
                                    </div>
                                </div>
                            </td>
                            <td class="align-middle" :data-title="trans('em.bookings')">
                                {{ earning.customer_paid_total }} {{ earning.currency }}
                            </td>
                            <td class="align-middle" :data-title="trans('em.promocode')">
                                {{ earning.promocode_total }} {{ earning.currency }}
                            </td>
                            <td class="align-middle" :data-title="trans('em.commission')">
                                {{ earning.admin_commission_total }} {{ earning.currency }}
                            </td>
                            <td class="align-middle text-success" :data-title="trans('em.profit')">
                                <i class="fas fa-sack-dollar"></i> {{ earning.organiser_earning_total }} {{ earning.currency }}
                            </td>
                            <td class="align-middle" :data-title="trans('em.month')">
                                {{ moment(earning.month_year, 'MM YYYY').format('MMM ,YYYY') }}
                            </td>
                            <td class="align-middle" :data-title="trans('em.transferred')">
                                <span class="badge bg-success" v-if="earning.transferred > 0">{{ trans('em.paid') }}</span>
                                <span class="badge bg-primary" v-else>{{ trans('em.pending') }}</span>
                            </td>
                        </tr>
                        <tr v-if="event_earning.length <= 0">
                            <td colspan="6" class="text-center align-middle">{{ trans('em.no_bookings') }}</td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="px-4 pb-4"  v-if="event_earning.length > 0">
                <pagination-component v-if="pagination.last_page > 1" :pagination="pagination" :offset="pagination.total" :path="'/myearning'" @paginate="eventEarning()">
                </pagination-component>
            </div>
        </div>
    </div>
</template>


<script>

import PaginationComponent from '../../../../../../eventmie-pro/resources/js/common_components/Pagination.vue'
import mixinsFilters from '../../../../mixins.js';



export default {
    
    mixins:[
        mixinsFilters
    ],

    props: [
        // pagination query string
        'page',
        
    ],
    
    components: {
        PaginationComponent,
    },

    
    data() {
        return {
            event_earning : [],
            total_earnings : [],
            
            moment     : moment,
            edit_index : null,
             pagination: {
                'current_page': 1
            },
            currency   : null,

            date_range : [],
            start_date : '',
            end_date   : '',
            
            // date shortucts like today, tommorrow
            shortcuts: [
                {
                    text: trans('em.today'),
                    onClick: () => {
                        this.date_range = [moment().toDate(), moment().toDate() ]
                    }
                },
                {
                    text: trans('em.tomorrow'),
                    onClick: () => {
                        this.date_range = [moment().add(1,'day').toDate(), moment().add(1,'day').toDate()]
                    }
                },
                {
                    text: trans('em.this')+' '+trans('em.weekend'),
                    onClick: () => {
                        this.date_range = [moment().endOf("week").toDate(), moment().endOf("week").toDate()]
                    }
                },
                {
                    text: trans('em.this')+' '+trans('em.week'),
                    onClick: () => {
                        this.date_range = [moment().startOf("week").toDate(), moment().endOf("week").toDate()]
                    }
                },
                {
                    text: trans('em.next')+' '+trans('em.week'),
                    onClick: () => {
                        this.date_range = [moment().add(1, 'weeks').startOf("week").toDate(), moment().add(1, 'weeks').endOf("week").toDate()]
                    }
                },
                {
                    text: trans('em.this')+' '+trans('em.month'),
                    onClick: () => {
                        this.date_range = [moment().startOf("month").toDate(), moment().endOf("month").toDate()]
                    }
                },
                {
                    text: trans('em.next')+' '+trans('em.month'),
                    onClick: () => {
                        this.date_range = [moment().add(1, 'months').startOf("month").toDate(), moment().add(1, 'months').endOf("month").toDate()]
                    }
                },
            ],

            events    : [],
            event_id  : 0,
        }
    },

     computed: {
        current_page() {
            // get page from route
            if(typeof this.page === "undefined")
                return 1;
            return this.page;
        },
    },

    methods:{

        // return route with event slug
        eventSlug(slug){
            
            if(slug)
            {
                return route('eventmie.events_show',[slug]);
            }    
        },


        // searching by date 
        dateRange: function () {
            var is_date_null = 0;
            if(Object.keys(this.date_range).length > 0 )
            {
                // convert date range to server side date
                this.date_range.forEach(function(value, key) {
                    if(value != null) {
                        is_date_null = 1;

                        if(key == 0)
                            this.start_date   =  this.convert_date(value); // convert local start_date to server date then searching by date
                        
                        if(key == 1)
                            this.end_date     =  this.convert_date(value); // convert local end_date to server date then searching by date
                    }
                }.bind(this));
                
                // date reset
                if(is_date_null <= 0){
                    this.start_date  = '';
                    this.end_date    = '';
                }

                this.eventEarning();
            }
            
        },

        // get all events
        getMyEvents() {
            axios.get(route('eventmie.all_myevents'))
            .then(res => {
                this.events  = res.data.myevents;
            })
            .catch(error => {
                
            });
        },

        // get event earning
        eventEarning() {
            
            if(typeof this.start_date === "undefined") {
                this.start_date     = '';
            }
            if(typeof this.end_date === "undefined") {
                this.end_date     = '';
            }

            axios.get(route('eventmie.organiser_event_earning')+'?page='+this.current_page+'&event_id='+this.event_id+'&start_date='
                         +this.start_date+'&end_date='+this.end_date)
            .then(res => {
                
                this.event_earning   = res.data.event_earning.data;
                this.pagination = {
                    'total' : res.data.event_earning.total,
                    'per_page' : res.data.event_earning.per_page,
                    'current_page' : res.data.event_earning.current_page,
                    'last_page' : res.data.event_earning.last_page,
                    'from' : res.data.event_earning.from,
                    'to' : res.data.event_earning.to,
                    'links' : res.data.event_earning.links,
                };
            })
            .catch(error => {
                
            });
        },

        //net total
        totalEarning() {
             axios.get(route('eventmie.organiser_total_earning')+'?event_id='+this.event_id)
            .then(res => {
                
                this.total_earnings   = res.data.total_earning;
                this.currency   = res.data.currency;
                
            })
            .catch(error => {
                
            });
        }
    },
   
    mounted() {
        
        this.getMyEvents();
        this.eventEarning();
        this.totalEarning();
    },

    watch : {
        date_range: function () {
            this.dateRange();
        },

        event_id: function () {
            this.eventEarning();
            this.totalEarning();
        },

    }
}
</script>


