import useAuth from '@/core/hooks/useAuth';
import { setAuth } from '@/core/utils/auth';
import addressApi from '@/lib/address/api/addressApi';
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
import { useEffect, useState, useMemo } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'react-hot-toast';
import switchAccountApi from '../api/switchAccountApi';
import FormBisnis from '~/modules/register/components/FormBisnis.tsx';
import RegistrasiBisnis from '~/modules/register/components/RegistrasiBisnis.tsx';
import { Radio, RadioGroup, Stack, Divider, Button } from '@chakra-ui/react';
import { useRegisterStore } from '~/modules/register/stores/useRegisterStore.ts';
import { registerUser } from '~/services/auth';
import { useMutation } from 'react-query';
import { isValid } from 'zod';
import Spinner from "@/core/components/elements/Spinner/LogoSpinner";
import useDevice from '@/core/hooks/useDevice';
import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
const SwitchAccount = ({ company_type, setIsAprove, setUbahAkun }) => {
const { isDesktop, isMobile } = useDevice();
const auth = useAuth();
const [isOpen, setIsOpen] = useState(true);
const toggle = () => setIsOpen(!isOpen);
const [isPKP, setIsPKP] = useState(true);
const [isTerdaftar, setIsTerdaftar] = useState(false);
const [isChecked, setIsChecked] = useState(false);
const [selectedValueBisnis, setSelectedValueBisnis] = useState('false');
const [selectedValue, setSelectedValue] = useState('PKP');
const [buttonSubmitClick, setButtonSubmitClick] = useState(false);
const [changeConfirmation, setChangeConfirmation] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [isLoadingPopup, setIsLoadingPopup] = useState(false);
const { register, setValue, handleSubmit } = useForm({
defaultValues: {
email: '',
name: '',
phone: '',
password: '',
},
});
const mutation = useMutation({
mutationFn: (data) => registerUser(data),
});
const [notValid, setNotValid] = useState(false);
const {
form,
isCheckedTNC,
isValidCaptcha,
errors,
validate,
updateForm,
resetForm,
} = useRegisterStore();
const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]);
useEffect(() => {
const loadProfile = async () => {
const dataProfile = await addressApi({ id: auth.partnerId });
setValue('email', dataProfile?.email);
setValue('name', dataProfile?.name);
setValue('phone', dataProfile?.phone);
};
if (auth) loadProfile();
}, [auth, setValue]);
useEffect(() => {
if (selectedValue === 'PKP') {
updateForm('is_pkp', 'true');
validate();
} else {
updateForm('is_pkp', 'false');
validate();
}
}, [selectedValue]);
useEffect(() => {
if (isTerdaftar) {
updateForm('is_terdaftar', 'true');
validate();
} else {
updateForm('is_terdaftar', 'false');
validate();
}
}, [isTerdaftar]);
useEffect(() => {
const loadProfile = async () => {
const dataProfile = await addressApi({
id: auth.parentId ? auth.parentId : auth.parent_id,
});
if (dataProfile?.companyType === 'nonpkp') {
setSelectedValue('PKP');
}
if (auth?.company) {
updateForm('email_partner', dataProfile?.email);
updateForm('business_name', dataProfile?.name);
updateForm('industry_id', `${dataProfile?.industryId}`);
updateForm('company_type_id', `${dataProfile?.companyTypeId}`);
updateForm('nama_wajib_pajak', dataProfile?.taxName);
updateForm('npwp', dataProfile?.npwp);
updateForm('sppkp', dataProfile?.sppkp);
updateForm('alamat_wajib_pajak', dataProfile?.alamatWajibPajak);
updateForm('alamat_bisnis', dataProfile?.alamatBisnis);
validate();
}
};
if (auth) loadProfile();
}, [auth, setValue]);
useEffect(() => {
updateForm('name', '-');
updateForm('email', 'example@mail.com');
updateForm('password', 'example@mail.com');
updateForm('phone', '081234567890');
validate();
}, [buttonSubmitClick, changeConfirmation]);
const handleChangeBisnis = (value) => {
resetForm();
setSelectedValueBisnis(value);
if (value === 'true') {
validate();
setIsTerdaftar(true);
} else {
validate();
setIsTerdaftar(false);
}
};
const handleChange = (value) => {
setSelectedValue(value);
if (value === 'PKP') {
validate();
setIsPKP(true);
} else {
validate();
setIsPKP(false);
setIsPKP(false);
}
};
const onSubmitHandler = async () => {
setChangeConfirmation(false); // Tutup popup konfirmasi
setIsLoadingPopup(true); // Munculkan popup loading
updateForm('parent_id', `${auth.parentId}`);
const data = form;
if (!isFormValid) {
setNotValid(true);
setButtonSubmitClick(!buttonSubmitClick);
toast.error('Form belum valid. Mohon periksa kembali input Anda.');
setIsLoadingPopup(false);
return;
}
try {
const isUpdated = await switchAccountApi({ data });
if (isUpdated?.switch === 'pending') {
toast.success('Berhasil mengajukan ubah akun', { duration: 1500 });
if (typeof window !== 'undefined') {
localStorage.setItem('autoCheckProfile', 'true');
}
setTimeout(() => {
setIsAprove('pending');
setUbahAkun('pending');
window.location.reload();
}, 1500);
} else {
toast.error('Gagal mengubah akun. Silakan coba lagi nanti atau hubungi admin jika masalah tetap terjadi.');
setIsLoadingPopup(false);
}
} catch (error) {
console.error(error);
toast.error('Terjadi kesalahan saat menghubungi server, silahkan cek internet Anda atau hubungi admin Indoteknik.');
setIsLoadingPopup(false);
}
};
const onSubmitHandlerCancel = async (values) => {
window.location.reload();
};
return (
<>
Bisnis Terdaftar di Indoteknik?
Tipe Bisnis