<template>
    <v-layout>
        <v-flex xs12>
            <v-card>
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>DAFTAR E-FAKTUR</v-toolbar-title>
                    <v-spacer></v-spacer>
                </v-toolbar>
                <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true">
                    {{msgsnackbar}}
                    <v-btn flat @click="updateAlert_success(false)">
                        Tutup
                    </v-btn>
                </v-snackbar>
                <v-layout row style="background:#bbdefb;padding-top:3px;">
                    <v-flex xs4 class="text-sm-left" pa-2>
                        <v-text-field class="xs4 ma-1" label="Tahun" placeholder="Tahun" outline v-model="xyear" hide-details></v-text-field>
                    </v-flex>
                    <v-flex xs4 class="text-sm-left" pa-2>
                    <v-select class="xs4 mini-select ma-1" :items="xmonthes" style="font-size:14px" item-text="name" return-object v-model="xselectedmonth"
                            label="Bulan" outline hide-details></v-select>
                    </v-flex>
                    <v-flex xs4 class="text-sm-left" pa-2>
                </v-layout>
                <v-divider></v-divider>
                <div>

                    <v-layout row class="scroll-container" style="max-height:600px;overflow: auto;">
                        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                            <v-data-table :headers="headers" :items="vdeliveryorders" :loading="isLoading" hide-actions class="elevation-1">
                                <template slot="items" slot-scope="props">
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.tanggal}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.sysFKapusSumIsPosted}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.sysFaktur08SumAmount}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.sysFaktur08SumTotal}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.sysFaktur08SumTargetPct}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.sysFaktur08SumActualPct}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.sysFaktur08SumIsPosted}}</td>
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>
                   <!-- <v-layout row class="scroll-container" style="max-height:600px;overflow: auto;">
                        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                            <div class="rTable">
                                <div class="rTableGroup">
                                    <div class="rTableRow">
                                        <div style="width:25%" class="rTableCell rfirstCell">TOTAL KAS PUSAT</div>
                                        <div style="width:25%" class="rTableCell rfirstCell">{{totalkapus}}</div>
                                        
                                    </div>
                                    <div class="rTableRow">
                                        <div style="width:25%" class="rTableCell rfirstCell">TOTAL KAS CABANG</div>
                                        <div style="width:25%" class="rTableCell rfirstCell">{{totalkacab_only}}</div>
                                    </div>
                                    <div class="rTableRow">
                                        <div style="width:25%" class="rTableCell rfirstCell">TOTAL KAS </div>
                                        <div style="width:25%" class="rTableCell rfirstCell">{{totalkacab}}</div>
                                    </div>
                                    <div class="rTableRow">
                                        <div style="width:25%" class="rTableCell rfirstCell">RATA-RATA (%)</div>
                                        <div style="width:25%" class="rTableCell rfirstCell">{{avgkapus}}</div>
                                    </div>
                                </div>
                            </div>
                        </v-flex>
                    </v-layout> -->

                    <v-divider></v-divider>
                    <!-- <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page" :length="xtotal_page"></v-pagination> -->
                </div>


            </v-card>
        </v-flex>
    </v-layout>
</template>

<style scoped>
    .searchbox .v-input.v-text-field .v-input__slot {
        min-height: 60px;
    }

    .searchbox .v-btn {
        min-height: 60px;
    }

    table.v-table tbody td,
    table.v-table tbody th {
        height: 40px;
    }

    table.v-table thead tr {
        height: 40px;
    }

    .textinput {
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
        background-color: white;
        background-position: 10px 10px;
        background-repeat: no-repeat;
        padding-left: 40px;
        width: 100%;
        padding: 8px 10px;
        margin-bottom: 5px;
        box-sizing: border-box;
        border: 1px solid #607d8b;

    }

    .textinput:focus {
        width: 100%;
    }

    .textinput:focus::-webkit-input-placeholder {
        color: transparent;
    }

    .textinput:focus::-moz-placeholder {
        color: transparent;
    }

    .textinput:-moz-placeholder {
        color: transparent;
    }

    .boxoutline {
        color: red;
        border: 1px solid red;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #ffffff;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxoutline:hover {
        background: rgba(0, 0, 0, 0.07) !important;
        font-size: 15px;
        font-weight: 700;
    }

    .boxsolid {
        color: #ffffff;
        border: 1px solid #ffffff;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #f44336;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxsolid:hover {
        background: #f44336de;
        font-size: 15px;
        font-weight: 700;
    }

    .scroll-container {
        scroll-padding: 50px 0 0 50px;
    }

    ::-webkit-scrollbar {
        width: 7px;
    }

    .rTable {
        display: table;
        width: 100%;
        border-spacing: 0 .2em;
    }

    .rTableGroup {
        display: table-row-group;
    }

    .rTableRow {
        display: table-row;
        font-size: 14px;
        -webkit-flex-wrap: wrap;
        /* Safari 6.1+ */
        flex-wrap: wrap;

    }

    .rTableHeading {
        display: table-header-group;
        background-color: #fffff;
    }

    .rTableCell {
        display: table-cell;
        padding-left: 1px;
        padding-right: 1px;
        padding-top: 4px;
        padding-bottom: 1px;
        line-height: 1;
        border: 1px solid #757575;
        background-color: #fffff;
        border-spacing: 2px;
    }

    .rTableHead {
        display: table-cell;
        padding-left: 1px;
        padding-right: 1px;
        padding-top: 1px;
        padding-bottom: 0px;
        background-color: #fffff;
    }

    .rTableHeading {
        display: table-header-group;
        text-transform: uppercase;
        font-weight: 1;
    }

    .rTableFoot {
        display: table-footer-group;
        font-weight: bold;
        background-color: #ddd;
    }

    .rTableBody {
        display: table-row-group;
    }

    .rfirstCell {
        border-left: 5px solid #757575;
        border-top-left-radius: 1px;
        border-bottom-left-radius: 1px;
    }

    /* this targets the default scrollbar (compulsory) */

    ::-webkit-scrollbar-track {
        background-color: #73baf3;
    }

    /* the new scrollbar will have a flat appearance with the set background color */

    ::-webkit-scrollbar-thumb {
        background-color: #2196f3;
    }

    /* this will style the thumb, ignoring the track */

    ::-webkit-scrollbar-button {
        background-color: #0079da;
    }

    /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

    ::-webkit-scrollbar-corner {
        background-color: black;
    }

    /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
