<template>
    <div class="row m-b-10 parentContainer ">
        <div class="col m-b-5">
            <div class="row bg-white no-margin p-b-10">
                <div class="col-3 b-t b-primary" style="border-top-width: 2px;"></div>
                <div class="col b-t b-grey hint-text" style="border-top-width: 2px;"></div>
            </div>
            <div class="row bg-white no-margin align-items-center justify-content-center p-b-10">
                <div class="col-9">
                    <div class="row">
                        <div class="col">
                            <div class="row">
                                <div class="col">
                                    <div class="row bold align-items-center">
                                        <div class="col-3 p-r-10">
                                            <div class="block p-l-10 p-r-10 p-t-5 p-b-5 bg-primary text-white fs-9 b-rad-none text-center">
                                                {{this.company.marking}}
                                            </div>
                                        </div>
                                        <div class="col-auto p-l-0 fs-10">{{this.company.name}}</div>
                                    </div>
                                </div>
                            </div>
                            <div class="row m-t-5">
                                <div class="col">
                                    <div class="row align-items-center">
                                        <div class="col-auto p-r-5">
                                            <div class="p-l-10 p-r-10 p-t-5 p-b-5 bg-master-lightest muted b-rad-none fs-8">
                                                <i class="fa fa-map-marker"></i>
                                            </div>
                                        </div>
                                        <div class="col p-l-0 fs-9 muted overflow-ellipsis">
                                            <small class="fs-9">{{this.company.street_one || ''}} {{ this.company.street_two || '' }} {{this.company.city || '' }} {{this.company.post_code || ''}} {{this.company.state || ''}} {{this.company.country || ''}}</small>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-auto">
                    <div class="row h-100">
                        <div class="col">
                            <div class="row no-margin">
                                <div class="col-auto text-right p-t-10 p-b-10 bg-master-lightest m-r-5">
                                    <a class="pointer" @click='toggleEdit()' v-show="!isEditing">
                                        <div class="row link align-items-center justify-content-center h-100" data-toggle="tooltip" title="Edit" data-placement="bottom">
                                            <div class="col">
                                                <i class="fa fa-pencil text-info"></i>
                                            </div>
                                        </div>
                                    </a>
                                    <a class="pointer" @click='closeEdit()' v-show="isEditing">
                                        <div class="row link align-items-center justify-content-center h-100" data-toggle="tooltip" title="Edit" data-placement="bottom">
                                            <div class="col">
                                                <i class="fa fa-times text-info"></i>
                                            </div>
                                        </div>
                                    </a>
                                </div>
                                <div class="col-auto text-right p-t-10 p-b-10 m-r-5 bg-master-lightest">
                                    <a :href="route('companies.profile', {companyId: this.company.id})">
                                        <div class="row link align-items-center justify-content-center h-100" data-toggle="tooltip" title="Manage" data-placement="bottom">
                                            <div class="col">
                                                <i class="fa fa-chevron-right text-master"></i>
                                            </div>
                                        </div>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row bg-white no-margin">
                <div class="col no-padding">
                    <div class="slider" :class="{close: !isEditing}" >
                        <company-form-component class="padding-15 b-t b-grey" v-on:collapseEdit="closeEdit()" v-on:updateLayout="resizeSlider()" :data="this.company"></company-form-component>
                    </div>
                </div>
            </div>
            <confirm-modal-component
                    title="Remove Company"
                    :message="'Are you sure you want to remove <strong>'+company.name+'</strong>?'"
                    type="delete"
                    danger
                    v-on:confirm="deleteCompany()"></confirm-modal-component>
        </div>
    </div>
</template>

<script>
    export default {
        props: ['data'],
        data() {
            return {
                company: this.data,
                isEditing: false
            }
        },
        mounted() {
            Event.$on('collapseEdit', () => {
                this.closeEdit();
            });
        },
        methods: {
            route: route,
            deleteCompany(){
                fetch(route('api.company.delete', {'id': this.company.id}), {
                   method: 'delete'
                }).then(response => response.json())
                    .then(data => {
                        Event.$emit('updateCompanyList');
                    })
                    .catch(error => console.log(error))
            },
            closeEdit(){
                this.isEditing = false;
            },
            toggleEdit(){
                Event.$emit('collapseEdit');
                this.isEditing = !this.isEditing;
            },
            resizeSlider(){
                this.$refs.upDown.layout()
            }
        }
    }
</script>