<template>
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col">
                            <h5 class="all-caps m-b-5 bold no-margin">Change Email Address</h5>
                        </div>
                    </div>
                    <div class="row m-b-5 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                        <div class="col">
                            <small class="bold fs-10 text-danger">{{ error }}</small>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.email">
                                <label>New Email Address</label>
                                <input type="text" class="form-control" v-model="parameters.email">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" data-dismiss="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm">Update</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { useFormHandler } from '@/composables/useFormHandler';
import { useVuelidate } from '@vuelidate/core';
import { required, email } from '@vuelidate/validators';
import route from '@/general/functions/router';

interface Props {
    data: any;
    section: string;
}

const props = defineProps<Props>();

const { parameters, submit, error } = useFormHandler({
    section: props.section,
    data: {
        email: props.data.email
    }
});

const rules = {
    parameters: {
        email: { required, email }
    }
};

const v$ = useVuelidate(rules, { parameters });

const submitForm = async () => {
    const isValid = await v$.value.$validate();
    if (!isValid) return;
    submit(route('api.company.team.create'), 'post', props.section, true, false, parameters.value);
};
</script>
