<template>
    <v-layout>

        <!-- 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 -->

        <!-- Alert dialog -->
        <v-dialog v-model="dialogdalert" 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="dialogdalert = false">
                        Tutup
                    </v-btn>
                    <v-btn color="primary" flat @click="saveForm()">
                        Yakin lah
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <!-- End alert dialog -->

        <v-flex xs12>
            <v-card class="mb-2" color="white">
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>DEFAULT SETTING</v-toolbar-title>
                </v-toolbar>
                <v-divider></v-divider>
                <v-layout row class="scroll-container" style="max-height:645px;overflow: auto;">
                    <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                        <v-card class="mb-2 pa-2">
                            <v-card-text class="pt-2 pb-0">
                                <v-layout row wrap>
                                    <v-flex xs12 pa-1>
                                        <v-autocomplete class="mb-1" placeholder="Ketikkan minimal 3 karakter"
                                            :items="companys" v-model="selected_company" :loading="loading_company"
                                            :search-input.sync="search_company" auto-select-first item-text="M_CompanyName"
                                            item-value="M_CompanyID" label="Pilih Kel. Pelanggan" return-object outline
                                            clearable no-data-text="Pilih Company">
                                            <template slot="item" slot-scope="{ item }">
                                                <v-list-tile-content>
                                                    <v-list-tile-title v-text="item.M_CompanyName"></v-list-tile-title>
                                                </v-list-tile-content>
                                            </template>
                                        </v-autocomplete>
                                        <p v-if="checkError('requireselected_company')" class="error pl-2 pr-2"
                                            style="color:#fff">Pilih Company dulu</p>
                                    </v-flex>
                                </v-layout>

                                <v-layout row wrap>
                                    <v-flex xs12 pa-1>
                                        <v-autocomplete class="mb-1" placeholder="Ketikkan minimal 3 karakter" :items="mous"
                                            v-model="selected_mou" :loading="loading_mou" :search-input.sync="search_mou"
                                            auto-select-first item-text="M_MouName" item-value="M_MouID"
                                            label="Pilih Agremeent" return-object outline clearable>
                                            <template slot="item" slot-scope="{ item }">
                                                <v-list-tile-content>
                                                    <v-list-tile-title v-text="item.M_MouName"></v-list-tile-title>
                                                </v-list-tile-content>
                                            </template>
                                        </v-autocomplete>
                                        <p v-if="checkError('requireselected_mou')" class="error pl-2 pr-2"
                                            style="color:#fff">Pilih Agreement dulu</p>
                                    </v-flex>
                                </v-layout>
                                <v-layout row wrap>
                                    <v-flex xs12 pa-1>
                                        <v-autocomplete v-model="selectedLocation" :items="locationList" :loading="false"
                                            item-text="M_LocationName" outline label="Lokasi" placeholder="Lokasi"
                                            return-object></v-autocomplete>
                                    </v-flex>
                                </v-layout>

                                <v-layout>
                                    <v-flex xs12 pa-1>
                                        <v-text-field class="xs6 ma-1" label="Harga" v-model="price" type="number" outline
                                            hide-details>
                                        </v-text-field>
                                        <p v-if="checkError('requirexprice')" class="error pl-2 pr-2" style="color:#fff">
                                            Harga belum diisi</p>
                                    </v-flex>
                                </v-layout>


                                <div class="text-md-right mt-3">
                                    <v-btn color="primary" dark class="white--text ma-1" @click="openAddForm()">
                                        SIMPAN
                                    </v-btn>
                                </div>

                            </v-card-text>
                        </v-card>
                    </v-flex>
                </v-layout>

            </v-card>
        </v-flex>


    </v-layout>
</template>

<style scoped>
table.v-table tbody td,
table.v-table tbody th {
    height: 40px;
}

table.v-table thead tr {
    height: 40px;
}

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

