<?php
/**
 * ADMIN UPDATED ITEMS MANAGEMENT PAGE
 * File: admin/updated-items.php
 * Review and approve/reject item updates
 */

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

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

// Initialize models
$item = new Item();

// Get filter parameters
$status_filter = $_GET['status'] ?? 'pending';
$search = $_GET['search'] ?? '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = ADMIN_ITEMS_PER_PAGE;

// Build filters
$filters = [];
if ($status_filter !== 'all') {
    $filters['status'] = $status_filter;
}
if (!empty($search)) {
    $filters['search'] = $search;
}

// Get item updates
$total_updates = $item->countItemUpdates($filters);
$total_pages = ceil($total_updates / $per_page);
$offset = ($page - 1) * $per_page;
$item_updates = $item->getItemUpdates($filters, $per_page, $offset);

// Get statistics
$stats = [
    'total' => $item->countItemUpdates(),
    'pending' => $item->countItemUpdates(['status' => 'pending']),
    'approved' => $item->countItemUpdates(['status' => 'approved']),
    'rejected' => $item->countItemUpdates(['status' => 'rejected'])
];

// Page variables
$page_title = "Updated Items Review";
$active_menu = "updated_items";

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

<div class="main-content">

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

    <div class="page-header">
        <div class="header-left">
            <h1><i class="fas fa-sync-alt"></i> Updated Items Review</h1>
            <p class="text-muted">Review and approve/reject changes made by vendors</p>
        </div>
        <div class="header-right">
            <a href="items.php" class="btn btn-outline-primary">
                <i class="fas fa-box"></i> All Items
            </a>
        </div>
    </div>

    <!-- Content -->
    <div class="content">

    <!-- Statistics Cards -->
    <?php /*
    <div class="stats-grid mb-4">
        <div class="stat-card">
            <div class="stat-icon bg-primary">
                <i class="fas fa-sync-alt"></i>
            </div>
            <div class="stat-details">
                <h3><?= number_format($stats['total']) ?></h3>
                <p>Total Updates</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon bg-warning">
                <i class="fas fa-clock"></i>
            </div>
            <div class="stat-details">
                <h3><?= number_format($stats['pending']) ?></h3>
                <p>Pending Review</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon bg-success">
                <i class="fas fa-check-circle"></i>
            </div>
            <div class="stat-details">
                <h3><?= number_format($stats['approved']) ?></h3>
                <p>Approved Updates</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon bg-danger">
                <i class="fas fa-times-circle"></i>
            </div>
            <div class="stat-details">
                <h3><?= number_format($stats['rejected']) ?></h3>
                <p>Rejected Updates</p>
            </div>
        </div>
    </div>
    */ ?>

    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" class="filter-form">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">Status</label>
                        <select name="status" class="form-control">
                            <option value="pending" <?= $status_filter === 'pending' ? 'selected' : '' ?>>Pending Review</option>
                            <option value="approved" <?= $status_filter === 'approved' ? 'selected' : '' ?>>Approved</option>
                            <option value="rejected" <?= $status_filter === 'rejected' ? 'selected' : '' ?>>Rejected</option>
                            <option value="all" <?= $status_filter === 'all' ? 'selected' : '' ?>>All Status</option>
                        </select>
                    </div>
                    
                    <div class="col-md-6">
                        <label class="form-label">Search</label>
                        <input type="text" name="search" class="form-control" 
                               placeholder="Search items, vendors, update reason..." 
                               value="<?= esc($search) ?>">
                    </div>
                    
                    <div class="col-md-2">
                        <label class="form-label">&nbsp;</label>
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary flex-fill">
                                <i class="fas fa-search"></i> Filter
                            </button>
                            <a href="updated-items.php" class="btn btn-secondary">
                                <i class="fas fa-redo"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Updated Items Table -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">
                <i class="fas fa-list"></i> Item Updates
                <span class="badge bg-secondary"><?= number_format($total_updates) ?> updates</span>
            </h5>
        </div>
        <div class="card-body p-0">
            <?php if (empty($item_updates)): ?>
                <div class="empty-state py-5">
                    <i class="fas fa-sync-alt fa-3x text-muted mb-3"></i>
                    <p class="text-muted">No item updates found</p>
                    <?php if ($status_filter === 'pending'): ?>
                        <a href="items.php" class="btn btn-primary mt-2">
                            <i class="fas fa-box"></i> View All Items
                        </a>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th width="80">Item</th>
                                <th>Update Details</th>
                                <th>Vendor</th>
                                <th width="120">Changes</th>
                                <th width="100">Status</th>
                                <th width="150">Submitted</th>
                                <th width="200">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($item_updates as $update): 
                                $changes = json_decode($update['changes'], true);
                                $change_count = is_array($changes) ? count($changes) : 0;
                            ?>
                                <tr>
                                    <td>
                                        <img src="<?= upload('items/' . $update['item_image']) ?>" 
                                             alt="<?= esc($update['item_title']) ?>"
                                             class="item-thumbnail">
                                    </td>
                                    <td>
                                        <strong><?= esc($update['item_title']) ?></strong><br>
                                        <?php if (!empty($update['update_reason'])): ?>
                                            <small class="text-muted">
                                                <strong>Reason:</strong> <?= esc($update['update_reason']) ?>
                                            </small><br>
                                        <?php endif; ?>
                                        <small class="text-muted">
                                            Update ID: #<?= $update['id'] ?>
                                        </small>
                                    </td>
                                    <td>
                                        <strong><?= esc($update['vendor_name']) ?></strong><br>
                                        <small class="text-muted"><?= esc($update['vendor_email']) ?></small>
                                    </td>
                                    <td>
                                        <span class="badge bg-info"><?= $change_count ?> fields</span><br>
                                        <small class="text-muted">
                                            <?php if ($change_count > 0): ?>
                                                <?= implode(', ', array_slice(array_keys($changes), 0, 3)) ?>
                                                <?php if ($change_count > 3): ?>...<?php endif; ?>
                                            <?php endif; ?>
                                        </small>
                                    </td>
                                    <td><?= format_status_badge($update['status']) ?></td>
                                    <td>
                                        <?= format_date($update['created_at']) ?><br>
                                        <small class="text-muted"><?= format_time_ago($update['created_at']) ?></small>
                                    </td>
                                    <td>
                                        <div class="btn-group-vertical btn-group-sm">
                                            <?php if ($update['status'] === 'pending'): ?>
                                                <div class="btn-group-vertical btn-group-sm">
                                                    <a href="item-update-review.php?id=<?= $update['id'] ?>" 
                                                    class="btn btn-sm btn-info">
                                                        <i class="fas fa-eye"></i> Review
                                                    </a>
                                                    
                                                    <a href="item-update-approve.php?id=<?= $update['id'] ?>&confirm=1" 
                                                    class="btn btn-sm btn-success"
                                                    onclick="return confirm('Are you sure you want to approve these changes?')">
                                                        <i class="fas fa-check"></i> Approve
                                                    </a>
                                                    
                                                    <a href="item-update-reject.php?id=<?= $update['id'] ?>&confirm=1" 
                                                    class="btn btn-sm btn-danger"
                                                    onclick="return confirm('Are you sure you want to reject these changes?')">
                                                        <i class="fas fa-times"></i> Reject
                                                    </a>
                                                </div>
                                            <?php else: ?>
                                                <small class="text-muted text-center">
                                                    Reviewed by: <?= esc($update['reviewer_email'] ?? 'System') ?><br>
                                                    <?= format_date($update['reviewed_at']) ?>
                                                </small>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <?php if ($total_pages > 1): ?>
                    <div class="card-footer">
                        <?php 
                        $pagination_params = array_filter([
                            'status' => $status_filter !== 'pending' ? $status_filter : null,
                            'search' => !empty($search) ? $search : null
                        ]);
                        echo render_pagination($page, $total_pages, 'updated-items.php', $pagination_params); 
                        ?>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>

<style>
.item-thumbnail {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 8px;
}

.btn-group-vertical .btn,
.btn-group-vertical form {
    margin-bottom: 2px;
    width: 100%;
}

.btn-group-vertical form .btn {
    width: 100%;
}
</style>

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