<template>
    <div class="card card-default no-border">
        <notification-component ref="notification"></notification-component>
        <loading-component  v-show="isLoading"></loading-component>
        <div class="card-block no-padding">
            <div class="row p-t-30">
                <div class="col no-padding">
                    <div class="b-l b-primary p-l-15 m-b-15" style="border-left-width: 3px;">
                        <h6 class="bold all-caps no-margin text-primary">New Order</h6>
                        <div class="muted all-caps fs-12">Create A New Order</div>
                    </div>
                </div>
            </div>
            <form method="post" @submit.prevent="addOrder">
                <div class="row">
                    <div class="col no-padding">
                        <div class="form-group form-group-default input-group">
                            <div class="row w-100">
                                <div class="col no-padding">
                                    <label class="muted block">Order No.</label>
                                    <input class="form-control block" name="marking" v-model="order.marking">
                                </div>
                                <div class="col-auto no-padding" @click="generateMarking()">
                                    <div class="input-group-addon pointer inline">
                                        <div class="row align-items-center justify-content-center h-100">
                                            <div class="col">
                                                <i class="fa fa-random text-success"></i>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col text-right no-padding">
                                <div class="row">
                                    <div class="col no-padding">
                                        <button class="btn b-rad-none btn-default" data-dismiss="modal">Cancel</button>
                                        <button type="submit" class="btn b-rad-none btn-primary">Create Order</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</template>
<script>
    import notificationConstant from '../../../../js/notification';
    export default {
        props: {
            'data': {
                type: String,
                required: true
            }
        },
        data() {
            return {
                order: {
                    marking: '',
                    name: ''
                },
                isLoading: false
            }
        },
        mounted: function() {
            $(this.$el).find('form').validate({
                rules: {
                    marking: "required"
                },
                errorPlacement: function ( error, element ) {

                    if(element.parents('.form-group').hasClass('form-group')){
                        error.insertAfter( element.parents('.form-group') );
                    } else {
                        error.insertAfter( element );
                    }

                },
                highlight: function(element) {
                    $(element).parents(".form-group").addClass('has-error');
                },
                unhighlight: function(element) {
                    $(element).parents(".form-group").removeClass('has-error')
                }
            })
        },
        created() {
            this.generateMarking();
        },
        methods: {
            addOrder() {
                if($(this.$el).find('form').valid()) {

                    //turn on loading animation
                    this.isLoading = true;
                    fetch(route('api.order.create', {companyId: this.data}), {
                        method: 'post',
                        body: JSON.stringify(this.order),
                        headers: {
                            'content-type': 'application/json'
                        }
                    }).then(() => {
                        this.clearForm();
                        //turn off loading animation
                        this.isLoading = false;

                        let notification = this.$refs.notification;
                        notification.title = notificationConstant.ORDER.ORDER_CREATED.TITLE;
                        notification.message = notificationConstant.ORDER.ORDER_CREATED.MESSAGE;

                        notification.execute();

                        $(this.$el).parents('.modal').modal('hide')

                        Event.$emit('updateOrdersList');

                    })
                }
            },
            generateMarking(){
                this.order.marking = Math.floor(Math.random() * 799999) + 200000;
            },
            clearForm(){
                this.generateMarking();
            }
        }
    }
</script>