<template>
    <v-layout>
        <!-- Alert dialog -->
        <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="deleteRow()">
                        Yakin lah
                    </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 -->
        <!-- Form Dialog -->
	    <v-dialog v-model="dialog_form" max-width="600px">
            <v-card>
                <v-card-title>
                    <span class="headline">Form Konversi Satuan</span>
                </v-card-title>
                <v-divider></v-divider>
                <v-progress-linear v-if="loading_save" :indeterminate="true"></v-progress-linear>
                <v-card-text class="pt-0 pb-0">
                    <v-layout wrap>
                        <v-flex pb-1 pa-2 xs12>
                            <v-autocomplete
                                label="Satuan Awal"
                                v-model="fromitemunitconvert"
                                @blur="checkItemconvertForm"
                                :items="xfromitemunit"
                                :search-input.sync="search_fromunit"
                                auto-select-first
                                no-filter
                                item-text="name"
                                return-object
                                :loading="get_data_status"
                                no-data-text="Pilih Satuan"
                                :error="error_fromitemunit_message !== ''"
                                :error-messages="error_fromitemunit_message"
                            >
                                <template slot="item" slot-scope="{ item }">
                                <v-list-tile-content>
                                    <v-list-tile-title v-text="item.name"></v-list-tile-title>
                                </v-list-tile-content>
                                </template>
                            </v-autocomplete>  
            
                            </v-flex>
                            <v-flex pb-1 pa-2 xs12>
                                <v-autocomplete
                                label="Satuan Akhir"
                                v-model="toitemunitconvert"
                                @blur="checkItemconvertForm"
                                :items="xtoitemunit"
                                :search-input.sync="search_tounit"
                                auto-select-first
                                no-filter
                                item-text="name"
                                return-object
                                :loading="get_data_status"
                                no-data-text="Pilih Satuan"
                                :error="error_toitemunit_message !== ''"
                                :error-messages="error_toitemunit_message"
                            >
                                <template slot="item" slot-scope="{ item }">
                                <v-list-tile-content>
                                    <v-list-tile-title v-text="item.name"></v-list-tile-title>
                                </v-list-tile-content>
                                </template>
                            </v-autocomplete>  
                            </v-flex>
                            <v-flex pb-1 pa-2 xs12>
                                <v-text-field
                            outline
                            v-model="Amount"
                            label="Nilai Konversi"
                            :disabled="amount_input_disable" 
                            @blur="checkItemconvertForm"
                            :error="error_amount_message !== ''"
                            :error-messages="error_amount_message"
                            ></v-text-field>
                            </v-flex>
                            
                  
                    </v-layout>
                </v-card-text>
                <v-divider></v-divider>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn 
                    color="error" 
                    flat 
                    @click="closeDialog()"
                    :disabled="loading_save"
                    >Tutup</v-btn
                    >
                    <v-btn 
                    v-if="xact === 'add'" 
                    color="primary" 
                    :disabled="loading_save" 
                    @click="saveFormItemconvert()"
                    >Simpan</v-btn
                    >
                    <v-btn
                    v-if="xact === 'edit'"
                    color="primary"
                    :disabled="loading_save"
                    @click="saveEdit()"
                    >Simpan Perubahan</v-btn
                    >                            
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End Form Dialog -->

        <v-flex xs12>
            <v-card>
	            <v-toolbar color="blue lighten-3" dark scroll-off-screen scroll-target="#scrolling-techniques">

                    <v-toolbar-title class="headline font-weight-bold">Konversi Satuan</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn class="text-xs-right" @click="openForm()" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                </v-toolbar>

                <!-- Data Tabel -->
                <div id="scrolling-techniques" class="scroll-y" :style="{ 'height': windowHeight }">
                    <v-card elevation="0">
                        <v-card-text>
                            <v-layout row>
                                <v-flex xs3>
                                    <v-text-field
                                    v-model="xsearch"
                                    label="Cari"
                                    placeholder="Ketikkan Satuan"
                                    outline
                                    hide-details
                                    ></v-text-field>
                                </v-flex>
                            </v-layout>
                        </v-card-text>
                    </v-card>
                    <v-divider></v-divider>
                    <v-data-table
                        :headers="headers"
                        :items="vsitemunitconvert"
                        :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 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 && 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-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.FromItemUnitName}}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.ToItemUnitName}}</td>
                            <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Amount}}</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="openEdit(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="confirmDelete(props.item)">delete</v-icon>
                                        </template>
                                        <span>Delete</span>
                                </v-tooltip>
                                <!-- <v-toolbar flat dense>
                                    <v-btn @click="openEdit(props.item)" small color="primary" dark>ubah <v-icon small right dark>edit</v-icon></v-btn>
                                    <v-divider vertical></v-divider>
                                    <v-btn @click="confirmDelete(props.item)" small color="danger" dark>hapus <v-icon small right dark>clear</v-icon></v-btn>
                                </v-toolbar> -->
                            </td>
                        </template>
                    </v-data-table>
                </div>
                <!-- End Data Tabel -->
                <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.outerHeight - ((38/100)*window.outerHeight) + "px",
            ItemName: '',
            FromItemUnitName: '',
            ToItemUnitName: '',
            Amount: 0,
            xid: 0,
            search_fromunit:'',
            search_tounit:'',
            id_fromitemunit: 0,
            id_toitemunit: 0,
            error_itemunitconvert_form:false,
            error_item_message:'',
            error_fromitemunit_message:'',
            error_toitemunit_message:'',
            error_amount_message: '',
            msgalert: '',
            dialogdeletealert: false,
            clr: "success",
            amount_input_disable: false,
            statusEdit: false,
            headers: [
                {
                    text: "SATUAN AWAL",
                    align: "left",
                    sort: true,
                    sortable: true,
                    value: "FromItemUnitName",
                    width: "25%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "SATUAN AKHIR",
                    align: "left",
                    sort: true,
                    sortable: true,
                    value: "ToItemUnitName",
                    width: "25%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "NILAI KONVERSI",
                    align: "left",
                    sort: true,
                    sortable: true,
                    value: "Amount",
                    width: "25%",
                    class: "pa-2 blue lighten-3 white--text"
                },
                {
                    text: "AKSI",
                    align: "center",
                    sort: false,
                    sortable: false,
                    value: "mr",
                    width: "10%",
                    class: "pa-2 blue lighten-3 white--text"
                }
            ]
        };
    },
    mounted() {
        this.$store.dispatch("itemunitconvert/search")
        // this.$store.dispatch("itemunitconvert/searchitemx")
        
    },
    computed: {
        get_data_status: {
            get() {
                return this.$store.state.itemunitconvert.get_data_status
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_get_data_status", val)
            }
        },
        dialog_form: {
            get() {
                return this.$store.state.itemunitconvert.dialog_form
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_dialog_form", val)
            }
        },
        vsitemunitconvert() {
            return this.$store.state.itemunitconvert.itemunitconvert
        },
        isLoading() {
            return this.$store.state.itemunitconvert.search_status == 1
        },
        curr_page: {
            get() {
                return this.$store.state.itemunitconvert.current_page
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_current_page", val)
                this.$store.commit("itemunitconvert/update_last_id", -1)
                this.$store.dispatch("itemunitconvert/search")
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.itemunitconvert.total_itemunitconverts
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_total_itemunitconverts", val)
            }
        },
        pagination: {
            get() {
                return this.$store.state.itemunitconvert.pagination
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_pagination", val)
            }
        },
        xsearch: {
            get() {
                return this.$store.state.itemunitconvert.x_search
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_x_search", val)
            }
        },
        xitems() {
            return this.$store.state.itemunitconvert.itemx
        },
        itemconvert: {
            get() {
                // console.log("cek item_convert", this.$store.state.itemunitconvert.item_convert);
                return this.$store.state.itemunitconvert.item_convert
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_item_convert", val)
                this.$store.dispatch("itemunitconvert/fromitemunit")
                this.$store.dispatch("itemunitconvert/toitemunit")
            }
        },
        xfromitemunit:{
            // console.log("xfromitemnunit cek", this.$store.state.itemunitconvert.fromitemunits);
            get() {
                return this.$store.state.itemunitconvert.fromitemunits
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_fromitemunits",val)
            }
        },
        fromitemunitconvert:{
            get() {
                return this.$store.state.itemunitconvert.fromitemunit_convert
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_fromitemunit_convert",val)
                this.amount_input_disable = false
                this.Amount = 0
                if(!_.isEmpty(this.toitemunitconvert) && parseInt(this.toitemunitconvert.id) > 0){
                    let fromunit = val.id
                    let tounit = this.toitemunitconvert.id
                    if(parseInt(fromunit) == parseInt(tounit)){
                        this.amount_input_disable = true
                        this.Amount = 1
                    }
                }
            }
        },
        xtoitemunit:{
            get() {
                return this.$store.state.itemunitconvert.toitemunits
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_toitemunits",val)
            }
        },
        toitemunitconvert:{
            get() {
                return this.$store.state.itemunitconvert.toitemunit_convert
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_toitemunit_convert",val)
                this.amount_input_disable = false
                this.Amount = 0
                if(!_.isEmpty(this.fromitemunitconvert) && parseInt(this.fromitemunitconvert.id) > 0){
                    let fromunit = this.fromitemunitconvert.id
                    let tounit = val.id
                    if(parseInt(fromunit) == parseInt(tounit)){
                        this.amount_input_disable = true
                        this.Amount = 1
                    }
                }
            }
        },
        xact: {
            get() {
                return this.$store.state.itemunitconvert.act
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_act", val)
            }
        },
        loading_save: {
            get() {
                return this.$store.state.itemunitconvert.loading_save
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_loading_save", val)
            },
        },
        snackbar: {
            get() {
                return this.$store.state.itemunitconvert.alert_success;
            },
            set(val) {
                this.$store.commit("itemunitconvert/update_alert_success", val);
            },
        },
        msgsnackbar() {
            return this.$store.state.itemunitconvert.msg_success;
        }
    },
    methods: {
        openForm() {
            this.xact = 'add'
            this.$store.commit("itemunitconvert/update_last_id", -1)
            this.itemconvert = ""
            this.fromitemunitconvert = ""
            this.toitemunitconvert = ""
            this.Amount = 0;

            // console.log("cek amount open form", this.Amount);
            this.dialog_form = true
            this.statusEdit = false
        },
        isSelected(p) {
            return p.id == this.$store.state.itemunitconvert.selected_itemunitconvert.id
        },
        selectMe(selected) {
            this.$store.commit("itemunitconvert/update_selected_itemunitconvert", selected)
            this.$store.commit("itemunitconvert/update_last_id", selected.id)
        },
        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("itemunitconvert/search")
            }
        },
        thr_search: _.debounce(function () {
            this.$store.dispatch("itemunitconvert/search")
        }, 1000),
        checkItemconvertForm(){
            this.resetErrors()
            this.error_itemunitconvert_form = false

            if(this.fromitemunitconvert == undefined) {
                    return
            }
            if(Object.keys(this.fromitemunitconvert).length === 0){
                this.error_fromitemunit_message = 'Pilih salah satu satuan awal'
                this.error_itemunitconvert_form = true
            }

            if(this.toitemunitconvert == undefined) {
                    return
            }
            if(Object.keys(this.toitemunitconvert).length === 0){
                this.error_toitemunit_message = 'Pilih salah satu satuan akhir'
                this.error_itemunitconvert_form = true
            }
            
            if(this.Amount === ''){
                this.error_amount_message = 'Anda belum mengisi'
                this.error_itemunitconvert_form = true
            }
        },
        saveFormItemconvert(){
            this.resetErrors()
            this.checkItemconvertForm()
            if(this.fromitemunitconvert == undefined) {
                    return
            }
            if(this.toitemunitconvert == undefined) {
                    return
            }
            let prm = {}
            prm.id_item = this.itemconvert.id_item
            prm.id_fromitemunit = this.fromitemunitconvert.id
            prm.id_toitemunit = this.toitemunitconvert.id
            prm.amount = this.Amount

            if(!this.error_itemunitconvert_form)
            this.$store.dispatch("itemunitconvert/save",prm)
        },
        resetErrors(){
            this.error_item_message = ''
            this.error_fromitemunit_message = ''
            this.error_toitemunit_message = ''
            this.error_amount_message = ''
        },
        resetItemunitconvert(){
            this.Amount = 0
            this.ItemName = ''
            this.FromItemUnitName = ''
            this.ToItemUnitName = ''
        },
        closeDialog() {
            this.Amount = 0;
            this.resetErrors();
            this.dialog_form = false;
        },
        // edit data
        openEdit(item){
            this.xact = 'edit'
            this.amount_input_disable = true
            this.$store.commit("itemunitconvert/update_last_id", item.id)
            this.selectMe(item)
            this.xid = item.id
            this.resetItemunitconvert()

           

            this.xfromitemunit = [
                {
                    id: item.FromItemUnitID,
                    name: item.FromItemUnitName

                }
            ];
            this.fromitemunitconvert ={
                    id: item.FromItemUnitID,
                    name: item.FromItemUnitName

                }

            this.xtoitemunit = [
                {

                    id: item.ToItemUnitID,
                    name: item.ToItemUnitName
                }
            ];
            this.toitemunitconvert = {

            id: item.ToItemUnitID,
            name: item.ToItemUnitName
            }

            this.Amount = item.Amount

            this.dialog_form = true
            this.statusEdit = true
        },
        saveEdit(){
            this.resetErrors()
            this.checkItemconvertForm()
            if(this.fromitemunitconvert == undefined) {
                    return
            }
            if(this.toitemunitconvert == undefined) {
                    return
            }
            let prm = {}
            prm.id = this.xid
            prm.id_fromitemunit = this.fromitemunitconvert.id
            prm.id_toitemunit = this.toitemunitconvert.id
            prm.amount = this.Amount

            if(!this.error_itemunitconvert_form)
            this.$store.dispatch("itemunitconvert/saveEdit",prm)
        },
        confirmDelete(data){
            this.xid = data.id
            this.selectMe(data)
            this.msgalert = "Yakin, mau hapus konversi satuan  ?"
            this.dialogdeletealert = true
        },
        deleteRow(){
            this.dialogdeletealert = false
            this.$store.commit("itemunitconvert/update_last_id", -1)
            let prm = {
                id:this.xid
            }
            this.$store.dispatch("itemunitconvert/delete",prm)
        },

        thr_search_fromunit: _.debounce(function() {
            this.$store.dispatch("itemunitconvert/fromitemunit", this.search_fromunit);
        }, 2000),
        thr_search_tounit: _.debounce(function() {
            this.$store.dispatch("itemunitconvert/toitemunit", this.search_tounit);
        }, 2000),
    },
    watch: {
        xsearch(val, old) {
            this.xsearch = val
            this.thr_search()
        },
        search_fromunit(val, old) {
        if (val == old) return;
        if (!val) return;
        if (val.length < 1) return;
        this.thr_search_fromunit();
        },
        search_tounit(val, old) {
        if (val == old) return;
        if (!val) return;
        if (val.length < 1) return;
        this.thr_search_tounit();
        },
        
    },
   
}
</script>