<template>
    <div class="card card-default no-border no-margin">
        <notification-component ref="notification"></notification-component>
		<loading-component  v-show="isLoading"></loading-component>
        <div class="card-block">
            <div class="row" v-show="!this.isEdit">
                <div class="col">
                    <div class="b-l b-success p-l-15 m-b-15" style="border-left-width: 3px;">
                        <h6 class="bold all-caps no-margin text-success">New Contact</h6>
                        <div class="muted all-caps fs-12">Create A New Contact Details</div>
                    </div>
                </div>
            </div>
            <form method="post" @submit.prevent="updateContact">
                <div class="row">
                    <div class="col no-padding">
                        <div class="row">
                            <div class="col p-l-0">
                                <div class="form-group form-group-default required">
                                    <label class="muted">Name</label>
                                    <input class="form-control" v-model="contact.name">
                                </div>
                            </div>
                            <div class="col p-r-0">
                                <div class="form-group form-group-default required">
                                    <label class="muted">Designation</label>
                                    <input class="form-control" v-model="contact.designation">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col p-l-0">
                                <div class="form-group form-group-default required">
                                    <label class="muted">Phone Number</label>
                                    <input class="form-control" v-model="contact.contact">
                                </div>
                            </div>
                            <div class="col p-r-0">
                                <div class="form-group form-group-default required">
                                    <label class="muted">Email Address</label>
                                    <input class="form-control" v-model="contact.email">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col no-padding text-right">
                                <div v-show="!isEdit">
                                    <button type="button" @click="collapseEdit()" class="btn b-rad-none btn-default">Cancel</button>
                                    <button class="btn b-rad-none btn-success">Add Contact</button>
                                </div>
                                <div v-show="isEdit">
                                    <button type="button" @click="collapseEdit()" class="btn b-rad-none btn-default">Cancel</button>
                                    <button class="btn b-rad-none btn-success">Save Changes</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</template>
<script>
    import notificationConstant from '../../../../js/notification';
    export default {
        props: {
            'type': {
                type: String,
                required: true
            },
            'reference': {
                type: String,
                required: true
            },
            'data': {
                type: Object,
                default: null
            }
        },
        data(){
            return {
                contact: {
                    id: '',
                    name: '',
                    designation: '',
                    contact: '',
                    email: ''
                },
                isLoading: false,
                isEdit: false
            }
        },
        created() {
            if(this.data !== null){
                this.isEdit = true;
                this.contact = this.data;
            }
        },
        methods: {
            updateContact() {

                //turn on loading animation
                this.isLoading = true;

                var url = this.isEdit ? route('api.contact.update', {type: this.type, reference:  this.reference, id: this.contact.id}) : route('api.contact.create', {type: this.type, reference:  this.reference}),
                    method = this.isEdit ? 'put' : 'post';

                fetch(url, {
                    method: method,
                    body: JSON.stringify(this.contact),
                    headers: {
                        'content-type': 'application/json'
                    }
                }).then((response) => {
                    let notification = this.$refs.notification;
                    if(!this.isEdit){
                        Event.$emit('updateContactsList');
                        this.clearForm();
                        notification.title = notificationConstant.WAREHOUSE.CONTACT.UPDATED.TITLE;
                        notification.title = notificationConstant.WAREHOUSE.CONTACT.UPDATED.MESSAGE;
                    }
                    else {
                        notification.title = notificationConstant.WAREHOUSE.CONTACT.CREATED.TITLE;
                        notification.title = notificationConstant.WAREHOUSE.CONTACT.CREATED.MESSAGE;
                    }
                    notification.execute();
                    //turn off loading animation
                    this.isLoading = false;

                    this.collapseEdit();
                })
            },
            collapseEdit(){
                this.$emit('collapseEdit');
            },
            clearForm(){
                this.addressBook = {
                    id: '',
                    name: '',
                    designation: '',
                    contact: '',
                    email: ''
                };
            }
        }
    }
</script>