<template>
    <div class="row parentContainer p-t-10 p-b-10">
        <div class="col-2">
            <div v-if="item.order">
                <a :href="route('customer.profile', item.order.company_module.marking)">{{item.order.company_module.marking}}</a>/<a :href="route('order.show', item.order.reference)">{{item.order.reference}}</a>
            </div>
            <div v-if="!item.order" class="text-danger">Unclaimed Packing List</div>
        </div>
        <div class="col text-center">{{item.packages.reduce((total, obj) => (obj.type === 2 ? 0 : obj.quantity) + total, 0)}}</div>
        <div class="col">{{ cbm }}</div>
        <div class="col">{{ overweight }}</div>
        <div class="col">{{ (parseFloat(cbm) + parseFloat(overweight)).toFixed(3)}}</div>
        <div class="col bold" :class="{'text-danger': item.status === 5, 'text-success': item.status !== 5}">{{item.transport ? 'Delivery' : item.status === 5 ? 'On Hold' : 'Release'}}</div>
        <div class="col">
            <p class="no-margin">{{item.transport ? (item.transport.current_schedule ? item.transport.current_schedule.eta : 'n/a') : 'n/a'}}</p>
        </div>
        <div class="col text-right" v-if="!item.transport">
            <div v-if="item.order">
                <div class="btn btn-xs btn-success pointer" v-if="item.status === 5" @click="updateStatus(item.status === 1 ? 1 : 2)"><i class="fa fa-check m-r-5"></i>Release</div>
                <div class="btn btn-xs btn-danger pointer" v-if="item.status !== 5" @click="updateStatus(5)"><i class="fa fa-hand-paper-o m-r-5"></i>Hold</div>
            </div>
            <div v-if="!item.order" class="text-danger">Unclaimed</div>
        </div>
    </div>
</template>
<script>
    import componentHandler from '../../../general/mixins/componentHandler';
    export default {
        computed: {
            cbm () {
                return (Math.ceil((this.item.packages.reduce((total, obj) => (obj.type === 2 ? 0 : obj.cbm) + total, 0)) * 1000) / 1000).toFixed(3)
            },
            overweight(){
                return (Math.ceil((this.item.packages.reduce((total, obj) => (obj.type === 2 ? obj.cbm : 0) + total, 0)) * 1000) / 1000).toFixed(3)
            }
        },
        methods: {
            updateStatus(status){
                this.submit(this.route('api.packing_list.status.update', this.item.id, status), 'put', '', true, true)
            },
            successHandler(response){
                this.item = response.payload.data;
            }
        },
        mixins: [componentHandler]
    }
</script>