</style>

<script>
    module.exports = {
        data() {
            return {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                dialogdeletealert: false,
                msgalert: "",
                valid: false,
                xid: 0,
                search_courier: "",
                items: [],
                xname: "",
                xcode: "",
                name: "",
                isdefault: false,
                xdestination: "",
                xup: "",
                xaddress: "",
                scode: "",
                page: 1,
                headers: [{
                        text: "TANGGAL",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    
                    {
                        text: "KAPUS",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "FAKTUR 08",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    
                    {
                        text: "TOTAL",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    
                    {
                        text: "TARGET %",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "ACTUAL %",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "POSTED",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: true,
                    page: 1,
                    rowsPerPage: 100,
                    sortBy: 'id DESC',
                    totalItems: this.$store.state.kapus.total_filter_deliveryorders
                }
            };
        },
        components: {
            'one-date-picker': httpVueLoader('./oneDatePicker2.vue')
        },
        async mounted() {
            await this.$store.dispatch("kapus/getmonth")
            this.searchDeliveryOrder()
            /*
               await this.$store.dispatch("kapus/lookup", {
                deliveryorder: this.$store.state.kapus.selected_month.id,
                paymenttype: this.$store.state.kapus.selected_paymenttype.id,
                year: this.xyear,
                search: this.xsearch,
                current_page: 1,
                lastid: -1
            })
            */
            /*
            this.$store.dispatch("box/lookup", {
                current_page: 1,
             id: this.$store.state.kapus.selected_deliveryorder.id
            })
            this.$store.dispatch("kapus/searchsample", this.search_sample)
            */
        },
        computed: {
            xact() {
                return this.$store.state.kapus.act
            },
            xerrors() {
                return this.$store.state.kapus.errors
            },
            xshowall() {
                return this.$store.state.kapus.show_all
            },
            vdeliveryorders() {
                let order = this.$store.state.kapus.deliveryorders
                return order 
            },
            xtotaldeliveryorders() {
                return this.$store.state.kapus.total_deliveryorders
            },
            xtotalfilterdeliveryorders() {
                return this.$store.state.kapus.total_filter_deliveryorders
            },
            xmonthes: {
                get() {
                    return this.$store.state.kapus.monthes
                },
                set(val) {
                    this.$store.commit("kapus/update_monthes", val)
                }
            },
            xselectedmonth: {
                get() {
                    return this.$store.state.kapus.selected_month
                },
                set(val) {
                    //testet total 
                    this.$store.commit("kapus/update_totalkapus",0)
                    this.$store.commit("kapus/update_totalkacab",0)
                    this.$store.commit("kapus/update_avgkapus",0)
                    this.$store.commit("kapus/update_avgkacab",0)

                    this.$store.commit("kapus/update_selected_month", val)
                    this.searchDeliveryOrder()
                }
            },
            xpaymenttypes: {
                get() {
                    return this.$store.state.kapus.paymenttypes
                },
                set(val) {
                    this.$store.commit("kapus/update_paymenttypes", val)
                }
            },
            xselectedpaymenttype: {
                get() {
                    return this.$store.state.kapus.selected_paymenttype
                },
                set(val) {
                    //testet total 
                    this.$store.commit("kapus/update_totalkapus",0)
                    this.$store.commit("kapus/update_totalkacab",0)
                    this.$store.commit("kapus/update_avgkapus",0)
                    this.$store.commit("kapus/update_avgkacab",0)

                    this.$store.commit("kapus/update_selected_paymenttype", val)
                    this.searchDeliveryOrder()
                }
            },
            xyear: {
                get() {
                    return this.$store.state.kapus.xyear
                },
                set(val) {
                    //testet total 
                    this.$store.commit("kapus/update_totalkapus",0)
                    this.$store.commit("kapus/update_totalkacab",0)
                    this.$store.commit("kapus/update_avgkapus",0)
                    this.$store.commit("kapus/update_avgkacab",0)

                    this.$store.commit("kapus/update_xyear", val)
                    this.searchDeliveryOrder()
                }
            },
            init_date: {
                get() {
                    return this.$store.state.kapus.init_date
                },
                set(val) {
                    this.$store.commit("kapus/update_init_date", val)
                }
            },
            xsearch: {
                get() {
                    return this.$store.state.kapus.x_search
                },
                set(val) {
                    this.$store.commit("kapus/update_x_search", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.kapus.current_page
                },
                set(val) {
                    this.$store.commit("kapus/update_current_page", val)
                    this.$store.dispatch("kapus/lookup", {
                        deliveryorder: this.$store.state.kapus.selected_month.id,
                        paymenttype: this.$store.state.kapus.selected_paymenttype.id,
                        year: this.xyear,
                        search: this.xsearch,
                        current_page: val,
                        lastid: -1
                    })
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.kapus.total_deliveryorders
                },
                set(val) {
                    this.$store.commit("kapus/update_total_deliveryorders", val)
                }
            },
            totalkapus: {
                get() {
                    return this.$store.state.kapus.totalkapus
                },
                set(val) {
                    this.$store.commit("kapus/update_totalkapus", val)
                }
            },
            totalkacab: {
                get() {
                    return this.$store.state.kapus.totalkacab
                },
                set(val) {
                    this.$store.commit("kapus/update_totalkacab", val)
                }
            },
            totalkacab_only : {
                get() {
                    return this.$store.state.kapus.totalkacab_only
                }
            },
            avgkapus: {
                get() {
                    return this.$store.state.kapus.avgkapus
                },
                set(val) {
                    this.$store.commit("kapus/update_avgkapus", val)
                }
            },
            avgkacab: {
                get() {
                    return this.$store.state.kapus.avgkacab
                },
                set(val) {
                    this.$store.commit("kapus/update_avgkacab", val)
                }
            },
            dialogdeliveryorder() {
                return this.$store.state.kapus.dialog_form_deliveryorder
            },
            snackbar: {
                get() {
                    return this.$store.state.kapus.alert_success
                },
                set(val) {
                    this.$store.commit("kapus/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.kapus.msg_success
            },
            lookupstatus() {
                return this.$store.state.kapus.lookup_deliveryorder
            },
            xstartdate: {
                get() {
                    return this.$store.state.kapus.startdate
                },
                set(val) {
                    this.$store.commit("kapus/update_startdate", val)
                }
            },
            isexternal: {
                get() {
                    return this.$store.state.kapus.isexternal
                },
                set(val) {
                    this.$store.commit("kapus/update_isexternal", val)
                    this.$store.dispatch("kapus/searchdestination", {
                        search: this.search_destination,
                        tipe: val === true ? 'External' : 'Internal'
                    })
                }
            },
            xdeliveryordertypes() {
                return this.$store.state.kapus.deliveryordertypes
            },
            xdeliveryordertype: {
                get() {
                    return this.$store.state.kapus.deliveryordertype
                },
                set(val) {
                    this.$store.commit("kapus/update_deliveryordertype", val)
                }

            },
            isLoading() {
                return this.$store.state.kapus.loading
            },
        },
        methods: {
            updateShowAll(val) {
                this.$store.commit("kapus/update_show_all", val)
                this.$store.dispatch("kapus/lookup", {
                    deliveryorder: this.$store.state.kapus.selected_month.id,
                    paymenttype: this.$store.state.kapus.selected_paymenttype.id,
                    year: this.xyear,
                    search: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
            },
            searchDeliveryOrder(prm) {
                this.$store.dispatch("kapus/lookup", {
                    deliveryorder: this.$store.state.kapus.selected_month.id,
                    paymenttype: this.$store.state.kapus.selected_paymenttype.id,
                    year: this.xyear,
                    search: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                //this.init_date = prm.new_date
            },
            isSelected(p) {
                return p.id == this.$store.state.kapus.selected_deliveryorder.id
            },
            selectMe(sc) {
                this.$store.commit("kapus/update_selected_deliveryorder", sc)
                this.$store.commit("box/update_kapuspct", this.$store.state.kapus.selected_deliveryorder.target)
                this.$store.dispatch("box/selected", {
                    id: this.$store.state.kapus.selected_deliveryorder.id
                })
                this.$store.dispatch("box/selected2", {
                    id: this.$store.state.kapus.selected_deliveryorder.id
                })
                this.$store.dispatch("box/lookup", {
                    current_page: 1,
                    id: this.$store.state.kapus.selected_deliveryorder.id
                })
            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("kapus/lookup", {
                    deliveryorder: this.$store.state.kapus.selected_month.id,
                    paymenttype: this.$store.state.kapus.selected_paymenttype.id,
                    year: this.xyear,
                    search: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
            }, 1000)
        },
        watch: {
            xsearch(val, old) {
                console.log(val)
                this.xsearch = val
                this.thr_search()

            }

        }
    }
</script>
