<template>
    <v-layout>
        <template>

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

        </template>


        <v-flex xs12>
            <v-card class="scroll-container" style="/*max-height:400px;overflow: auto;*/">
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>INSTRUMENT</v-toolbar-title>
                    <v-spacer></v-spacer>
                </v-toolbar>
                <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true">
                    {{msgsnackbar}}
                    <v-btn flat @click="updateAlert_success(false)">
                        Tutup
                    </v-btn>
                </v-snackbar>
                <v-layout row style="background:#bbdefb;padding-top:5px;" justify-left>
                    <v-list-tile>
                        <input type="text" v-model="xsearch" class="textinput" placeholder="Cari ..."/>
                        </v-list-tile-content>
                    </v-list-tile>
                </v-layout>
                <v-divider></v-divider>
                <div>

                    <v-layout row>
                        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                            <v-data-table :headers="headers" :items="vinstruments" :loading="isLoading" :pagination.sync="pagination" 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.description}}</td>
                                </template>
                                <template>
                                    <div class="text-xs-center">
                                        <v-pagination v-model="page" :length="15" :total-visible="1"></v-pagination>
                                    </div>
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>



                    <v-divider></v-divider>
                </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 {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                dialogdeletealert: false,
                msgalert: "",
                xid: 0,
                xsearch: "",
                items: [],
                name: '',
                scode: '',
                page: 1,
                headers: [{
                    text: "NAMA",
                    align: "center",
                    sortable: false,
                    value: "mr",
                    width: "10%",
                    class: "pa-2 blue lighten-3 white--text"
                }],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'id ASC',
                    totalItems: this.$store.state.instrument.total_filter_instruments
                }
            };
        },
        mounted() {
            this.$store.dispatch("instrument/lookup", {
                search: this.xsearch,
                all: 'Y'
            })
        },
        computed: {
            xact() {
                return this.$store.state.instrument.act
            },
            xerrors() {
                return this.$store.state.instrument.errors
            },
            xshowall() {
                return this.$store.state.instrument.show_all
            },
            vinstruments() {
                return this.$store.state.instrument.instruments
            },
            xtotalinstruments() {
                return this.$store.state.instrument.total_instruments
            },
            xtotalfilterinstruments() {
                return this.$store.state.instrument.total_filter_instruments
            },
            dialoginstrument() {
                return this.$store.state.instrument.dialog_form_instrument
            },
            snackbar: {
                get() {
                    return this.$store.state.instrument.alert_success
                },
                set(val) {
                    this.$store.commit("instrument/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.instrument.msg_success
            },
            lookupstatus() {
                return this.$store.state.instrument.lookup_instrument
            },
            xinstrumenttypes() {
                return this.$store.state.instrument.instrumenttypes
            },
            xinstrumenttype: {
                get() {
                    return this.$store.state.instrument.instrumenttype
                },
                set(val) {
                    this.$store.commit("instrument/update_instrumenttype", val)
                }

            },
            isLoading() {
                return this.$store.state.instrument.search_status == 1
            },
            xcities() {
                return this.$store.state.instrument.cities
            },
            cityaddress: {
                get() {
                    return this.$store.state.instrument.city_address
                },
                set(val) {
                    this.$store.commit("instrument/update_city_address", val)
                    this.$store.dispatch("instrument/getdistrict", this.$store.state.instrument.city_address)
                }
            },
            xdoctors() {
                return this.$store.state.instrument.doctors
            },
            doctor: {
                get() {
                    return this.$store.state.instrument.doctor
                },
                set(val) {
                    this.$store.commit("instrument/update_doctor", val)
                }
            },
            xdistricts() {
                return this.$store.state.instrument.districts
            },
            districtaddress: {
                get() {
                    return this.$store.state.instrument.district_address
                },
                set(val) {
                    this.$store.commit("instrument/update_district_address", val)
                    this.$store.dispatch("instrument/getkelurahan", this.$store.state.instrument.district_address)
                }
            },
            xkelurahans() {
                return this.$store.state.instrument.kelurahans
            },
            kelurahanaddress: {
                get() {
                    return this.$store.state.instrument.kelurahan_address
                },
                set(val) {
                    this.$store.commit("instrument/update_kelurahan_address", val)
                }
            }
        },
        methods: {
            updateShowAll(val) {
                this.$store.commit("instrument/update_show_all", val)
                this.$store.dispatch("instrument/lookup", {
                    search: this.xsearch,
                    all: this.xshowall
                })
            },
            searchInstrument() {
                this.$store.dispatch("instrument/lookup", {
                    search: this.xsearch,
                    all: 'Y'
                })
            },
            isSelected(p) {
                return p.id == this.$store.state.instrument.selected_instrument.id
            },
            subname(name) {
                var xname = name
                if (xname.length > 18) {
                    xname = xname.substring(0, 18) + '...'
                }
                return xname
            },
            selectMe(sc) {
                this.$store.commit("instrument/update_selected_instrument", sc)
                this.$store.dispatch("assay/lookup", {
                    id: this.$store.state.instrument.selected_instrument.id
                })
                this.$store.dispatch("convert/lookup", {
                    id: this.$store.state.instrument.selected_instrument.id
                })
            },
            updateDialogFormPerusahaan() {
                this.$store.commit("instrument/update_dialog_form_instrument", false)
            },
            openFormPerusahaan() {
                this.search_city = ''
                this.$store.commit("instrument/update_cities", [])
                this.cityaddress = {}
                this.search_doctor = ''
                this.$store.commit("instrument/update_doctors", [])
                this.doctor = {}
                this.$store.commit("instrument/update_districts", [])
                this.districtaddress = {}
                this.$store.commit("instrument/update_kelurahans", [])
                this.kelurahanaddress = {}
                this.instrumenttypenew = ""
                this.$refs.forminstrument.reset()
                this.$refs.forminstrument.resetValidation()
                this.$store.commit("instrument/update_act", 'new')
                this.$store.commit("instrument/update_dialog_form_instrument", true)


            },
            thr_search_city: _.debounce(function () {
                this.$store.dispatch("instrument/searchcity", this.search_city)
            }, 2000),
            thr_search_doctor: _.debounce(function () {
                this.$store.dispatch("instrument/searchdoctor", this.search_doctor)
            }, 2000),
            saveFormPerusahaan() {
                if (this.$refs.forminstrument.validate()) {
                    var newinstrumenttype = this.instrumenttypenew
                    if ((_.isEmpty(this.instrumenttypenew))) {
                        this.$store.dispatch("instrument/save", {
                            name: this.instrumentname,
                            address: this.instrumentaddress,
                            city: this.cityaddress.M_CityID,
                            doctor: this.doctor.M_DoctorID,
                            district: this.districtaddress.M_DistrictID,
                            kelurahan: this.kelurahanaddress.M_KelurahanID,
                            phone: this.instrumentphone,
                            email: this.instrumentemail,
                            pic: this.instrumentpic,
                            instrumenttypenew: this.instrumenttypenew,
                            instrumenttype: this.xinstrumenttype.M_InstrumentTypeID,
                            isdefault: this.isdefault === true ? "Y" : "N",
                            islabfrom: this.islabfrom === true ? "Y" : "N",
                            islabto: this.islabto === true ? "Y" : "N"
                        })
                    } else {
                        this.$store.dispatch("instrument/save", {
                            name: this.instrumentname,
                            address: this.instrumentaddress,
                            city: this.cityaddress.M_CityID,
                            doctor: this.doctor.M_DoctorID,
                            district: this.districtaddress.M_DistrictID,
                            kelurahan: this.kelurahanaddress.M_KelurahanID,
                            phone: this.instrumentphone,
                            email: this.instrumentemail,
                            pic: this.instrumentpic,
                            instrumenttypenew: this.instrumenttypenew,
                            instrumenttype: 0,
                            isdefault: this.isdefault === true ? "Y" : "N",
                            islabfrom: this.islabfrom === true ? "Y" : "N",
                            islabto: this.islabto === true ? "Y" : "N"
                        })
                    }

                }
            },
            updateFormPerusahaan() {
                if (this.$refs.forminstrument.validate()) {
                    var newinstrumenttype = this.instrumenttypenew
                    if (newinstrumenttype == "") {
                        this.$store.dispatch("instrument/update", {
                            id: this.xid,
                            name: this.instrumentname,
                            city: this.cityaddress.M_CityID,
                            doctor: this.doctor.M_DoctorID,
                            district: this.districtaddress.M_DistrictID,
                            kelurahan: this.kelurahanaddress.M_KelurahanID,
                            address: this.instrumentaddress,
                            phone: this.instrumentphone,
                            email: this.instrumentemail,
                            pic: this.instrumentpic,
                            instrumenttypenew: this.instrumenttypenew,
                            instrumenttype: this.xinstrumenttype.M_InstrumentTypeID,
                            isdefault: this.isdefault === true ? "Y" : "N",
                            islabfrom: this.islabfrom === true ? "Y" : "N",
                            islabto: this.islabto === true ? "Y" : "N"
                        })
                    } else {
                        this.$store.dispatch("instrument/update", {
                            id: this.xid,
                            name: this.instrumentname,
                            city: this.cityaddress.M_CityID,
                            doctor: this.doctor.M_DoctorID,
                            district: this.districtaddress.M_DistrictID,
                            kelurahan: this.kelurahanaddress.M_KelurahanID,
                            address: this.instrumentaddress,
                            phone: this.instrumentphone,
                            email: this.instrumentemail,
                            pic: this.instrumentpic,
                            instrumenttypenew: this.instrumenttypenew,
                            instrumenttype: 0,
                            isdefault: this.isdefault === true ? "Y" : "N",
                            islabfrom: this.islabfrom === true ? "Y" : "N",
                            islabto: this.islabto === true ? "Y" : "N"
                        })
                    }


                }
            },
            updateAlert_success(val) {
                this.$store.commit("instrument/update_alert_success", val)
            },
            editPerusahaan(data) {
                this.xid = data.id
                this.instrumentname = data.name
                this.$store.commit("instrument/update_cities", [{
                    M_CityID: data.M_CityID,
                    M_CityName: data.M_CityName
                }])
                this.cityaddress = {
                    M_CityID: data.M_CityID,
                    M_CityName: data.M_CityName
                }
                this.$store.commit("instrument/update_doctors", [{
                    M_DoctorID: data.M_DoctorID,
                    M_DoctorNames: data.M_DoctorNames
                }])
                this.doctor = {
                    M_DoctorID: data.M_DoctorID,
                    M_DoctorNames: data.M_DoctorNames
                }
                this.$store.commit("instrument/update_districts", [{
                    M_DistrictID: data.M_DistrictID,
                    M_DistrictName: data.M_DistrictName
                }])
                this.districtaddress = {
                    M_DistrictID: data.M_DistrictID,
                    M_DistrictName: data.M_DistrictName
                }
                this.$store.commit("instrument/update_kelurahans", [{
                    M_KelurahanID: data.M_KelurahanID,
                    M_KelurahanName: data.M_KelurahanName
                }])
                this.kelurahanaddress = {
                    M_KelurahanID: data.M_KelurahanID,
                    M_KelurahanName: data.M_KelurahanName
                }
                this.instrumentaddress = data.address
                this.instrumentphone = data.phone
                this.instrumentemail = data.email
                this.instrumentpic = data.pic
                this.instrumenttypenew = ""
                this.isdefault = data.isdefault === 'N' ? false : true
                this.islabfrom = data.islabfrom === 'N' ? false : true
                this.islabto = data.islabto === 'N' ? false : true
                this.$store.commit("instrument/update_act", 'edit')
                this.$store.commit("instrument/update_dialog_form_instrument", true)
                var instrumenttypes = this.$store.state.instrument.instrumenttypes
                var idx_instrumenttype = _.findIndex(instrumenttypes, function (o) {
                    return o.M_InstrumentTypeID == data.instrumenttypeid
                })
                var instrumenttype = instrumenttypes[idx_instrumenttype]
                this.$store.commit("instrument/update_instrumenttype", instrumenttype)
            },
            deletePerusahaan(data) {
                this.xid = data.id
                var xdata = {
                    id: data.id,
                    name: data.name,
                    assays: 'xxx'
                }
                this.$store.commit("instrument/update_selected_instrument", xdata)
                this.msgalert = "Yakin, mau hapus instrument " + data.name + "  ?"
                this.dialogdeletealert = true
            },
            changeNewInstrumentType(value) {
                this.readonlytypeinstrument = value === true ? true : false
                this.readonlytypeinstrumentnew = value === true ? false : true
            },
            newInstrumentType() {
                readonlytypeinstrument: true
                readonlytypeinstrumentnew: false
            },
            closeDeleteAlert() {
                this.$store.dispatch("instrument/delete", {
                    instrumentid: this.$store.state.instrument.selected_instrument.id,
                    instrumentname: this.$store.state.instrument.selected_instrument.name
                })
                this.dialogdeletealert = false
            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("instrument/lookup", {
                    search: this.xsearch,
                    all: 'Y'
                })
            }, 1000)
        },
        watch: {
            xsearch(val, old) {
                    console.log(val)
                    this.xsearch = val
                    this.thr_search()
                
            },
            search_city(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.instrument.update_autocomplete_status == 1) return
                this.thr_search_city()
            },
            search_doctor(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.instrument.update_autocomplete_status == 1) return
                this.thr_search_doctor()
            }
        }
    }
</script>