<template>
    <v-sheet tile>
        <template v-if="action != 'delete'">
            <v-row dense>
                <v-col cols="8">
                    <cs-text-box 
                        ref="Nume"
                        :control="controls.name"
                        :errors="errorsManager.ByField('name')"
                        v-model="activeRecord.name"
                        @focus="errorsManager.ResetField('name')"
                        @blur="errorsManager.Validate('name')"
                    />
                </v-col>

                <v-col cols="4">
                    <cs-text-box 
                        :control="controls.vat"
                        :errors="errorsManager.ByField('vat')"
                        v-model="activeRecord.vat"
                        @focus="errorsManager.ResetField('vat')"
                        @blur="errorsManager.Validate('vat')"
                    />
                </v-col>

                <v-col cols="8">
                    <cs-text-box 
                        :control="controls.email"
                        :errors="errorsManager.ByField('email')"
                        v-model="activeRecord.email"
                        @focus="errorsManager.ResetField('email')"
                        @blur="errorsManager.Validate('email')"
                    />
                </v-col>

                <v-col cols="4">
                    <cs-text-box 
                        :control="controls.phone"
                        :errors="errorsManager.ByField('phone')"
                        v-model="activeRecord.phone"
                        @focus="errorsManager.ResetField('phone')"
                        @blur="errorsManager.Validate('phone')"
                    />
                </v-col>

                <v-col cols="12">
                    <cs-edit-box 
                        :control="controls.details"
                        :errors="errorsManager.ByField('details')"
                        v-model="activeRecord.details"
                        @focus="errorsManager.ResetField('details')"
                        @blur="errorsManager.Validate('details')"
                    />
                </v-col>

                <v-col cols=12>
                   <city-selector 
                        :initialRegionId="!! activeRecord.city ? activeRecord.region_id : null"
                        :initialCountryId="!! activeRecord.city && !! activeRecord.city.region ? activeRecord.city.region.country_id : null"
                        :initialCityId="activeRecord.city_id"
                        v-model="activeRecord.city_id"
                   />
                </v-col>

                <v-col cols="6">
                    <cs-text-box 
                        :control="controls.street"
                        :errors="errorsManager.ByField('street')"
                        v-model="activeRecord.street"
                        @focus="errorsManager.ResetField('street')"
                        @blur="errorsManager.Validate('street')"
                    />
                </v-col>

                <v-col cols="3">
                    <cs-text-box 
                        :control="controls.street_number"
                        :errors="errorsManager.ByField('street_number')"
                        v-model="activeRecord.street_number"
                        @focus="errorsManager.ResetField('street_number')"
                        @blur="errorsManager.Validate('street_number')"
                    />
                </v-col>

                <v-col cols="3">
                    <cs-text-box 
                        :control="controls.postal_code"
                        :errors="errorsManager.ByField('postal_code')"
                        v-model="activeRecord.postal_code"
                        @focus="errorsManager.ResetField('postal_code')"
                        @blur="errorsManager.Validate('postal_code')"
                    />
                </v-col>

                <v-col cols="12">
                    <cs-edit-box 
                        :control="controls.address"
                        :errors="errorsManager.ByField('address')"
                        v-model="activeRecord.address"
                        @focus="errorsManager.ResetField('address')"
                        @blur="errorsManager.Validate('address')"
                    />
                </v-col>
                
                <v-col cols="12">
                    <v-sheet class="mx-2">
                        <newsletter-control 
                            v-model="activeRecord"
                        />
                    </v-sheet>
                </v-col>
            </v-row>
        </template>

        <cs-info-alert 
            v-else
            color="red"
            icon="mdi-alert"
            :message="__('Sunteți sigur că doriți ștergerea clientului :name?', {
                name: activeRecord.name
            })"
        />
    </v-sheet>
</template>

<script>
    export default {

        props: {
            controls: {default: () => ({})},
            activeRecord: {required: true},
            errorsManager: {required: true},
            action: {required: true},
            currentAction: {required: true},
        },

        methods: {
            Focus() {
            }
        },

        mounted() {
            if(this.action != 'delete')
            {
                setTimeout( () => {
                    this.Focus();
                }, 150);
            }    
        },

        components: {
            CsTextBox: require('@CsVueComp/controls/texts/CsTextBox').default, 
            CsEditBox: require('@CsVueComp/controls/texts/CsEditBox').default, 
            CsInfoAlert: require('@CsVueComp/sheets/CsInfoAlert').default,
        },

        name: 'CustomerInput',
    }
</script>