<template>
    <v-sheet tile>

        <template v-if="action != 'delete'">
            <v-row dense>
                <v-col cols="6">
                    <cs-text-box 
                        ref="First"
                        :control="controls.last_name"
                        :errors="errorsManager.ByField('last_name')"

                        v-model="activeRecord.last_name"
                        
                        @focus="errorsManager.ResetField('last_name')"
                        @blur="errorsManager.Validate('last_name')"
                    />
                </v-col>

                <v-col cols="6">
                    <cs-text-box 
                        :control="controls.first_name"
                        :errors="errorsManager.ByField('first_name')"

                        v-model="activeRecord.first_name"
                        
                        @focus="errorsManager.ResetField('first_name')"
                        @blur="errorsManager.Validate('first_name')"
                    />
                </v-col>

                <v-col cols="7">
                    <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="5">
                    <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="6">
                    <cs-password-box 
                        :control="controls.password"
                        :errors="errorsManager.ByField('password')"

                        v-model="activeRecord.password"
                        
                        @focus="errorsManager.ResetField('password')"
                        @blur="errorsManager.Validate('password')"
                    />
                </v-col>

                <v-col cols="6">
                    <cs-password-box 
                        :control="controls.password_confirmation"
                        :errors="errorsManager.ByField('password_confirmation')"

                        v-model="activeRecord.password_confirmation"
                        
                        @focus="errorsManager.ResetField('password_confirmation')"
                        @blur="errorsManager.Validate('password_confirmation')"
                    />
                </v-col>

                <v-col cols="12">
                    <cs-file-uploader 
                        ref="Avatar"
                        :control="controls.avatar"
                        :immediateInput="false"
                        v-model="activeRecord.avatar"
                        @uploaded="OnFileUploaded"
                        @issaved="OnFileIsSaved"
                    />
                </v-col>
            </v-row>
        </template>

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

<script>
    export default {

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

        methods: {
            Focus() {
                this.$refs.First.Focus();
            },

            OnFileIsSaved(saved) {
                this.$emit('file-saved', saved);
            },

            OnFileUploaded(file) {
                this.$emit('file-uploaded', file);
            },
        },

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

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

        name: 'UserInput',
    }
</script>