<template>
    <v-layout>
        <!-- Alert dialog -->
        <v-dialog v-model="dialogresend" max-width="30%">
            <v-card>
                <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                    Peringatan !
                </v-card-title>
                <v-card-text class="pt-2 pb-2">
                    <v-layout row>
                        <v-flex xs12 d-flex>
                            <v-layout row>
                                <v-flex pb-1 xs12>
                                    <v-layout row>
                                        <v-flex pt-2 pr-2 xs12>
                                            {{ msgalert }}
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialogresend = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" flat @click="Resend()">
                        Yakin
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert dialog -->

        <!-- Snackbar -->
        <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true" :color="clr"
            :value="snackbar">
            {{ msgsnackbar }}
            <v-btn flat @click="snackbar = false"> Tutup </v-btn>
        </v-snackbar>
        <!-- End Snackbar -->

        <!-- ERROR DIALOG -->
        <v-dialog v-model="dialog_error" max-width="500px">
            <v-card>
                <v-card-title>
                    <span>ERROR !</span>
                    <v-spacer></v-spacer>
                </v-card-title>
                <v-divider></v-divider>
                <div class="ma-3 red--text">{{ msgError }}</div>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="primary" flat @click="dialog_error = false">Close</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- END ERROR DIALOG -->

        <v-flex xs12>
            <v-card>
                <!-- Toolbar -->
                <v-toolbar color="blue darken-1" dark scroll-off-screen scroll-target="#scrolling-techniques">

                    <v-toolbar-title class="headline font-weight-bold">Whatsapp Home Service</v-toolbar-title>
                </v-toolbar>
                <!-- End Toolbar -->
                <div id="scrolling-techniques" class="scroll-y" :style="{ 'height': windowHeight }">
                    <!-- search -->
                    <v-card elevation="0">
                        <v-card-text>
                            <v-layout row>
                                <v-flex pr-2 xs2>
                                    <v-menu class="pr-2" v-model="menufilterdate" :close-on-content-click="false"
                                        :nudge-right="40" lazy transition="scale-transition" offset-y full-width
                                        max-width="290px" min-width="290px">
                                        <template v-slot:activator="{ on }">
                                            <v-text-field v-model="filterComputedDateFormatted" label="Tanggal" outline
                                                hide-details readonly v-on="on"
                                                @blur="date = deFormatedDate(filterComputedDateFormatted)"></v-text-field>
                                        </template>
                                        <v-date-picker no-title v-model="xdate"
                                            @input="menufilterdate = false"></v-date-picker>
                                    </v-menu>
                                </v-flex>
                            </v-layout>
                        </v-card-text>
                    </v-card>
                    <!-- end search -->
                    <v-divider></v-divider>
                    <!-- data tabel -->
                    <v-data-table :headers="headers" :items="vservice" :loading="isLoading" hide-actions>
                        <template v-slot:headers="props">
                            <tr>
                                <th v-for="header in props.headers" :key="header.text"
                                    @click="changeSort(header.value, pagination.descending, header)" :width="header.width">
                                    <v-icon color="blue darken-2"
                                        v-if="pagination.sortBy === header.value && header.sort && pagination.descending == 'desc'"
                                        small>arrow_upward</v-icon>
                                    <v-icon color="blue darken-2"
                                        v-if="pagination.sortBy === header.value && header.sort && pagination.descending == 'asc'"
                                        small>arrow_downward</v-icon>
                                    <v-icon color="grey lighten-2" v-if="pagination.sortBy !== header.value && header.sort"
                                        small>arrow_downward</v-icon>

                                    {{ header.text }}
                                </th>
                            </tr>
                        </template>
                        <v-progress-linear v-slot:progress color="blue" indeterminate></v-progress-linear>
                        <template v-slot:no-data>
                            <v-alert :value="true" color="error" icon="warning">
                                Belum ditemukan data
                            </v-alert>
                        </template>
                        <template v-slot:items="props">
                            <td class="text-xs-center pa-2" v-bind:class="{ 'blue lighten-5': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.HS_WhatsAppType }}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'blue lighten-5': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.HS_WhatsAppName }}
                                <br />
                                <span v-if="props.item.HS_WhatsAppIsSent === 'Y'" class="font-weight-bold"
                                    style="color:#800000">{{ props.item.queue_id }}</span>
                            </td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'blue lighten-5': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.HS_WhatsAppNoHP }}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'blue lighten-5': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.HS_WhatsAppMessage }}</td>
                            <td class="text-xs-center pa-2">
                                <v-toolbar flat dense>
                                    <v-chip
                                        v-if="props.item.HS_WhatsAppIsSent === 'N' || props.item.HS_WhatsAppIsSent === 'R'"
                                        small label dark>PENDING</v-chip>
                                    <v-chip v-if="props.item.HS_WhatsAppIsSent === 'Y'" small color="primary" label
                                        dark>TERKIRIM</v-chip>
                                    <v-btn @click="openResend(props.item)" v-if="props.item.HS_WhatsAppIsSent === 'Y'" small
                                        color="orange" dark>Kirim Ulang </v-btn>
                                    <v-chip v-if="props.item.HS_WhatsAppIsSent === 'E'" small color="error" label
                                        dark>ERROR</v-chip>
                                </v-toolbar>
                            </td>
                        </template>
                    </v-data-table>
                    <!-- end data tabel -->
                </div>
                <v-divider></v-divider>
                <!-- pagination -->
                <v-flex xs12 class="text-xs-left pt-3 pb-3">
                    <v-pagination v-model="curr_page" :length="xtotal_page"></v-pagination>
                </v-flex>
                <!-- end pagination -->
            </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;
}

