// 'use client';
// import { useState, useEffect } from 'react';
// import { motion } from 'framer-motion';
// import { useRouter } from 'next/navigation';
// import {
// DollarSign,
// TrendingUp,
// Wallet,
// Star,
// Eye,
// Download,
// CalendarDays
// } from 'lucide-react';
// import toast, { Toaster } from 'react-hot-toast';
// import AuthService from '@/app/services/AuthService';
// const StatCard = ({ title, value, icon: Icon, color }) => {
// return (
//
//
// {title}
// {value}
//
// );
// };
// const PropertyProfitCard = ({ property, onViewDetails }) => {
// const formatCurrency = (amount) => `$${amount?.toLocaleString()}`;
// return (
//
//
//
//
//
{property.title}
// {property.isNotSeized && (
//
// غير محجوز
//
// )}
//
//
{property.location}
//
//
//
//
الإيرادات
//
{formatCurrency(property.revenue)}
//
//
//
العمولة
//
{formatCurrency(property.commission)}
//
//
//
المتبقي
//
{formatCurrency(property.remaining)}
//
//
//
//
//
// التقييم العام:
// {property.valuation}
//
//
//
// مؤجر {property.rentedCount} مرة
//
//
//
//
//
// );
// };
// const PropertyCalendar = ({ year, month }) => {
// const [currentMonth, setCurrentMonth] = useState(new Date(year, month - 1));
// const monthNames = ['يناير', 'فبراير', 'مارس', 'إبريل', 'مايو', 'يونيو', 'يوليو', 'أغسطس', 'سبتمبر', 'أكتوبر', 'نوفمبر', 'ديسمبر'];
// const weekDays = ['إثنين', 'ثلاثاء', 'أربعاء', 'خميس', 'جمعة', 'سبت', 'أحد'];
// const getDaysInMonth = (date) => {
// return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
// };
// const getFirstDayOfMonth = (date) => {
// const day = new Date(date.getFullYear(), date.getMonth(), 1).getDay();
// return day === 0 ? 6 : day - 1;
// };
// const daysInMonth = getDaysInMonth(currentMonth);
// const firstDayIndex = getFirstDayOfMonth(currentMonth);
// const cells = [];
// for (let i = 0; i < firstDayIndex; i++) {
// cells.push(
);
// }
// for (let d = 1; d <= daysInMonth; d++) {
// cells.push(
//
// {d}
//
// );
// }
// return (
//
//
//
//
// {monthNames[currentMonth.getMonth()]} {currentMonth.getFullYear()}
//
//
//
//
//
//
//
// {weekDays.map(day => (
//
{day}
// ))}
//
//
// {cells}
//
//
// );
// };
// export default function OwnerProfitsPage() {
// const router = useRouter();
// const [user, setUser] = useState(null);
// const [isLoading, setIsLoading] = useState(true);
// const [summary] = useState({
// totalRevenue: 4290,
// totalCommission: 644,
// remainingBalance: 3647,
// });
// const [properties] = useState([
// {
// id: 1,
// title: 'Damascus Olive Residence',
// location: 'دمشق، المزة',
// isNotSeized: true,
// revenue: 3240,
// commission: 486,
// remaining: 2754,
// valuation: 'جيد جدا',
// rentedCount: 18,
// },
// ]);
// useEffect(() => {
// if (AuthService.isGuest()) {
// router.push('/auth/choose-role');
// return;
// }
// if (!AuthService.isOwner()) {
// router.push('/');
// return;
// }
// const authUser = AuthService.getUser();
// if (authUser) {
// setUser({
// name: authUser.name || authUser.email,
// email: authUser.email,
// });
// }
// setIsLoading(false);
// }, [router]);
// const formatCurrency = (amount) => `$${amount?.toLocaleString()}`;
// const handleViewDetails = (property) => {
// toast.info(`عرض تفاصيل ${property.title}`);
// };
// const handleExportReport = () => {
// toast.success('جاري تصدير التقرير...');
// };
// if (isLoading) {
// return (
//
// );
// }
// return (
//
//
//
//
//
دفتر الحسابات
//
نظرة عامة على أرباح المالك
//
//
//
//
//
//
//
//
عقاراتي
//
// {properties.map((property) => (
//
// ))}
//
//
//
// {/*
//
//
*/}
//
//
// );
// }
'use client';
import { useState, useEffect } from 'react';
import { motion } from 'framer-motion';
import { useRouter } from 'next/navigation';
import { Download, Loader2 } from 'lucide-react';
import toast, { Toaster } from 'react-hot-toast';
import * as XLSX from 'xlsx';
import { useTranslation } from 'react-i18next';
import AuthService from '@/app/services/AuthService';
export default function OwnerProfitsPage() {
const { t, i18n } = useTranslation();
const router = useRouter();
const [user, setUser] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [tableData, setTableData] = useState([]);
const sampleData = [
{
id: 1,
property: 'A000000001',
bookingNumber: 'XX-101',
fromDate: '2025-05-01',
toDate: '2025-05-07',
amountReceived: 500,
platformCommission: 0,
transferredToOwner: 0,
transferReceipt: '—',
},
{
id: 2,
property: 'A000000002',
bookingNumber: 'XX-202',
fromDate: '2025-05-10',
toDate: '2025-05-15',
amountReceived: 300,
platformCommission: 0,
transferredToOwner: 0,
transferReceipt: '—',
},
{
id: 3,
property: 'A000000003',
bookingNumber: 'XX-309',
fromDate: '2025-06-01',
toDate: '2025-06-05',
amountReceived: 800,
platformCommission: 150,
transferredToOwner: 0,
transferReceipt: 'قيد الانتظار',
},
];
const computeRows = (data) => {
return data.map((item) => {
const platformProfit = item.amountReceived * 0.05;
const ownerDue = item.amountReceived - platformProfit;
return {
...item,
platformProfit,
ownerDue,
};
});
};
useEffect(() => {
if (AuthService.isGuest()) {
router.push('/auth/choose-role');
return;
}
if (!AuthService.isOwner()) {
router.push('/');
return;
}
const authUser = AuthService.getUser();
if (authUser) {
setUser({
name: authUser.name || authUser.email,
email: authUser.email,
});
}
const stored = localStorage.getItem('ownerProfitsTable');
if (stored) {
setTableData(computeRows(JSON.parse(stored)));
} else {
setTableData(computeRows(sampleData));
localStorage.setItem('ownerProfitsTable', JSON.stringify(sampleData));
}
setIsLoading(false);
}, [router]);
const totals = tableData.reduce(
(acc, row) => {
acc.totalAmountReceived += row.amountReceived;
acc.totalCommission += row.platformCommission;
acc.totalPlatformProfit += row.platformProfit;
acc.totalOwnerDue += row.ownerDue;
acc.totalTransferred += row.transferredToOwner;
return acc;
},
{
totalAmountReceived: 0,
totalCommission: 0,
totalPlatformProfit: 0,
totalOwnerDue: 0,
totalTransferred: 0,
}
);
const handleExportReport = () => {
try {
const exportData = tableData.map((row) => ({
[t('profits.colProperty')]: row.property,
[t('profits.colBookingNumber')]: row.bookingNumber,
[t('profits.colFromDate')]: row.fromDate,
[t('profits.colToDate')]: row.toDate,
[t('profits.colAmountReceived')]: row.amountReceived,
[t('profits.colPlatformCommission')]: row.platformCommission,
[t('profits.colPlatformProfit')]: row.platformProfit,
[t('profits.colOwnerDue')]: row.ownerDue,
[t('profits.colTransferred')]: row.transferredToOwner,
[t('profits.colTransferReceipt')]: row.transferReceipt,
}));
exportData.push({
[t('profits.colProperty')]: t('profits.totalGeneral'),
[t('profits.colBookingNumber')]: '',
[t('profits.colFromDate')]: '',
[t('profits.colToDate')]: '',
[t('profits.colAmountReceived')]: totals.totalAmountReceived,
[t('profits.colPlatformCommission')]: totals.totalCommission,
[t('profits.colPlatformProfit')]: totals.totalPlatformProfit,
[t('profits.colOwnerDue')]: totals.totalOwnerDue,
[t('profits.colTransferred')]: totals.totalTransferred,
[t('profits.colTransferReceipt')]: '—',
});
const worksheet = XLSX.utils.json_to_sheet(exportData);
const colWidths = [
{ wch: 15 },
{ wch: 12 },
{ wch: 12 },
{ wch: 12 },
{ wch: 14 },
{ wch: 14 },
{ wch: 16 },
{ wch: 16 },
{ wch: 16 },
{ wch: 18 },
];
worksheet['!cols'] = colWidths;
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, t('profits.sheetName'));
XLSX.writeFile(workbook, `${t('profits.fileName')}${new Date().toISOString().slice(0,19).replace(/:/g, '-')}.xlsx`);
toast.success(t('profits.exportSuccess'));
} catch (error) {
console.error(t('profits.exportErrorLog'), error);
toast.error(t('profits.exportError'));
}
};
if (isLoading) {
return (
);
}
return (
{t('profits.pageTitle')}
{t('profits.greeting')} {user?.name}
| {t('profits.colProperty')} |
{t('profits.colBookingNumber')} |
{t('profits.colFromDate')} |
{t('profits.colToDate')} |
{t('profits.colAmountReceived')} |
{t('profits.colPlatformCommission')} |
{t('profits.colPlatformProfit')} {t('profits.colPlatformProfitSub')}
|
{t('profits.colOwnerDue')} |
{t('profits.colTransferred')} |
{t('profits.colTransferReceipt')} |
{tableData.map((row, idx) => (
|
{row.property}
|
{row.bookingNumber}
|
{row.fromDate}
|
{row.toDate}
|
{row.amountReceived}
|
{row.platformCommission}
|
{row.platformProfit}
|
{row.ownerDue}
|
{row.transferredToOwner}
|
{row.transferReceipt}
|
))}
|
{t('profits.totalGeneral')}
|
{totals.totalAmountReceived}
|
{totals.totalCommission}
|
{totals.totalPlatformProfit}
|
{totals.totalOwnerDue}
|
{totals.totalTransferred}
|
— |
{t('profits.note')}
{t('profits.colPlatformProfit')} {t('profits.noteMiddle')}5%{t('profits.noteEnd', { colName: t('profits.colAmountReceived') })}
);
}