import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar"; import { Button } from "./ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "./ui/dialog"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "./ui/card"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue,} from "./ui/select"; import { Separator } from "./ui/separator"; import { Textarea } from "./ui/textarea"; import React, { useState, useEffect } from 'react'; import {AvatarUpload} from './AvatarUpload'; import {localizeTime} from "../lib/localizeTime"; import Loader from "./ui/loader"; import { Eye, Pencil, Trash2, Download, ChevronUp, ChevronDown, Search } from "lucide-react"; import { PDFDownloadLink } from '@react-pdf/renderer'; import InvoicePDF from "../lib/InvoicePDF"; import { useIsLoggedIn } from '../lib/isLoggedIn'; import PasswordUpdateCard from './PasswordUpdateCard'; interface SessionData { [key: string]: any; } interface UserData { success: boolean; session_data: SessionData; user_avatar: string; } interface BillingItems { [key: string]: any; } type ToastState = { visible: boolean; message: string; }; type Invoice = { billing_id: string; }; type PaymentData = { txn_id: string; user_email: string; }; export default function ProfilePage() { const { isLoggedIn, loading, sessionData } = useIsLoggedIn(); const typedSessionData = sessionData as SessionData | null; const [userData, setUserData] = useState(null); const [invoiceList, setInvoiceList] = useState([]); const [error, setError] = useState(null); const [dialogOpen, setDialogOpen] = useState(false); const [selectedData, setSelectedData] = useState(null); const [toast, setToast] = useState({ visible: false, message: '' }); const [txnId, setTxnId] = useState(''); const [userEmail, setUserEmail] = useState(''); const USER_API_URL = 'https://host-api.cs1.hz.siliconpin.com/v1/users/'; const INVOICE_API_URL = 'https://host-api.cs1.hz.siliconpin.com/v1/invoice/'; useEffect(() => { const fetchSessionData = async () => { try { const response = await fetch(`${USER_API_URL}?query=get-user`, { credentials: 'include', // Crucial for cookies headers: { 'Accept': 'application/json' } } ); const data = await response.json(); if (!response.ok || !data.success) { throw new Error(data.error || 'Session fetch failed'); } setUserData(data); return data.session_data; } catch (error) { console.error('Fetch error:', error); throw error; } }; const getInvoiceListData = async () => { try { const response = await fetch(`${USER_API_URL}?query=invoice-info`, { method: 'GET', credentials: 'include', // Crucial for cookies headers: { 'Accept': 'application/json' } }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); if (!data.success) { throw new Error(data.message || 'Session fetch failed'); } setInvoiceList(data.data); // Fix: Use `data.data` instead of `data` return data.data; // Fix: `session_data` does not exist in response } catch (error) { console.error('Fetch error:', error); throw error; } }; fetchSessionData(); getInvoiceListData(); }, []); const showToast = (message: string) => { setToast({ visible: true, message }); setTimeout(() => setToast({ visible: false, message: '' }), 3000); }; const handlePanelBuyNow = (invoice: Invoice) => { // setTxnId(data.txn_id); // setUserEmail(data.user_email); window.location.href = `/make-payment?query=get-initiated_payment&orderId=${invoice.billing_id}`; showToast('Redirecting to payment page...'); }; const handleViewItems = (items: BillingItems) => { setDialogOpen(true); setSelectedData(items); }; const sessionLogOut = () => { fetch(`${USER_API_URL}?query=logout`, { method: 'GET', credentials: 'include' }) .then(response => response.json()) .then(data => { if(data.success === true){ window.location.href = '/'; } console.log('Logout Console', data.success); }) } if (loading) { return ; } // Then handle not logged in state if (!isLoggedIn) { return (

You are not logged in! Please login first to access this page.{" "} Click Here to login

); } // Then handle error state if (error) { return
Error: {error}
; } // Then handle case where user data hasn't loaded yet if (!userData) { return ; } return (
Personal Information Update your personal information and avatar.
JP {typedSessionData?.id && }
Billing Information View All
View your billing history. { invoiceList.slice(0, 5).map((invoice, index) => ( )) }
Order ID Invoice Date Description Amount Status Action
{invoice.billing_id} {invoice?.created_at.split(' ')[0]}

{invoice.service}

{invoice.amount} {invoice.status.charAt(0).toUpperCase() + invoice.status.slice(1)} } fileName={`invoice_${invoice.billing_id}.pdf`} className="text-[#6d9e37] hover:text-green-600" > {({ loading }) => (loading ? '...' : )} { invoice.status !== 'completed' ? ( ) : '' }
Danger Zone These actions are irreversible. Proceed with caution.
Billing Details Review the details for Billing ID: {selectedData?.billing_id}
Service: {selectedData?.service}
cycle: {selectedData?.cycle}
Amount: ₹{selectedData?.amount}
User: {selectedData?.user}
Status: {selectedData?.status}

Created At: {localizeTime(selectedData?.created_at)}
Updated At: {selectedData?.updated_at ? localizeTime(selectedData.updated_at) : '—'}
Silicon ID: {selectedData?.siliconId}
{/* Optional: Add action buttons here */} {/* */}
); }