<template>
    <LoadingComponent :props="loading" />
    <section class="pt-28 pb-16">
        <div class="container max-w-[360px] pt-6 p-4 sm:shadow-card rounded-2xl bg-white">
            <h2 class="capitalize mb-6 text-center text-[22px] font-semibold leading-[34px] text-heading">{{
                $t('label.lets_get_started')
                }}
            </h2>
            <form @submit.prevent="save">
                <div class="mb-6">
                    <label for="phone" class="text-xs capitalize mb-1 font-normal">
                        {{ $t('label.mobile_number') }}
                    </label>
                    <div :class="errors.phone ? 'invalid' : ''"
                        class="w-full h-12 rounded-lg border px-4 flex items-center border-[#D9DBE9]" v-bind:class="errors.phone ? 'invalid' : ''">
                        <div class="w-fit flex-shrink-0 dropdown-group">
                            <button type="button" class="flex items-center gap-1">
                                {{ flag }}
                                <span class="whitespace-nowrap flex-shrink-0 text-sm">{{ props.form.code }}</span>
                                <input type="hidden" v-model="props.form.code">
                            </button>
                        </div>
                        <input id="phone" v-model="props.form.phone" v-on:keyup.enter="save"
                            v-on:keypress="phoneNumber($event)" type="text"
                            class="pl-4 text-sm w-full h-full text-heading">
                    </div>
                    <small class="db-field-alert" v-if="errors.phone">
                        {{ errors.phone[0] }}
                    </small>
                </div>
                <button type="submit"
                    class="w-full h-12 text-center capitalize font-medium rounded-3xl mb-6 text-white bg-primary">
                    {{ $t('label.next') }}
                </button>
                <div class="flex items-center justify-center gap-2">
                    <span class="text-xs text-[#6E7191]">{{ $t('label.already_have_an_account') }}</span>
                    <router-link :to="{ name: 'auth.login' }" class="text-xs font-medium text-primary">
                        {{ $t('label.login') }}
                    </router-link>
                </div>
            </form>
        </div>
    </section>
</template>

<script>

import appService from "../../../services/appService";
import askEnum from "../../../enums/modules/askEnum"
import alertService from "../../../services/alertService";
import LoadingComponent from "../components/LoadingComponent.vue";
import { useFrontendCountryCodeStore } from "../../../stores/frontend/frontendCountryCode";
import { useFrontendSettingStore } from "../../../stores/frontend/frontendSetting";
import { useFrontendSignupStore } from "../../../stores/frontend/frontendSignup";

export default {
    name: "SignupPhoneComponent",
    components: { LoadingComponent },
    data() {
        return {
            loading: {
                isActive: false,
            },
            props: {
                form: {
                    phone: "",
                    code: "",
                },
            },
            flag: "",
            country_code: "",
            errors: {},
            phone_verification: "",
            frontendCountryCodeStore: useFrontendCountryCodeStore(),
            frontendSettingStore: useFrontendSettingStore(),
            frontendSignupStore: useFrontendSignupStore(),
        };
    },
    mounted() {
        this.loading.isActive = true;
        this.frontendSettingStore.fetchLists().then(res => {
            this.defaultCountryCode = res.data.data.company_country_code;
            this.frontendCountryCodeStore.fetchShow(this.defaultCountryCode).then(res => {
                this.props.form.code = res.data.data.calling_code;
                this.country_code = res.data.data.calling_code;
                this.flag = res.data.data.flag_emoji;
                this.loading.isActive = false;
            }).catch((err) => {
                this.loading.isActive = false;
            });
            this.loading.isActive = false;
        }).catch((err) => {
            this.loading.isActive = false;
        });
    },
    computed: {
        countryCode: function () {
            return this.frontendCountryCodeStore.show;
        },
        setting: function () {
            return this.frontendSettingStore.lists;
        }
    },
    methods: {
        phoneNumber(e) {
            return appService.phoneNumber(e);
        },
        save: function () {
            try {
                this.loading.isActive = true;
                this.frontendSignupStore.otp(this.props.form).then((res) => {
                    this.loading.isActive = false;

                    if (this.setting.site_phone_verification === askEnum.NO) {
                        this.$router.push({ name: "auth.signupRegister" });
                    } else {
                        alertService.success(res.data.message);
                        this.$router.push({ name: "auth.signupVerify" });
                    }

                    this.props.form = {
                        phone: "",
                        code: this.country_code,
                    };
                    this.errors = {};
                }).catch((err) => {
                    this.loading.isActive = false;
                    this.errors = err.response.data.errors;
                });
            } catch (err) {
                this.loading.isActive = false;
                alertService.error(err);
            }
        },
    },
}
</script>