<template>
    <div class="row" @keyup.enter="submitForm">
        <div class="col">
            <div class="row">
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col">
                            <h3 class="all-caps m-b-5 no-margin text-center">Edit Contact Number</h3>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.phone">
                                <label>Phone</label>
                                <input type="text" class="form-control" v-model="parameters.phone">
                            </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 } from '@vuelidate/validators';
import route from '@/general/functions/router';

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

const props = defineProps<Props>();

const { parameters, submit } = useFormHandler({
    section: props.section,
    data: {
        type: [0, 1].includes(props.data?.type) ? props.data.type : 0,
        email: props.data?.contact ? props.data.contact.email : '',
        phone: props.data?.contact ? props.data.contact.phone : ''
    }
});

const rules = {
    parameters: {
        phone: { required }
    }
};

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

const submitForm = async () => {
    const isValid = await v$.value.$validate();
    if (!isValid) return;

    submit(route('api.company.profile.update', props.data.id), 'put', props.section, true, true, parameters.value, {
        successHandler: () => {
            window.location.reload();
        }
    });
};
</script>
