<?php
session_start();

// التحقق من تسجيل الدخول
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

require_once 'db_connect.php';

// جلب بيانات مرتجعات الشراء
$returns = [];
try {
    $stmt = $conn->prepare("
        SELECT pr.id, pr.invoice_id, pr.treasury_id, pr.amount, pr.return_date, 
               pi.invoice_number, t.name AS treasury_name
        FROM purchase_returns pr
        LEFT JOIN purchase_invoices pi ON pr.invoice_id = pi.id
        LEFT JOIN treasuries t ON pr.treasury_id = t.id
        ORDER BY pr.return_date DESC
    ");
    $stmt->execute();
    $returns = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    error_log("Error fetching purchase returns: " . $e->getMessage());
    echo "<pre>Error: " . $e->getMessage() . "</pre>";
}
?>

<!DOCTYPE html>
<html lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Astra - مرتجعات الشراء</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            min-height: 100vh;
            direction: rtl;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #2ECC71, #3498DB, #E74C3C);
            overflow-x: hidden;
            animation: gradientShift 10s ease infinite;
            background-size: 400% 400%;
        }

        @keyframes gradientShift {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .container-fluid {
            padding: 20px;
            height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            padding: 20px;
            margin-bottom: 20px;
            animation: fadeIn 1s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        h2 {
            color: #2ECC71;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
            text-shadow: 0 0 5px rgba(46, 204, 113, 0.5);
        }

        .table {
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
        }

        .table thead {
            background: #2ECC71;
            color: #fff;
        }

        .table tbody tr:hover {
            background: rgba(46, 204, 113, 0.1);
        }

        .btn {
            border-radius: 10px;
            padding: 6px 15px;
            font-weight: bold;
            transition: all 0.4s ease;
            position: relative;
            overflow: hidden;
            font-size: 0.9rem;
        }

        .btn-back {
            background: linear-gradient(45deg, #9B59B6, #8E44AD);
            border: none;
            color: #fff;
        }

        .btn-back:hover {
            background: linear-gradient(45deg, #8E44AD, #7D3C98);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(155, 89, 182, 0.5);
        }

        .btn i {
            margin-left: 8px;
        }

        .btn-view {
            background: linear-gradient(45deg, #3498DB, #2980B9);
            border: none;
            color: #fff;
            margin-left: 5px;
        }

        .btn-view:hover {
            background: linear-gradient(45deg, #2980B9, #2471A3);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.5);
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="card">
            <h2><i class="fas fa-undo me-2"></i> مرتجعات الشراء</h2>

            <!-- جدول مرتجعات الشراء -->
            <div class="table-responsive mt-3">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>رقم المرتجع</th>
                            <th>رقم الفاتورة</th>
                            <th>الخزنة</th>
                            <th>المبلغ المرتجع</th>
                            <th>تاريخ المرتجع</th>
                            <th>الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($returns as $return): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($return['id']); ?></td>
                                <td><?php echo htmlspecialchars($return['invoice_number'] ?? 'غير محدد'); ?></td>
                                <td><?php echo htmlspecialchars($return['treasury_name'] ?? 'غير محدد'); ?></td>
                                <td><?php echo number_format($return['amount'], 2); ?> ج.م</td>
                                <td><?php echo htmlspecialchars(date('Y-m-d', strtotime($return['return_date']))); ?></td>
                                <td>
                                    <a href="purchase_return_details.php?id=<?php echo $return['id']; ?>" class="btn btn-view btn-sm"><i class="fas fa-eye"></i> عرض</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($returns)): ?>
                            <tr>
                                <td colspan="6" class="text-center">لا توجد مرتجعات شراء مسجلة.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

            <div class="text-center mt-3">
                <button class="btn btn-back" onclick="window.location.href='dashboard.php'"><i class="fas fa-arrow-right"></i> العودة إلى لوحة التحكم</button>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>