/* 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 {
            windowHeight: window.innerHeight - ((38 / 100) * window.outerHeight) + 'px',
            menufilterdate: false,
            date: new Date().toISOString().substr(0, 10),
            whatsappid: "",
            dialogresend: false,
            msgalert: "",
            clr: "success",
            headers: [
                {
                    text: "TYPE",
                    align: "center",
                    sortable: false,
                    sort: true,
                    value: "HS_WhatsAppType",
                    width: "15%",
                    class: "pa-2 blue darken-3 white--text"
                },
                {
                    text: "NAMA",
                    align: "center",
                    sortable: false,
                    sort: true,
                    value: "HS_WhatsAppName",
                    width: "15%",
                    class: "pa-2 blue darken-3 white--text"
                },
                {
                    text: "N0 HP",
                    align: "center",
                    sortable: false,
                    sort: true,
                    value: "HS_WhatsAppNoHP",
                    width: "15%",
                    class: "pa-2 blue darken-3 white--text"
                },
                {
                    text: "PESAN",
                    align: "center",
                    sortable: false,
                    sort: true,
                    value: "HS_WhatsAppMessage",
                    width: "45%",
                    class: "pa-2 blue darken-3 white--text"
                },
                {
                    text: "STATUS",
                    align: "center",
                    sortable: false,
                    sort: false,
                    value: "mr",
                    width: "10%",
                    class: "pa-2 blue darken-3 white--text"
                }
            ]
        };
    },
    mounted() {
        this.$store.dispatch("service/search")
    },
    computed: {
        vservice() {
            return this.$store.state.service.services
        },
        isLoading() {
            return this.$store.state.service.search_status
        },
        curr_page: {
            get() {
                return this.$store.state.service.current_page
            },
            set(val) {
                this.$store.commit("service/update_current_page", val)
                this.$store.commit("service/update_last_id", -1)
                this.$store.dispatch("service/search")
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.service.total_service
            },
            set(val) {
                this.$store.commit("service/update_total_service", val)
            }
        },
        pagination: {
            get() {
                return this.$store.state.service.pagination
            },
            set(val) {
                this.$store.commit("service/update_pagination", val)
            }
        },
        xdate: {
            get() {
                return this.$store.state.service.date
            },
            set(val) {
                this.$store.commit("service/update_date", val)
            }
        },
        filterComputedDateFormatted() {
            return this.formatDate(this.xdate)
        },
        snackbar: {
            get() {
                return this.$store.state.service.alert_success;
            },
            set(val) {
                this.$store.commit("service/update_alert_success", val);
            }
        },
        msgsnackbar() {
            return this.$store.state.service.msg_success;
        },
        msgError() {
            return this.$store.state.service.error_message;
        },
        dialog_error: {
            get() {
                return this.$store.state.service.alert_error;
            },
            set(val) {
                this.$store.commit("service/update_alert_error", val);
            },
        }
    },
    methods: {
        isSelected(p) {
            return p.HS_WhatsAppID == this.$store.state.service.selected_service.HS_WhatsAppID
        },
        selectMe(selected) {
            this.$store.commit("service/update_selected_service", selected)
            this.$store.commit("service/update_last_id", selected.HS_WhatsAppID)
        },
        changeSort(value, sort, header) {
            if (header.sort) {
                var newVal = value
                var newSort = sort == "asc" ? "desc" : "asc"
                if (newVal !== this.pagination.sortBy)
                    newSort = "asc"

                this.pagination = { descending: newSort, sortBy: newVal }
                this.$store.dispatch("service/search")
            }
        },
        thr_search: _.debounce(function () {
            this.$store.dispatch("service/search")
        }, 1000),
        formatDate(date) {
            if (!date) return null

            const [year, month, day] = date.split('-')
            return `${day}-${month}-${year}`
        },
        deFormatedDate(date) {
            if (!date) return null

            const [day, month, year] = date.split('-')
            return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
        },
        openResend(item) {
            this.selectMe(item)
            this.whatsappid = item.HS_WhatsAppID
            this.msgalert = "Yakin mau kirim ulang ?"
            // console.log("cek id wa", this.whatsappid)
            this.dialogresend = true
        },
        Resend() {
            let prm = {
                whatsappid: this.whatsappid
            }
            this.$store.dispatch("service/resend", prm)
            // console.log("cek prm id", prm)
            this.dialogresend = false
        }
    },
    watch: {
        xdate(val, old) {
            this.xdate = val
            this.thr_search()
        },
    }
}
</script>