<?php
ini_set('display_errors', 0);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log');

session_start();
require_once("../config/config.php");

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: vendor-management.php");
    exit();
}

$id = (int)$_GET['id'];

// Fetch vendor details
$stmt = $conn->prepare("SELECT * FROM vendor_master WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    header("Location: vendor-management.php");
    exit();
}

$vendor = $result->fetch_assoc();
$stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Vendor - <?= htmlspecialchars($vendor['vendor_code']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #a855f7;
            --accent: #c084fc;
            --glass: rgba(255,255,255,0.10);
            --glass-border: rgba(255,255,255,0.20);
        }

        * { margin:0; padding:0; box-sizing:border-box; }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(135deg, rgba(0,0,0,0.78), rgba(0,0,0,0.92)), 
                        url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 20px 15px;
            min-height: 100vh;
        }

        .container { max-width: 1100px; margin: auto; }

        .glass-container {
            background: var(--glass);
            backdrop-filter: blur(24px);
            border-radius: 32px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 80px rgba(0,0,0,0.65);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.35), rgba(147,51,234,0.25));
            padding: 50px 40px;
            text-align: center;
            position: relative;
        }
        .header h1 {
            font-size: clamp(28px, 5vw, 42px);
            font-weight: 700;
        }

        .content { padding: 45px 40px; }

        .vendor-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 35px;
            flex-wrap: wrap;
            gap: 15px;
        }

        .vendor-code {
            font-size: 2.2rem;
            font-weight: 700;
            color: #c4b5fd;
        }

        .badges {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }

        .badge {
            padding: 8px 20px;
            border-radius: 50px;
            font-weight: 700;
            font-size: 0.95rem;
        }

        .status-Active   { background: #22c55e; color: white; }
        .status-Inactive { background: #64748b; color: white; }

        .category-EQP     { background: #3b82f6; color: white; }
        .category-LOW-ANS { background: #f59e0b; color: white; }
        .category-VEN     { background: #a855f7; color: white; }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
            gap: 25px;
            margin-bottom: 40px;
        }

        .info-card {
            background: rgba(255,255,255,0.06);
            border: 1px solid rgba(255,255,255,0.15);
            border-radius: 20px;
            padding: 30px;
        }

        .info-card h3 {
            margin-bottom: 20px;
            font-size: 1.35rem;
            display: flex;
            align-items: center;
            gap: 12px;
            color: #ddd6fe;
        }

        .detail-row {
            margin-bottom: 22px;
        }

        .detail-row:last-child { margin-bottom: 0; }

        .label {
            font-size: 0.95rem;
            opacity: 0.75;
            margin-bottom: 6px;
        }

        .value {
            font-size: 1.15rem;
            font-weight: 600;
            word-break: break-word;
        }

        .empty {
            opacity: 0.5;
            font-style: italic;
        }

        .actions {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            justify-content: center;
            margin-top: 40px;
        }

        .btn {
            padding: 14px 32px;
            border-radius: 16px;
            font-weight: 700;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
        }

        .btn-back {
            background: rgba(255,255,255,0.15);
            color: white;
        }

        .btn-edit {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }

        @media (max-width: 768px) {
            .glass-container { border-radius: 24px; }
            .header, .content { padding: 35px 25px; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>

<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">

        <!-- Header -->
        <div class="header">
            <a href="vendor-management.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px; top:35px; font-weight:500;">
                ← Back to Vendors
            </a>
            <h1><i class="fas fa-user-tie"></i> Vendor Profile</h1>
        </div>

        <div class="content">

            <!-- Vendor Header -->
            <div class="vendor-header">
                <div>
                    <div class="vendor-code"><?= htmlspecialchars($vendor['vendor_code']) ?></div>
                    <h2 style="margin:0; font-size:2rem;"><?= htmlspecialchars($vendor['vendor_name']) ?></h2>
                </div>
                <div class="badges">
                    <span class="badge status-<?= strtolower($vendor['vendor_status'] ?? 'Active') ?>">
                        <?= htmlspecialchars($vendor['vendor_status'] ?? 'Active') ?>
                    </span>
                    <span class="badge category-<?= htmlspecialchars($vendor['vendor_category'] ?? 'VEN') ?>">
                        <?= htmlspecialchars($vendor['vendor_category'] ?? 'General') ?>
                    </span>
                </div>
            </div>

            <div class="info-grid">

                <!-- Contact Information -->
                <div class="info-card">
                    <h3><i class="fas fa-address-book"></i> Contact Information</h3>
                    <div class="detail-row">
                        <div class="label">Address</div>
                        <div class="value">
                            <?= !empty($vendor['vendor_address']) ? nl2br(htmlspecialchars($vendor['vendor_address'])) : '<span class="empty">Not provided</span>' ?>
                        </div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Contact Number</div>
                        <div class="value"><?= htmlspecialchars($vendor['vendor_contact'] ?? '—') ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Email Address</div>
                        <div class="value">
                            <?= !empty($vendor['vendor_email']) 
                                ? '<a href="mailto:'.htmlspecialchars($vendor['vendor_email']).'" style="color:#c4b5fd;">'.htmlspecialchars($vendor['vendor_email']).'</a>' 
                                : '<span class="empty">Not provided</span>' ?>
                        </div>
                    </div>
                </div>

                <!-- Business Information -->
                <div class="info-card">
                    <h3><i class="fas fa-building"></i> Business Details</h3>
                    <div class="detail-row">
                        <div class="label">GST Number</div>
                        <div class="value"><?= htmlspecialchars($vendor['gst_number'] ?? '—') ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">PAN Number</div>
                        <div class="value"><?= htmlspecialchars($vendor['pan_number'] ?? '—') ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Payment Terms</div>
                        <div class="value"><?= htmlspecialchars($vendor['vendor_payment_term'] ?? '—') ?></div>
                    </div>
                </div>
            </div>

            <!-- Action Buttons -->
            <div class="actions">
                <a href="vendor-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to List
                </a>
                <a href="edit-vendor.php?id=<?= $vendor['id'] ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit Vendor
                </a>
            </div>
        </div>
    </div>
</div>

</body>
</html>

<?php $conn->close(); ?>