<?php
session_start();

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

require_once 'db_connect.php';

// جلب تفاصيل المرتجع
$return = [];
$return_items = [];
$return_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($return_id > 0) {
    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
            WHERE pr.id = :id
        ");
        $stmt->execute([':id' => $return_id]);
        $return = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($return) {
            $stmt = $conn->prepare("
                SELECT pri.id, pri.product_id, pri.quantity, pri.unit_price, 
                       i.name AS product_name
                FROM purchase_return_items pri
                LEFT JOIN items i ON pri.product_id = i.id
                WHERE pri.return_id = :return_id
            ");
            $stmt->execute([':return_id' => $return_id]);
            $return_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
    } catch (PDOException $e) {
        error_log("Error fetching purchase return details: " . $e->getMessage());
        echo "<pre>Error: " . $e->getMessage() . "</pre>";
    }
}

if (!$return) {
    die("لم يتم العثور على مرتجع بهذا الرقم!");
}
?>

<!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-back {
            background: linear-gradient(45deg, #9B59B6, #8E44AD);
            border: none;
            color: #fff;
            border-radius: 10px;
            padding: 6px 15px;
            font-weight: bold;
            transition: all 0.4s ease;
            font-size: 0.9rem;
        }

        .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;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="card">
            <h2><i class="fas fa-info-circle me-2"></i> تفاصيل مرتجع الشراء</h2>

            <div class="row g-3 mb-4">
                <div class="col-md-4">
                    <label class="form-label"><i class="fas fa-file-invoice"></i> رقم المرتجع</label>
                    <input type="text" class="form-control" value="<?php echo htmlspecialchars($return['id']); ?>" readonly>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><i class="fas fa-file-invoice"></i> رقم الفاتورة</label>
                    <input type="text" class="form-control" value="<?php echo htmlspecialchars($return['invoice_number'] ?? 'غير محدد'); ?>" readonly>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><i class="fas fa-lock"></i> الخزنة</label>
                    <input type="text" class="form-control" value="<?php echo htmlspecialchars($return['treasury_name'] ?? 'غير محدد'); ?>" readonly>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><i class="fas fa-money-bill"></i> المبلغ المرتجع</label>
                    <input type="text" class="form-control" value="<?php echo number_format($return['amount'], 2); ?> ج.م" readonly>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><i class="fas fa-calendar-day"></i> تاريخ المرتجع</label>
                    <input type="text" class="form-control" value="<?php echo htmlspecialchars(date('Y-m-d', strtotime($return['return_date']))); ?>" readonly>
                </div>
            </div>

            <!-- جدول المنتجات المرتجعة -->
            <div class="table-responsive mt-3">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>اسم المنتج</th>
                            <th>الكمية المرتجعة</th>
                            <th>سعر الوحدة</th>
                            <th>الإجمالي</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($return_items as $item): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($item['product_name'] ?? 'غير معروف'); ?></td>
                                <td><?php echo htmlspecialchars($item['quantity']); ?></td>
                                <td><?php echo number_format($item['unit_price'], 2); ?> ج.م</td>
                                <td><?php echo number_format($item['quantity'] * $item['unit_price'], 2); ?> ج.م</td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($return_items)): ?>
                            <tr>
                                <td colspan="4" 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='purchase_return_invoices.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>