<template>
    <div>
        <v-layout class="mb-2 " column>
            <v-card height="650px" >
             
                    <v-layout row>
                        <v-flex pa-2 xs6>
                            <v-btn block v-if="!_.isEmpty(selected_anamnese_tab)" depressed  title="Anamnesa" style="min-width:25px!important" dark   color="primary">
                                Anamnesa
                            </v-btn>
                            <v-btn block v-if="_.isEmpty(selected_anamnese_tab)"  @click="changeHeaderTab('anamnese')" title="Anamnesa" style="min-width:25px!important"    >
                                Anamnesa
                            </v-btn>
                        </v-flex>
                        <v-flex pa-2 xs6>
                            <v-btn block v-if="!_.isEmpty(selected_doctor_tab)"  depressed   title="Pemeriksaan"  style="min-width:25px!important" color="primary">
                                Pemeriksaan
                            </v-btn>
                            <v-btn block v-if="_.isEmpty(selected_doctor_tab)"  @click="changeHeaderTab('doctor')"  title="Pemeriksaan"  style="min-width:25px!important">
                                Pemeriksaan
                            </v-btn>
                        </v-flex>
                    </v-layout>
                    <v-divider></v-divider>
             
                
                    <div style="height:60px">
                        <v-layout class="ml-2 mr-2 mb-4 mt-2" row>
                        <v-flex xs12>
                            <v-autocomplete
                                label="Dokter Pemeriksa"
                                v-model="selected_doctor"
                                :items="doctors"
                                no-filter
                                :disabled="selected_patient.orderIsCheck === 'D'"
                                item-text="name"
                                return-object
                                no-data-text="Pilih Dokter Pemeriksa"
                                >
                            </v-autocomplete>
                        </v-flex>
                    </v-layout>
                    </div>
                    
                    <v-divider class="mt-2 mb-1"></v-divider>
                    <div v-if="!_.isEmpty(selected_doctor_tab)" style="height:416px">
                        <v-layout  v-for="(doctor_tab,idx) in doctor_tabs" :key="idx" row>
                        <v-flex class="pl-2 pr-2 pb-0 mb-1" xs12>
                            <v-btn v-if="!isSelectedDoctorTab(doctor_tab.id)" @click="changeButton('doctor',doctor_tab)"  small block large>{{doctor_tab.name}}</v-btn>
                            <v-btn v-if="isSelectedDoctorTab(doctor_tab.id)" @click="changeButton('doctor',doctor_tab)" color="primary" small block depressed large>{{doctor_tab.name}}</v-btn>
                        </v-flex>
                    </v-layout>
                    </div>
                    <div v-if="!_.isEmpty(selected_anamnese_tab)" style="height:416px">
                        <v-layout  v-for="(anamnese_tab,idx) in anamnese_tabs" :key="idx" row>
                        <v-flex class="pl-2 pr-2 pb-0 mb-1" xs12>
                            <v-btn v-if="!isSelectedAnamneseTab(anamnese_tab.id)" @click="changeButton('anamnese',anamnese_tab)"  small block large>{{anamnese_tab.name}}</v-btn>
                            <v-btn v-if="isSelectedAnamneseTab(anamnese_tab.id)" @click="changeButton('anamnese',anamnese_tab)" color="primary" small block depressed large>{{anamnese_tab.name}}</v-btn>
                        </v-flex>
                    </v-layout>
                    </div>
                    
                    <v-divider v-if="!_.isEmpty(selected_doctor_tab)" class="mt-2 mb-1"></v-divider>
                    <v-card-actions>
                               <v-btn class="mt-2" v-if="!_.isEmpty(selected_doctor_tab) && !_.isEmpty(selected_patient) && selected_patient.orderIsCheck === 'Y'" @click="endSession()" block style="min-height:45px" color="red lighten-2" dark>SELESAI</v-btn>
                            </v-card-actions>
            </v-card>
        </v-layout>
       
        
       
       
     
      
    </div>
</template>

<style scoped>
.div_bottom {
    position: absolute;
right:    0;
bottom:   0;
}
</style>

