<template>
    <v-layout row wrap>
        <v-flex xs12 pa-2>
            <v-autocomplete
                label="Pemeriksaan"
                v-model="selected_test"
                :items="tests"
                :search-input.sync="search_test"
                auto-select-first
                no-filter
                return-object
                item-text="T_TestName"
                :loading="is_loading"
                no-data-text="Pilih Pemeriksaan"
                hide-details
                clearable
                >
                <template
                    slot="item"
                    slot-scope="{ item }"
                    >
                    <v-list-tile-content>
                    <v-list-tile-title v-text="item.T_TestName"></v-list-tile-title>
                    <!-- <v-list-tile-sub-title v-text="getMou(item)"></v-list-tile-sub-title> -->
                    </v-list-tile-content>
                </template>

            </v-autocomplete>
        </v-flex>

        <v-flex xs12 pa-2>
            <v-radio-group v-model="is_internal" row hide-details>
                <v-radio label="Rujuk Cabang" value="Y"></v-radio>
                <v-radio label="Rujuk Eksternal" value="N"></v-radio>
            </v-radio-group>
        </v-flex>

        <v-flex xs12 pa-2 v-show="is_internal == 'N'">
            <v-autocomplete
                label="Rujukan Lab Eksternal"
                v-model="selected_company"
                :items="companies"
                :search-input.sync="search_company"
                auto-select-first
                no-filter
                return-object
                item-text="M_CompanyName"
                :loading="is_loading"
                no-data-text="Pilih Company"
                hide-details
                clearable
                >
                <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-sub-title v-text="getMou(item)"></v-list-tile-sub-title> -->
                    </v-list-tile-content>
                </template>

            </v-autocomplete>
        </v-flex>

        

        <v-flex xs12 pa-2 v-show="is_internal == 'Y'">
            <v-select
                :items="regionals"
                v-model="selected_regional"
                return-object
                item-text="S_RegionalName"
                item-value="S_RegionalID"
                label="Regional"
                hide-details
            ></v-select>
        </v-flex>

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

        <v-flex xs12>
            <v-btn color="primary" :dark="btnAddEnable" @click="pxAdd" :disabled="!btnAddEnable">Tambahkan</v-btn>
        </v-flex>
    </v-layout>
</template>

<script>
module.exports = {
    computed : {
        search_company: {
            get() {
                return this.$store.state.ro_master.search_company
            },

            set(val)  {
                if (val == null) return
                this.$store.commit('ro_master/update_search_company',val)
            }
        },

        selected_company: {
            get() {
                return this.$store.state.ro_master.selected_company
            },
            set(val) {
                this.$store.commit("ro_master/update_selected_company",val)
            }
        },

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

        search_test: {
            get() {
                return this.$store.state.ro_master.search_test
            },

            set(val)  {
                if (val == null) return
                this.$store.commit('ro_master/update_search_test',val)
            }
        },

        selected_test: {
            get() {
                return this.$store.state.ro_master.selected_test
            },
            set(val) {
                this.$store.commit("ro_master/update_selected_test",val)
            }
        },

        tests() {
            return this.$store.state.ro_master.tests
        },

        is_loading() {
            return this.$store.state.ro_master.search_status == 1
        },

        regionals () {
            return this.$store.state.ro_master.regionals
        },

        selected_regional : {
            get () { return this.$store.state.ro_master.selected_regional },
            set (v) { this.$store.commit('ro_master/update_selected_regional', v) }
        },

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

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

        is_internal : {
            get () { return this.$store.state.ro_master.add_is_internal },
            set (v) { this.$store.commit('ro_master/update_add_is_internal', v) }
        },

        btnAddEnable () {
            let x = this.$store.state.ro_master

            if (!x.selected_test.T_TestID) return false
            if (x.add_is_internal == "N" && !x.selected_company) return false
            if (x.add_is_internal == "N" && !x.selected_company.M_CompanyID) return false
            if (x.add_is_internal == "Y" && !x.selected_branch) return false
            if (x.add_is_internal == "Y" && !x.selected_branch.M_BranchID) return false
            if (x.add_is_internal == "Y" && !x.selected_regional) return false
            if (x.add_is_internal == "Y" && !x.selected_regional.S_RegionalID) return false

            return true
        }
    },

    watch : {
        search_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.ro_master.search_status == 1 ) return  
            console.log("5-val:"+val)
            this.$store.commit("ro_master/update_search_company",val)
            this.thr_search_company()
        },

        search_test(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.ro_master.search_status == 1 ) return  
            console.log("5-val:"+val)
            this.$store.commit("ro_master/update_search_test",val)
            this.thr_search_test()
        }
    },

    methods : {
        thr_search_company: _.debounce( function () {
            this.$store.dispatch("ro_master/search_company")
        }, 700),

        thr_search_test: _.debounce( function () {
            this.$store.dispatch("ro_master/search_test")
        }, 700),

        pxAdd() {
            let pxs = this.$store.state.ro_master.pxs
            let total_px = this.$store.state.ro_master.total_px
            let total_page = this.$store.state.ro_master.total_page

            let x = {
                T_OrderRefMasterID:0, 
                T_TestID: this.$store.state.ro_master.selected_test.T_TestID,
                T_TestName: this.$store.state.ro_master.selected_test.T_TestName,
                M_CompanyID: this.$store.state.ro_master.selected_company.M_CompanyID,
                M_CompanyName: this.$store.state.ro_master.selected_company.M_CompanyName,
                M_BranchName: "",
                M_BranchID: 0,
                S_RegionalID: 0,
                S_RegionalName: '',
                is_internal: this.is_internal }
            if (this.is_internal == "Y") {
                x.M_CompanyID = 0
                x.M_CompanyName = ""
                x.M_BranchName = this.$store.state.ro_master.selected_branch.M_BranchName
                x.M_BranchID = this.$store.state.ro_master.selected_branch.M_BranchID
                x.S_RegionalID = this.$store.state.ro_master.selected_regional.S_RegionalID
                x.S_RegionalName = this.$store.state.ro_master.selected_regional.S_RegionalName 
            }
            pxs.push(x)
            
            this.$store.commit('ro_master/update_pxs', { records: pxs, total: total_px, total_page: total_page })

            this.$store.commit('ro_master/update_selected_company', {})
            this.$store.commit('ro_master/update_selected_test', {})
            this.$store.commit('ro_master/update_search_company', "")
            this.$store.commit('ro_master/update_search_test', "")
            this.$store.commit('ro_master/update_selected_branch', {})
            this.$store.commit('ro_master/update_selected_regional', {})
        }
    },

    mounted () {
        this.$store.dispatch('ro_master/search_regional')
    }
}
</script>