prepare("UPDATE users SET first_name = ?, last_name = ?, email = ? WHERE id = ?");
$stmt->execute([$first_name, $last_name, $email, $user_id]);
$_SESSION['profile_message'] = 'اطلاعات شما با موفقیت بهروزرسانی شد.';
$_SESSION['profile_message_type'] = 'success';
} catch (PDOException $e) {
// Check for duplicate email error
if ($e->errorInfo[1] == 1062) {
$_SESSION['profile_message'] = 'این ایمیل قبلاً ثبت شده است. لطفاً ایمیل دیگری را امتحان کنید.';
} else {
$_SESSION['profile_message'] = 'خطا در بهروزرسانی اطلاعات.';
}
$_SESSION['profile_message_type'] = 'danger';
}
}
header('Location: profile.php?page=account');
exit;
} elseif ($_POST['action'] === 'update_password') {
$new_password = $_POST['new_password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
if (strlen($new_password) < 8) {
$_SESSION['profile_message'] = 'رمز عبور جدید باید حداقل ۸ کاراکتر باشد.';
$_SESSION['profile_message_type'] = 'danger';
} elseif ($new_password !== $confirm_password) {
$_SESSION['profile_message'] = 'رمزهای عبور جدید با هم مطابقت ندارند.';
$_SESSION['profile_message_type'] = 'danger';
} elseif (!empty($new_password)) {
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
if ($stmt->execute([$hashed_password, $user_id])) {
$_SESSION['profile_message'] = 'رمز عبور شما با موفقیت تغییر کرد.';
$_SESSION['profile_message_type'] = 'success';
} else {
$_SESSION['profile_message'] = 'خطا در تغییر رمز عبور.';
$_SESSION['profile_message_type'] = 'danger';
}
}
header('Location: profile.php?page=account');
exit;
} elseif ($_POST['action'] === 'add_address') {
$province = trim($_POST['province'] ?? '');
$city = trim($_POST['city'] ?? '');
$address_line = trim($_POST['address_line'] ?? '');
$postal_code = trim($_POST['postal_code'] ?? '');
$is_default = isset($_POST['is_default']);
if (empty($province) || empty($city) || empty($address_line) || empty($postal_code)) {
$_SESSION['profile_message'] = 'لطفاً تمام فیلدهای آدرس را پر کنید.';
$_SESSION['profile_message_type'] = 'danger';
} else {
$pdo->beginTransaction();
try {
if ($is_default) {
$stmt = $pdo->prepare("UPDATE user_addresses SET is_default = 0 WHERE user_id = ?");
$stmt->execute([$user_id]);
}
$stmt = $pdo->prepare("INSERT INTO user_addresses (user_id, province, city, address_line, postal_code, is_default) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$user_id, $province, $city, $address_line, $postal_code, $is_default ? 1 : 0]);
$pdo->commit();
$_SESSION['profile_message'] = 'آدرس جدید با موفقیت اضافه شد.';
$_SESSION['profile_message_type'] = 'success';
} catch (PDOException $e) {
$pdo->rollBack();
$_SESSION['profile_message'] = 'خطا در افزودن آدرس.';
$_SESSION['profile_message_type'] = 'danger';
}
}
header('Location: profile.php?page=addresses');
exit;
} elseif ($_POST['action'] === 'delete_address') {
$address_id = $_POST['address_id'] ?? 0;
$stmt = $pdo->prepare("DELETE FROM user_addresses WHERE id = ? AND user_id = ?");
if ($stmt->execute([$address_id, $user_id])) {
$_SESSION['profile_message'] = 'آدرس با موفقیت حذف شد.';
$_SESSION['profile_message_type'] = 'success';
} else {
$_SESSION['profile_message'] = 'خطا در حذف آدرس.';
$_SESSION['profile_message_type'] = 'danger';
}
header('Location: profile.php?page=addresses');
exit;
} elseif ($_POST['action'] === 'set_default_address') {
$address_id = $_POST['address_id'] ?? 0;
$pdo->beginTransaction();
try {
$stmt1 = $pdo->prepare("UPDATE user_addresses SET is_default = 0 WHERE user_id = ?");
$stmt1->execute([$user_id]);
$stmt2 = $pdo->prepare("UPDATE user_addresses SET is_default = 1 WHERE id = ? AND user_id = ?");
$stmt2->execute([$address_id, $user_id]);
$pdo->commit();
$_SESSION['profile_message'] = 'آدرس پیشفرض با موفقیت تغییر کرد.';
$_SESSION['profile_message_type'] = 'success';
} catch (PDOException $e) {
$pdo->rollBack();
$_SESSION['profile_message'] = 'خطا در تغییر آدرس پیشفرض.';
$_SESSION['profile_message_type'] = 'danger';
}
header('Location: profile.php?page=addresses');
exit;
}
}
// Retrieve flash message
if (isset($_SESSION['profile_message'])) {
$flash_message = $_SESSION['profile_message'];
$flash_message_type = $_SESSION['profile_message_type'];
unset($_SESSION['profile_message']);
unset($_SESSION['profile_message_type']);
}
// Fetch user data
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Fetch user addresses
$stmt_addresses = $pdo->prepare("SELECT * FROM user_addresses WHERE user_id = ? ORDER BY is_default DESC, id DESC");
$stmt_addresses->execute([$user_id]);
$addresses = $stmt_addresses->fetchAll(PDO::FETCH_ASSOC);
// Fetch user orders with items
$stmt_orders = $pdo->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC");
$stmt_orders->execute([$user_id]);
$orders = $stmt_orders->fetchAll(PDO::FETCH_ASSOC);
// Calculate total purchase amount from COMPLETED orders
$total_purchase_amount = 0;
foreach ($orders as $order) {
if (strtolower($order['status']) === 'completed') {
$total_purchase_amount += $order['total_amount'];
}
}
$page_title = 'حساب کاربری';
require_once 'includes/header.php';
?>
حساب کاربری من
منوی حساب کاربری
= htmlspecialchars(($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')); ?>
= htmlspecialchars($user['email'] ?? ''); ?>
= htmlspecialchars($flash_message); ?>
سلام، = htmlspecialchars($user['first_name'] ?? 'کاربر'); ?> عزیز!
به پنل کاربری خود خوش آمدید. از اینجا میتوانید آخرین سفارشات خود را مشاهده کرده، اطلاعات حساب خود را مدیریت کنید و آدرسهای خود را بهروزرسانی نمایید.
تعداد کل سفارشات
= count($orders); ?>
مجموع خرید شما
= number_format($total_purchase_amount); ?> تومان
شما هنوز هیچ سفارشی ثبت نکردهاید.
شماره سفارش
تاریخ
وضعیت
مبلغ کل
عملیات
'در انتظار پرداخت',
'processing' => 'در حال پردازش',
'shipped' => 'ارسال شده',
'completed' => 'تکمیل شده',
'cancelled' => 'لغو شده',
];
$order_status_lower = strtolower($order['status']);
$status_label = $status_map[$order_status_lower] ?? htmlspecialchars($order['status']);
$status_class = 'status-' . htmlspecialchars($order_status_lower);
?>
#= $order['id']; ?>
= jdate('d F Y', strtotime($order['created_at'])); ?>
= $status_label; ?>
= number_format($order['total_amount']); ?> تومان
نمایش جزئیات
شما هنوز هیچ آدرسی ثبت نکردهاید.
= htmlspecialchars(implode(', ', array_filter([$address['province'], $address['city'], $address['address_line'], $address['postal_code']]))) ?>
پیشفرض
حذف
انتخاب به عنوان پیشفرض
صفحه مورد نظر یافت نشد.