<?php
/**
 * ADMIN ORDER DETAILS PAGE
 * File: admin/order-details.php
 * View detailed information about a specific order
 */

require_once '../app/Core/init.php';
require_once '../app/Models/OrderRating.php';

// Check admin authentication
Auth::requireAdmin();

$page_title = "Order Details";
$active_menu = "orders";

// Get order ID
$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$order_id) {
    flash('error', 'Invalid order ID', 'danger');
    redirect('orders.php');
}

// Initialize models
$order_model = new Order();
$item_model = new Item();
$customer_model = new Customer();

// Get order details
$order = $order_model->find($order_id);

if (!$order) {
    flash('error', 'Order not found', 'danger');
    redirect('orders.php');
}

// Get related data
$order_items = [];
$payment_info = null;
$order_history = [];

// Get order items if needed
// This would depend on your order_items table structure

// Handle flash messages
$flash_message = flash('message');

include '../templates/layouts/admin/head.php';
?>

<div class="main-content">
    <?php include '../templates/layouts/admin/header.php'; ?>
    
    <!-- Page Header -->
    <div style="padding: 30px; background: white; border-bottom: 1px solid #eee;">
        <h1 style="font-size: 28px; font-weight: 700; color: var(--dark); margin-bottom: 5px;">
            <i class="fas fa-shopping-cart"></i> Order Details
        </h1>
        <div style="display: flex; gap: 8px; font-size: 14px; color: #666;">
            <a href="index.php" style="color: var(--primary); text-decoration: none;">Home</a>
            <span>/</span>
            <a href="orders.php" style="color: var(--primary); text-decoration: none;">Orders</a>
            <span>/</span>
            <span>Order #<?= Security::escapeOutput($order['order_number']) ?></span>
        </div>
    </div>
    
    <!-- Content -->
    <div style="padding: 30px;">
        
        <!-- Flash Messages -->
        <?php if ($flash_message): ?>
            <div class="alert alert-<?= $flash_message['type'] ?>">
                <?= Security::escapeOutput($flash_message['message']) ?>
            </div>
        <?php endif; ?>
        
        <div class="row">
            <div class="col-lg-8">
                <!-- Order Information -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 30px;">
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
                        <h3 style="font-size: 18px; font-weight: 600; margin: 0; color: var(--dark);">
                            <i class="fas fa-info-circle"></i> Order Information
                        </h3>
                        <span class="status-badge status-<?= $order['status'] ?>">
                            <?= ucfirst($order['status']) ?>
                        </span>
                    </div>
                    
                    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;">
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;"><b>Order Number:</b></label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($order['order_number']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;"><b>Status:</b></label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= ucfirst($order['status']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;"><b>Duration:</b></label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?php
                                $start = new DateTime($order['start_date']);
                                $end = new DateTime($order['end_date']);
                                $duration = $start->diff($end);
                                echo $duration->days + 1 . ' days';
                                
                                echo " (" . format_date($order['start_date'], 'M d, Y')." - ".format_date($order['end_date'], 'M d, Y') . ")";
                            ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;"><b>Total Amount:</b></label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--success); margin: 0;">
                                <?= format_money($order['total_amount']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;"><b>Rental:</b></label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= format_money($order['subtotal']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;"><b>Security Deposit:</b></label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= format_money($order['security_deposit']) ?>
                            </p>
                        </div>
                    </div>
                    
                    <?php if (!empty($order['notes'])): ?>
                        <div style="margin-top: 20px;">
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Notes</label>
                            <p style="color: var(--dark); line-height: 1.6;">
                                <?= Security::escapeOutput($order['notes']) ?>
                            </p>
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- Customer Information -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 30px;">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-user"></i> Customer Information
                    </h3>
                    
                    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;">
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Name</label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($order['first_name'] . ' ' . $order['last_name']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Phone</label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($order['customer_phone']) ?>
                            </p>
                        </div>
                    </div>
                    
                    <div style="margin-top: 20px;">
                        <a href="customer-details.php?id=<?= $order['customer_id'] ?>" class="btn btn-primary">
                            <i class="fas fa-eye"></i> View Customer Details
                        </a>
                    </div>
                </div>
                
                <!-- Item Information -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 30px;">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-box"></i> Item Information
                    </h3>
                    
                    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;">
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Item Title</label>
                            <p style="font-size: 16px; font-weight: 600; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($order['item_title']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Vendor</label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= Security::escapeOutput($order['vendor_name']) ?>
                            </p>
                        </div>
                        <div>
                            <label style="display: block; margin-bottom: 8px; font-weight: 600; color: #666;">Price per Day</label>
                            <p style="font-size: 16px; color: var(--dark); margin: 0;">
                                <?= format_money($order['price_per_day']) ?>
                            </p>
                        </div>
                    </div>
                    
                    <div style="margin-top: 20px; display: flex; gap: 10px; flex-wrap: wrap;">
                        <a href="item-details.php?id=<?= $order['item_id'] ?>" class="btn btn-primary">
                            <i class="fas fa-eye"></i> View Item Details
                        </a>
                        <?php if (!empty($order['vendor_id'])): ?>
                            <a href="vendor-details.php?id=<?= $order['vendor_id'] ?>" class="btn btn-secondary">
                                <i class="fas fa-store"></i> View Vendor Details
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-4">
                <!-- Quick Actions -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05);">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-cog"></i> Quick Actions
                    </h3>
                    
                    <div style="display: flex; flex-direction: column; gap: 10px;">
                        <a href="order-update-status.php?id=<?= $order_id ?>&status=confirmed" class="btn btn-success">
                            <i class="fas fa-check"></i> Confirm Order
                        </a>
                        <!-- <a href="order-update-status.php?id=<?= $order_id ?>&status=completed" class="btn btn-primary">
                            <i class="fas fa-check-double"></i> Mark Completed
                        </a> -->
                        <a href="order-update-status.php?id=<?= $order_id ?>&status=cancelled" class="btn btn-warning">
                            <i class="fas fa-times"></i> Cancel Order
                        </a>
                    </div>
                </div>
                
                <!-- Order Timeline -->
                <div style="background: white; border-radius: 12px; padding: 25px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-top: 20px;">
                    <h3 style="font-size: 18px; font-weight: 600; margin-bottom: 20px; color: var(--dark);">
                        <i class="fas fa-history"></i> Order Timeline
                    </h3>
                    
                    <div style="font-size: 14px; color: #666;">
                        <div style="margin-bottom: 15px; padding-left: 20px; position: relative;">
                            <div style="position: absolute; left: 0; top: 5px; width: 8px; height: 8px; background: var(--success); border-radius: 50%;"></div>
                            <strong>Order Placed</strong><br>
                            <?= format_date($order['created_at'], 'M d, Y h:i A') ?>
                        </div>
                        
                        <?php if ($order['confirmed_at']): ?>
                            <div style="margin-bottom: 15px; padding-left: 20px; position: relative;">
                                <div style="position: absolute; left: 0; top: 5px; width: 8px; height: 8px; background: var(--info); border-radius: 50%;"></div>
                                <strong>Order Confirmed</strong><br>
                                <?= format_date($order['confirmed_at'], 'M d, Y h:i A') ?>
                            </div>
                        <?php endif; ?>
                        
                        <?php if ($order['completed_at']): ?>
                            <div style="margin-bottom: 15px; padding-left: 20px; position: relative;">
                                <div style="position: absolute; left: 0; top: 5px; width: 8px; height: 8px; background: var(--success); border-radius: 50%;"></div>
                                <strong>Order Completed</strong><br>
                                <?= format_date($order['completed_at'], 'M d, Y h:i A') ?>
                            </div>
                        <?php endif; ?>
                        
                        <?php if ($order['updated_at']): ?>
                            <div style="padding-left: 20px; position: relative;">
                                <div style="position: absolute; left: 0; top: 5px; width: 8px; height: 8px; background: #6c757d; border-radius: 50%;"></div>
                                <strong>Last Updated</strong><br>
                                <?= format_date($order['updated_at'], 'M d, Y h:i A') ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>


                <!-- ================================================================
                    RATINGS DISPLAY COMPONENT
                    ================================================================
                    Add this section to ALL order-details.php pages (customer, vendor, admin)
                    Place it after the order information section
                    ================================================================ -->

                <?php
                // Get ratings for this order
                $ratingModel = new OrderRating();
                $ratings = $ratingModel->getByOrder($order['id']);
                $customerRating = $ratingModel->getCustomerRating($order['id']);
                $vendorRating = $ratingModel->getVendorRating($order['id']);

                // Function to display stars
                function displayRatingStars($rating) {
                    $html = '';
                    for ($i = 1; $i <= 5; $i++) {
                        if ($i <= $rating) {
                            $html .= '<i class="fas fa-star text-warning"></i>';
                        } else {
                            $html .= '<i class="far fa-star text-muted"></i>';
                        }
                    }
                    return $html;
                }
                ?>

                <?php if ($order['status'] === 'completed'): ?>
                    <?php if (Auth::userType() === 'customer' && !$customerRating): ?>
                    <!-- Customer: Show Rate Order Button -->
                    <div class="card mb-4">
                        <div class="card-body text-center py-4">
                            <i class="fas fa-star fa-2x text-warning mb-3"></i>
                            <h6 class="mb-3">How was your experience?</h6>
                            <button type="button" class="btn btn-primary" onclick="openRatingModal(<?= $order['id'] ?>)">
                                <i class="fas fa-star me-2"></i>Rate This Order
                            </button>
                        </div>
                    </div>
                    <?php elseif (Auth::userType() === 'vendor' && !$vendorRating): ?>
                    <!-- Vendor: Show Rate Customer Button -->
                    <div class="card mb-4">
                        <div class="card-body text-center py-4">
                            <i class="fas fa-star fa-2x text-warning mb-3"></i>
                            <h6 class="mb-3">Rate your customer</h6>
                            <button type="button" class="btn btn-primary" onclick="openVendorRatingModal(<?= $order['id'] ?>)">
                                <i class="fas fa-star me-2"></i>Rate Customer
                            </button>
                        </div>
                    </div>
                    <?php endif; ?>
                <?php endif; ?>


                <br>

                <?php if (!empty($ratings)): ?>
                <!-- Ratings Section -->
                <div class="card mb-4">
                    <div class="card-header bg-white">
                        <h5 class="mb-0">
                            <i class="fas fa-star text-warning me-2"></i>Ratings & Reviews
                        </h5>
                    </div>
                    <div class="card-body">
                        <?php if ($customerRating): ?>
                        <!-- Customer's Rating -->
                        <div class="mb-4 pb-4 border-bottom">
                            <div class="d-flex align-items-start">
                                <div class="flex-shrink-0">
                                    <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center" 
                                        style="width: 50px; height: 50px;">
                                        <i class="fas fa-user"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="mb-1">Customer Rating</h6>
                                    <small class="text-muted">
                                        <?= date('M d, Y - h:i A', strtotime($customerRating['created_at'])) ?>
                                    </small>
                                    
                                    <!-- Item Rating -->
                                    <div class="mt-3">
                                        <strong>Item Rating:</strong>
                                        <div class="d-inline-block ms-2">
                                            <?= displayRatingStars($customerRating['item_rating']) ?>
                                            <span class="ms-2 text-muted">(<?= $customerRating['item_rating'] ?>/5)</span>
                                        </div>
                                        <?php if (!empty($customerRating['item_review'])): ?>
                                        <p class="mt-2 mb-0 text-muted">
                                            <i class="fas fa-quote-left me-1"></i>
                                            <?= htmlspecialchars($customerRating['item_review']) ?>
                                            <i class="fas fa-quote-right ms-1"></i>
                                        </p>
                                        <?php endif; ?>
                                    </div>
                                    
                                    <!-- Vendor Rating -->
                                    <div class="mt-3">
                                        <strong>Vendor Rating:</strong>
                                        <div class="d-inline-block ms-2">
                                            <?= displayRatingStars($customerRating['vendor_rating']) ?>
                                            <span class="ms-2 text-muted">(<?= $customerRating['vendor_rating'] ?>/5)</span>
                                        </div>
                                        <?php if (!empty($customerRating['vendor_review'])): ?>
                                        <p class="mt-2 mb-0 text-muted">
                                            <i class="fas fa-quote-left me-1"></i>
                                            <?= htmlspecialchars($customerRating['vendor_review']) ?>
                                            <i class="fas fa-quote-right ms-1"></i>
                                        </p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($vendorRating): ?>
                        <!-- Vendor's Rating -->
                        <div class="mb-0">
                            <div class="d-flex align-items-start">
                                <div class="flex-shrink-0">
                                    <div class="rounded-circle bg-success text-white d-flex align-items-center justify-content-center" 
                                        style="width: 50px; height: 50px;">
                                        <i class="fas fa-store"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="mb-1">Vendor Rating</h6>
                                    <small class="text-muted">
                                        <?= date('M d, Y - h:i A', strtotime($vendorRating['created_at'])) ?>
                                    </small>
                                    
                                    <!-- Customer Rating -->
                                    <div class="mt-3">
                                        <strong>Customer Rating:</strong>
                                        <div class="d-inline-block ms-2">
                                            <?= displayRatingStars($vendorRating['customer_rating']) ?>
                                            <span class="ms-2 text-muted">(<?= $vendorRating['customer_rating'] ?>/5)</span>
                                        </div>
                                        <?php if (!empty($vendorRating['customer_review'])): ?>
                                        <p class="mt-2 mb-0 text-muted">
                                            <i class="fas fa-quote-left me-1"></i>
                                            <?= htmlspecialchars($vendorRating['customer_review']) ?>
                                            <i class="fas fa-quote-right ms-1"></i>
                                        </p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (!$customerRating && !$vendorRating): ?>
                        <div class="text-center text-muted py-3">
                            <i class="fas fa-star-half-alt fa-2x mb-2"></i>
                            <p class="mb-0">No ratings yet for this order</p>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>

            </div>
        </div>
    </div>
</div>

<style>
.btn {
    border: none;
    border-radius: 6px;
    padding: 10px 20px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
    display: inline-block;
    font-size: 14px;
    text-align: center;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: #0056b3;
    transform: translateY(-2px);
}

.btn-success {
    background: var(--success);
    color: white;
}

.btn-success:hover {
    background: #1e7e34;
}

.btn-info {
    background: var(--info);
    color: white;
}

.btn-info:hover {
    background: #138496;
}

.btn-warning {
    background: var(--warning);
    color: white;
}

.btn-warning:hover {
    background: #e0a800;
}

.alert {
    padding: 15px 20px;
    border-radius: 8px;
    margin-bottom: 20px;
}

.alert-success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.alert-danger {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    color: white;
}

.status-pending {
    background: var(--warning);
}

.status-confirmed {
    background: var(--info);
}

.status-completed {
    background: var(--success);
}

.status-cancelled {
    background: var(--danger);
}
</style>

<?php include '../templates/layouts/admin/footer.php'; ?>
