<template>
    <v-layout>
        <!-- Snackbar -->
    <v-snackbar
      v-model="snackbar"
      :timeout="5000"
      :multi-line="false"
      :vertical="false"
      :top="true"
      :color="clr"
      :value="snackbar"
    >
      {{ success_message }}
      <v-btn flat @click="snackbar = false"> Tutup </v-btn>
    </v-snackbar>
    <!-- End Snackbar -->
        <!-- alert -->
        <v-dialog v-model="dialogdeletealert" 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="dialogdeletealert = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" flat @click="closeDeleteAlert()">
                        Yakin lah
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- alert -->

        <!-- Alert dialog Form Kosong Start -->
        <v-dialog v-model="dialogalertshow" 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>
                                            <!-- <p id="error-break-line">{{ msgalertshow }}</p> -->
                                            <p style="white-space:pre-wrap;">{{ msgalertshow }}</p>
                                        </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="dialogalertshow = false">
                        Tutup
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert dialog Form Kosong End -->

        <!-- form -->
        <v-dialog v-model="dialogticketdispenser" persistent max-width="800px">
            <v-card>
                <v-card-title>
                    <span class="title">Form Ticket Dispenser</span></v-card-title>
                <v-card-text class="pt-0 pb-0">
                    <v-form ref="formunit" v-model="valid" lazy-validation>
                        <v-layout row wrap>
                            <!-- kiri -->
                            <v-flex xs12 pt-5 pr-3 pl-5 pb-3>
                                <v-text-field label="Tiket Code" v-model="ticket_code" outline></v-text-field>
                                <v-text-field label="Tiket Name" type="text" v-model="ticket_name"
                                    outline></v-text-field>
                            </v-flex>
                            <!-- kiri -->
                        </v-layout>
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="blue darken-1" flat @click="updateDialogFormTicketDispenser()">Tutup</v-btn>
                    <v-btn v-if="xact === 'new'" color="blue darken-1" flat
                        @click="saveFormTicketDispenser()">Simpan</v-btn>
                    <v-btn v-if="xact === 'edit'" color="blue darken-1" flat @click="updateFormTicketDispenser()">Simpan
                        Perubahan</v-btn>
                </v-card-actions>
                </v-form>
            </v-card>
        </v-dialog>
        <!-- form -->

        <v-flex xs12>

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

                    <v-btn @click="openFormTicketDispenser(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                    <v-toolbar-title class="headline font-weight-bold">Master Unit</v-toolbar-title>
                    <v-spacer></v-spacer> -->
                <!-- search -->
                <!-- <div>
                        <v-text-field v-model="xsearch" append-icon="search" label="Search" single-line
                            hide-details></v-text-field>
                    </div> -->
                <!-- search -->
                <!-- </v-toolbar> -->

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

                    <v-toolbar-title class="headline font-weight-bold">Ticket Dispenser</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn class="text-xs-right" @click="openFormTicketDispenser(0)"
                        icon><v-icon>library_add</v-icon></v-btn>
                </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-layout row>
                                    <v-flex pr2 xs3>
                                        <v-text-field v-model="xsearch" label="Search"
                                            placeholder="Cari nama ticket dispenser" outline
                                            hide-details></v-text-field>
                                    </v-flex>
                                </v-layout>

                            </v-layout>
                        </v-card-text>
                    </v-card>
                    <!-- search -->
                    <v-divider></v-divider>

                    <!-- klu pake data dari server -->
                    <v-data-table :headers="headers" :items="vticketdispensers" :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 v-if="pagination.descending === 'asc'" small>arrow_upward</v-icon>
                                    <v-icon v-if="pagination.descending === 'desc'" small>arrow_downward</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="blue darken-2" v-if="
                                    pagination.sortBy === header.value &&
                                    header.sort && pagination.descending === 'desc'" small>arrow_upward</v-icon>

                                    <v-icon color="grey lighten-2" v-if="pagination.sortBy !== header.value &&
                                    header.sort" small>arrow_upward</v-icon>

                                    {{ header.text }}
                                </th>
                            </tr>
                        </template>
                        <v-progress-linear v-slot:progress color="blue" indeterminate></v-progress-linear>
                        <!-- notif -->
                        <template v-slot:no-data>
                            <v-alert v-if="vticketdispensers.length === 0" :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.ticketBoothCode }}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'blue lighten-5': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.ticketBoothName }}
                            </td>
                            <td class="text-xs-left pa-2" v-bind:class="{ 'blue lighten-5': isSelected(props.item) }" @click="selectMe(props.item)">
                                
                                <v-toolbar flat dense>
                                    <v-btn @click="editTicketDispenser(props.item)" small color="primary" dark>ubah
                                        <v-icon small right dark>edit</v-icon></v-btn>
                                    <v-divider vertical></v-divider>
                                    <v-btn small @click="deleteTicketDispenser(props.item)" color="danger" dark>hapus
                                        <v-icon small right dark>clear</v-icon></v-btn>
                                </v-toolbar>
                            </td>
                        </template>
                        <template v-slot:no-results>
                            <v-alert :value="true" color="error" icon="warning">
                                Pencarian "{{ xsearch }}" tidak ada hasil.
                            </v-alert>
                        </template>
                    </v-data-table>

                </div>
                <v-divider></v-divider>
                <v-flex xs12 class="text-xs-left pt-3 pb-3">
                    <v-pagination v-model="curr_page" :length="xtotal_page"></v-pagination>
                </v-flex>
            </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 {
            clr: "success",
            windowHeight: window.innerHeight - 300 + 'px',
            page: 1,
            dialogdeletealert: false,
            msgalert: "",
            xname: '',
            valid: false,
            xid: 0,
            isDefault: false,
            xnameRules: [
                v => !!v || 'Nama harus diisi'
            ],
            category: [],

            headers: [
                {
                    text: "Code",
                    align: "center",
                    sortable: false,
                    sort: true,
                    value: "ticketBoothCode",
                    width: "10%",
                    class: "pa-2 blue darken-3 white--text"
                },
                {
                    text: "Name",
                    align: "center",
                    sortable: false,
                    sort: true,
                    value: "ticketBoothName",
                    width: "80%",
                    class: "pa-2 blue darken-3 white--text"
                },
                {
                    text: "Action",
                    align: "center",
                    sortable: false,
                    value: "mr",
                    width: "10%",
                    class: "pa-2 blue darken-3 white--text"
                }
            ],

            ticket_code: '',
            ticket_name: '',

            // alert
            dialogalertshow: false,
            msgalertshow: '',
        };
    },
    mounted() {
        // ambil data
        this.$store.commit("ticketdispenser/update_last_id", -1)
        this.$store.dispatch("ticketdispenser/lookupbyname")
    },
    computed: {

        // list ticket dispenser
        vticketdispensers() {
            return this.$store.state.ticketdispenser.ticketdispensers
        },

        // paging
        pagination: {
            get() {
                return this.$store.state.ticketdispenser.pagination
            },
            set(val) {
                this.$store.commit("ticketdispenser/update_pagination", val)
            }
        },

        // current page
        curr_page: {
            get() {
                return this.$store.state.ticketdispenser.current_page
            },
            set(val) {
                this.$store.commit("ticketdispenser/update_current_page", val)
                this.$store.commit("ticketdispenser/update_last_id", -1)
                this.$store.dispatch("ticketdispenser/lookupbyname")
            }
        },

        // wkt search
        xsearch: {
            get() {
                return this.$store.state.ticketdispenser.x_search
            },
            set(val) {
                this.$store.commit("ticketdispenser/update_x_search", val)
            }
        },

        xtotal_page: {
            get() {
                return this.$store.state.ticketdispenser.total_ticketdispensers
            },
            set(val) {
                this.$store.commit("ticketdispenser/update_total_bloodrequests", val)
            }
        },

        xact() {
            return this.$store.state.ticketdispenser.act
        },

        // handle error
        xerrors() {
            return this.$store.state.ticketdispenser.errors
        },

        dialogticketdispenser() {
            return this.$store.state.ticketdispenser.dialog_form_ticket_dispenser
        },

        isLoading() {
            return this.$store.state.ticketdispenser.search_status == 1
        },

        // filter
        xtotalticketdispensers() {
            return this.$store.state.ticketdispenser.total_ticketdispensers
        },
        xtotalfilterticketdispensers() {
            return this.$store.state.ticketdispenser.total_filter_ticketdispensers
        },
        success_message() {
            return this.$store.state.ticketdispenser.success_message;
        },
        snackbar: {
            get() {
                return this.$store.state.ticketdispenser.snackbar;
            },
            set(val) {
                this.$store.commit("ticketdispenser/update_snackbar", val);
            },
        },
    },
    methods: {
        // click sort order by
        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 }
                // console.log(this.pagination)
                this.$store.commit("ticketdispenser/update_last_id", -1)
                this.$store.dispatch("ticketdispenser/lookupbyname")
            }
        },

        // tutup dialog
        updateDialogFormTicketDispenser() {
            this.$store.commit("ticketdispenser/update_dialog_form_ticket_dispenser", false)
        },

        // open form
        openFormTicketDispenser() {
            this.xname = ""
            this.isdefault = "N"

            // reset form
            this.ticket_code = ""
            this.ticket_name = ""


            // this.$refs.formunit.reset()
            // this.$refs.formunit.resetValidation()
            this.$store.commit("ticketdispenser/update_act", 'new')
            this.$store.commit("ticketdispenser/update_dialog_form_ticket_dispenser", true)
        },

        // save form
        saveFormTicketDispenser() {
            this.msgalertshow = ''

            if (_.isEmpty(this.ticket_code)) {
                this.msgalertshow = 'Anda belum mengisi kode tiket'
                this.dialogalertshow = true
            } else {
                if (_.isEmpty(this.ticket_name)) {
                    this.msgalertshow = 'Anda belum mengisi nama tiket'
                    this.dialogalertshow = true
                } else {
                    var prm = {}
                    prm = {
                        "code": this.ticket_code,
                        "name": this.ticket_name,
                    }
                    // console.log(prm);

                    this.$store.dispatch("ticketdispenser/add", prm)
                }
            }
        },

        // hapus
        deleteTicketDispenser(data) {
            // console.log(data)
            this.xid = data.ticketBoothID
            // console.log(data);
            var xdata = {
                id: data.ticketBoothID,
            }
            // console.log(data);
            this.$store.commit("ticketdispenser/update_selected_ticketdispenser", xdata)
            this.msgalert = "Yakin, mau hapus tiket dengan code " + data.ticketBoothCode + "  ?"
            this.dialogdeletealert = true
        },

        closeDeleteAlert() {
            this.$store.dispatch("ticketdispenser/delete", {
                id: this.$store.state.ticketdispenser.selected_ticketdispenser.ticketBoothID,
                // xname: this.$store.state.specialist.selected_specialist.name
            })
            this.dialogdeletealert = false
        },

        // hover biru2
        isSelected(p) {
            return p.ticketBoothID == this.$store.state.ticketdispenser.selected_ticketdispenser.ticketBoothID
        },
        selectMe(selected) {
            this.$store.commit("ticketdispenser/update_selected_ticketdispenser", selected)
            this.$store.commit("ticketdispenser/update_last_id", selected.ticketBoothID)
        },
        // edit
        editTicketDispenser(data) {
         
            this.selectMe(data)

            this.isEditForm = true

            this.ticket_code = data.ticketBoothCode
            this.ticket_name = data.ticketBoothName

            this.xid = data.ticketBoothID

            this.$store.commit("ticketdispenser/update_act", 'edit')
            this.$store.commit("ticketdispenser/update_dialog_form_ticket_dispenser", true)
        },

        // proses edit
        updateFormTicketDispenser() {

            if (_.isEmpty(this.ticket_code)) {
                this.msgalertshow = 'Anda belum mengisi kode tiket'
                this.dialogalertshow = true
            } else {
                if (_.isEmpty(this.ticket_name)) {
                    this.msgalertshow = 'Anda belum mengisi nama tiket'
                    this.dialogalertshow = true
                } else {
                    this.$store.dispatch("ticketdispenser/edit", {
                        "id": this.xid,
                        "code": this.ticket_code,
                        "name": this.ticket_name
                    })
                }
            }
        },

        // alert
        updateAlert_success(val) {
            this.$store.commit("ticketdispenser/update_alert_success", val)
        },

        // search
        thr_search: _.debounce(function () {
            this.$store.commit("ticketdispenser/update_last_id", -1)
            this.$store.dispatch("ticketdispenser/lookupbyname")
        }, 1000),
    },
    watch: {
        xsearch(val, old) {
            // console.log(val)
            this.xsearch = val
            this.thr_search()
        },
    }
}
</script>