<script>
module.exports = {
    mounted() {

    },
    data: () => ({

    }),
    computed: {
        doctors: {
            get() {
                return this.$store.state.patient.doctors
            },
            set(val) {
                this.$store.commit("patient/update_doctors", val)
            }
        },
        selected_doctor: {
            get() {
                return this.$store.state.patient.selected_doctor
            },
            set(val) {
                this.$store.commit("patient/update_selected_doctor", val)
                this.changeDoctor(val)
            }
        },
        selected_patient: {
            get() {
                return this.$store.state.patient.selected_patient
            },
            set(val) {
                this.$store.commit("patient/update_selected_patient", val)
            }
        },
        doctor_pj() {
            return this.$store.state.patient.doctor_pj
        },
        doctor_tabs() {
            return this.$store.state.patient.doctor_tabs
        },
        selected_doctor_tab: {
            get() {
                return this.$store.state.patient.selected_doctor_tab
            },
            set(val) {
                this.$store.commit("patient/update_selected_doctor_tab", val)
               
            }
        },
        anamnese_tabs() {
            return this.$store.state.patient.anamnese_tabs
        },
        selected_anamnese_tab: {
            get() {
                return this.$store.state.patient.selected_anamnese_tab
            },
            set(val) {
                this.$store.commit("patient/update_selected_anamnese_tab", val)
            }
        },
        pgrs_save() {

            return this.$store.state.patient.pgrs_save
        },
        active() {
            return this.$store.state.button_active
        },
        dialogconfirmationdelete: {
            get() {
                return this.$store.state.patient.dialog_confirmation_delete
            },
            set(val) {
                this.$store.commit("patient/update_dialog_confirmation_delete", val)
            }
        }
    },
    methods: {
        changeHeaderTab(value){
            
            if(value === 'doctor'){
                
                if(this.selected_patient.orderIsCheck !== 'D'){
                    var prm = {}
                    prm.orderid = this.$store.state.patient.selected_patient.orderID
                    prm.doctor = this.$store.state.patient.selected_doctor
                    prm.note = this.$store.state.patient.xnote
                    prm.json_form = this.$store.state.patient.riwayats
                    prm.selected_anamnese_tab = this.$store.state.patient.selected_anamnese_tab
                    prm.type = this.$store.state.patient.type_field
                    prm.new_tab = this.$store.state.patient.selected_anamnese_tab
                    prm.end_session = false
                    this.$store.dispatch("patient/save_anamnese_get_data",prm)
                }else{
                    this.$store.commit("patient/update_selected_anamnese_tab", {})
                    let doctor_tabs = this.$store.state.patient.doctor_tabs
                    this.$store.commit("patient/update_selected_doctor_tab", doctor_tabs[0])
                    this.$store.dispatch("patient/get_data")
                }
                
                
            }else{
                if(this.selected_patient.orderIsCheck === 'D'){
                    this.selected_anamnese_tab = this.anamnese_tabs[0]
                    this.selected_doctor_tab = {}
                    this.$store.dispatch("patient/get_data_anamnese")
                }else{
                    var selected_tab = this.$store.state.patient.selected_doctor_tab
                    if(selected_tab.id !== 'ADDITIONAL' ){
                        var prm = {}
                        prm.orderid = this.$store.state.patient.selected_patient.orderID
                        prm.doctor = this.$store.state.patient.selected_doctor
                        prm.note = this.$store.state.patient.xnote
                        prm.vital_sign = this.$store.state.patient.fisiks
                        prm.selected_doctor_tab = selected_tab
                        prm.type = this.$store.state.patient.type_field
                        prm.new_tab = this.$store.state.patient.selected_doctor_tab
                        prm.end_session = false
                        this.$store.dispatch("patient/save_doctor_getdata", prm)
                    }else{
                        this.$store.state.patient.header_tab = 0
                        this.$store.state.patient.select_subgroup = []
                        this.$store.state.patient.search_test = ""
                        var prm = {}
                        prm.orderid = this.$store.state.patient.selected_patient.orderID
                        prm.doctor = this.$store.state.patient.selected_doctor
                        prm.selected_test = this.$store.state.patient.selected_test
                        prm.end_session = false
                        prm.new_tab = this.$store.state.patient.selected_doctor_tab
                        if(prm.selected_test.length > 0)
                            this.$store.dispatch("patient/savepx_getdata", prm)
                    }
                    
                }
                
            }
        },
        changeDoctor(val){
            this.$store.dispatch("patient/changedoctor",{'doctor':val,'orderid':this.selected_patient.orderID})
        },
        endSession(){
            //this.$store.dispatch("patient/endsession",this.selected_patient)
            let bf_tab = this.$store.state.patient.selected_doctor_tab
            if(bf_tab.id === 'ADDITIONAL'){
                var prm = {}
                prm.orderid = this.$store.state.patient.selected_patient.orderID
                prm.doctor = this.$store.state.patient.selected_doctor
                prm.selected_test = this.$store.state.patient.selected_test
                prm.new_tab = bf_tab
                prm.end_session = true
                if(prm.selected_test.length > 0)
                    this.$store.dispatch("patient/savepx", prm)
                else
                this.$store.dispatch("patient/endsession",this.selected_patient)

            }else{
                var prm = {}
                prm.orderid = this.$store.state.patient.selected_patient.orderID
                prm.doctor = this.$store.state.patient.selected_doctor
                prm.note = this.$store.state.patient.xnote
                prm.vital_sign = this.$store.state.patient.fisiks
                prm.selected_doctor_tab = this.$store.state.patient.selected_doctor_tab
                prm.type = this.$store.state.patient.type_field
                prm.new_tab = bf_tab
                prm.end_session = true
                this.$store.dispatch("patient/savetext",prm)
            }
        },
        isSelectedDoctorTab(id){
            return id == this.selected_doctor_tab.id
        },
        isSelectedAnamneseTab(id){
            return id == this.selected_anamnese_tab.id
        },
        changeButton(type,tab) {
            if(type === 'doctor'){
                //this.selected_doctor_tab = tab
                this.$store.commit("patient/update_header_tab", 0)
                
                if(tab.id !== "VITAL"){
                    this.$store.commit("patient/update_type_field", "TEXT")
                }

                //this.$store.dispatch("patient/get_data")

                /*if(tab.id !== 'ADDITIONAL'){
                    let difftest = this.compareSelectedTest()
                    if(difftest){
                        this.$store.commit("patient/update_msginfo", "Anda belum melakukan simpan")
                        this.$store.commit("patient/update_dialoginfo", true)
                        this.selected_doctor_tab = {id:'ADDITIONAL',name:'Pemeriksaan Penunjang'}
                    }
                }*/
                let bf_tab = this.$store.state.patient.selected_doctor_tab
                if(bf_tab.id === 'ADDITIONAL'){
                    var prm = {}
                    prm.orderid = this.$store.state.patient.selected_patient.orderID
                    prm.doctor = this.$store.state.patient.selected_doctor
                    prm.selected_test = this.$store.state.patient.selected_test
                    prm.new_tab = tab
                    prm.end_session = false
                    if(prm.selected_test.length > 0)
                        this.$store.dispatch("patient/savepx", prm)
                    else
                        this.$store.dispatch("patient/get_data")

                }else{
                    var prm = {}
                    prm.orderid = this.$store.state.patient.selected_patient.orderID
                    prm.doctor = this.$store.state.patient.selected_doctor
                    prm.note = this.$store.state.patient.xnote
                    prm.vital_sign = this.$store.state.patient.fisiks
                    prm.selected_doctor_tab = this.$store.state.patient.selected_doctor_tab
                    prm.type = this.$store.state.patient.type_field
                    prm.new_tab = tab
                    prm.end_session = false
                    this.$store.dispatch("patient/savetext",prm)
                }
                
            }else{
                //this.selected_anamnese_tab = tab
                if(tab.id === "OPERATING"){
                    this.$store.commit("patient/update_type_field", "TEXT")
                }
                this.$store.commit("patient/update_header_tab", 0)
                //this.$store.dispatch("patient/get_data_anamnese")
                var prm = {}
                prm.orderid = this.$store.state.patient.selected_patient.orderID
                prm.doctor = this.$store.state.patient.selected_doctor
                prm.note = this.$store.state.patient.xnote
                prm.json_form = this.$store.state.patient.riwayats
                prm.selected_anamnese_tab = this.$store.state.patient.selected_anamnese_tab
                prm.type = this.$store.state.patient.type_field
                prm.new_tab = tab
                this.$store.dispatch("patient/savetextanamnese",prm)
            }
            
        },
        checkError(value) {
            var errors = this.$store.state.patient.errors
            if (errors.includes(value)) {
                return true
            } else {
                return false
            }
        },
        compareSelectedTest(){
            let seltest = this.$store.state.patient.selected_test
            let seltest_before = this.$store.state.patient.selected_test_before
            let found_diff = false
            seltest.forEach( function(test) {
                let exist_test = seltest_before.filter(function(item) {
                return item.T_TestID === test.T_TestID
                })
                if(exist_test.length === 0)
                found_diff = true
            })

            return found_diff
        
        },
    }
}
</script>