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

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

// === SECURITY: Must be admin ===
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header('Location: ../login.php'); exit;
}

$company = null;
$error = '';

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $error = "Invalid company ID.";
} else {
    $id = (int)$_GET['id'];

    $sql = "SELECT id, company_name, company_email, company_address, company_phone, created_at, updated_at 
            FROM companies WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows === 0) {
        $error = "Company not found.";
    } else {
        $company = $result->fetch_assoc();
    }
    $stmt->close();
}

// Handle Delete
if (isset($_GET['delete']) && $company) {
    $deleteStmt = $conn->prepare("DELETE FROM companies WHERE id = ?");
    $deleteStmt->bind_param('i', $id);
    $deleteStmt->execute();
    $deleteStmt->close();
    header("Location: company-management.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Company</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #7c3aed;
            --primary-light: #8b5cf6;
            --secondary: #06b6d4;
            --success: #10b981;
            --danger: #ef4444;
            --warning: #f59e0b;
            --dark: #0f172a;
            --darker: #020617;
            --card-bg: rgba(255, 255, 255, 0.08);
            --border: rgba(255, 255, 255, 0.1);
            --text: #f1f5f9;
            --text-light: #cbd5e1;
            --text-lighter: #94a3b8;
            --radius: 1rem;
            --radius-sm: .75rem;
            --transition: all .3s cubic-bezier(.4, 0, .2, 1);
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, var(--darker), var(--dark));
            color: var(--text);
            min-height: 100vh;
            padding: 2rem 1rem;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .background {
            position: fixed;
            top: 0; left: 0; width: 100%; height: 100%;
            z-index: -1; overflow: hidden;
        }
        .gradient-orb {
            position: absolute;
            border-radius: 50%;
            filter: blur(80px);
            opacity: .3;
            animation: float 25s ease-in-out infinite;
        }
        .orb-1 { width: 500px; height: 500px; background: linear-gradient(45deg, var(--primary), var(--secondary)); top: -200px; left: -200px; }
        .orb-2 { width: 400px; height: 400px; background: linear-gradient(45deg, var(--secondary), var(--success)); bottom: -150px; right: -150px; animation-delay: -8s; }
        @keyframes float {
            0%,100% { transform: translate(0,0) scale(1); }
            25% { transform: translate(100px,100px) scale(1.1); }
            50% { transform: translate(0,200px) scale(.9); }
            75% { transform: translate(-100px,100px) scale(1.05); }
        }

        .view-card {
            background: var(--card-bg);
            backdrop-filter: blur(20px);
            border: 1px solid var(--border);
            border-radius: var(--radius);
            width: 100%;
            max-width: 560px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,.3);
            animation: fadeInUp .6s ease-out;
        }
        @keyframes fadeInUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .card-header {
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            padding: 1.75rem 2rem;
            text-align: center;
            color: white;
        }
        .card-header h1 {
            font-size: 1.8rem;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: .75rem;
        }
        .card-header h1 i {
            font-size: 1.6rem;
        }

        .card-body {
            padding: 2rem;
        }

        .detail-group {
            display: flex;
            flex-direction: column;
            margin-bottom: 1.5rem;
            padding-bottom: 1.5rem;
            border-bottom: 1px dashed var(--border);
        }
        .detail-group:last-of-type {
            border-bottom: none;
            padding-bottom: 0;
            margin-bottom: 0;
        }
        .detail-label {
            display: flex;
            align-items: center;
            gap: .5rem;
            color: var(--text-light);
            font-size: .9rem;
            font-weight: 500;
            margin-bottom: .5rem;
            text-transform: uppercase;
            letter-spacing: .5px;
        }
        .detail-label i {
            width: 18px;
            text-align: center;
            color: var(--primary-light);
        }
        .detail-value {
            color: var(--text);
            font-size: 1.1rem;
            word-break: break-word;
        }
        .detail-value code {
            background: rgba(255,255,255,.1);
            padding: .25rem .5rem;
            border-radius: .35rem;
            font-family: 'Courier New', monospace;
            font-size: .95rem;
            color: var(--success);
        }

        .timestamps {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1rem;
            margin: 2rem 0;
            padding: 1rem;
            background: rgba(255,255,255,.05);
            border-radius: var(--radius-sm);
            border: 1px solid var(--border);
            font-size: .9rem;
        }
        .timestamps div {
            display: flex;
            flex-direction: column;
        }
        .timestamps strong {
            color: var(--text-light);
            margin-bottom: .25rem;
            font-weight: 600;
        }

        .card-actions {
            display: flex;
            gap: 1rem;
            justify-content: center;
            flex-wrap: wrap;
            margin-top: 2rem;
        }
        .btn {
            padding: .75rem 1.5rem;
            border: none;
            border-radius: var(--radius-sm);
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            gap: .5rem;
            text-decoration: none;
            font-size: .95rem;
        }
        .btn-back {
            background: var(--card-bg);
            color: var(--text-light);
            border: 1px solid var(--border);
        }
        .btn-edit {
            background: linear-gradient(135deg, var(--warning), #fbbf24);
            color: white;
        }
        .btn-delete {
            background: linear-gradient(135deg, var(--danger), #f87171);
            color: white;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0,0,0,.3);
        }

        .error-card {
            text-align: center;
            padding: 3rem 2rem;
        }
        .error-card h1 {
            font-size: 2rem;
            margin-bottom: 1rem;
            color: var(--danger);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: .75rem;
        }
        .error-alert {
            background: rgba(239, 68, 68, .15);
            border: 1px solid rgba(239, 68, 68, .3);
            color: var(--danger);
            padding: 1rem 1.5rem;
            border-radius: var(--radius-sm);
            margin: 1.5rem 0;
            font-size: .95rem;
        }

        @media (max-width: 480px) {
            .card-header { padding: 1.5rem; }
            .card-header h1 { font-size: 1.5rem; }
            .card-body { padding: 1.5rem; }
            .timestamps { grid-template-columns: 1fr; }
            .card-actions { flex-direction: column; }
            .btn { width: 100%; justify-content: center; }
        }
    </style>
</head>
<body>

<div class="background">
    <div class="gradient-orb orb-1"></div>
    <div class="gradient-orb orb-2"></div>
</div>

<?php if ($error): ?>
<div class="view-card">
    <div class="error-card">
        <h1>Error: Error</h1>
        <div class="error-alert">
            <?= htmlspecialchars($error) ?>
        </div>
        <div class="card-actions">
            <a href="company-management.php" class="btn btn-back">
                Back to List
            </a>
        </div>
    </div>
</div>

<?php elseif ($company): ?>
<div class="view-card">
    <div class="card-header">
        <h1>Company: <?= htmlspecialchars($company['company_name']) ?></h1>
    </div>

    <div class="card-body">

        <div class="detail-group">
            <div class="detail-label">Email: Email</div>
            <div class="detail-value">
                <code><?= htmlspecialchars($company['company_email']) ?></code>
            </div>
        </div>

        <div class="detail-group">
            <div class="detail-label">Phone: Phone</div>
            <div class="detail-value">
                <?= htmlspecialchars($company['company_phone']) ?>
            </div>
        </div>

        <div class="detail-group">
            <div class="detail-label">Address: Address</div>
            <div class="detail-value">
                <?= nl2br(htmlspecialchars($company['company_address'])) ?>
            </div>
        </div>

        <div class="timestamps">
            <div>
                <strong>Created</strong>
                <span><?= date('M j, Y \a\t g:i A', strtotime($company['created_at'])) ?></span>
            </div>
            <div>
                <strong>Updated</strong>
                <span><?= date('M j, Y \a\t g:i A', strtotime($company['updated_at'])) ?></span>
            </div>
        </div>

        <div class="card-actions">
            <a href="company-management.php" class="btn btn-back">
                Back
            </a>
            <a href="edit-company.php?id=<?= $company['id'] ?>" class="btn btn-edit">
                Edit
            </a>
            <a href="?id=<?= $company['id'] ?>&delete=1" 
               class="btn btn-delete"
               onclick="return confirm('Permanently delete \"<?= addslashes(htmlspecialchars($company['company_name'])) ?>\"?\nThis cannot be undone.')">
                Delete
            </a>
        </div>
    </div>
</div>
<?php endif; ?>

</body>
</html>