<template>
  
    <v-dialog
      v-model="dialog"
      max-width="40%"
    >
        <v-card>
            <v-card-title
                class="headline grey lighten-2 pt-2 pb-2"
                primary-title
            >
            Form Pengiriman
            </v-card-title>
            <v-card-text class="pt-2 pb-2">
                <v-layout row>
                    <v-flex xs12 d-flex>
                        <v-select
                            v-model="selected_type_delivery"
                            :items="type_delivery"
                            item-text = "name"
                            return-object
                            label="Type Delivery"
                            >
                            <template
                                slot="item"
                                slot-scope="{ item }"
                                >
                                <v-list-tile-content>
                                    <v-list-tile-title v-text="item.name"></v-list-tile-title>
                                </v-list-tile-content>
                            </template>
                        </v-select>
                    </v-flex>
                </v-layout>
                <v-layout row>
                    <v-flex xs12 d-flex>
                        <v-layout row v-if="selected_type_delivery.type == 'EMAIL'">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2  xs12 >
                                        <v-text-field
                                            v-model="vtext"
                                            label="Alamat email"
                                            
                                        ></v-text-field>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                        <v-layout row v-if="selected_type_delivery.type == 'WHATSAPP' && selected_type_delivery.source == 'PATIENT'">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2  xs12 >
                                        <v-text-field
                                            :value="xhp_patient"
                                            label="Nomor Whatsapp"
                                            readonly="true"
                                        ></v-text-field>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                        <v-layout row v-if="selected_type_delivery.type == 'WHATSAPP' && selected_type_delivery.source == 'DOCTOR'">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2  xs12 >
                                        <v-text-field
                                            :value="xhp_doctor"
                                            label="Nomor Whatsapp"
                                            readonly="true"
                                        ></v-text-field>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                        <v-layout row v-if="selected_type_delivery.type == 'TELEGRAM' && selected_type_delivery.source == 'DOCTOR'">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2  xs12 >
                                        <v-text-field
                                            :value="xhp_doctor"
                                            label="Nomor Whatsapp"
                                            readonly="true"
                                        ></v-text-field>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                        <v-layout row v-if="selected_type_delivery.type == 'TELEGRAM' && selected_type_delivery.source == 'PATIENT'">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2  xs12 >
                                        <v-text-field
                                            :value="xhp_patient"
                                            label="Nomor Whatsapp"
                                            readonly="true"
                                        ></v-text-field>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                        <v-layout row v-else-if="selected_type_delivery.id== 2">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2 pr-2 xs12>
                                        <v-select
                                            item-text="description"
                                            return-object
                                            :items="xaddress_patient" 
                                            v-model="xselected_patient_address"
                                            label="Alamat Pasien"
                                        ></v-select>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                        <v-layout row v-else-if="selected_type_delivery.id == 4">
                            <v-flex pb-1 xs12>
                                <v-layout row>
                                    <v-flex pt-2 pr-2 xs12>
                                        <v-select
                                            item-text="description"
                                            return-object
                                            :items="xaddress_doctor" 
                                            v-model="xselected_doctor_address"
                                            label="Alamat Dokter"
                                        ></v-select>
                                    </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="dialog = false"
                >
                Tutup
                </v-btn>
                <v-btn
                    color="primary"
                    flat
                    @click="addDelivery()"
                >
                Tambahkan
                </v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
  
</template>

<style scoped>
</style>

<script>
module.exports = {
    data(){
        return {
            vilageid:0,
            datatext : '',
            search : '',
            selected_patient_address:{},
            selected_doctor_address:{},
            opt_address :[{name:'Patient',value:'patient'},{name:'Doctor',value:'doctor'}],
            selected_address_type:''
        }
    },
    computed : {
        type_delivery() {
            return this.$store.state.delivery.deliveries
        },
        xaddress_patient() {
            return this.$store.state.delivery.address_patient
        },
        xhp_patient() {
            return this.$store.state.delivery.hp_patient
        },
        xaddress_doctor() {
            return this.$store.state.delivery.address_doctor
        },
        xhp_doctor() {
            return this.$store.state.delivery.hp_doctor
        },
        selected_type_delivery : {
            get() {
                return this.$store.state.delivery.selected_delivery
            },
            set(val) {
                this.$store.commit("delivery/update_selected_delivery",val)
            }
        },
        xselected_patient_address : {
            get() {
                return this.selected_patient_address
            },
            set(val) {
                this.selected_patient_address = val
            }
        },
        xselected_doctor_address : {
            get() {
                return this.selected_doctor_address
            },
            set(val) {
                this.selected_doctor_address = val
            }
        },
        vtext : {
            get() {
                return this.datatext
            },
            set(val) {
                this.datatext = val
            }
        },
        dialog: {
            get() {
                return this.$store.state.verification.dialog_delivery_is_active
            },
            set(val) {
                this.$store.commit('verification/update_dialog_delivery_is_active',val)
            }
        },
        xselected_vilage: {
            get() {
                this.$store.state.vilage.selected_vilage
            },
            set(val) {
                this.$store.commit("vilage/update_selected_vilage",val)
            }
        },
        xvilages() {
            return this.$store.state.vilage.vilages
        },
        is_loading() {
            return this.$store.state.vilage.search_status == 1
        }
    },
    methods: {
        addDelivery() {
            this.dialog = false
            var vid = 0
            var destination = this.datatext
            var xcode = this.selected_type_delivery.code
            this.datatext = ''
            var addressid = 0
            var vilageid = 0
            if(this.selected_type_delivery.id === '2' || this.selected_type_delivery.id === 2){
                var addr = this.selected_patient_address
                addressid = addr.id
                vilageid = addr.vilageid
                //destination = addr.description
            }
            else if(this.selected_type_delivery.id === '4' || this.selected_type_delivery.id === 4){
                var addr = this.selected_doctor_address
                addressid = addr.id
                vilageid = addr.vilageid
                destination = addr.description
            }
            else if(this.selected_type_delivery.id === '6' || this.selected_type_delivery.id === 6){
                addressid = 0
                vilageid = 0
                destination = this.xhp_patient
            }
            else if(this.selected_type_delivery.id === '8' || this.selected_type_delivery.id === 8){
                addressid = 0
                vilageid = 0
                destination = this.xhp_patient
            }
            else if(this.selected_type_delivery.id === '7' || this.selected_type_delivery.id === 7){
                addressid = 0
                vilageid = 0
                destination = this.xhp_doctor
            }
            else if(this.selected_type_delivery.id === '9' || this.selected_type_delivery.id === 9){
                addressid = 0
                vilageid = 0
                destination = this.xhp_doctor
            }

            if(this.selected_type_delivery.type == 'PICKUP')
                var xlabel = this.selected_type_delivery.name
            else
                var xlabel = this.selected_type_delivery.name+' : '+destination

            var prm = {
                    id:vid,
                    xid:0,
                    code:xcode,
                    chex:true,
                    deliveryid:this.selected_type_delivery.id,
                    typedeliveryid:this.selected_type_delivery.typeid,
                    addressid:addressid,
                    vilageid:vilageid,
                    note:'',
                    type:'indigo',
                    label:xlabel,
                    destination:destination
                }
            
            this.$store.commit("vilage/update_selected_vilage",{})
            this.$emit('add-data-delivery', prm)
        }
    }
}
</script>
