<template>
    <div>
        <v-layout  v-if="xselected_patient" mt-2 row>
            <v-flex xs12>
                <v-card>
                    <v-card-text style="padding-top:2px">
                        <v-subheader v-if="xselected_patient.status === 'V'" red--text text--lighten-1> 
                            <v-flex text-md-right>
                                <v-btn  @click="updateDoctor()" small color="info">Update</v-btn>
                            </v-flex>
                        </v-subheader>
                        <v-divider></v-divider>
                        <v-layout row>
                            <v-flex pa-2 xs6>
                                <v-select
                                    item-text="name"
                                    return-object
                                    :items="xdoctors" 
                                    v-model="xselecteddoctor"
                                    label="Dokter*"
                                ></v-select>
                                <p  v-if="checkError('requiredoctor')" class="error pl-2 pr-2" style="color:#fff">Dokter dipilih dulu dong</p>
                            </v-flex>
                            <v-flex pa-2 xs6>
                                <v-select
                                    item-text="name"
                                    return-object
                                    :items="xdoctoraddress" 
                                    v-model="xselecteddoctoraddress"
                                    label="Alamat Dokter*"
                                ></v-select>
                                <p  v-if="checkError('requiredoctoraddress')" class="error pl-2 pr-2" style="color:#fff">Alamatnya juga dong dipilih</p>
                            </v-flex>
                        </v-layout> 
                    </v-card-text>         
                </v-card>
            </v-flex>
        </v-layout>
    </div>
</template>

<style scoped>

</style>

<script>
module.exports = {
    data: () => ({
          
    }),
    computed: {
        xselected_patient(){
            //console.log(this.$store.state.sample.selected_transaction)
            return this.$store.state.sample.selected_transaction
        },
        xselecteddoctor: {
            get() {
                return this.$store.state.sample.selected_doctor
            },
            set(val) {
                this.$store.commit("sample/update_selected_doctor",val)
                this.$store.dispatch("sample/getdoctoraddress",{
                    id:val.id
                })
            }
        },
        xdoctors(){
            return this.$store.state.sample.doctors
        },
        xselecteddoctoraddress: {
            get() {
                return this.$store.state.sample.selected_doctor_address
            },
            set(val) {
                this.$store.commit("sample/update_selected_doctor_address",val)
                
            }
        },
        xdoctoraddress(){
            return this.$store.state.sample.doctor_address
        }
    },
    methods : {
        checkError(value){
            var errors = this.$store.state.sample.errors
            if(errors.includes(value)){
                return true
            }
            else{
                return false
            }
        },
        updateDoctor(){
            this.$store.commit("sample/update_errors",[])
            var errors = this.$store.state.sample.errors
            var value = this.$store.state.sample.selected_transaction
            if(_.isEmpty(this.$store.state.sample.selected_doctor)){
                errors.push("requiredoctor")
            }
            if(_.isEmpty(this.$store.state.sample.selected_doctor_address)){
                errors.push("requiredoctoraddress")
            }
            if(errors.length === 0){
                var msg = "Anda yakin akan merubah data dokter atau alamat dokter "+value.T_SampleTypeName+" dari pasien "+value.patient_fullname+" ? "
                this.$store.commit("sample/update_msg_action",msg)
                this.$store.commit("sample/update_act",'updatedoctor')
                this.$store.commit("sample/update_dialog_action",true)
            }
        }
    }
}
</script>
