<template>
    <div class="inline">
        <div class="btn btn-sm btn-success b-rad-none requestModal p-t-10 p-b-10"  data-type="confirmOrder">Confirm Warehouse</div>
        <modal-component type="confirmOrder">
            <loading-component style="left: 0;" v-show="isLoading"></loading-component>
            <div class="card card-default no-border">
                <div class="card-block no-padding">
                    <div class="row p-t-30">
                        <div class="col no-padding">
                            <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">Confirm Warehouse</h6>
                                <div class="muted all-caps fs-12">Setup and confirm order</div>
                            </div>
                        </div>
                    </div>
                    <form method="post" @submit.prevent="addOrder">
                        <div class="row">
                            <div class="col no-padding">
                                <div class="row m-b-10">
                                    <div class="col no-padding">
                                        <div class="row m-b-10"  v-show="this.showWarehouse">
                                            <div class="col no-padding">
                                                <div class="form-group form-group-default form-group-default-select2 b-rad-none m-b-0" >
                                                    <label class="muted" style="z-index: 10000;">Warehouse</label>
                                                    <select-component  :options="this.warehouses" v-model="order.warehouse_id"></select-component>
                                                </div>
                                            </div>
                                            <div class="col-auto padding-5 bg-master-lighter pointer hide" @click="showWarehouse = !showWarehouse; order.warehouse_id = null">
                                                <div class="row align-items-center justify-content-center h-100">
                                                    <div class="col">
                                                        <i class="fa fa-times text-danger m-l-5 m-r-5"></i>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <action-date-component v-model="complete_date"></action-date-component>
                                    </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" @click="confirmOrder()" data-dismiss="modal">Confirm Warehouse</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </modal-component>
    </div>
</template>
<script>
    export default {
        props: {
            'data': {
                type: Number,
                required: true
            }
        },
        data() {
            return {
                order: {
                    warehouse_id: null,
                },
                complete_date: null,
                warehouses: [],
                showWarehouse: true,
                showCompleteDate: false,
                isLoading: false
            }
        },
        created() {
            this.fetchWarehouses();
        },
        methods: {
            fetchWarehouses(){
                //turn on loading animation
                this.isLoading = true;

                fetch(route('api.warehouse.list', {'all': true})).then(response => response.json())
                    .then(response => {

                        this.warehouses = $.map(response.data, function(value){
                            return {'id': value.id, 'text': value.name};
                        });

                        //turn off loading animation
                        this.isLoading = false;

                    })
                    .catch(error => console.log(error))
            },
            confirmOrder() {

                //turn on loading animation
                this.isLoading = true;
                fetch(route('api.order.warehouse.update', {orderId: this.data}), {
                    method: 'post',
                    body: JSON.stringify(this.order),
                    headers: {
                        'content-type': 'application/json'
                    }
                }).then(() => {
                    this.$emit('updateStep', {'complete_date': this.complete_date, 'type': 'WAREHOUSE_CONFIRMED'});
                    this.clearForm();

                    //turn off loading animation
                    this.isLoading = false;
                });
            },
            clearForm(){
                this.order.warehouse_id = null;
                this.complete_date = null;
                this.showCompleteDate = false;
            }
        }
    }
</script>