<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$vendor_id = $_GET['id'] ?? '';

if (empty($vendor_id)) {
    header('Location: list_vendors.php');
    exit();
}

// Get vendor details
$sql = "SELECT v.*, u.email, u.phone as user_phone
        FROM vendors v
        LEFT JOIN users u ON v.user_id = u.id
        WHERE v.id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $vendor_id);
$stmt->execute();
$result = $stmt->get_result();
$vendor = $result->fetch_assoc();

if (!$vendor) {
    header('Location: list_vendors.php');
    exit();
}

// Get completed jobs
$sql_jobs = "SELECT COUNT(*) as total, SUM(final_cost) as total_revenue
              FROM maintenance_requests 
              WHERE assigned_vendor_id = ? AND status = 'Completed'";

$stmt_jobs = $conn->prepare($sql_jobs);
$stmt_jobs->bind_param("i", $vendor_id);
$stmt_jobs->execute();
$result_jobs = $stmt_jobs->get_result();
$stats = $result_jobs->fetch_assoc();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Vendor Details')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Vendors', 'title' => 'Vendor Details')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="list_vendors.php" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Vendors
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0"><?php echo htmlspecialchars($vendor['company_name']); ?></h4>
                                </div>
                                <div class="card-body">
                                    <div class="row mb-3">
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Contact Person:</strong> <?php echo htmlspecialchars($vendor['contact_person'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Email:</strong> <?php echo htmlspecialchars($vendor['email'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Phone:</strong> <?php echo htmlspecialchars($vendor['phone'] ?? 'N/A'); ?></p>
                                        </div>
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Specialization:</strong> <?php echo htmlspecialchars($vendor['specialization'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Status:</strong> 
                                                <span class="badge bg-soft-<?php echo strtolower($vendor['status']) == 'active' ? 'success' : 'danger'; ?>">
                                                    <?php echo htmlspecialchars($vendor['status']); ?>
                                                </span>
                                            </p>
                                            <p class="mb-2"><strong>Rating:</strong> 
                                                <?php if ($vendor['average_rating']): ?>
                                                    <?php echo number_format($vendor['average_rating'], 1); ?> / 5.0
                                                <?php else: ?>
                                                    No ratings yet
                                                <?php endif; ?>
                                            </p>
                                        </div>
                                    </div>
                                    <?php if ($vendor['address']): ?>
                                        <div class="mb-3">
                                            <h6 class="text-muted mb-2">Address</h6>
                                            <p><?php echo nl2br(htmlspecialchars($vendor['address'])); ?></p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Statistics</h4>
                                </div>
                                <div class="card-body">
                                    <div class="mb-3">
                                        <p class="mb-1 text-muted">Completed Jobs</p>
                                        <h4 class="mb-0"><?php echo $stats['total'] ?? 0; ?></h4>
                                    </div>
                                    <div class="mb-3">
                                        <p class="mb-1 text-muted">Total Revenue</p>
                                        <h4 class="mb-0 text-success">KES <?php echo number_format($stats['total_revenue'] ?? 0, 2); ?></h4>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
