<template>
    <div>
        <div class="modal fade" id="companyForm" aria-hidden="true" role="dialog" data-backdrop="static">
            <div class="modal-dialog modal-xl">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">{{ $t('Company') }}</h4>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Code') }}<sup class="text-danger">*</sup></label>
                                <input type="text" class="form-control" v-model="form.code" :placeholder="$t('Code')"
                                    :class="{ 'is-invalid': form.errors.has('code') }">
                                <div class="valid text-danger" v-if="form.errors.has('code')"
                                    v-html="form.errors.get('code')" ></div>
                            </div>

                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Name khmer') }}</label>
                                <input type="text" class="form-control" v-model="form.name_kh"
                                    :placeholder="$t('Name Khmer')"
                                    :class="{ 'is-invalid': form.errors.has('name_kh') }">
                                <div class="valid text-danger" v-if="form.errors.has('name_kh')"
                                    v-html="form.errors.get('name_kh')" ></div>
                            </div>
                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Name English') }}<sup class="text-danger">*</sup></label>
                                <input type="text" class="form-control" v-model="form.name_en"
                                    :placeholder="$t('Name English')"
                                    :class="{ 'is-invalid': form.errors.has('name_en') }">
                                <div class="valid text-danger" v-if="form.errors.has('name_en')"
                                    v-html="form.errors.get('name_en')" ></div>
                            </div>
                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Contact Number') }}<sup class="text-danger">*</sup></label>
                                <input type="text" class="form-control" v-model="form.contact_number"
                                    :placeholder="$t('Contact Number')"
                                    :class="{ 'is-invalid': form.errors.has('contact_number') }">
                                <div class="valid text-danger" v-if="form.errors.has('contact_number')"
                                    v-html="form.errors.get('contact_number')" ></div>
                            </div>

                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Email') }}<sup class="text-danger"></sup></label>
                                <input type="text" class="form-control" v-model="form.email" :placeholder="$t('Email')"
                                    :class="{ 'is-invalid': form.errors.has('email') }">
                                <div class="valid text-danger" v-if="form.errors.has('email')"
                                    v-html="form.errors.get('email')" ></div>
                            </div>
                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Address') }}<sup class="text-danger"></sup></label>
                                <suggestions v-model="form.address" :onInputChange="onCountryInputChange"
                                    inputClass="form-control"></suggestions>
                                <div class="valid text-danger" v-if="form.errors.has('address')"
                                    v-html="form.errors.get('address')" ></div>
                            </div>
                            <div class="col-sm-6 mb-3">
                                <label class="form-label">{{ $t('Website') }}<sup class="text-danger"></sup></label>
                                <input type="text" class="form-control" v-model="form.website"
                                    :placeholder="$t('Website')" :class="{ 'is-invalid': form.errors.has('website') }">
                                <div class="valid text-danger" v-if="form.errors.has('website')"
                                    v-html="form.errors.get('website')" ></div>
                            </div>
                            <div class="col-sm-6">
                                <strong>{{ $t('Logo') }}</strong>
                                <div class="col-sm-12 mt-0 px-0 pl-0">
                                    <div class="banner-image">
                                        <div class="timage"><strong>{{ $t('Choose') }}</strong></div>
                                        <div class="image">
                                            <img style="width: 50%" alt="" class="mx-auto d-block"
                                                v-bind:src="action ? 'uploads/companies/' + this.form.logo : this.form.logo" />
                                        </div>
                                        <input @change="onChanageImage" hidden type="file"
                                            :class="{ 'is-invalid': form.errors.has('logo') }"
                                            class="custom-file-input form-control" id="logo" />
                                        <div class="valid text-danger" v-if="form.errors.has('logo')"
                                            v-html="form.errors.get('logo')" ></div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-dark " data-bs-dismiss="modal">
                            <i class="fa-solid fa-arrow-left"></i> {{ $t('Close') }}
                        </button>
                        <button type="submit" :disabled="form.busy" class="btn btn-success "
                            @click="$emit('submitData')">
                            <i class="fa-solid fa-floppy-disk"></i>
                            {{ $t('Submit') }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        form: {},
    },
    data() {
        return {
            action: false,
        }
    },
    mounted() {
    },
    methods: {
        onChanageImage(event) {
            let files = event.target.files
            if (!files.length) return;
            this.action = false;
            let render = new FileReader();
            render.onload = (file) => {
                this.form.logo = file.target.result;
            };
            render.readAsDataURL(files[0]);
        },
        async onCountryInputChange(query) {
            if (query.trim().length === 0) {
                return null
            }
            let data = await axios.get('admin-api/address-autocomplete', { params: { 'searchText': query } });
            return data.data;
        },
    }
};
</script>
