<template>
    <v-dialog
        v-model="dialog"
        max-width="500px"
    >
        <v-card>
            <v-card-text>
                <v-layout row wrap>
                    <v-flex xs12>
                        <v-text-field
                            label="Pemeriksaan"
                            readonly
                            v-model="px_name"
                        ></v-text-field>    
                    </v-flex>

                    <v-flex xs12>
                        <v-select
                            :items="types"
                            item-value="val"
                            item-text="label"
                            v-model="selected_type"
                            label="Tipe Rujukan"
                        ></v-select>
                    </v-flex>

                    <v-flex xs12 pb-4 v-show="selected_type == 'N'">
                        <v-autocomplete
                            label="Perusahaan"
                            v-model="selected_company"
                            :items="companies"
                            :search-input.sync="query_company"
                            
                            auto-select-first
                            no-filter
                            return-object
                            clearable
                            item-text="M_CompanyName"
                            :loading="is_loading"
                            no-data-text="Pilih Perusahaan"
                            hide-details
                            >
                            <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>
                    </v-flex>

                    <v-flex xs12 v-show="selected_type == 'Y'">
                        <v-select
                            :items="branches"
                            v-model="selected_branch"
                            return-object
                            item-text="M_BranchName"
                            item-value="M_BranchID"
                            label="Cabang"
                        ></v-select>
                    </v-flex>

                    <v-flex xs12>
                        <v-select
                            :items="sample_types"
                            v-model="selected_sample_type"
                            return-object
                            item-text="T_SampleTypeName"
                            item-value="T_SampleTypeID"
                            label="Specimen"
                        ></v-select>
                    </v-flex>
                </v-layout>
            </v-card-text>

            <v-card-actions>
                <v-btn color="red" flat dark @click="dialog = false">Tutup</v-btn>
                <v-spacer></v-spacer>
                <v-btn color="primary" @click="save">Simpan</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script>
module.exports = {
    data () {
        return {
            types : [
                {val:"Y", label:"Rujuk Internal"},
                {val:"N", label:"Rujuk Eksternal"}
            ]
        }
    },

    computed : {
        dialog : {
            get () {
                return this.$store.state.testrefnew.dialog_new
            },
            set (v) {
                this.$store.commit('testrefnew/update_dialog_new', v)
            }
        },

        companies () {
            return this.$store.state.testrefnew.companies
        },

        selected_company : {
            get () { return this.$store.state.testrefnew.selected_company },
            set (v) { this.$store.commit('testrefnew/update_selected_company', v) }
        },

        query_company : {
            get () {
                return this.$store.state.testrefnew.query_company
            },
            set (v) {
                this.$store.commit('testrefnew/update_query_company', v)
            }
        },

        branches () {
            return this.$store.state.testrefnew.branches
        },

        selected_branch : {
            get () { return this.$store.state.testrefnew.selected_branch },
            set (v) { this.$store.commit('testrefnew/update_selected_branch', v) }
        },

        sample_types () {
            return this.$store.state.testrefnew.sample_types
        },

        selected_sample_type : {
            get () { return this.$store.state.testrefnew.selected_sample_type },
            set (v) { this.$store.commit('testrefnew/update_selected_sample_type', v) }
        },

        px_name() {
            return this.$store.state.testref.selected_px.Nat_TestName
        },

        selected_type : {
            get () { return this.$store.state.testrefnew.is_internal },
            set (v) { this.$store.commit('testrefnew/update_is_internal', v) }
        },

        is_loading () { return false }
    },

    methods : {
        save() {
            this.$store.dispatch('testrefnew/save')
        },

        thr_search: _.debounce( function () {
            this.$store.dispatch("testrefnew/search_company")
        }, 200),
    },

    mounted () {
        this.$store.dispatch('testrefnew/search_company')
        this.$store.dispatch('testrefnew/search_branch')
        this.$store.dispatch('testrefnew/search_sampletype')
    },

    watch : {
        query_company(val, old) {
            
            if (val == null || typeof val == 'undefined') val = ""
            // console.log("1-val:"+val)
            if (val == old ) return
            // console.log("2-val:"+val)
            // if (! val) return
            // console.log("3-val:"+val)
            // if (val.length < 1 ) return
            // console.log("4-val:"+val)
            if (this.$store.state.testrefnew.search_status == 1 ) return  
            // console.log("5-val:"+val)
            this.$store.commit("testrefnew/update_query_company", val)
            this.thr_search()
        },

        dialog(val, old) {
            if (val && !old) {
                let edit = this.$store.state.testrefnew.edit
                if (edit) {
                    let ref = this.$store.state.testref.selected_px
                    console.log(ref)
                    if (ref.T_TestRefID != null) {
                        this.selected_type = ref.T_TestRefIsInternal

                        // SAMPLE TYPE
                        this.selected_sample_type = {
                            T_SampleTypeID: ref.T_SampleTypeID,
                            T_SampleTypeName: ref.T_SampleTypeName
                        }
                        
                        if (ref.T_TestRefIsInternal == "Y") {
                            // BRANCH
                            this.selected_branch = {
                                M_BranchID: ref.M_BranchID,
                                M_BranchName: ref.M_BranchName
                            }
                        } else {
                            // COMPANY
                            this.query_company = ref.M_CompanyName
                            this.selected_company = {
                                M_CompanyID: ref.M_CompanyID,
                                M_CompanyName: ref.M_CompanyName
                            }
                        }
                        
                    }
                    
                } else {
                    this.selected_branch = {}
                    this.selected_company = {}
                }
            }
        }
    }
}
</script>