<script>
module.exports = {
    data() {
        return {
            dialogdalert: false,
            msgalert: "",
            clr: "success",
            search_mou: "",
            search_company: ""
        };
    },
    mounted() {
        this.$store.dispatch("setting/get_data")
        this.$store.dispatch("setting/getLocationList")
    },
    computed: {
        price: {
            get() {
                return this.$store.state.setting.price
            },
            set(val) {
                this.$store.commit("setting/update_price", val)
            }
        },
        mous: {
            get() {
                return this.$store.state.setting.mous
            },
            set(val) {
                this.$store.commit("setting/update_mous", val)
            }
        },
        selected_mou: {
            get() {
                return this.$store.state.setting.selected_mou
            },
            set(val) {
                this.$store.commit("setting/update_selected_mou", val)
            }
        },
        loading_mou() {
            return this.$store.state.setting.loading
        },
        snackbar: {
            get() {
                return this.$store.state.setting.alert_success
            },
            set(val) {
                this.$store.commit("setting/update_alert_success", val)
            }
        },
        selectedLocation: {
            get() {
                return this.$store.state.setting.selectedLocation
            },
            set(val) {
                this.$store.commit("setting/update_selectedLocation", val)
            }
        },
        msgsnackbar() {
            return this.$store.state.setting.msg_success
        },
        dialog_error: {
            get() {
                return this.$store.state.setting.alert_error
            },
            set(val) {
                this.$store.commit("setting/update_alert_error", val)
            },
        },
        msgError() {
            return this.$store.state.setting.error_message
        },
        companys() {
            return this.$store.state.setting.companys
        },
        locationList() {
            return this.$store.state.setting.locationList
        },
        selected_company: {
            get() {
                return this.$store.state.setting.selected_company
            },
            set(val) {
                this.$store.commit("setting/update_selected_company", val)
                this.selected_mou = {}
                this.mous = []
            }
        },
        loading_company() {
            return this.$store.state.setting.loading
        }
    },
    methods: {
        reloadPage() {
            window.location.reload();
        },
        thr_search_mou: _.debounce(function () {
            this.$store.dispatch("setting/searchmou", {
                companyid: this.selected_company.M_CompanyID,
                search: this.search_mou
            })
        }, 1000),
        thr_search_company: _.debounce(function () {
            this.$store.dispatch("setting/searchcompany", {
                search: this.search_company
            })
        }, 1000),
        checkError(value) {
            var errors = this.$store.state.setting.errors
            if (errors.includes(value)) {
                return true
            } else {
                return false
            }
        },
        openAddForm() {
            this.$store.commit("setting/update_errors", [])
            var errors = this.$store.state.setting.errors

            if (_.isEmpty(this.selected_mou)) {
                errors.push("requireselected_mou")
            }
            /*if (parseInt(this.price) == 0) {
                errors.push("requirexprice")
            }*/
            if (_.isEmpty(this.selected_company)) {
                errors.push("requireselected_company")
            }

            if (errors.length === 0) {
                this.msgalert = "Yakin, Mau Default Setting Klinik " + "(" + this.selected_mou.M_MouName + ")" + ", dengan lokasi " + this.selectedLocation.M_LocationName + "  ?"
                this.dialogdalert = true
            } else {
                this.dialogdalert = false
            }
        },
        saveForm() {
            this.dialogdalert = false
            let prm = {
                mouid: this.selected_mou.M_MouID,
                price: this.price,
                locationid: this.selectedLocation.M_LocationID,
                stationid: this.selectedLocation.M_LocationT_SampleStationID
            }

            console.log(prm)
            this.$store.dispatch('setting/savesetting', prm)
        }
    },
    watch: {
        search_company(val, old) {
            if (val == old) return
            if (!val) return

            if (val.length > 2) {
                this.$store.dispatch("setting/searchcompany", {
                    search: this.search_company
                })
                this.thr_search_company()
            }
        },
        search_mou(val, old) {
            if (val == old) return
            if (!val) return

            if (val.length > 2) {
                if (this.selected_company === undefined) {
                    return
                } else {
                    this.$store.dispatch("setting/searchmou", {
                        companyid: this.selected_company.M_CompanyID,
                        search: this.search_mou
                    })
                    this.thr_search_mou()
                }
            }
        }
    }
}
</script>