<template>
    <div class="warehouseForm">
        <notification-component ref="notification"></notification-component>
		<loading-component  v-show="isLoading"></loading-component>
        <div class="row m-t-20" v-show="!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 Warehouse</h6>
                    <div class="muted all-caps fs-12">Create A New Warehouse</div>
                </div>
            </div>
        </div>
        <form method="post" @submit.prevent="updateWarehouse">
            <div class="row">
                <div class="col no-padding">
                    <div class="form-group form-group-default required">
                        <label class="muted">Warehouse Name</label>
                        <input class="form-control" v-model="warehouse.name">
                    </div>
                    <div class="form-group form-group-default required">
                        <label class="muted">Email Address</label>
                        <input class="form-control" v-model="warehouse.email">
                    </div>
                    <address-component v-on:changeAddress="updateAddress($event)" ref="address" :data="address()"></address-component>
                    <div class="row">
                        <div class="col text-right no-padding">
                            <div v-show="!isEdit">
                                <button class="btn b-rad-none btn-success">Add Warehouse</button>
                            </div>
                            <div v-show="isEdit">
                                <button class="btn b-rad-none btn-success" @click="updateWarehouse" data-dismiss="modal">Save Changes</button>
                                <button type="button" class="btn b-rad-none btn-default" @click="collapseEdit()">Cancel</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</template>
<script>
    import notificationConstant from '../../../../js/notification';
    export default {
        props: {
            'data': {
                type: Object,
                default: null
            }
        },
        data() {
            return {
                warehouse: {
                    id: '',
                    name: '',
                    email: '',
                    street_one: '',
                    street_two: '',
                    city: '',
                    state: '',
                    post_code: '',
                    country: ''
                },
				isLoading: false,
                isEdit: false
            }
        },
        created() {
            if(this.data !== null){
                this.isEdit = true;
                this.warehouse = this.data;
            }
        },
        methods: {
            updateWarehouse() {

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

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

                fetch(url, {
                    method: method,
                    body: JSON.stringify(this.warehouse),
                    headers: {
                        'content-type': 'application/json'
                    }
                }).then((response) => {
                    let notification = this.$refs.notification;
                    if(!this.isEdit){
                        Event.$emit('updateWarehouseList');
                        this.clearForm();
                        notification.title = notificationConstant.WAREHOUSE.DETAILS_CREATED.TITLE;
                        notification.message = notificationConstant.WAREHOUSE.DETAILS_CREATED.MESSAGE;
                    }
                    else {
                        notification.title = notificationConstant.WAREHOUSE.DETAILS_CREATED.UPDATED.TITLE;
                        notification.message = notificationConstant.WAREHOUSE.DETAILS_CREATED.UPDATED.MESSAGE;
                    }

                    notification.execute();

                    //turn off loading animation
                    this.isLoading = false;
                    $('.modal').modal('hide');
                    this.collapseEdit();
                })
            },
            updateAddress(address){
                this.warehouse.street_one = address.street_one;
                this.warehouse.street_two = address.street_two;
                this.warehouse.city = address.city;
                this.warehouse.state = address.state;
                this.warehouse.post_code = address.post_code;
                this.warehouse.country = address.country;
            },
            collapseEdit(){
                this.$emit('collapseEdit');
            },
            address(){
                return {
                    street_one: this.warehouse.street_one,
                    street_two: this.warehouse.street_two,
                    city: this.warehouse.city,
                    state: this.warehouse.state,
                    post_code: this.warehouse.post_code,
                    country: this.warehouse.country
                };
            },
            clearForm(){
                this.warehouse = {
                    id: '',
                    name: '',
                    email: '',
                    street_one: '',
                    street_two: '',
                    city: '',
                    state: '',
                    post_code: '',
                    country: ''
                };

                this.$refs.address.resetAddress();
            }
        }
    }
</script>