<template>
    <div>
        <table class="table is-fullwidth is-striped is-bordered">
            <thead>
            <tr>
                <th>Núcleo</th>
                <th><abbr title="Total sin tomar en cuenta las devoluciones">Total parcial $</abbr></th>
                <th><abbr title="Devoluciones correspondientes al núcleo">Devoluciones $</abbr></th>
                <th><abbr title="Total a Pagar por el núleo">Total real $</abbr></th>
                <th class="is-1"><abbr title="Aprobacion">Aprobación</abbr></th>
            </tr>
        </thead>
        <tbody>
            <pedidos-admin-fila-pedido v-for="pedido in this.pedidos"
            :pedido="pedido" :key="pedido.id">
        </pedidos-admin-fila-pedido>
        </tbody>
        </table>
        <table class="table is-striped is-bordered">
            <tr>
                <th colspan="2" class="has-background-black has-text-white has-text-centered">TOTALES</th>
            </tr>
            <tr>
                <th>Total a recaudar:</th>
                <td class="has-text-right">$ {{ totalAprobadosMenosDevoluciones() }}</td>
            </tr>
            <tr v-if="totalBonosBarriales > 0">
                <th>Total bonos barriales:</th>
                <td class="has-text-right">$ {{ totalBonosBarriales }}</td>
            </tr>
            <tr v-if="totalDevoluciones() > 0">
                <th>Total devoluciones:</th>
                <td class="has-text-right">- $ {{ totalDevoluciones() }}</td>
            </tr>
            <tr>
                <th>Total a depositar:</th>
                <td class="has-text-right">$ {{ totalAprobados() - totalBonosBarriales }}</td>
            </tr>
        </table>
    </div>
</template>

<script>
import PedidosAdminFilaPedidoVue from './PedidosAdminFilaPedido.vue';
export default {
    name: "PedidosAdminTablaPedidos",
    components: {
        PedidosAdminFilaPedidoVue
    },
    props: {
        pedidos: {
            type: Array,
            required: true
        },
        bonosDeTransporte: {
            type: Number,
            required: true
        },
        totalBonosBarriales: {
            type: Number,
            required: true
        },
    },
    methods: {
        totalBonosBarriales() {
            let suma = 0;
            let aprobados = this.pedidos.filter(p => p.aprobado);
            for (let i = 0; i < aprobados.length; i++) {
                let bonoBarrial = aprobados[i].productos.find(p => p.nombre.includes("barrial"))
                if (bonoBarrial) {
                    suma += this.$limpiarInt(bonoBarrial.pivot.total)
                }
            }
            return suma;
        },
        totalDevoluciones() {
            let suma = 0
            let aprobados = this.pedidos.filter(p => p.aprobado);
            for (let i = 0; i < aprobados.length; i++) {
                suma = aprobados[i].devoluciones_total
            }
            suma += parseInt(this.bonosDeTransporte)*15
            return suma;
        },
        totalAprobadosMenosDevoluciones() {
            let suma = 0
            let aprobados = this.pedidos.filter(p => p.aprobado);
            for (let i = 0; i < aprobados.length; i++) {
                suma += this.$limpiarFloat(aprobados[i].subtotal_bonos)
                suma += this.$limpiarFloat(aprobados[i].subtotal_productos)
                suma -= aprobados[i].devoluciones_total
            }
            suma += parseInt(this.bonosDeTransporte)*15
            return suma;
        },
        totalAprobados() {
            let suma = 0
            let aprobados = this.pedidos.filter(p => p.aprobado);
            for (let i = 0; i < aprobados.length; i++) {
                suma += this.$limpiarFloat(aprobados[i].subtotal_bonos)
                suma += this.$limpiarFloat(aprobados[i].subtotal_productos)
            }
            suma += parseInt(this.bonosDeTransporte)*15
            return suma;
        }
    },
}
</script>

<style>

</style>