<?php
/**
 * VENDOR DISPUTES/REPORTS PAGE
 * File: vendor/disputes.php
 * View all reported issues
 */

require_once '../app/Core/init.php';
Auth::requireVendor();

$vendor_user_id = Auth::id();
$db = Database::getInstance();

// Get vendor details
$vendor = new Vendor();
$vendor_data = $vendor->getByUserId($vendor_user_id);

if (!$vendor_data) {
    flash('error', 'Please complete your profile first.', 'danger');
    redirect('vendor/profile.php');
}

// Get all disputes involving this vendor
$disputes_sql = "SELECT d.*, 
                 o.order_number,
                 i.title as item_title,
                 CONCAT(c.first_name, ' ', c.last_name) as customer_name,
                 (SELECT COUNT(*) FROM dispute_messages dm WHERE dm.dispute_id = d.id AND dm.sender_type != 'vendor' AND dm.is_read_by_vendor = 0) as unread_count,
                 (SELECT message FROM dispute_messages dm WHERE dm.dispute_id = d.id ORDER BY dm.created_at DESC LIMIT 1) as last_message,
                 (SELECT created_at FROM dispute_messages dm WHERE dm.dispute_id = d.id ORDER BY dm.created_at DESC LIMIT 1) as last_message_time
                 FROM disputes d
                 JOIN orders o ON d.order_id = o.id
                 JOIN items i ON o.item_id = i.id
                 JOIN customers c ON o.customer_id = c.id
                 WHERE o.vendor_id = ?
                 ORDER BY d.created_at DESC";

$disputes = $db->fetchAll($disputes_sql, [$vendor_data['id']]);

$page_title = 'Disputes & Reports - ' . APP_NAME;
include '../templates/layouts/vendor/header.php';
?>

<div class="main-content">
    <div class="page-header mb-4">
        <h1><i class="fas fa-exclamation-triangle"></i> Disputes & Reports</h1>
        <p class="text-muted">Manage issues reported by customers</p>
    </div>

    <!-- Stats -->
    <div class="row mb-4">
        <?php
        $open_count = 0;
        $in_progress_count = 0;
        $resolved_count = 0;
        foreach ($disputes as $d) {
            if ($d['status'] === 'open') $open_count++;
            if ($d['status'] === 'in_progress') $in_progress_count++;
            if ($d['status'] === 'resolved') $resolved_count++;
        }
        ?>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-warning"><i class="fas fa-exclamation-circle"></i></div>
                <div class="stat-details">
                    <h3><?= $open_count ?></h3>
                    <p>Open</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-info"><i class="fas fa-tasks"></i></div>
                <div class="stat-details">
                    <h3><?= $in_progress_count ?></h3>
                    <p>In Progress</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-success"><i class="fas fa-check-circle"></i></div>
                <div class="stat-details">
                    <h3><?= $resolved_count ?></h3>
                    <p>Resolved</p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card">
                <div class="stat-icon bg-primary"><i class="fas fa-list"></i></div>
                <div class="stat-details">
                    <h3><?= count($disputes) ?></h3>
                    <p>Total</p>
                </div>
            </div>
        </div>
    </div>

    <?php if (empty($disputes)): ?>
        <div class="card">
            <div class="card-body text-center py-5">
                <i class="fas fa-clipboard-list fa-3x text-muted mb-3"></i>
                <h5>No Disputes Found</h5>
                <p class="text-muted">No issues have been reported yet.</p>
            </div>
        </div>
    <?php else: ?>
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">All Disputes</h5>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Dispute #</th>
                                <th>Order</th>
                                <th>Customer</th>
                                <th>Issue Type</th>
                                <th>Status</th>
                                <th>Last Message</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($disputes as $dispute): ?>
                                <?php
                                $status_colors = [
                                    'open' => 'warning',
                                    'in_progress' => 'info',
                                    'resolved' => 'success',
                                    'closed' => 'secondary'
                                ];
                                $color = $status_colors[$dispute['status']] ?? 'secondary';
                                ?>
                                <tr class="<?= $dispute['unread_count'] > 0 ? 'table-primary' : '' ?>">
                                    <td>
                                        <strong><?= esc($dispute['dispute_number']) ?></strong>
                                        <?php if ($dispute['unread_count'] > 0): ?>
                                            <br><span class="badge bg-danger"><?= $dispute['unread_count'] ?> new</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <strong><?= esc($dispute['order_number']) ?></strong><br>
                                        <small class="text-muted"><?= esc($dispute['item_title']) ?></small>
                                    </td>
                                    <td><?= esc($dispute['customer_name']) ?></td>
                                    <td><?= ucwords(str_replace('_', ' ', $dispute['dispute_type'])) ?></td>
                                    <td>
                                        <span class="badge bg-<?= $color ?>">
                                            <?= ucfirst(str_replace('_', ' ', $dispute['status'])) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php if ($dispute['last_message_time']): ?>
                                            <small><?= time_ago($dispute['last_message_time']) ?></small>
                                        <?php else: ?>
                                            <small class="text-muted">No messages</small>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="dispute-details.php?id=<?= $dispute['id'] ?>" class="btn btn-sm btn-primary">
                                            <i class="fas fa-comments"></i> View
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>

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