<template>
    <v-layout>

        <!-- 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="error" flat @click="dialogdeletealert = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" @click="closeDeleteAlert()">
                        Yakin lah
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- alert -->

        <!-- form -->
        <v-dialog v-model="dialogunit" persistent max-width="600px">
            <v-card>
                <v-card-title>
                    <span class="title">Form Unit</span></v-card-title>
                <v-card-text class="pt-0 pb-0">
                    <v-form ref="formunit" v-model="valid" lazy-validation>
                        <v-layout wrap>
                            <v-flex xs12>
                                <v-text-field v-model="xname" label="Nama*" :rules="xnameRules" required></v-text-field>
                            </v-flex>
                            <v-flex>
                                <p v-for="(xerror, idx) in xerrors" class="error pl-2 pr-2" style="color:#fff">
                                    {{ xerror.msg }}</p>
                            </v-flex>
                        </v-layout>
                </v-card-text>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="error" flat @click="updateDialogFormUnit()">Tutup</v-btn>
                    <v-btn v-if="xact === 'new'" color="primary" @click="saveFormUnit()">Simpan</v-btn>
                    <v-btn v-if="xact === 'edit'" color="primary" @click="updateFormUnit()">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="openFormUnit(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 lighten-3" dark scroll-off-screen scroll-target="#scrolling-techniques">

                    <v-toolbar-title class="headline font-weight-bold">Master Data Unit</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn class="text-xs-right" @click="openFormUnit(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-flex xs3>
                                    <v-text-field v-model="xsearch" label="Search"
                                        placeholder="Cari kode atau nama unit" outline hide-details></v-text-field>
                                </v-flex>
                            </v-layout>
                        </v-card-text>
                    </v-card>
                    <!-- search -->
                    <v-divider></v-divider>

                    <v-data-table :headers="headers" :items="vitemunits" :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"
                                    :align="header.align"
                                    :class="header.class">
                                    <!-- <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="grey lighten-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 && 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>
                        <template v-slot:items="props">
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.ItemUnitCode }}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">{{ props.item.ItemUnitName }}</td>
                            <td class="text-xs-center pa-2" v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                @click="selectMe(props.item)">
                                <v-tooltip bottom>
                                        <template v-slot:activator="{ on, attrs }">
                                            <v-icon v-bind="attrs" v-on="on" small class="ml-3" @click="editUnit(props.item)">edit</v-icon>
                                        </template>
                                        <span>Edit</span>
                                </v-tooltip>
                                <v-tooltip bottom>
                                        <template v-slot:activator="{ on, attrs }">
                                            <v-icon v-bind="attrs" v-on="on" small class="ml-3" @click="deleteUnit(props.item)">delete</v-icon>
                                        </template>
                                        <span>Delete</span>
                                </v-tooltip>
                                <!-- <v-toolbar flat dense>
                                    <v-btn @click="editUnit(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="deleteUnit(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 }}" tiada 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 {
            windowHeight: window.innerHeight - 300 + 'px',
            page: 1,
            dialogdeletealert: false,
            msgalert: "",
            xname: '',
            valid: false,
            xid: 0,
            isDefault: false,
            xnameRules: [
                v => !!v || 'Nama harus diisi'
            ],
            headers: [{
                text: "KODE",
                align: "left",
                sortable: false,
                sort: true,
                value: "ItemUnitCode",
                width: "10%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "NAMA",
                align: "left",
                sortable: false,
                sort: true,
                value: "ItemUnitName",
                width: "80%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "AKSI",
                align: "center",
                sortable: false,
                value: "mr",
                width: "10%",
                class: "pa-2 blue lighten-3 white--text"
            }
            ],

        };
    },
    mounted() {
        // ambil data
        this.$store.commit("unit/update_last_id", -1)
        this.$store.dispatch("unit/lookupbyname")
    },
    computed: {
        // list data masuk ke objek vkelurahans
        vitemunits() {
            return this.$store.state.unit.units
        },

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

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

        xtotal_page: {
            get() {
                return this.$store.state.unit.total_units
            },
            set(val) {
                this.$store.commit("unit/update_total_units", val)
            }
        },

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

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

        dialogunit() {
            return this.$store.state.unit.dialog_form_unit
        },

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

        // filter
        xtotalunits() {
            return this.$store.state.unit.total_units
        },
        xtotalfilterunits() {
            return this.$store.state.unit.total_filter_units
        },
    },
    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("unit/update_last_id", -1)
                this.$store.dispatch("unit/lookupbyname")
            }
        },

        // tutup dialog
        updateDialogFormUnit() {
            this.$store.commit("unit/update_dialog_form_unit", false)
        },

        // open form
        openFormUnit() {
            console.log('open dialog');
            this.xname = ""
            this.isdefault = "N"
            this.$refs.formunit.reset()
            this.$refs.formunit.resetValidation()
            this.$store.commit("unit/update_act", 'new')
            this.$store.commit("unit/update_dialog_form_unit", true)
        },

        // save form
        saveFormUnit() {
            if (this.$refs.formunit.validate()) {
                this.$store.dispatch("unit/add", {
                    unit_name: this.xname
                })

            }
        },

        // hapus
        deleteUnit(data) {
            this.xid = data.ItemUnitID
            console.log(data);
            var xdata = {
                id: data.ItemUnitID,
                name: data.ItemUnitName,
            }
            this.$store.commit("unit/update_selected_unit", xdata)
            this.msgalert = "Yakin, mau hapus item unit " + data.ItemUnitName + "  ?"
            this.dialogdeletealert = true
        },

        closeDeleteAlert() {
            this.$store.commit("unit/update_last_id", -1);
            this.$store.dispatch("unit/delete", {
                id: this.$store.state.unit.selected_unit.id,
                name: this.$store.state.unit.selected_unit.name,
            })
            this.dialogdeletealert = false
        },

        // hover biru2
        isSelected(p) {
            return p.ItemUnitID == this.$store.state.unit.selected_unit.ItemUnitID
        },
        selectMe(selected) {
            this.$store.commit("unit/update_selected_unit", selected)
            this.$store.commit("unit/update_last_id", selected.ItemUnitID)
        },
        // edit
        editUnit(data) {
            this.selectMe(data)

            this.xid = data.ItemUnitID
            // this.xcode = data.code
            this.xname = data.ItemUnitName
            // this.isdefault = data.T_SpecialistIsDefault === 'N' ? false : true
            this.$store.commit("unit/update_act", 'edit')
            this.$store.commit("unit/update_dialog_form_unit", true)
        },

        // proses edit
        updateFormUnit() {
            if (this.$refs.formunit.validate()) {
                this.$store.dispatch("unit/edit", {
                    id: this.xid,
                    unit_name: this.xname
                })
            }
        },

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

        // search
        thr_search: _.debounce(function () {
            this.$store.commit("unit/update_last_id", -1)
            this.$store.dispatch("unit/lookupbyname")

        }, 1000)
    },
    watch: {
        xsearch(val, old) {
            // console.log(val)
            this.xsearch = val
            this.thr_search()

        }
    }
}
</script>