<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="primary" flat @click="dialogdeletealert = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" flat @click="deleteRow()">
                        Yakin lah
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert dialog -->
        <!-- 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 -->
        <!-- 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 -->
        <!-- Dialog -->
        <v-dialog v-model="dialog_form" persistent max-width="600px">
            <v-card>
                <v-form ref="formitemgroup" lazy-validation>
                    <v-card-title>
                        <span class="title">Form Mater Data Item</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-2">
                        <v-layout wrap>
                        <v-flex xs12>
                            <v-text-field
                                v-model="xcode"
                                label="Kode*"
                                :error="code_error_message !== ''"
                                :error-messages="code_error_message" @blur="codeValidation"
                                required
                            ></v-text-field>
                            <v-text-field
                                v-model="xname"
                                label="Nama*"
                                :error="name_error_message !== ''"
                                :error-messages="name_error_message" @blur="nameValidation"
                                required
                            ></v-text-field>
                            <v-text-field
                                v-model="xqty"
                                label="Default Qty*"
                                :error="qty_error_message !== ''"
                                :error-messages="qty_error_message" @blur="qtyValidation"            
                                type="number"
                                required
                            ></v-text-field>
                            <v-layout row wrap>
                                <v-flex xs12>
                                    <v-select v-model="xunit" :items="vsatuan" item-text="M_SatuanName"
                                        :error="unit_error_message !== ''"
                                        :error-messages="unit_error_message" @blur="unitValidation"
                                        item-value="M_SatuanID" return-object label="Default Satuan*" required>
                                    </v-select>
                                </v-flex>
                            </v-layout>

                            <v-layout row wrap>
                            <!-- Default Show -->
                                <v-flex xs6>
                                    <v-radio-group v-model="xshow">
                                        <template v-slot:label>
                                            <div>Tampilkan Otomatis*</div>
                                        </template>
                                        <v-radio value="Y">
                                            <template v-slot:label>
                                            <div>Ya</div>
                                            </template>
                                        </v-radio>
                                        <v-radio value="N">
                                            <template v-slot:label>
                                            <div>Tidak</div>
                                            </template>
                                        </v-radio>
                                    </v-radio-group>
                                </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="blue darken-1" flat @click="dialog_form = false" :disabled="loading_save"
                        >Tutup</v-btn
                        >
                        <v-btn
                        v-if="xact === 'add'"
                        color="blue darken-1"
                        :disabled="loading_save"
                        flat
                        @click="saveAdd()"
                        >Simpan</v-btn
                        >
                        <v-btn
                        v-if="xact === 'edit'"
                        color="blue darken-1"
                        :disabled="loading_save"
                        flat
                        @click="saveEdit()"
                        >Simpan Perubahan</v-btn
                        >
                    </v-card-actions>
                </v-form>
            </v-card>
        </v-dialog>
        <!-- End Dialog -->

        <v-flex xs12>
            <v-card>
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>Master Data Item</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <div>
                        <v-btn icon @click="openAddForm()"><v-icon>library_add</v-icon></v-btn>
                    </div>
                </v-toolbar>
                <v-layout row style="background:white;padding-top:5px;" justify-left>
                    <v-flex xs4 pl-1>
                        <v-text-field class="ma-1" label="Search" placeholder="Cari Kode atau Nama Item" outline
                            v-model="xsearch" hide-details></v-text-field>
                    </v-flex>
                </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="vconsumable" :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.rownumber }}</td>
                                    <td class="text-xs-left pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.M_ConsumableCode }}</td>
                                    <td class="text-xs-left pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.M_ConsumableName }}</td>
                                    <td class="text-xs-center pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.M_ConsumableDefaultShow }}</td>
                                    <td class="text-xs-center pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.M_ConsumableDefaultQty }}</td>  
                                    <td class="text-xs-left pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.M_SatuanName }}</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)" 
                                                    color="primary">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)" 
                                                    color="error">clear
                                                </v-icon>
                                            </template>
                                            <span>Hapus</span>
                                        </v-tooltip>
                                    </td>
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>

                    <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>
                </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;
}

