import React, { useState, useEffect } from "react"; import axios from 'axios'; import { Dialog,DialogActions,DialogContent,DialogTitle,TextField,Button,MenuItem,Select,InputLabel,FormControl,Typography, InputAdornment,Grid,Box,List,ListItem, } from "@mui/material"; import UploadFileIcon from "@mui/icons-material/UploadFile"; import Divider from '@mui/material/Divider'; import { initialMember } from "./Columns"; import { apiConfig } from '@/config/api.config'; import ConfirmDialog from '@/components/confirm'; import { toast } from 'sonner'; const BASE_URL_MASTER_DATA = apiConfig.service_master_data; const BASE_URL_CUSTOMER = apiConfig.service_customer; // MAIN PAGE const CustomerDialog = ({ open, handleClose, handleSubmit, initialData, viewStats, handleReject, page, fetchCustomers }: any) => { const [formData, setFormData] = useState(initialData || initialMember); const [viewOnly, setViewOnly] = useState(viewStats || false); const [municipios, setMunicipios] = useState([]); const [aldeias, setAldeias] = useState([]); const [postoAdm, setPostoAdm] = useState([]); const [sucos, setSucos] = useState([]); const [profession, setProfession] = useState([]); const [groupData] = useState({ reguler: `This fill can not be empty!`, premium: `This field required only for Premium or Agent`, agent: `This field required only for Agent` }) useEffect(() => { setFormData(initialData || {}); // Sync formData when initialData changes fetchMasterData() }, [initialData]); async function fetchMasterData() { try { let getProfession = await axios.get(`${BASE_URL_MASTER_DATA}/profession/list`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setProfession(getProfession.data.data.list) let getMunicipios = await axios.get(`${BASE_URL_MASTER_DATA}/municipios/list`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setMunicipios(getMunicipios.data.data.list) let getPostoAdms = await axios.get(`${BASE_URL_MASTER_DATA}/postoadms/list`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setPostoAdm(getPostoAdms.data.data.list) let getSucos = await axios.get(`${BASE_URL_MASTER_DATA}/sucos/list`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setSucos(getSucos.data.data.list) let getAldeias = await axios.get(`${BASE_URL_MASTER_DATA}/aldeias/list`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setAldeias(getAldeias.data.data.list) } catch (error) { console.log(error); } } const handleChange = async (e: any) => { const { name, value } = e.target; if (name === 'municipio' || name === 'posto_adms' || name === 'suco') await getMasterAfter(name, value); if (name === "file_selfie" || name === "photouser" || name === 'file_document_id' || name === "file_document_id_selfie" || name === "file_commercial_license") { // FOR FILE ONLY setFormData({ ...formData, [name]: e.target.files[0] }); } else { setFormData({ ...formData, [name]: value }); } }; async function getMasterAfter(name: string, id: any) { if (name === 'municipio') { let getMunicipiosPosto = await axios.get(`${BASE_URL_MASTER_DATA}/municipios/postoadms/${id}`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setPostoAdm(getMunicipiosPosto.data.data) } if (name === 'posto_adms') { let getPostoSuco = await axios.get(`${BASE_URL_MASTER_DATA}/postoadms/sucos/${id}`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setSucos(getPostoSuco.data.data) } if (name === 'suco') { let getSucoAldeias = await axios.get(`${BASE_URL_MASTER_DATA}/sucos/aldeias/${id}`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setAldeias(getSucoAldeias.data.data) } } const onSubmit = () => { if (page === 'kyc' && !formData.description) return toast.warning(`Description for approval needed!`) handleSubmit(formData); // handleClose(); }; const onReject = () => { handleReject(formData); handleClose(); } function generateDate(date: any, type: any) { if (!date) return '' const today = new Date(date); if (type === 'datetime') return today.toISOString().replace('T', ' ').substring(0, 19); return today.toISOString().split('T')[0]; } return ( Customer Form ({viewOnly ? 'View' : 'Edit'})
Customer Data Created: {generateDate(formData.created_at, 'datetime')}
{ formData.isneedapproval == 1 ? (
User Request for approval
) : ('') } {/* {groupData.reguler} */} {fileTextFile("Photo", formData.photouser, "photouser", handleChange)} Gender Identity Type {/* AGENT & PREMIUM DATA */} {fileTextFile("File Selfie", formData.file_selfie, "file_selfie", handleChange)} {"file_selfie"} {fileTextFile("File Document", formData.file_document_id, "file_document_id", handleChange)} {"file_document_id"} {fileTextFile("File Document & Selfie", formData.file_document_id_selfie, "file_document_id_selfie", handleChange)} {"file_document_id_selfie"} {fileTextFile("File Commercial License", formData.file_commercial_license, "file_commercial_license", handleChange)} {"file_commercial_license"} {/* AGENT & PREMIUM DATA */} {/* */} Profession {/* */} {/* */} Status Municipio Posto Suco Aldeia Bank Bank Name {/* {getAdmAccess(page, formData, handleClose, fetchCustomers)} */} { page === 'kyc' ? ( <> Approval ) : (<> {getAdmAccess(page, formData, handleClose, fetchCustomers, viewOnly, setViewOnly)} {formData.id ? showCustomerWallet(formData.id) : ""} ) }
{ page === 'kyc' ? ( ) : ('') } { formData.isneedapproval == 1 && page === 'kyc' ? ( ) : ('') }
); }; export default CustomerDialog; function fileTextFile(label: string, value: any, name: string, handleChange: any) { return ( {/* */} ), }} /> ) } // ACCESS ADM function getAdmAccess(page: string, data: any, handleClose: any, fetchCustomers: any, viewOnly: any, setViewOnly: any) { const [dialogOpen, setDialogOpen] = useState(false); const [dialogType, setDialogType] = useState(''); const [changeGroup, setChangeGroup] = useState(''); const [changeGroupD, setChangeGroupD] = useState(false); const [groups, setGroups] = useState([]); useEffect(() => { fetchGroups() }, []); const fetchGroups = async () =>{ try { let getGroups = await axios.get(`${BASE_URL_CUSTOMER}/groups/list`, { params: { limit: 50, page: 1, with_deleted: false, order_field: 'name', order_direction: 'ASC', } }); setGroups(getGroups.data.data.list); } catch (error: any) { console.error(error.message); toast.error(error.message) } } const handleYes = async () => { try { if (dialogType === "update status") { let statusNext = getPinStatus(data.status).res; if (statusNext) await axios.put(`${BASE_URL_CUSTOMER}/customer/statuspin`, { customerid: data.id, status: statusNext }) else toast.error("Handle Active/Suspend only") toast.success("Success Update Status") } if (dialogType === "reset pin") { if (data.id) await axios.post(`${BASE_URL_CUSTOMER}/customer/resetpin`, { customerid: data.id }) else throw({ message: 'data.id not found' }) toast.success("Pin will send to customer MSISDN") } } catch (error: any) { toast.error(error.message) } finally { setDialogOpen(false) handleClose() } } function buttonStatus() { setDialogType('update status') setDialogOpen(true) } function buttonResetPin() { setDialogType('reset pin') setDialogOpen(true) } async function buttonChangeGroup() { try { let dataObj = { customerid: data.id, destination_group: changeGroup } if (data.group_id === changeGroup) return toast.warning(`You update same group as the exist customer group`) if (dataObj.customerid && dataObj.destination_group) { await axios.post(`${BASE_URL_CUSTOMER}/customer/change-group`, dataObj) } await fetchCustomers() toast.success('Success Change group') } catch (error: any) { console.log(error); toast.error(error.message) } finally { await fetchCustomers() setChangeGroupD(false) handleClose() } } function openChangeGroupDialog() { setChangeGroup(data.group_id); setChangeGroupD(true) } if (page !== "kyc") { return ( Access Administration Pin Status : {getPinStatus(data.status).msg} Reset PIN Change Group Edit Member {/* */} setChangeGroupD(false)} fullWidth> Are you sure to change customer Group? Destination Group setDialogOpen(false)} title="Confirm Action" content={`Are you sure you want to ${dialogType}?`} onYes={handleYes} onNo={() => setDialogOpen(false)} /> ) } else { return ('') } } function getPinStatus(status: string) { if (status === "Y") return { msg: 'Active', btn: 'Block PIN', res: 'Block' }; if (status === "N") return { msg: 'Not Active', btn: 'Activate PIN', res: null }; if (status === "P") return { msg: 'Suspend PIN', btn: 'Unblock PIN', res: 'UnBlock' }; if (status === "O") return { msg: 'Suspend OTP', btn: 'Unblock OTP', res: null }; return { msg: "None", btn: 'No status found', res: null } } // CUSTOMER WALLET function showCustomerWallet(customerid: any) { const [customerWallet, setCustomerWallet] = useState([]); if (!customerid) return '' useEffect(() => { fetchCustomerWallet() }, []); async function fetchCustomerWallet() { try { let getCustWallet = await axios.get(`${BASE_URL_CUSTOMER}/customer/wallet`, { params: { customerid: customerid }}); setCustomerWallet(getCustWallet.data.data.wallets) } catch (error: any) { toast.error(error.message) } } return ( Wallet Member {customerWallet.length ? (customerWallet.map((item:any, index:any) => ( Name {item.wallet.name} Description {item.wallet.description} Transaction Today {item.transaction_number_today} {index < customerWallet.length - 1 && } ))): "No wallet"} ) }