// '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 AuthService from '@/app/services/AuthService';
export default function OwnerProfitsPage() {
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) => ({
'العقار': row.property,
'رقم الحجز': row.bookingNumber,
'من تاريخ': row.fromDate,
'حتى تاريخ': row.toDate,
'العروض المستلم': row.amountReceived,
'عمولة المنصة': row.platformCommission,
'ربح المنصة (5%)': row.platformProfit,
'المستحق للمالك': row.ownerDue,
'تم التحويل للمالك': row.transferredToOwner,
'رقم وصل التحويل': row.transferReceipt,
}));
exportData.push({
'العقار': 'الإجمالي العام',
'رقم الحجز': '',
'من تاريخ': '',
'حتى تاريخ': '',
'العروض المستلم': totals.totalAmountReceived,
'عمولة المنصة': totals.totalCommission,
'ربح المنصة (5%)': totals.totalPlatformProfit,
'المستحق للمالك': totals.totalOwnerDue,
'تم التحويل للمالك': totals.totalTransferred,
'رقم وصل التحويل': '—',
});
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, 'أرباح المالك');
XLSX.writeFile(workbook, `تقرير_الأرباح_${new Date().toISOString().slice(0,19).replace(/:/g, '-')}.xlsx`);
toast.success('تم تصدير التقرير بنجاح!');
} catch (error) {
console.error('خطأ في التصدير:', error);
toast.error('حدث خطأ أثناء تصدير التقرير');
}
};
if (isLoading) {
return (
جاري تحميل بيانات الأرباح...
);
}
return (
أرباح المالك
مرحباً {user?.name}
| العقار |
رقم الحجز |
من تاريخ |
حتى تاريخ |
العروض المستلم |
عمولة المنصة |
ربح المنصة (5% من العربون)
|
المستحق للمالك |
تم التحويل للمالك |
رقم وصل التحويل |
{tableData.map((row, idx) => (
|
{row.property}
|
{row.bookingNumber}
|
{row.fromDate}
|
{row.toDate}
|
{row.amountReceived}
|
{row.platformCommission}
|
{row.platformProfit}
|
{row.ownerDue}
|
{row.transferredToOwner}
|
{row.transferReceipt}
|
))}
|
الإجمالي العام
|
{totals.totalAmountReceived}
|
{totals.totalCommission}
|
{totals.totalPlatformProfit}
|
{totals.totalOwnerDue}
|
{totals.totalTransferred}
|
— |
ملاحظة:
ربح المنصة يُحتسب تلقائياً بنسبة 5% من قيمة «العروض المستلم».
);
}