/* 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 {
            dialogdeletealert: false,
            msgalert: "",
            xid: "",
            xcode: "",
            xname: "",
            xqty: "",
            xunit: "",
            xshow: "N",
            code_error_message: "",
            name_error_message: "",
            qty_error_message: "",
            unit_error_message: "",
            clr: "success",
            headers: [{
                text: "NO",
                align: "center",
                sortable: false,
                value: "mr",
                width: "5%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "KODE",
                align: "center",
                sortable: false,
                value: "mr",
                width: "15%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "NAMA",
                align: "center",
                sortable: false,
                value: "mr",
                width: "25%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "TAMPILAN OTOMATIS",
                align: "center",
                sortable: false,
                value: "mr",
                width: "10%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "DEFAULT QTY",
                align: "center",
                sortable: false,
                value: "mr",
                width: "15%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "DEFAULT SATUAN",
                align: "center",
                sortable: false,
                value: "mr",
                width: "15%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "AKSI",
                align: "center",
                sortable: false,
                value: "mr",
                width: "15%",
                class: "pa-2 blue lighten-3 white--text"
            }
            ]
        };
    },
    mounted() {
        this.$store.dispatch("consumable/searchconsumable")
        this.$store.dispatch("consumable/get_satuan")
    },
    computed: {
        vconsumable() {
            return this.$store.state.consumable.consumables
        },
        isLoading() {
            return this.$store.state.consumable.search_status == 1
        },
        curr_page: {
            get() {
                return this.$store.state.consumable.current_page
            },
            set(val) {
                this.$store.commit("consumable/update_current_page", val)
                this.$store.commit("consumable/update_last_id", -1)
                this.$store.dispatch("consumable/searchconsumable")
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.consumable.total_consumable
            },
            set(val) {
                this.$store.commit("consumable/update_total_consumable", val)
            }
        },
        xsearch: {
            get() {
                return this.$store.state.consumable.x_search
            },
            set(val) {
                this.$store.commit("consumable/update_x_search", val)
            }
        },
        dialog_form: {
            get() {
                return this.$store.state.consumable.dialog_form
            },
            set(val) {
                this.$store.commit("consumable/update_dialog_form", val)
            },
        },
        xact: {
            get() {
                return this.$store.state.consumable.act
            },
            set(val) {
                this.$store.commit("consumable/update_act", val)
            }
        },
        loading_save: {
            get() {
                return this.$store.state.consumable.loading_save
            },
            set(val) {
                this.$store.commit("consumable/update_loading_save", val)
            },
        },
        vsatuan() {
            return this.$store.state.consumable.satuans
        },
        snackbar: {
            get() {
                return this.$store.state.consumable.alert_success
            },
            set(val) {
                this.$store.commit("consumable/update_alert_success", val)
            }
        },
        msgsnackbar() {
            return this.$store.state.consumable.msg_success
        },
        dialog_error: {
            get() {
                return this.$store.state.consumable.alert_error
            },
            set(val) {
                this.$store.commit("consumable/update_alert_error", val)
            },
        },
        msgError() {
            return this.$store.state.consumable.error_message
        }
    },
    methods: {
        isSelected(p) {
            return p.M_ConsumableID == this.$store.state.consumable.selected_consumable.M_ConsumableID
        },
        selectMe(selected) {
            this.$store.commit("consumable/update_selected_consumable", selected)
            this.$store.commit("consumable/update_last_id", selected.M_ConsumableID)
        },
        thr_search: _.debounce(function () {
            this.$store.dispatch("consumable/searchconsumable")
        }, 1000),
        codeValidation() {
            if (!this.xcode) {
                this.code_error_message = "Anda belum mengisi"
            }
            if (this.xcode) {
                this.code_error_message = ""
            }
        },
        nameValidation() {
            if (!this.xname) {
                this.name_error_message = "Anda belum mengisi"
            }
            if (this.xname) {
                this.name_error_message = ""
            }
        },
        qtyValidation() {
            if (!this.xname) {
                this.qty_error_message = "Anda belum mengisi"
            }
            if (this.xname) {
                this.qty_error_message = ""
            }
        },
        unitValidation() {
            if (this.xunit.M_SatuanID < 1 || !this.xunit.M_SatuanID) {
                this.unit_error_message = "Anda belum mengisi"
            }
            if (this.xunit.M_SatuanID > 0 ) {
                this.unit_error_message = ""
            }
        },
        resetAllInput() {
            this.xcode = ""
            this.xname = ""
            this.xqty = ""
            this.xunit = ""
            this.code_error_message = ""
            this.name_error_message = ""
            this.qty_error_message = ""
            this.unit_error_message = ""
        },
        resetAllError() {
            this.code_error_message = ""
            this.name_error_message = ""
            this.qty_error_message = ""
            this.unit_error_message = ""
        },
        openAddForm() {
            this.dialog_form = true
            this.$store.commit("consumable/update_act", "add")
            this.xcode = ""
            this.xname = ""
            this.xqty = ""
            this.xunit = ""
            this.resetAllInput()
            this.$store.commit("consumable/update_last_id", -1)
        },
        saveAdd() {
            this.codeValidation()
            this.nameValidation()
            this.qtyValidation()
            this.unitValidation()

            if (this.code_error_message === "" &&
                this.name_error_message === "" &&
                this.qty_error_message === "" &&
                this.unit_error_message === "") 
                {
                    var prm = {
                        code: this.xcode,
                        name: this.xname,
                        defaultQty: this.xqty,
                        defaultShow: this.xshow,
                        satuanId: this.xunit.M_SatuanID
                    }
                    this.$store.dispatch("consumable/addconsumable", prm)
                }
        },
        openEdit(data) {
            this.selectMe(data)
            this.dialog_form = true
            this.xid = data.M_ConsumableID
            this.xcode = data.M_ConsumableCode
            this.xname = data.M_ConsumableName
            this.xqty = data.M_ConsumableDefaultQty
            this.xshow = data.M_ConsumableDefaultShow
            this.xunit = {
                M_SatuanID: data.M_ConsumableDefaultM_SatuanID
            }
            this.resetAllError() 
            this.$store.commit("consumable/update_act", "edit")
            this.$store.commit("consumable/update_last_id", data.M_ConsumableID)
        },
        saveEdit() {
            this.codeValidation()
            this.nameValidation()
            this.qtyValidation()
            this.unitValidation()
            if (this.code_error_message === "" &&
                this.name_error_message === "" &&
                this.qty_error_message === "" &&
                this.unit_error_message === "") 
                {
                    var prm = {
                        id: this.xid,
                        code: this.xcode,
                        name: this.xname,
                        defaultQty: this.xqty,
                        defaultShow: this.xshow,
                        satuanId: this.xunit.M_SatuanID
                    }
                    this.$store.dispatch("consumable/editconsumable", prm)
                }
        },
        confirmDelete(data) {
            this.dialogdeletealert = true
            this.xid = data.M_ConsumableID
            this.xname = data.M_ConsumableName
            this.msgalert = "Yakin, mau hapus " + data.M_ConsumableName + " ?"
            this.$store.commit("consumable/update_last_id", data.M_ConsumableID)
        },
        deleteRow() {
            this.$store.commit("consumable/update_last_id", -1)
            var prm = {
                id: this.xid,
                name: this.xname
            }
            this.$store.dispatch("consumable/deleteitem", prm)
            this.dialogdeletealert = false
        }
    },
    watch: {
        xsearch(val, old) {
            this.xsearch = val
            this.thr_search()
        }
    }
}
</script>